create interface SettingGui and used it insted of AstractSettingGui.

This commit is contained in:
alexcrea 2024-06-17 20:49:44 +02:00
parent db1dfdf144
commit 33fac3eed4
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
11 changed files with 58 additions and 39 deletions

View file

@ -6,7 +6,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment; import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import java.util.Collections; import java.util.Collections;
@ -18,7 +18,7 @@ import java.util.function.Consumer;
* *
* @param <T> Type of the factory of the type of setting the gui should edit. * @param <T> Type of the factory of the type of setting the gui should edit.
*/ */
public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<WrappedEnchantment, T> implements ValueUpdatableGui { public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<WrappedEnchantment, T> implements ValueUpdatableGui {
/** /**
* Constructor for a gui displaying available enchantment to edit a enchantment setting. * Constructor for a gui displaying available enchantment to edit a enchantment setting.

View file

@ -7,13 +7,13 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
public abstract class SettingGuiListConfigGui< T, S extends AbstractSettingGui.SettingGuiFactory> extends ElementListConfigGui< T >{ public abstract class SettingGuiListConfigGui< T, S extends SettingGui.SettingGuiFactory> extends ElementListConfigGui< T >{
protected HashMap<T, GuiItem> guiItemMap; protected HashMap<T, GuiItem> guiItemMap;
protected HashMap<T, S> factoryMap; protected HashMap<T, S> factoryMap;

View file

@ -15,7 +15,7 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
/** /**
* An instance gui used to edit a setting. * An instance gui used to edit a setting.
*/ */
public abstract class AbstractSettingGui extends ChestGui { public abstract class AbstractSettingGui extends ChestGui implements SettingGui {
protected static final String CLICK_LORE = "\u00A77Click Here to change the value"; protected static final String CLICK_LORE = "\u00A77Click Here to change the value";
@ -95,27 +95,12 @@ public abstract class AbstractSettingGui extends ChestGui {
*/ */
protected abstract Pattern getGuiPattern(); protected abstract Pattern getGuiPattern();
/**
* Called when the associated setting need to be saved.
*
* @return true if the save was successful. false otherwise
*/
public abstract boolean onSave();
/**
* If this function return true
* the gui assume the associated setting can be saved.
*
* @return true if there is a change to the setting. false otherwise
*/
public abstract boolean hadChange();
/** /**
* Most of the time a setting gui will be called from a global gui. * Most of the time a setting gui will be called from a global gui.
* <p> * <p>
* It is better to keep a factory that hold setting data than find what parameters to use every time. * It is better to keep a factory that hold setting data than find what parameters to use every time.
*/ */
public abstract static class SettingGuiFactory { public abstract static class SettingGuiFactory implements SettingGui.SettingGuiFactory {
@NotNull @NotNull
protected String configPath; protected String configPath;
@NotNull @NotNull
@ -148,11 +133,5 @@ public abstract class AbstractSettingGui extends ChestGui {
return config; return config;
} }
/**
* Create a gui using setting parameters and current setting value.
*
* @return A new instance of the implemented setting gui.
*/
public abstract AbstractSettingGui create();
} }
} }

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings; package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; 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.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -233,7 +234,7 @@ public class BoolSettingsGui extends AbstractSettingGui {
} }
@Override @Override
public AbstractSettingGui create() { public Gui create() {
// Get current value or default // Get current value or default
boolean now = getConfiguredValue(); boolean now = getConfiguredValue();
// create new gui // create new gui

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings; package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; 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.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -471,7 +472,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
} }
@Override @Override
public AbstractSettingGui create() { public Gui create() {
// Get value or default // Get value or default
BigDecimal now = getConfiguredValue(); BigDecimal now = getConfiguredValue();
// create new gui // create new gui

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings; package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; 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.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -321,7 +322,7 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
} }
@Override @Override
public AbstractSettingGui create() { public Gui create() {
// Get value or default // Get value or default
int nowItem = getConfiguredValue(); int nowItem = getConfiguredValue();
// create new gui // create new gui

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings; package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; 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.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -370,7 +371,7 @@ public class IntSettingsGui extends AbstractSettingGui {
} }
@Override @Override
public AbstractSettingGui create() { public Gui create() {
// Get value or default // Get value or default
int now = getConfiguredValue(); int now = getConfiguredValue();
// create new gui // create new gui

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings; package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; 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.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -240,7 +241,7 @@ public class ItemSettingGui extends AbstractSettingGui {
} }
@Override @Override
public AbstractSettingGui create() { public Gui create() {
// Get current value or default // Get current value or default
ItemStack now = getConfiguredValue(); ItemStack now = getConfiguredValue();
// create new gui // create new gui

View file

@ -0,0 +1,33 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
public interface SettingGui {
/**
* Called when the associated setting need to be saved.
*
* @return true if the save was successful. false otherwise
*/
boolean onSave();
/**
* If this function return true
* the gui assume the associated setting can be saved.
*
* @return true if there is a change to the setting. false otherwise
*/
boolean hadChange();
interface SettingGuiFactory {
/**
* Create a gui using setting parameters and current setting value.
*
* @return A new instance of the implemented setting gui.
*/
Gui create();
}
}

View file

@ -7,6 +7,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -17,12 +18,12 @@ import java.util.function.Consumer;
*/ */
public class GuiGlobalActions { public class GuiGlobalActions {
public final static String NO_EDIT_PERM = "§cYou do not have permission to edit the config"; public static final String NO_EDIT_PERM = "§cYou do not have permission to edit the config";
/** /**
* A Consumer that should be used if the item goal is to do nothing on click. * A Consumer that should be used if the item goal is to do nothing on click.
*/ */
public final static Consumer<InventoryClickEvent> stayInPlace = (event) -> event.setCancelled(true); public static final Consumer<InventoryClickEvent> stayInPlace = event -> event.setCancelled(true);
/** /**
* Create a consumer to create and open a new GUI. * Create a consumer to create and open a new GUI.
@ -80,7 +81,7 @@ public class GuiGlobalActions {
* @param factory The setting gui factory. * @param factory The setting gui factory.
* @return A consumer to create and open a new setting GUI. * @return A consumer to create and open a new setting GUI.
*/ */
public static @NotNull Consumer<InventoryClickEvent> openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory) { public static @NotNull Consumer<InventoryClickEvent> openSettingGuiAction(SettingGui.SettingGuiFactory factory) {
return event -> { return event -> {
event.setCancelled(true); event.setCancelled(true);
Gui gui = factory.create(); Gui gui = factory.create();
@ -119,7 +120,7 @@ public class GuiGlobalActions {
* @return A consumer to open a global GUI. * @return A consumer to open a global GUI.
*/ */
public static @NotNull Consumer<InventoryClickEvent> saveSettingAction( public static @NotNull Consumer<InventoryClickEvent> saveSettingAction(
@NotNull AbstractSettingGui setting, @NotNull SettingGui setting,
@NotNull ValueUpdatableGui goal) { @NotNull ValueUpdatableGui goal) {
return event -> { return event -> {
event.setCancelled(true); event.setCancelled(true);

View file

@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -132,7 +133,7 @@ public class GuiGlobalItems {
* @return A save setting item. * @return A save setting item.
*/ */
public static GuiItem saveItem( public static GuiItem saveItem(
@NotNull AbstractSettingGui setting, @NotNull SettingGui setting,
@NotNull ValueUpdatableGui goal) { @NotNull ValueUpdatableGui goal) {
ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM); ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM);
@ -179,7 +180,7 @@ public class GuiGlobalItems {
*/ */
public static GuiItem openSettingGuiItem( public static GuiItem openSettingGuiItem(
@NotNull ItemStack item, @NotNull ItemStack item,
@NotNull AbstractSettingGui.SettingGuiFactory factory @NotNull SettingGui.SettingGuiFactory factory
) { ) {
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance); return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
} }
@ -199,7 +200,7 @@ public class GuiGlobalItems {
* @return A formatted GuiItem that will create and open a GUI for the setting. * @return A formatted GuiItem that will create and open a GUI for the setting.
*/ */
public static GuiItem createGuiItemFromProperties( public static GuiItem createGuiItemFromProperties(
@NotNull AbstractSettingGui.SettingGuiFactory factory, @NotNull SettingGui.SettingGuiFactory factory,
@NotNull Material itemMat, @NotNull Material itemMat,
@NotNull StringBuilder itemName, @NotNull StringBuilder itemName,
@NotNull Object value, @NotNull Object value,