diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java index 12953e8..9e04c5a 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java @@ -22,7 +22,7 @@ public class BoolSettingsGui extends AbstractSettingGui{ private final boolean before; private boolean now; - private BoolSettingsGui(BoolSettingFactory holder, boolean now) { + protected BoolSettingsGui(BoolSettingFactory holder, boolean now) { super(3, holder.title, holder.parent); this.holder = holder; this.before = now; @@ -129,9 +129,10 @@ public class BoolSettingsGui extends AbstractSettingGui{ @NotNull String title; ValueUpdatableGui parent; boolean defaultVal; - private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent, - String configPath, ConfigHolder config, - boolean defaultVal){ + protected BoolSettingFactory( + @NotNull String title, ValueUpdatableGui parent, + String configPath, ConfigHolder config, + boolean defaultVal){ super(configPath, config); this.title = title; this.parent = parent; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java new file mode 100644 index 0000000..28ae717 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java @@ -0,0 +1,223 @@ +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 org.bukkit.Material; +import org.bukkit.event.inventory.InventoryClickEvent; +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.gui.ValueUpdatableGui; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; + +import java.util.Arrays; +import java.util.function.Consumer; + +public class EnchantCostSettingsGui extends IntSettingsGui { + + protected final static String ITEM_PATH = ".item"; + protected final static String BOOK_PATH = ".book"; + + private int beforeBook = -1; + private int nowBook = -1; + + protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem, int nowBook) { + super(holder, nowItem); + + this.beforeBook = nowBook; + this.nowBook = nowBook; + + this.step = holder.steps[0]; + + updateValueDisplay(); + initStepsValue(); + } + + + @Override + public Pattern getGuiPattern() { + return new Pattern( + "abc1bMVP0", + "D001s-v+0", + "B0010000S" + ); + } + + @Override + protected void prepareReturnToDefault(){ + ItemStack item = new ItemStack(Material.COMMAND_BLOCK); + ItemMeta meta = item.getItemMeta(); + + // assume holder is an instance of EnchantCostSettingFactory + EnchantCostSettingFactory holder = (EnchantCostSettingFactory) this.holder; + + meta.setDisplayName("\u00A7eReset to default value"); + meta.setLore(Arrays.asList( + "\u00A77Default book value is: " + holder.defaultBookVal, + "\u00A77Default item value is: " + holder.defaultVal)); + item.setItemMeta(meta); + returnToDefault = new GuiItem(item, event -> { + event.setCancelled(true); + nowBook = holder.defaultBookVal; + now = holder.defaultVal; + updateValueDisplay(); + update(); + }, CustomAnvil.instance); + } + + @Override + protected void updateValueDisplay(){ + super.updateValueDisplay(); + PatternPane pane = getPane(); + + // assume holder is an instance of EnchantCostSettingFactory + EnchantCostSettingFactory holder = ((EnchantCostSettingFactory) this.holder); + + int nowBook; + if(this.nowBook == -1){ + nowBook = holder.getConfiguredBookValue(); + }else{ + nowBook = this.nowBook; + } + + // minus item + GuiItem minusItem; + if(nowBook > holder.min){ + int planned = Math.max(holder.min, nowBook - step); + ItemStack item = new ItemStack(Material.RED_TERRACOTTA); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7c-"+(nowBook-planned)+"\u00A7r)"); + meta.setLore(AbstractSettingGui.CLICK_LORE); + item.setItemMeta(meta); + + minusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance); + }else{ + minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); + } + pane.bindItem('M', minusItem); + + //plus item + GuiItem plusItem; + if(nowBook < holder.max){ + int planned = Math.min(holder.max, nowBook + step); + ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-nowBook)+"\u00A7r)"); + meta.setLore(AbstractSettingGui.CLICK_LORE); + item.setItemMeta(meta); + + plusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance); + }else{ + plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); + } + pane.bindItem('P', plusItem); + + // "result" display + ItemStack resultPaper = new ItemStack(Material.PAPER); + ItemMeta resultMeta = resultPaper.getItemMeta(); + resultMeta.setDisplayName("\u00A7eValue: "+nowBook); + resultPaper.setItemMeta(resultMeta); + GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance); + + pane.bindItem('V', resultItem); + + // reset to default + GuiItem returnToDefault; + if(now != holder.defaultVal || nowBook != holder.defaultBookVal){ + returnToDefault = this.returnToDefault; + }else{ + returnToDefault = GuiGlobalItems.backgroundItem(); + } + pane.bindItem('D', returnToDefault); + + + } + + protected Consumer updateNowBookConsumer(int planned){ + return event->{ + event.setCancelled(true); + nowBook = planned; + updateValueDisplay(); + update(); + }; + } + + @Override + protected char getMidStepChar() { + return 'b'; + } + + @Override + public boolean onSave() { + if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + holder.config.getConfig().set(holder.configPath+ITEM_PATH, now); + holder.config.getConfig().set(holder.configPath+BOOK_PATH, now); + return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE); + } + return true; + } + + @Override + public boolean hadChange() { + return super.hadChange() || nowBook != beforeBook; + } + + public static EnchantCostSettingFactory factory(@NotNull String title, ValueUpdatableGui parent, + String configPath, ConfigHolder config, + int min, int max, int defaultItemVal, int defaultBookVal, + int... steps){ + return new EnchantCostSettingFactory( + title,parent, + configPath, config, + min, max, defaultItemVal, defaultBookVal, steps); + } + + + public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory { + + int defaultBookVal; + + protected EnchantCostSettingFactory( + @NotNull String title, ValueUpdatableGui parent, + String configPath, ConfigHolder config, + int min, int max, int defaultItemVal, int defaultBookVal, + int... steps){ + + super(title,parent, + configPath, config, + min, max, defaultItemVal, steps); + this.defaultBookVal = defaultBookVal; + } + + @NotNull + public String getTitle() { + return title; + } + + @Override + public int getConfiguredValue() { + return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal); + } + + public int getConfiguredBookValue(){ + return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal); + } + + @Override + public AbstractSettingGui create() { + // Get value or default + int nowItem = getConfiguredValue(); + // Get value or default + int nowBook = getConfiguredValue(); + // create new gui + return new EnchantCostSettingsGui(this, nowItem, nowBook); + } + + } + +} + diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java index 9fcb5c6..c01a9ff 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java @@ -20,16 +20,16 @@ import java.util.function.Consumer; public class IntSettingsGui extends AbstractSettingGui{ - private final IntSettingFactory holder; - private final int before; - private int now; - private int step; + protected final IntSettingFactory holder; + protected final int before; + protected int now; + protected int step; - private IntSettingsGui(IntSettingFactory holder, int now) { + protected IntSettingsGui(IntSettingFactory holder, int now) { super(3, holder.title, holder.parent); assert holder.steps.length > 0 && holder.steps.length <= 9; this.holder = holder; - this.before =now; + this.before = now; this.now = now; this.step = holder.steps[0]; @@ -48,7 +48,7 @@ public class IntSettingsGui extends AbstractSettingGui{ ); } - GuiItem returnToDefault; + protected GuiItem returnToDefault; protected void prepareReturnToDefault(){ ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemMeta meta = item.getItemMeta(); @@ -136,7 +136,7 @@ public class IntSettingsGui extends AbstractSettingGui{ GuiItem background = GuiGlobalItems.backgroundItem(); PatternPane pane = getPane(); - for (char i = 'a'; i < 'a'+9; i++) { + for (char i = 'a'; i < (getMidStepChar()-'a')*2+1; i++) { pane.bindItem(i, background); } // Then update legit step values @@ -145,7 +145,7 @@ public class IntSettingsGui extends AbstractSettingGui{ protected void updateStepValue(){ if(holder.steps.length <= 1) return; // We assume steps have a length of 2k+1 cause its more pretty - char val = 'e'; // e is the middle, maybe rework this part to remove magic number. + char val = getMidStepChar(); // Offset val -= (char) ((holder.steps.length-1)/2); @@ -157,6 +157,10 @@ public class IntSettingsGui extends AbstractSettingGui{ } + protected char getMidStepChar(){ + return 'e'; + } + protected GuiItem stepGuiItem(int stepIndex){ int stepValue = holder.steps[stepIndex]; @@ -227,9 +231,10 @@ public class IntSettingsGui extends AbstractSettingGui{ @NotNull String title; ValueUpdatableGui parent; int min; int max; int defaultVal; int[] steps; - private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent, - String configPath, ConfigHolder config, - int min, int max, int defaultVal, int... steps){ + protected IntSettingFactory( + @NotNull String title, ValueUpdatableGui parent, + String configPath, ConfigHolder config, + int min, int max, int defaultVal, int... steps){ super(configPath, config); this.title = title; this.parent = parent;