mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
progress on int setting gui.
This commit is contained in:
parent
14456901e4
commit
22e1181b47
6 changed files with 272 additions and 15 deletions
|
|
@ -3,6 +3,7 @@ package xyz.alexcrea.cuanvil.gui;
|
|||
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
|
@ -13,7 +14,7 @@ public class GuiGlobalActions {
|
|||
public static Consumer<InventoryClickEvent> stayInPlace = (event) -> event.setCancelled(true);
|
||||
|
||||
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiFactory(
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiAction(
|
||||
@NotNull Class<? extends Gui> clazz,
|
||||
@NotNull Class<?>[] argClass,
|
||||
@NotNull Object... args){
|
||||
|
|
@ -32,15 +33,35 @@ public class GuiGlobalActions {
|
|||
};
|
||||
}
|
||||
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiFactory(
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiAction(
|
||||
@NotNull Class<? extends Gui> clazz){
|
||||
return openGuiFactory(clazz, new Class<?>[0]);
|
||||
return openGuiAction(clazz, new Class<?>[0]);
|
||||
}
|
||||
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiFactory(@NotNull Gui goal) {
|
||||
public static @NotNull Consumer<InventoryClickEvent> openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory){
|
||||
return event -> {
|
||||
event.setCancelled(true);
|
||||
Gui gui = factory.create();
|
||||
gui.show(event.getWhoClicked());
|
||||
};
|
||||
}
|
||||
|
||||
public static @NotNull Consumer<InventoryClickEvent> openGuiAction(@NotNull Gui goal) {
|
||||
return event -> {
|
||||
event.setCancelled(true);
|
||||
goal.show(event.getWhoClicked());
|
||||
};
|
||||
}
|
||||
|
||||
public static @NotNull Consumer<InventoryClickEvent> saveSettingAction(
|
||||
@NotNull AbstractSettingGui setting,
|
||||
@NotNull Gui goal) {
|
||||
return event -> {
|
||||
event.setCancelled(true);
|
||||
// Save setting
|
||||
setting.onSave();
|
||||
// Then show
|
||||
goal.show(event.getWhoClicked());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,13 +8,14 @@ import org.bukkit.Material;
|
|||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
|
||||
|
||||
// maybe use builder patern ?
|
||||
public class GuiGlobalItems {
|
||||
|
||||
// return
|
||||
public static GuiItem toGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
|
||||
return new GuiItem(item, GuiGlobalActions.openGuiFactory(goal), CustomAnvil.instance);
|
||||
return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
|
||||
}
|
||||
|
||||
// statically create back itemstack
|
||||
|
|
@ -53,4 +54,25 @@ public class GuiGlobalItems {
|
|||
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
|
||||
}
|
||||
|
||||
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_TERRACOTTA;
|
||||
public static GuiItem saveItem(
|
||||
@NotNull AbstractSettingGui setting,
|
||||
@NotNull Gui goal){
|
||||
|
||||
ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName("\u00A7aSave");
|
||||
item.setItemMeta(meta);
|
||||
return new GuiItem(item,
|
||||
GuiGlobalActions.saveSettingAction(setting, goal),
|
||||
CustomAnvil.instance);
|
||||
}
|
||||
|
||||
public static GuiItem openSettingGuiItem(
|
||||
@NotNull ItemStack item,
|
||||
@NotNull AbstractSettingGui.SettingGuiFactory factory
|
||||
){
|
||||
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class MainConfigGui extends ChestGui {
|
|||
Pattern pattern = new Pattern(
|
||||
"000000000",
|
||||
"001234500",
|
||||
"000000000"
|
||||
"Q00000000"
|
||||
);
|
||||
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
|
||||
addPane(pane);
|
||||
|
|
@ -41,14 +41,20 @@ public class MainConfigGui extends ChestGui {
|
|||
GuiItem placeholder5 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
|
||||
|
||||
|
||||
pane.bindItem('2', placeholder1);
|
||||
pane.bindItem('3', placeholder2);
|
||||
pane.bindItem('4', placeholder3);
|
||||
pane.bindItem('5', placeholder4);
|
||||
pane.bindItem('6', placeholder5);
|
||||
pane.bindItem('1', placeholder1);
|
||||
pane.bindItem('2', placeholder2);
|
||||
pane.bindItem('3', placeholder3);
|
||||
pane.bindItem('4', placeholder4);
|
||||
pane.bindItem('5', placeholder5);
|
||||
|
||||
// quit item
|
||||
ItemStack quitItemstack = new ItemStack(Material.BARRIER);
|
||||
GuiItem quitItem = new GuiItem(quitItemstack, event -> {
|
||||
event.setCancelled(true);
|
||||
event.getWhoClicked().closeInventory();
|
||||
},CustomAnvil.instance);
|
||||
pane.bindItem('Q', quitItem);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,16 @@
|
|||
package xyz.alexcrea.cuanvil.gui.config;
|
||||
|
||||
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;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
|
||||
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
|
||||
|
||||
public class BasicConfigGui extends ChestGui {
|
||||
|
||||
|
|
@ -23,7 +28,7 @@ public class BasicConfigGui extends ChestGui {
|
|||
private void init(){
|
||||
Pattern pattern = new Pattern(
|
||||
"000000000",
|
||||
"000000000",
|
||||
"010000000",
|
||||
"B00000000"
|
||||
);
|
||||
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
|
||||
|
|
@ -32,6 +37,12 @@ public class BasicConfigGui extends ChestGui {
|
|||
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
|
||||
GuiGlobalItems.addBackgroundItem(pane);
|
||||
|
||||
ItemStack setting1Item = new ItemStack(Material.STONE);
|
||||
AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", CustomAnvil.instance.getConfig(), 0,42,2,1);
|
||||
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
|
||||
pane.bindItem('1', setting1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
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.type.ChestGui;
|
||||
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
|
||||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
|
||||
|
||||
public abstract class AbstractSettingGui extends ChestGui {
|
||||
|
||||
public AbstractSettingGui(int rows, @NotNull TextHolder title, Gui parent) {
|
||||
super(rows, title, CustomAnvil.instance);
|
||||
initBase(parent);
|
||||
}
|
||||
|
||||
public AbstractSettingGui(int rows, @NotNull String title, Gui parent) {
|
||||
this(rows, StringHolder.of(title), parent);
|
||||
}
|
||||
|
||||
private PatternPane pane;
|
||||
private void initBase(Gui parent){
|
||||
Pattern pattern = getGuiPattern();
|
||||
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
|
||||
addPane(pane);
|
||||
|
||||
GuiGlobalItems.addBackItem(pane, parent);
|
||||
GuiGlobalItems.addBackgroundItem(pane);
|
||||
|
||||
pane.bindItem('S', GuiGlobalItems.saveItem(this, parent));
|
||||
|
||||
}
|
||||
|
||||
protected PatternPane getPane() {
|
||||
return pane;
|
||||
}
|
||||
|
||||
// S, b, 0 is used by: save, back, background
|
||||
protected abstract Pattern getGuiPattern();
|
||||
|
||||
public abstract void onSave();
|
||||
|
||||
|
||||
public abstract static class SettingGuiFactory{
|
||||
public abstract AbstractSettingGui create();
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,147 @@
|
|||
package xyz.alexcrea.cuanvil.gui.config.settings;
|
||||
|
||||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||
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.configuration.ConfigurationSection;
|
||||
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.gui.GuiGlobalActions;
|
||||
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class IntSettingsGui extends AbstractSettingGui{
|
||||
|
||||
private final IntSettingFactory holder;
|
||||
private int now;
|
||||
private int step;
|
||||
|
||||
private IntSettingsGui(IntSettingFactory holder, int now) {
|
||||
super(3, holder.title, holder.parent);
|
||||
assert holder.steps.length > 0 && holder.steps.length <= 9;
|
||||
this.holder = holder;
|
||||
this.now = now;
|
||||
this.step = holder.steps[0];
|
||||
|
||||
updateValueDisplay();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Pattern getGuiPattern() {
|
||||
return new Pattern(
|
||||
"000000000",
|
||||
"00-0v0+00",
|
||||
"B0000000S"
|
||||
);
|
||||
}
|
||||
|
||||
protected void updateValueDisplay(){
|
||||
|
||||
PatternPane pane = getPane();
|
||||
|
||||
// minus item
|
||||
GuiItem minusItem;
|
||||
if(now > holder.min){
|
||||
int planned = Math.max(holder.min, now - step);
|
||||
ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(planned + " (-"+(now-planned)+")"); //TODO add color
|
||||
item.setItemMeta(meta);
|
||||
|
||||
minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
|
||||
}else{
|
||||
minusItem = GuiGlobalItems.backgroundItem();
|
||||
}
|
||||
pane.bindItem('-', minusItem);
|
||||
|
||||
//plus item
|
||||
// may do a function to generalise ?
|
||||
GuiItem plusItem;
|
||||
if(now < holder.max){
|
||||
int planned = Math.min(holder.max, now + step);
|
||||
ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(planned + " (+"+(planned-now)+")"); //TODO add color
|
||||
item.setItemMeta(meta);
|
||||
|
||||
plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
|
||||
}else{
|
||||
plusItem = GuiGlobalItems.backgroundItem();
|
||||
}
|
||||
pane.bindItem('+', plusItem);
|
||||
|
||||
// "result" display
|
||||
ItemStack resultPaper = new ItemStack(Material.PAPER);
|
||||
ItemMeta resultMeta = resultPaper.getItemMeta();
|
||||
resultMeta.setDisplayName(""+now); //TODO color and text
|
||||
resultPaper.setItemMeta(resultMeta);
|
||||
GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
|
||||
|
||||
pane.bindItem('v', resultItem);
|
||||
|
||||
}
|
||||
|
||||
protected Consumer<InventoryClickEvent> updateNowConsumer(int planned){
|
||||
return event->{
|
||||
event.setCancelled(true);
|
||||
now = planned;
|
||||
updateValueDisplay();
|
||||
update();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSave() {
|
||||
holder.section.set(holder.configPath, now);
|
||||
// TODO SAVE (also backup before)
|
||||
|
||||
}
|
||||
|
||||
public static SettingGuiFactory factory(@NotNull String title, Gui parent,
|
||||
String configPath, ConfigurationSection section,
|
||||
int min, int max, int defaultVal, int... steps){
|
||||
return new IntSettingFactory(
|
||||
title,parent,
|
||||
configPath, section,
|
||||
min, max, defaultVal, steps);
|
||||
}
|
||||
|
||||
|
||||
public static class IntSettingFactory extends SettingGuiFactory{
|
||||
@NotNull String title; Gui parent;
|
||||
String configPath; ConfigurationSection section;
|
||||
int min; int max; int defaultVal; int[] steps;
|
||||
|
||||
private IntSettingFactory(@NotNull String title, Gui parent,
|
||||
String configPath, ConfigurationSection section,
|
||||
int min, int max, int defaultVal, int... steps){
|
||||
this.title = title;
|
||||
this.parent = parent;
|
||||
this.configPath = configPath;
|
||||
this.section = section;
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.defaultVal = defaultVal;
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractSettingGui create() {
|
||||
// Get value or default
|
||||
//TODO maybe get section dynamically (and maybe same for save ?)
|
||||
int now = section.getInt(this.configPath, this.defaultVal);
|
||||
// create new gui
|
||||
return new IntSettingsGui(this, now);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue