new work penalty

also removed old penalty config gui
This commit is contained in:
alexcrea 2025-02-11 13:11:30 +01:00
parent ea19ffc4a1
commit b6d2c63b86
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
7 changed files with 66 additions and 128 deletions

View file

@ -1,114 +1,35 @@
package xyz.alexcrea.cuanvil.config; package xyz.alexcrea.cuanvil.config;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.util.AnvilUseType;
import xyz.alexcrea.cuanvil.gui.config.settings.EnumSettingGui;
import java.util.ArrayList; import java.util.EnumMap;
import java.util.List;
public enum WorkPenaltyType implements EnumSettingGui.ConfigurableEnum { public class WorkPenaltyType {
DEFAULT("default", true, true, "§aDefault", Material.LIME_TERRACOTTA),
ADDITIVE("add_only", false, true, "§eAdd Only", Material.YELLOW_TERRACOTTA),
INCREASE("increase_only", true, false, "§eIncrease Only", Material.YELLOW_TERRACOTTA),
DISABLED("disabled", false, false, "§cDisabled", Material.RED_TERRACOTTA),
;
private final String name; public record WorkPenaltyPart(
private final boolean penaltyIncrease; boolean penaltyIncrease,
private final boolean penaltyAdditive; boolean penaltyAdditive) {
private final String configName; public static WorkPenaltyPart ONLY_TRUE_PART = new WorkPenaltyPart(true, true);
private final Material configMaterial;
WorkPenaltyType(String name, boolean penaltyIncrease, boolean penaltyAdditive, String configName, Material configMaterial) {
this.name = name;
this.penaltyIncrease = penaltyIncrease;
this.penaltyAdditive = penaltyAdditive;
this.configName = configName;
this.configMaterial = configMaterial;
} }
public boolean isPenaltyIncreasing() { private final EnumMap<AnvilUseType, WorkPenaltyPart> partMap;
return penaltyIncrease;
public WorkPenaltyType(@Nullable EnumMap<AnvilUseType, WorkPenaltyPart> partMap) {
this.partMap = new EnumMap<>(partMap != null ? partMap : new EnumMap<>(AnvilUseType.class));
} }
public boolean isPenaltyAdditive() { public WorkPenaltyPart getPenaltyInfo(AnvilUseType type) {
return penaltyAdditive; return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART);
} }
private boolean doRepresentThisType(String toTest){ public boolean isPenaltyIncreasing(AnvilUseType type) {
return name.equalsIgnoreCase(toTest); return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART).penaltyIncrease;
} }
@NotNull public boolean isPenaltyAdditive(AnvilUseType type) {
public static WorkPenaltyType fromString(@Nullable String toTest){ return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART).penaltyAdditive;
if(toTest == null) return DEFAULT;
// Test if it matches any of values
for (WorkPenaltyType value : values()) {
if(value.doRepresentThisType(toTest)){
return value;
}
}
// Use default if not found
return DEFAULT;
} }
@NotNull
public static WorkPenaltyType next(@NotNull WorkPenaltyType now){
return switch (now){
case DEFAULT -> ADDITIVE;
case ADDITIVE -> INCREASE;
case INCREASE -> DISABLED;
case DISABLED -> DEFAULT;
};
}
@Override
public ItemStack configurationGuiItem() {
ItemStack displayedItem = new ItemStack(this.configMaterial);
ItemMeta valueMeta = displayedItem.getItemMeta();
assert valueMeta != null;
valueMeta.setDisplayName(this.configName);
List<String> lore = new ArrayList<>();
lore.add(configDisplayForAdd());
lore.add(configDisplayForIncrease());
lore.add("");
lore.add(AbstractSettingGui.CLICK_LORE);
valueMeta.setLore(lore);
displayedItem.setItemMeta(valueMeta);
return displayedItem;
}
public String configDisplayForAdd(){
return ("§7Add penalty: " + (penaltyAdditive ? "§aYes" : "§cNo"));
}
public String configDisplayForIncrease(){
return ("§7Increase penalty: " + (penaltyIncrease ? "§aYes" : "§cNo"));
}
@Override
public String configName() {
return this.name;
}
@Override
public String configurationGuiName() {
return this.configName;
}
} }

View file

@ -14,12 +14,10 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.config.WorkPenaltyType;
import xyz.alexcrea.cuanvil.dependency.packet.PacketManager; import xyz.alexcrea.cuanvil.dependency.packet.PacketManager;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui; import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.EnumSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
@ -28,7 +26,6 @@ import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List;
/** /**
* Global config to edit basic basic settings. * Global config to edit basic basic settings.
@ -89,7 +86,7 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
private IntSettingsGui.IntSettingFactory itemRenameCost; // r character private IntSettingsGui.IntSettingFactory itemRenameCost; // r character
private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost; // S character private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost; // S character
private EnumSettingGui.EnumSettingFactory<WorkPenaltyType> workPenaltyType; // W character //TODO private EnumSettingGui.EnumSettingFactory<WorkPenaltyType> workPenaltyType; // W character
private BoolSettingsGui.BoolSettingFactory allowColorCode; // c character private BoolSettingsGui.BoolSettingFactory allowColorCode; // c character
private BoolSettingsGui.BoolSettingFactory allowHexColor; // h character private BoolSettingsGui.BoolSettingFactory allowHexColor; // h character
@ -215,7 +212,7 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
// Work Penalty // Work Penalty
// ------------- // -------------
this.workPenaltyType = new EnumSettingGui.EnumSettingFactory<>("§8Work Penalty Type", this, /*TODO this.workPenaltyType = new EnumSettingGui.EnumSettingFactory<>("§8Work Penalty Type", this,
ConfigOptions.WORK_PENALTY_TYPE, ConfigHolder.DEFAULT_CONFIG ConfigOptions.WORK_PENALTY_TYPE, ConfigHolder.DEFAULT_CONFIG
) { ) {
@NotNull @NotNull
@ -237,8 +234,8 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
"§7Work penalty increase the price for every anvil use.", "§7Work penalty increase the price for every anvil use.",
"§7This config allow you to choose the comportment of work penalty.", "§7This config allow you to choose the comportment of work penalty.",
"", "",
value.configDisplayForAdd(), "IN PROGRESS",
value.configDisplayForIncrease() "IN PROGRESS"
); );
} }
@ -249,7 +246,7 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
return WorkPenaltyType.next(now); return WorkPenaltyType.next(now);
} }
}; };*/
// ------------- // -------------
// Color config // Color config
@ -377,8 +374,8 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
pane.bindItem('S', illegalCostItem); pane.bindItem('S', illegalCostItem);
// work penalty type // work penalty type
GuiItem workPenaltyType = this.workPenaltyType.getItem(Material.DAMAGED_ANVIL, "§aWork Penalty Type"); //TODO GuiItem workPenaltyType = this.workPenaltyType.getItem(Material.DAMAGED_ANVIL, "§aWork Penalty Type");
pane.bindItem('W', workPenaltyType); //TODO pane.bindItem('W', workPenaltyType);
// allow color code // allow color code
GuiItem allowColorCodeItem = this.allowColorCode.getItem(); GuiItem allowColorCodeItem = this.allowColorCode.getItem();

View file

@ -5,7 +5,6 @@ import io.delilaheve.util.ConfigOptions;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.config.WorkPenaltyType;
public class PluginSetDefault { public class PluginSetDefault {
@ -26,7 +25,6 @@ public class PluginSetDefault {
nbSet+= trySetDefault(config, ConfigOptions.ALLOW_HEXADECIMAL_COLOR, ConfigOptions.DEFAULT_ALLOW_HEXADECIMAL_COLOR); nbSet+= trySetDefault(config, ConfigOptions.ALLOW_HEXADECIMAL_COLOR, ConfigOptions.DEFAULT_ALLOW_HEXADECIMAL_COLOR);
nbSet+= trySetDefault(config, ConfigOptions.PERMISSION_NEEDED_FOR_COLOR, ConfigOptions.DEFAULT_PERMISSION_NEEDED_FOR_COLOR); nbSet+= trySetDefault(config, ConfigOptions.PERMISSION_NEEDED_FOR_COLOR, ConfigOptions.DEFAULT_PERMISSION_NEEDED_FOR_COLOR);
nbSet+= trySetDefault(config, ConfigOptions.USE_OF_COLOR_COST, ConfigOptions.DEFAULT_USE_OF_COLOR_COST); nbSet+= trySetDefault(config, ConfigOptions.USE_OF_COLOR_COST, ConfigOptions.DEFAULT_USE_OF_COLOR_COST);
nbSet+= trySetDefault(config, ConfigOptions.WORK_PENALTY_TYPE, WorkPenaltyType.DEFAULT.configName());
nbSet+= trySetDefault(config, ConfigOptions.DEFAULT_LIMIT_PATH, ConfigOptions.DEFAULT_ENCHANT_LIMIT); nbSet+= trySetDefault(config, ConfigOptions.DEFAULT_LIMIT_PATH, ConfigOptions.DEFAULT_ENCHANT_LIMIT);
if(nbSet > 0){ if(nbSet > 0){

View file

@ -4,7 +4,10 @@ import io.delilaheve.CustomAnvil
import io.delilaheve.util.EnchantmentUtil.enchantmentName import io.delilaheve.util.EnchantmentUtil.enchantmentName
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.config.WorkPenaltyType import xyz.alexcrea.cuanvil.config.WorkPenaltyType
import xyz.alexcrea.cuanvil.config.WorkPenaltyType.WorkPenaltyPart
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.util.AnvilUseType
import java.util.EnumMap
/** /**
* Config option accessors * Config option accessors
@ -34,7 +37,8 @@ object ConfigOptions {
const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color" const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color"
const val USE_OF_COLOR_COST = "use_of_color_cost" const val USE_OF_COLOR_COST = "use_of_color_cost"
const val WORK_PENALTY_TYPE = "work_penalty_type" //const val WORK_PENALTY_TYPE = "work_penalty_type" TODO move old value to config migration
const val WORK_PENALTY = "work_penalty"
const val DEFAULT_LIMIT_PATH = "default_limit" const val DEFAULT_LIMIT_PATH = "default_limit"
@ -113,7 +117,7 @@ object ConfigOptions {
private const val DEFAULT_ENCHANT_VALUE = 0 private const val DEFAULT_ENCHANT_VALUE = 0
// Default max before merge disabled (negative mean enabled) // Default max before merge disabled (negative mean enabled)
const val DEFAULT_MAX_BEFORE_MERGE_DISABLED = -1; const val DEFAULT_MAX_BEFORE_MERGE_DISABLED = -1
// ------------- // -------------
// Get methods // Get methods
@ -257,16 +261,35 @@ object ConfigOptions {
} }
/** /**
* How many xp should use of color should cost * How work penalties should work
*/ */
val workPenaltyType: WorkPenaltyType val workPenaltyType: WorkPenaltyType
get() { get() {
return WorkPenaltyType.fromString( val penaltyMap = EnumMap<AnvilUseType, WorkPenaltyPart>(AnvilUseType::class.java)
ConfigHolder.DEFAULT_CONFIG
.config for (type in AnvilUseType.entries) {
.getString(WORK_PENALTY_TYPE)); penaltyMap[type] = workPenaltyPart(type)
}
return WorkPenaltyType(penaltyMap)
} }
/**
* How work penalty should work
*/
fun workPenaltyPart(type: AnvilUseType): WorkPenaltyPart {
val config = ConfigHolder.CONFLICT_HOLDER.config
val path = WORK_PENALTY + "." + type.typeName
// Find values
val section = config.getConfigurationSection(path) ?: return WorkPenaltyPart.ONLY_TRUE_PART
val penaltyIncrease = section.getBoolean("penalty_increase", true)
val penaltyAdditive = section.getBoolean("penalty_additive", true)
return WorkPenaltyPart(penaltyIncrease, penaltyAdditive)
}
/** /**
* Default enchantment limit * Default enchantment limit
*/ */
@ -303,11 +326,11 @@ object ConfigOptions {
fun enchantLimit(enchantment: CAEnchantment): Int { fun enchantLimit(enchantment: CAEnchantment): Int {
// Test namespace // Test namespace
var limit = enchantLimit(enchantment.key.toString()) var limit = enchantLimit(enchantment.key.toString())
if(limit != null) return limit; if(limit != null) return limit
// Test legacy (name only) // Test legacy (name only)
limit = enchantLimit(enchantment.enchantmentName) limit = enchantLimit(enchantment.enchantmentName)
if(limit != null) return limit; if(limit != null) return limit
// get default (and test old legacy if present) // get default (and test old legacy if present)
return getDefaultLevel(enchantment.enchantmentName) return getDefaultLevel(enchantment.enchantmentName)
@ -348,11 +371,11 @@ object ConfigOptions {
): Int { ): Int {
// Test namespace // Test namespace
var limit = enchantmentValue(enchantment.key.toString(), isFromBook) var limit = enchantmentValue(enchantment.key.toString(), isFromBook)
if(limit != null) return limit; if(limit != null) return limit
// Test legacy (name only) // Test legacy (name only)
limit = enchantmentValue(enchantment.enchantmentName, isFromBook) limit = enchantmentValue(enchantment.enchantmentName, isFromBook)
if(limit != null) return limit; if(limit != null) return limit
// get default (and test old legacy if present) // get default (and test old legacy if present)
return getDefaultValue(enchantment, isFromBook) return getDefaultValue(enchantment, isFromBook)

View file

@ -23,8 +23,6 @@ import xyz.alexcrea.cuanvil.util.AnvilUseType
import xyz.alexcrea.cuanvil.util.AnvilXpUtil import xyz.alexcrea.cuanvil.util.AnvilXpUtil
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import java.util.logging.Level
/** /**
* Listener for anvil events * Listener for anvil events
*/ */

View file

@ -1,9 +1,10 @@
package xyz.alexcrea.cuanvil.util package xyz.alexcrea.cuanvil.util
enum class AnvilUseType { enum class AnvilUseType(val typeName: String) {
RENAME_ONLY, RENAME_ONLY("rename_only"),
MERGE, MERGE("merge"),
UNIT_REPAIR UNIT_REPAIR("unit_repair"),
;
} }

View file

@ -77,7 +77,7 @@ object AnvilXpUtil {
fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack, useType: AnvilUseType): Int { fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack, useType: AnvilUseType): Int {
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation // Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
// Calculate work penalty // Calculate work penalty
val penaltyType = ConfigOptions.workPenaltyType val penaltyType = ConfigOptions.workPenaltyPart(useType)
val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0 val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0
val rightPenalty = val rightPenalty =
@ -86,7 +86,7 @@ object AnvilXpUtil {
// Increase penalty on fusing or unit repair // Increase penalty on fusing or unit repair
if(penaltyType.isPenaltyIncreasing && (right != null || AnvilUseType.UNIT_REPAIR == useType)){ if(penaltyType.penaltyIncrease && (right != null || AnvilUseType.UNIT_REPAIR == useType)){
result.itemMeta?.let { result.itemMeta?.let {
(it as? Repairable)?.repairCost = leftPenalty * 2 + 1 (it as? Repairable)?.repairCost = leftPenalty * 2 + 1
result.itemMeta = it result.itemMeta = it
@ -101,7 +101,7 @@ object AnvilXpUtil {
"result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}" "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}"
) )
if(!penaltyType.isPenaltyAdditive) return 0 if(!penaltyType.penaltyAdditive) return 0
return leftPenalty + rightPenalty return leftPenalty + rightPenalty
} }