From f0ec2151b36a51aaa8821ea6e42d45a57150ef64 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Fri, 1 Mar 2024 02:02:52 +0100 Subject: [PATCH] add step selector for int setting gui. --- .../cuanvil/gui/config/BasicConfigGui.java | 2 +- .../gui/config/settings/IntSettingsGui.java | 77 ++++++++++++++++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java index 6f0da4c..46f2298 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java @@ -47,7 +47,7 @@ public class BasicConfigGui extends ValueUpdatableGui { public void updateGuiValues() { // Update item with value ItemStack setting1Item = new ItemStack(Material.STONE); - AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,42,2,1); + AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,255,2,1, 5, 10, 50, 100); GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1); pane.bindItem('1', setting1); 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 387d086..da60bf0 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 @@ -14,6 +14,8 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; +import java.util.Collections; +import java.util.List; import java.util.function.Consumer; public class IntSettingsGui extends AbstractSettingGui{ @@ -30,13 +32,14 @@ public class IntSettingsGui extends AbstractSettingGui{ this.step = holder.steps[0]; updateValueDisplay(); + initStepsValue(); } @Override public Pattern getGuiPattern() { return new Pattern( - "000000000", + "abcdefghi", "00-0v0+00", "B0000000S" ); @@ -58,7 +61,7 @@ public class IntSettingsGui extends AbstractSettingGui{ minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance); }else{ - minusItem = GuiGlobalItems.backgroundItem(); + minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('-', minusItem); @@ -75,7 +78,7 @@ public class IntSettingsGui extends AbstractSettingGui{ plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance); }else{ - plusItem = GuiGlobalItems.backgroundItem(); + plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('+', plusItem); @@ -99,6 +102,74 @@ public class IntSettingsGui extends AbstractSettingGui{ }; } + protected void initStepsValue(){ + // Put background glass on the background of 'a' to 'b' + GuiItem background = GuiGlobalItems.backgroundItem(); + PatternPane pane = getPane(); + + for (char i = 'a'; i < 'a'+9; i++) { + pane.bindItem(i, background); + } + // Then update legit step values + updateStepValue(); + } + 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. + // Offset + val -= (char) ((holder.steps.length-1)/2); + + // Then place items + PatternPane pane = getPane(); + for (int i = 0; i < holder.steps.length; i++) { + pane.bindItem(val+i, stepGuiItem(i)); + } + + } + + protected GuiItem stepGuiItem(int stepIndex){ + int stepValue = holder.steps[stepIndex]; + + // Get material properties + Material stepMat; + StringBuilder stepName = new StringBuilder("\u00A7"); + List stepLore; + Consumer clickEvent; + if(stepValue == step){ + stepMat = Material.GREEN_STAINED_GLASS_PANE; + stepName.append('a'); + stepLore = Collections.singletonList("\u00A77Value is changing by a step of "+stepValue); + clickEvent = GuiGlobalActions.stayInPlace; + }else{ + stepMat = Material.RED_STAINED_GLASS_PANE; + stepName.append('c'); + stepLore = Collections.singletonList("\u00A77Click here to change the value of a step by "+stepValue); + clickEvent = updateStepValue(stepValue); + } + stepName.append("Step of: ").append(stepValue); + + // Create item stack then gui item + ItemStack item = new ItemStack(stepMat); + ItemMeta meta = item.getItemMeta(); + + meta.setDisplayName(stepName.toString()); + meta.setLore(stepLore); + item.setItemMeta(meta); + + return new GuiItem(item, clickEvent, CustomAnvil.instance); + } + + protected Consumer updateStepValue(int stepValue){ + return event -> { + event.setCancelled(true); + this.step = stepValue; + updateStepValue(); + updateValueDisplay(); + update(); + }; + } + @Override public boolean onSave() { if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){