save buton now only display when change happen.

This commit is contained in:
alexcrea 2024-03-01 21:09:05 +01:00 committed by alexcrea
parent 6e0d1037bc
commit 75e75d92e6
5 changed files with 50 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.adventuresupport.StringHolder;
import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
@ -33,6 +34,8 @@ public abstract class AbstractSettingGui extends ChestGui {
this(rows, StringHolder.of(title), parent);
}
protected GuiItem saveItem;
protected GuiItem noChangeItem;
private void initBase(ValueUpdatableGui parent){
Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
@ -41,8 +44,17 @@ public abstract class AbstractSettingGui extends ChestGui {
GuiGlobalItems.addBackItem(pane, parent);
GuiGlobalItems.addBackgroundItem(pane);
pane.bindItem('S', GuiGlobalItems.saveItem(this, parent));
saveItem = GuiGlobalItems.saveItem(this, parent);
noChangeItem = GuiGlobalItems.noChangeItem();
pane.bindItem('S', noChangeItem);
}
@Override
public void update() {
pane.bindItem('S', hadChange() ? saveItem : noChangeItem);
super.update();
}
protected PatternPane getPane() {
@ -54,6 +66,8 @@ public abstract class AbstractSettingGui extends ChestGui {
public abstract boolean onSave();
public abstract boolean hadChange();
public abstract static class SettingGuiFactory{
protected String configPath;

View file

@ -17,11 +17,13 @@ import java.util.function.Consumer;
public class BoolSettingsGui extends AbstractSettingGui{
private final BoolSettingFactory holder;
private final boolean before;
private boolean now;
private BoolSettingsGui(BoolSettingFactory holder, boolean now) {
super(3, holder.title, holder.parent);
this.holder = holder;
this.before = now;
this.now = now;
updateValueDisplay();
@ -33,7 +35,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
return new Pattern(
"000000000",
"00-0v0+00",
"B0000000S"
"BD000000S"
);
}
@ -81,6 +83,11 @@ public class BoolSettingsGui extends AbstractSettingGui{
return true;
}
@Override
public boolean hadChange() {
return now != before;
}
public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
boolean defaultVal){

View file

@ -21,6 +21,7 @@ import java.util.function.Consumer;
public class IntSettingsGui extends AbstractSettingGui{
private final IntSettingFactory holder;
private final int before;
private int now;
private int step;
@ -28,6 +29,7 @@ public class IntSettingsGui extends AbstractSettingGui{
super(3, holder.title, holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder;
this.before =now;
this.now = now;
this.step = holder.steps[0];
@ -41,7 +43,7 @@ public class IntSettingsGui extends AbstractSettingGui{
return new Pattern(
"abcdefghi",
"00-0v0+00",
"B0000000S"
"BD000000S"
);
}
@ -55,7 +57,7 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.max(holder.min, now - step);
ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)");
meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
@ -72,7 +74,7 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.min(holder.max, now + step);
ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)");
meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
@ -85,7 +87,7 @@ public class IntSettingsGui extends AbstractSettingGui{
// "result" display
ItemStack resultPaper = new ItemStack(Material.PAPER);
ItemMeta resultMeta = resultPaper.getItemMeta();
resultMeta.setDisplayName("\u00A7e"+now);
resultMeta.setDisplayName("\u00A7eValue: "+now);
resultPaper.setItemMeta(resultMeta);
GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
@ -179,6 +181,11 @@ public class IntSettingsGui extends AbstractSettingGui{
return true;
}
@Override
public boolean hadChange() {
return now != before;
}
public static IntSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){

View file

@ -59,7 +59,8 @@ public class GuiGlobalItems {
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
}
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_TERRACOTTA;
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE;
private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE;
public static GuiItem saveItem(
@NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal){
@ -73,6 +74,19 @@ public class GuiGlobalItems {
CustomAnvil.instance);
}
private static final GuiItem NO_CHANGE_ITEM;
static {
ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A77No change. can't save.");
item.setItemMeta(meta);
NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
}
public static GuiItem noChangeItem(){
return NO_CHANGE_ITEM;
}
public static GuiItem openSettingGuiItem(
@NotNull ItemStack item,
@NotNull AbstractSettingGui.SettingGuiFactory factory