add step selector for int setting gui.

This commit is contained in:
alexcrea 2024-03-01 02:02:52 +01:00
parent de39968896
commit 942e7e861e
2 changed files with 75 additions and 4 deletions

View file

@ -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);

View file

@ -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<String> stepLore;
Consumer<InventoryClickEvent> 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<InventoryClickEvent> 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){