mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add work type config gui
also add custom recipe use type also fix increase not working for rename
This commit is contained in:
parent
b6d2c63b86
commit
2cb053e0ca
7 changed files with 255 additions and 61 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.config;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.alexcrea.cuanvil.util.AnvilUseType;
|
||||
|
||||
|
|
@ -11,7 +12,12 @@ public class WorkPenaltyType {
|
|||
boolean penaltyIncrease,
|
||||
boolean penaltyAdditive) {
|
||||
|
||||
public static WorkPenaltyPart ONLY_TRUE_PART = new WorkPenaltyPart(true, true);
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof WorkPenaltyPart other)) return false;
|
||||
|
||||
return other.penaltyIncrease == this.penaltyIncrease && other.penaltyAdditive == this.penaltyAdditive;
|
||||
}
|
||||
}
|
||||
|
||||
private final EnumMap<AnvilUseType, WorkPenaltyPart> partMap;
|
||||
|
|
@ -20,16 +26,31 @@ public class WorkPenaltyType {
|
|||
this.partMap = new EnumMap<>(partMap != null ? partMap : new EnumMap<>(AnvilUseType.class));
|
||||
}
|
||||
|
||||
public ImmutableMap<AnvilUseType, WorkPenaltyPart> getPartMap() {
|
||||
return ImmutableMap.copyOf(partMap);
|
||||
}
|
||||
|
||||
public WorkPenaltyPart getPenaltyInfo(AnvilUseType type) {
|
||||
return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART);
|
||||
return partMap.getOrDefault(type, type.getDefaultPenalty());
|
||||
}
|
||||
|
||||
public boolean isPenaltyIncreasing(AnvilUseType type) {
|
||||
return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART).penaltyIncrease;
|
||||
return partMap.getOrDefault(type, type.getDefaultPenalty()).penaltyIncrease;
|
||||
}
|
||||
|
||||
public boolean isPenaltyAdditive(AnvilUseType type) {
|
||||
return partMap.getOrDefault(type, WorkPenaltyPart.ONLY_TRUE_PART).penaltyAdditive;
|
||||
return partMap.getOrDefault(type, type.getDefaultPenalty()).penaltyAdditive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof WorkPenaltyType that)) return false;
|
||||
|
||||
for (AnvilUseType type : AnvilUseType.getEntries()) {
|
||||
if(!getPenaltyInfo(type).equals(that.getPenaltyInfo(type))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
|||
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.WorkPenaltyTypeSettingGui;
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
|
||||
|
|
@ -86,8 +87,6 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
|
|||
private IntSettingsGui.IntSettingFactory itemRenameCost; // r character
|
||||
private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost; // S character
|
||||
|
||||
//TODO private EnumSettingGui.EnumSettingFactory<WorkPenaltyType> workPenaltyType; // W character
|
||||
|
||||
private BoolSettingsGui.BoolSettingFactory allowColorCode; // c character
|
||||
private BoolSettingsGui.BoolSettingFactory allowHexColor; // h character
|
||||
|
||||
|
|
@ -208,46 +207,6 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
|
|||
ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST,
|
||||
1, 5, 10, 50, 100);
|
||||
|
||||
// -------------
|
||||
// Work Penalty
|
||||
// -------------
|
||||
|
||||
/*TODO this.workPenaltyType = new EnumSettingGui.EnumSettingFactory<>("§8Work Penalty Type", this,
|
||||
ConfigOptions.WORK_PENALTY_TYPE, ConfigHolder.DEFAULT_CONFIG
|
||||
) {
|
||||
@NotNull
|
||||
@Override
|
||||
public WorkPenaltyType getConfiguredValue() {
|
||||
return ConfigOptions.INSTANCE.getWorkPenaltyType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public WorkPenaltyType getDefault() {
|
||||
return WorkPenaltyType.DEFAULT;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> getDisplayLore(WorkPenaltyType value) {
|
||||
return List.of(
|
||||
"§7Work penalty increase the price for every anvil use.",
|
||||
"§7This config allow you to choose the comportment of work penalty.",
|
||||
"",
|
||||
"IN PROGRESS",
|
||||
"IN PROGRESS"
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public WorkPenaltyType next(@NotNull WorkPenaltyType now) {
|
||||
return WorkPenaltyType.next(now);
|
||||
}
|
||||
|
||||
};*/
|
||||
|
||||
// -------------
|
||||
// Color config
|
||||
// -------------
|
||||
|
|
@ -374,8 +333,8 @@ public class BasicConfigGui extends ChestGui implements ValueUpdatableGui {
|
|||
pane.bindItem('S', illegalCostItem);
|
||||
|
||||
// work penalty type
|
||||
//TODO GuiItem workPenaltyType = this.workPenaltyType.getItem(Material.DAMAGED_ANVIL, "§aWork Penalty Type");
|
||||
//TODO pane.bindItem('W', workPenaltyType);
|
||||
GuiItem workPenaltyType = WorkPenaltyTypeSettingGui.getDisplayItem(this, Material.DAMAGED_ANVIL, "§aWork Penalty Type");
|
||||
pane.bindItem('W', workPenaltyType);
|
||||
|
||||
// allow color code
|
||||
GuiItem allowColorCodeItem = this.allowColorCode.getItem();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,189 @@
|
|||
package xyz.alexcrea.cuanvil.gui.config.settings;
|
||||
|
||||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
|
||||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import io.delilaheve.util.ConfigOptions;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.config.WorkPenaltyType;
|
||||
import xyz.alexcrea.cuanvil.gui.config.global.BasicConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
|
||||
import xyz.alexcrea.cuanvil.util.AnvilUseType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class WorkPenaltyTypeSettingGui extends AbstractSettingGui {
|
||||
|
||||
private static final String INCREASING_EXPLANATION = "§eIncreasing§7: will penalty be increased (in item)";
|
||||
private static final String ADDING_EXPLANATION = "§eAdditive§7: will penalty be added to the cost";
|
||||
|
||||
private final @NotNull WorkPenaltyType currentType;
|
||||
private final @NotNull Map<AnvilUseType, WorkPenaltyType.WorkPenaltyPart> items;
|
||||
|
||||
public WorkPenaltyTypeSettingGui(@NotNull BasicConfigGui parent) {
|
||||
super(4, "§8Work Penalty Type", parent);
|
||||
|
||||
this.currentType = ConfigOptions.INSTANCE.getWorkPenaltyType();
|
||||
this.items = new EnumMap<>(this.currentType.getPartMap());
|
||||
|
||||
for (AnvilUseType type : AnvilUseType.getEntries()) {
|
||||
updateGuiForType(type);
|
||||
}
|
||||
}
|
||||
|
||||
public static GuiItem getDisplayItem(@NotNull BasicConfigGui parent,
|
||||
@NotNull Material itemMat,
|
||||
@NotNull String name) {
|
||||
List<String> displayLore = new ArrayList<>();
|
||||
|
||||
displayLore.add("§7Work penalty increase the price for every anvil use.");
|
||||
displayLore.add("§7This config allow you to choose the comportment of work penalty.");
|
||||
displayLore.add(INCREASING_EXPLANATION);
|
||||
displayLore.add(ADDING_EXPLANATION);
|
||||
displayLore.add("");
|
||||
|
||||
WorkPenaltyType workPenalty = ConfigOptions.INSTANCE.getWorkPenaltyType();
|
||||
for (AnvilUseType type : AnvilUseType.getEntries()) {
|
||||
String increasing = (workPenalty.isPenaltyIncreasing(type) ? "§a" : "§c") + "Increasing";
|
||||
String additive = (workPenalty.isPenaltyAdditive(type) ? "§a" : "§c") + "Additive";
|
||||
|
||||
displayLore.add("§e" + type.getTypeName() + ": " + increasing + " §7| " + additive);
|
||||
}
|
||||
|
||||
ItemStack item = new ItemStack(itemMat);
|
||||
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(name);
|
||||
meta.setLore(displayLore);
|
||||
|
||||
item.setItemMeta(meta);
|
||||
|
||||
return new GuiItem(item, (event) -> {
|
||||
event.setCancelled(true);
|
||||
HumanEntity player = event.getWhoClicked();
|
||||
|
||||
// Do not allow to open inventory if player do not have edit configuration permission
|
||||
if (!player.hasPermission(CustomAnvil.editConfigPermission)) {
|
||||
player.closeInventory();
|
||||
player.sendMessage(GuiGlobalActions.NO_EDIT_PERM);
|
||||
return;
|
||||
}
|
||||
new WorkPenaltyTypeSettingGui(parent).show(player);
|
||||
}, CustomAnvil.instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Pattern getGuiPattern() {
|
||||
return new Pattern(
|
||||
"00zy0xw00",
|
||||
"00ab0cd00",
|
||||
"001203400",
|
||||
"B0000000S"
|
||||
);
|
||||
}
|
||||
|
||||
public void updateGuiForType(AnvilUseType type) {
|
||||
PatternPane pane = getPane();
|
||||
int ordinal = type.ordinal();
|
||||
|
||||
int display = 'z' - ordinal;
|
||||
int increment = 'a' + ordinal;
|
||||
int additive = '1' + ordinal;
|
||||
|
||||
WorkPenaltyType.WorkPenaltyPart part = items.get(type);
|
||||
String increasingStr = (part.penaltyIncrease() ? "§a" : "§c") + "Increasing";
|
||||
String additiveStr = (part.penaltyAdditive() ? "§a" : "§c") + "Additive";
|
||||
|
||||
// Display item
|
||||
ItemStack displayItem = new ItemStack(type.getDisplayMat());
|
||||
|
||||
ArrayList<String> displayLore = new ArrayList<>();
|
||||
displayLore.add("§e" + type.getTypeName() + ": " + increasingStr + " §7| " + additiveStr);
|
||||
|
||||
ItemMeta meta = displayItem.getItemMeta();
|
||||
meta.setDisplayName("§e" + type.getDisplayName());
|
||||
meta.setLore(displayLore);
|
||||
displayItem.setItemMeta(meta);
|
||||
|
||||
pane.bindItem(display, new GuiItem(displayItem, (event) -> {
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
|
||||
// "Increment" item
|
||||
ItemStack incrementItem = new ItemStack(part.penaltyIncrease() ? Material.GREEN_TERRACOTTA : Material.RED_TERRACOTTA);
|
||||
|
||||
meta = incrementItem.getItemMeta();
|
||||
meta.setDisplayName(increasingStr);
|
||||
meta.setLore(List.of(INCREASING_EXPLANATION));
|
||||
incrementItem.setItemMeta(meta);
|
||||
|
||||
pane.bindItem(increment, new GuiItem(incrementItem, (event) -> {
|
||||
event.setCancelled(true);
|
||||
|
||||
WorkPenaltyType.WorkPenaltyPart newPart = new WorkPenaltyType.WorkPenaltyPart(!part.penaltyIncrease(), part.penaltyAdditive());
|
||||
items.replace(type, newPart);
|
||||
updateGuiForType(type);
|
||||
update();
|
||||
}));
|
||||
|
||||
// "Additive" item
|
||||
ItemStack additiveItem = new ItemStack(part.penaltyAdditive() ? Material.GREEN_TERRACOTTA : Material.RED_TERRACOTTA);
|
||||
|
||||
meta = additiveItem.getItemMeta();
|
||||
meta.setDisplayName(additiveStr);
|
||||
meta.setLore(List.of(ADDING_EXPLANATION));
|
||||
additiveItem.setItemMeta(meta);
|
||||
|
||||
pane.bindItem(additive, new GuiItem(additiveItem, (event) -> {
|
||||
event.setCancelled(true);
|
||||
|
||||
WorkPenaltyType.WorkPenaltyPart newPart = new WorkPenaltyType.WorkPenaltyPart(part.penaltyIncrease(), !part.penaltyAdditive());
|
||||
items.replace(type, newPart);
|
||||
updateGuiForType(type);
|
||||
update();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSave() {
|
||||
String path = ConfigOptions.WORK_PENALTY_ROOT;
|
||||
ConfigHolder configHolder = ConfigHolder.DEFAULT_CONFIG;
|
||||
FileConfiguration config = configHolder.getConfig();
|
||||
|
||||
items.forEach((key, value) -> {
|
||||
String partPath = path + "." + key.getTypeName();
|
||||
|
||||
if (key.getDefaultPenalty().equals(value)) {
|
||||
config.set(partPath, null);
|
||||
return;
|
||||
}
|
||||
|
||||
config.set(partPath + '.' + ConfigOptions.WORK_PENALTY_INCREASE, value.penaltyIncrease());
|
||||
config.set(partPath + '.' + ConfigOptions.WORK_PENALTY_ADDITIVE, value.penaltyAdditive());
|
||||
});
|
||||
|
||||
configHolder.saveToDisk(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hadChange() {
|
||||
for (AnvilUseType type : items.keySet()) {
|
||||
if (!currentType.getPenaltyInfo(type).equals(items.get(type))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,9 @@ object ConfigOptions {
|
|||
const val USE_OF_COLOR_COST = "use_of_color_cost"
|
||||
|
||||
//const val WORK_PENALTY_TYPE = "work_penalty_type" TODO move old value to config migration
|
||||
const val WORK_PENALTY = "work_penalty"
|
||||
const val WORK_PENALTY_ROOT = "work_penalty"
|
||||
const val WORK_PENALTY_INCREASE = "increase"
|
||||
const val WORK_PENALTY_ADDITIVE = "additive"
|
||||
|
||||
const val DEFAULT_LIMIT_PATH = "default_limit"
|
||||
|
||||
|
|
@ -278,14 +280,14 @@ object ConfigOptions {
|
|||
* How work penalty should work
|
||||
*/
|
||||
fun workPenaltyPart(type: AnvilUseType): WorkPenaltyPart {
|
||||
val config = ConfigHolder.CONFLICT_HOLDER.config
|
||||
val path = WORK_PENALTY + "." + type.typeName
|
||||
val config = ConfigHolder.DEFAULT_CONFIG.config
|
||||
val path = WORK_PENALTY_ROOT + "." + type.typeName
|
||||
|
||||
// Find values
|
||||
val section = config.getConfigurationSection(path) ?: return WorkPenaltyPart.ONLY_TRUE_PART
|
||||
val section = config.getConfigurationSection(path) ?: return type.defaultPenalty
|
||||
|
||||
val penaltyIncrease = section.getBoolean("penalty_increase", true)
|
||||
val penaltyAdditive = section.getBoolean("penalty_additive", true)
|
||||
val penaltyIncrease = section.getBoolean(WORK_PENALTY_INCREASE, true)
|
||||
val penaltyAdditive = section.getBoolean(WORK_PENALTY_ADDITIVE, true)
|
||||
|
||||
return WorkPenaltyPart(penaltyIncrease, penaltyAdditive)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,7 +91,12 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
event.result = resultItem
|
||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, recipe.xpCostPerCraft * amount, true)
|
||||
|
||||
// Maybe add an option on custom craft to ignore/not ignore penalty ??
|
||||
var xpCost = recipe.xpCostPerCraft * amount
|
||||
xpCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.CUSTOM_CRAFT)
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, xpCost, true)
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,29 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
enum class AnvilUseType(val typeName: String) {
|
||||
import org.bukkit.Material
|
||||
import xyz.alexcrea.cuanvil.config.WorkPenaltyType.WorkPenaltyPart
|
||||
|
||||
RENAME_ONLY("rename_only"),
|
||||
MERGE("merge"),
|
||||
UNIT_REPAIR("unit_repair"),
|
||||
enum class AnvilUseType(val typeName: String,
|
||||
val defaultPenalty: WorkPenaltyPart,
|
||||
val displayName: String, val displayMat: Material
|
||||
) {
|
||||
|
||||
RENAME_ONLY("rename_only",
|
||||
WorkPenaltyPart(false, true),
|
||||
"Rename Only", Material.NAME_TAG
|
||||
),
|
||||
MERGE("merge",
|
||||
WorkPenaltyPart(true, true),
|
||||
"Merge", Material.ANVIL
|
||||
),
|
||||
UNIT_REPAIR("unit_repair",
|
||||
WorkPenaltyPart(true, true),
|
||||
"Unit Repair", Material.DIAMOND
|
||||
),
|
||||
CUSTOM_CRAFT("custom_craft",
|
||||
WorkPenaltyPart(false, false),
|
||||
"Custom Craft", Material.CRAFTING_TABLE
|
||||
),
|
||||
;
|
||||
|
||||
}
|
||||
|
|
@ -84,9 +84,8 @@ object AnvilXpUtil {
|
|||
if (right == null) 0
|
||||
else (right.itemMeta as? Repairable)?.repairCost ?: 0
|
||||
|
||||
|
||||
// Increase penalty on fusing or unit repair
|
||||
if(penaltyType.penaltyIncrease && (right != null || AnvilUseType.UNIT_REPAIR == useType)){
|
||||
if(penaltyType.penaltyIncrease){
|
||||
result.itemMeta?.let {
|
||||
(it as? Repairable)?.repairCost = leftPenalty * 2 + 1
|
||||
result.itemMeta = it
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue