Add javadoc to most of the config classes (#3)

This commit is contained in:
alexcrea 2024-03-19 02:48:18 +01:00 committed by GitHub
commit c6bc94604c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 520 additions and 63 deletions

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config; package xyz.alexcrea.cuanvil.gui.config;
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.Orientable; import com.github.stefvanschie.inventoryframework.pane.Orientable;
import com.github.stefvanschie.inventoryframework.pane.OutlinePane; import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
import com.github.stefvanschie.inventoryframework.pane.Pane; import com.github.stefvanschie.inventoryframework.pane.Pane;
@ -19,16 +20,40 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
/**
* Abstract Global Config gui for enchantment setting configuration.
* @param <T> Type of the factory of the type of setting the gui should edit.
*/
public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends ValueUpdatableGui { public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends ValueUpdatableGui {
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
protected AbstractEnchantConfigGui(String title){ private final Gui backGui;
/**
* Constructor for a gui displaying available enchantment to edit a enchantment setting.
* @param title Title of the gui.
* @param backGui Gui to go back on click on the "back" button.
*/
protected AbstractEnchantConfigGui(String title, Gui backGui){
super(6, title, CustomAnvil.instance); super(6, title, CustomAnvil.instance);
this.backGui = backGui;
}
/**
* Constructor for a gui displaying available enchantment to edit a enchantment setting.
* @param title Title of the gui.
*/
protected AbstractEnchantConfigGui(String title){
this(title, MainConfigGui.INSTANCE);
} }
PatternPane backItems; PatternPane backgroundItems;
OutlinePane filledEnchant; OutlinePane filledEnchant;
// Why is called like it is rn
/**
* Initialise value updatable gui pattern
*/
protected void init(){ protected void init(){
// Back item panel // Back item panel
Pattern pattern = new Pattern( Pattern pattern = new Pattern(
@ -39,25 +64,29 @@ public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.Sett
"000000000", "000000000",
"B11111111" "B11111111"
); );
backItems = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); this.backgroundItems = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern);
addPane(backItems); addPane(this.backgroundItems);
GuiGlobalItems.addBackItem(backItems, MainConfigGui.INSTANCE); GuiGlobalItems.addBackItem(this.backgroundItems, this.backGui);
GuiGlobalItems.addBackgroundItem(backItems); GuiGlobalItems.addBackgroundItem(this.backgroundItems);
backItems.bindItem('1', GuiGlobalItems.backgroundItem(SECONDARY_BACKGROUND_MATERIAL)); this.backgroundItems.bindItem('1', GuiGlobalItems.backgroundItem(SECONDARY_BACKGROUND_MATERIAL));
// enchant item panel // enchant item panel
filledEnchant = new OutlinePane(0, 0, 9, 5); this.filledEnchant = new OutlinePane(0, 0, 9, 5);
filledEnchant.align(OutlinePane.Alignment.BEGIN); this.filledEnchant.align(OutlinePane.Alignment.BEGIN);
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); this.filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
addPane(filledEnchant); addPane(this.filledEnchant);
prepareValues(); prepareValues();
updateGuiValues(); updateGuiValues();
} }
private List<T> bookItemFactoryList; private List<T> bookItemFactoryList;
/**
* Prepare enchantment config gui displayed items factory.
*/
protected void prepareValues(){ protected void prepareValues(){
bookItemFactoryList = new ArrayList<>(); bookItemFactoryList = new ArrayList<>();

View file

@ -19,6 +19,9 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
/**
* Global config to edit basic basic settings.
*/
public class BasicConfigGui extends ValueUpdatableGui { public class BasicConfigGui extends ValueUpdatableGui {
public final static BasicConfigGui INSTANCE = new BasicConfigGui(); public final static BasicConfigGui INSTANCE = new BasicConfigGui();
@ -27,12 +30,18 @@ public class BasicConfigGui extends ValueUpdatableGui {
INSTANCE.init(); INSTANCE.init();
} }
/**
* Constructor of this Global gui for basic settings.
*/
private BasicConfigGui(){ private BasicConfigGui(){
super(3, "\u00A78Basic Config", CustomAnvil.instance); super(3, "\u00A78Basic Config", CustomAnvil.instance);
} }
PatternPane pane; PatternPane pane;
/**
* Initialise Basic gui
*/
private void init(){ private void init(){
Pattern pattern = new Pattern( Pattern pattern = new Pattern(
"000000000", "000000000",
@ -58,6 +67,9 @@ public class BasicConfigGui extends ValueUpdatableGui {
private IntSettingsGui.IntSettingFactory itemRenameCost; private IntSettingsGui.IntSettingFactory itemRenameCost;
private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost; private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost;
/**
* Prepare basic gui displayed items factory and static items..
*/
protected void prepareValues(){ protected void prepareValues(){
// limit repair item // limit repair item
this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?",this, this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?",this,

View file

@ -10,11 +10,14 @@ import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.StringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
/**
* Global Config gui for enchantment cost settings.
*/
public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSettingsGui.EnchantCostSettingFactory> { public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSettingsGui.EnchantCostSettingFactory> {
private final static String SECTION_NAME = "enchant_values"; private final static String SECTION_NAME = "enchant_values";
@ -25,6 +28,9 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
INSTANCE.init(); INSTANCE.init();
} }
/**
* Constructor of this Global gui for enchantment cost settings.
*/
private EnchantCostConfigGui() { private EnchantCostConfigGui() {
super("\u00A78Enchantment Level Limit"); super("\u00A78Enchantment Level Limit");
@ -33,7 +39,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
@Override @Override
public EnchantCostSettingsGui.EnchantCostSettingFactory getFactoryFromEnchant(Enchantment enchant) { public EnchantCostSettingsGui.EnchantCostSettingFactory getFactoryFromEnchant(Enchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH); String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH);
String prettyKey = StringUtil.snakeToUpperSpacedCase(key); String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
// try to find rarity. default to 0 if not found // try to find rarity. default to 0 if not found
EnchantmentRarity rarity = EnchantmentRarity.NO_RARITY; EnchantmentRarity rarity = EnchantmentRarity.NO_RARITY;
@ -41,7 +47,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity(); rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity();
}catch (IllegalArgumentException ignored){} }catch (IllegalArgumentException ignored){}
return EnchantCostSettingsGui.enchFactory(prettyKey+" Level Cost", this, return EnchantCostSettingsGui.enchantCostFactory(prettyKey+" Level Cost", this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255, SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
rarity.getItemValue(), rarity.getBookValue(), rarity.getItemValue(), rarity.getBookValue(),
1, 10, 50); 1, 10, 50);

View file

@ -6,10 +6,13 @@ import org.bukkit.enchantments.Enchantment;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.StringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Locale; import java.util.Locale;
/**
* Global Config gui for enchantment level limit settings.
*/
public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsGui.IntSettingFactory> { public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsGui.IntSettingFactory> {
private final static String SECTION_NAME = "enchant_limits"; private final static String SECTION_NAME = "enchant_limits";
@ -20,6 +23,9 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
INSTANCE.init(); INSTANCE.init();
} }
/**
* Constructor of this Global gui for enchantment level limit settings.
*/
private EnchantLimitConfigGui() { private EnchantLimitConfigGui() {
super("\u00A78Enchantment Level Limit"); super("\u00A78Enchantment Level Limit");
@ -28,7 +34,7 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
@Override @Override
public IntSettingsGui.IntSettingFactory getFactoryFromEnchant(Enchantment enchant) { public IntSettingsGui.IntSettingFactory getFactoryFromEnchant(Enchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT); String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
String prettyKey = StringUtil.snakeToUpperSpacedCase(key); String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
return IntSettingsGui.intFactory(prettyKey+" Level Limit", this, return IntSettingsGui.intFactory(prettyKey+" Level Limit", this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255, SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,

View file

@ -15,6 +15,9 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/**
* An instance gui used to edit a setting.
*/
public abstract class AbstractSettingGui extends ChestGui { public abstract class AbstractSettingGui extends ChestGui {
// Temporary values, until I get something better. // Temporary values, until I get something better.
@ -25,17 +28,34 @@ public abstract class AbstractSettingGui extends ChestGui {
private PatternPane pane; private PatternPane pane;
/**
* Prepare necessary object for a setting gui.
* @param rows Number of row for this gui.
* @param title Title of this gui.
* @param parent Parent gui to go back when completed.
*/
public AbstractSettingGui(int rows, @NotNull TextHolder title, ValueUpdatableGui parent) { public AbstractSettingGui(int rows, @NotNull TextHolder title, ValueUpdatableGui parent) {
super(rows, title, CustomAnvil.instance); super(rows, title, CustomAnvil.instance);
initBase(parent); initBase(parent);
} }
/**
* Prepare necessary object for a setting gui.
* @param rows Number of row for this gui.
* @param title Title of this gui.
* @param parent Parent gui to go back when completed.
*/
public AbstractSettingGui(int rows, @NotNull String title, ValueUpdatableGui parent) { public AbstractSettingGui(int rows, @NotNull String title, ValueUpdatableGui parent) {
this(rows, StringHolder.of(title), parent); this(rows, StringHolder.of(title), parent);
} }
protected GuiItem saveItem; protected GuiItem saveItem;
protected GuiItem noChangeItem; protected GuiItem noChangeItem;
/**
* Initialise and prepare value for this gui.
* @param parent Parent gui to go back when completed.
*/
private void initBase(ValueUpdatableGui parent){ private void initBase(ValueUpdatableGui parent){
Pattern pattern = getGuiPattern(); Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern); pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
@ -57,35 +77,76 @@ public abstract class AbstractSettingGui extends ChestGui {
super.update(); super.update();
} }
/**
* Get main pane for this setting gui.
* @return Main pattern pain of this gui.
*/
protected PatternPane getPane() { protected PatternPane getPane() {
return pane; return pane;
} }
// S, b, 0 is used by: save, back, background /**
* Used to get the gui pattern.
* Reserved character are:
* <ul>
* <li><b>S</b>: save setting button.</li>
* <li><b>B</b>: "back to previous gui" button.</li>
* <li><b>0</b>: default background item.</li>
* </ul>
* @return The gui's pattern.
*/
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(); 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(); public abstract boolean hadChange();
/**
* Most of the time a setting gui will be called from a global gui.
* <p>
* 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{
protected String configPath; protected String configPath;
protected ConfigHolder config; protected ConfigHolder config;
/**
* Constructor for settings gui factory
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
*/
protected SettingGuiFactory(String configPath, ConfigHolder config){ protected SettingGuiFactory(String configPath, ConfigHolder config){
this.configPath = configPath; this.configPath = configPath;
this.config = config; this.config = config;
} }
/**
* @return Configuration path of this setting.
*/
public String getConfigPath() { public String getConfigPath() {
return configPath; return configPath;
} }
/**
* @return Configuration holder of this setting.
*/
public ConfigHolder getConfigHolder() { public ConfigHolder getConfigHolder() {
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(); public abstract AbstractSettingGui create();
} }
} }

View file

@ -17,14 +17,22 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Collections; import java.util.Collections;
import java.util.function.Consumer; import java.util.function.Consumer;
/**
* An instance of a gui used to edit a boolean setting.
*/
public class BoolSettingsGui extends AbstractSettingGui{ public class BoolSettingsGui extends AbstractSettingGui{
private final BoolSettingFactory holder; private final BoolSettingFactory holder;
private final boolean before; private final boolean before;
private boolean now; private boolean now;
/**
* Create a boolean setting config gui.
* @param holder Configuration factory of this setting.
* @param now The defined value of this setting.
*/
protected BoolSettingsGui(BoolSettingFactory holder, boolean now) { protected BoolSettingsGui(BoolSettingFactory holder, boolean now) {
super(3, holder.title, holder.parent); super(3, holder.getTitle(), holder.parent);
this.holder = holder; this.holder = holder;
this.before = now; this.before = now;
this.now = now; this.now = now;
@ -42,7 +50,12 @@ public class BoolSettingsGui extends AbstractSettingGui{
"B0000000S" "B0000000S"
); );
} }
GuiItem returnToDefault;
protected GuiItem returnToDefault;
/**
* Prepare "return to default value" gui item.
*/
protected void prepareReturnToDefault(){ protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
@ -58,8 +71,10 @@ public class BoolSettingsGui extends AbstractSettingGui{
}, CustomAnvil.instance); }, CustomAnvil.instance);
} }
/**
* Update item using the setting value to match the new value
*/
protected void updateValueDisplay(){ protected void updateValueDisplay(){
PatternPane pane = getPane(); PatternPane pane = getPane();
// Get displayed value for this config. // Get displayed value for this config.
@ -93,6 +108,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
} }
/**
* @return A consumer to update the current setting's value.
*/
protected Consumer<InventoryClickEvent> inverseNowConsumer(){ protected Consumer<InventoryClickEvent> inverseNowConsumer(){
return event->{ return event->{
event.setCancelled(true); event.setCancelled(true);
@ -118,6 +136,15 @@ public class BoolSettingsGui extends AbstractSettingGui{
return now != before; return now != before;
} }
/**
* Create a bool setting factory from setting's parameters.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param defaultVal Default value if not found on the config.
* @return A factory for a boolean setting gui.
*/
public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent, public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
boolean defaultVal){ boolean defaultVal){
@ -127,11 +154,21 @@ public class BoolSettingsGui extends AbstractSettingGui{
defaultVal); defaultVal);
} }
/**
* A factory for a boolean setting gui that hold setting's information.
*/
public static class BoolSettingFactory extends SettingGuiFactory{ public static class BoolSettingFactory extends SettingGuiFactory{
@NotNull String title; ValueUpdatableGui parent; @NotNull String title; ValueUpdatableGui parent;
boolean defaultVal; boolean defaultVal;
/**
* Constructor for a boolean setting gui factory.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param defaultVal Default value if not found on the config.
*/
protected BoolSettingFactory( protected BoolSettingFactory(
@NotNull String title, ValueUpdatableGui parent, @NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
@ -143,18 +180,24 @@ public class BoolSettingsGui extends AbstractSettingGui{
this.defaultVal = defaultVal; this.defaultVal = defaultVal;
} }
/**
* @return Get setting's gui title.
*/
@NotNull @NotNull
public String getTitle() { public String getTitle() {
return title; return title;
} }
/**
* @return The configured value for the associated setting.
*/
public boolean getConfiguredValue(){ public boolean getConfiguredValue(){
return this.config.getConfig().getBoolean(this.configPath, this.defaultVal); return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
} }
@Override @Override
public AbstractSettingGui create() { public AbstractSettingGui create() {
// Get value or default // Get current value or default
boolean now = getConfiguredValue(); boolean now = getConfiguredValue();
// create new gui // create new gui
return new BoolSettingsGui(this, now); return new BoolSettingsGui(this, now);

View file

@ -19,6 +19,10 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer; import java.util.function.Consumer;
/**
* An instance of a gui used to edit an enchantment cost setting.
* May be considered as a 2 int setting.
*/
public class EnchantCostSettingsGui extends IntSettingsGui { public class EnchantCostSettingsGui extends IntSettingsGui {
protected final static String ITEM_PATH = ".item"; protected final static String ITEM_PATH = ".item";
@ -27,6 +31,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
private int beforeBook; private int beforeBook;
private int nowBook; private int nowBook;
/**
* Create an enchantment cost setting config gui.
* @param holder Configuration factory of this setting.
* @param nowItem The defined value of this setting item's value.
*/
protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) { protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) {
super(holder, nowItem); super(holder, nowItem);
@ -35,6 +44,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
initStaticItem(); initStaticItem();
} }
/**
* Initialise step items.
* Also bypassed init sequence to initialise books value
* as it used as one of the first call from the init sequence of the gui
*/
@Override @Override
protected void initStepsValue() { protected void initStepsValue() {
super.initStepsValue(); super.initStepsValue();
@ -53,6 +67,9 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
); );
} }
/**
* Initialise item that should not be updated late.
*/
private void initStaticItem() { private void initStaticItem() {
PatternPane pane = getPane(); PatternPane pane = getPane();
@ -166,6 +183,10 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
} }
/**
* @param planned Value to change current book cost setting to.
* @return A consumer to update the current book cost setting's value.
*/
protected Consumer<InventoryClickEvent> updateNowBookConsumer(int planned){ protected Consumer<InventoryClickEvent> updateNowBookConsumer(int planned){
return event->{ return event->{
event.setCancelled(true); event.setCancelled(true);
@ -197,7 +218,24 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
return super.hadChange() || nowBook != beforeBook; return super.hadChange() || nowBook != beforeBook;
} }
public static EnchantCostSettingFactory enchFactory(@NotNull String title, ValueUpdatableGui parent, /**
* Create an int setting factory from setting's parameters.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultItemVal Default item value if not found on the config.
* @param defaultBookVal Default book value if not found on the config.
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 3 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
* @return A factory for an enchant cost setting gui.
*/
public static EnchantCostSettingFactory enchantCostFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
int min, int max, int defaultItemVal, int defaultBookVal, int min, int max, int defaultItemVal, int defaultBookVal,
int... steps){ int... steps){
@ -207,11 +245,28 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
min, max, defaultItemVal, defaultBookVal, steps); min, max, defaultItemVal, defaultBookVal, steps);
} }
/**
* A factory for an enchantment cost setting gui that hold setting's information.
*/
public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory { public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory {
int defaultBookVal; int defaultBookVal;
/**
* Constructor for an enchantment cost setting gui factory.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultItemVal Default item value if not found on the config.
* @param defaultBookVal Default book value if not found on the config.
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 3 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
*/
protected EnchantCostSettingFactory( protected EnchantCostSettingFactory(
@NotNull String title, ValueUpdatableGui parent, @NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
@ -224,16 +279,17 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
this.defaultBookVal = defaultBookVal; this.defaultBookVal = defaultBookVal;
} }
@NotNull /**
public String getTitle() { * @return The configured value for the enchant setting item value.
return title; */
}
@Override @Override
public int getConfiguredValue() { public int getConfiguredValue() {
return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal); return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal);
} }
/**
* @return The configured value for the enchant setting book value.
*/
public int getConfiguredBookValue(){ public int getConfiguredBookValue(){
return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal); return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal);
} }
@ -249,4 +305,3 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
} }
} }

View file

@ -19,6 +19,9 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
/**
* An instance of a gui used to edit an int setting.
*/
public class IntSettingsGui extends AbstractSettingGui{ public class IntSettingsGui extends AbstractSettingGui{
protected final IntSettingFactory holder; protected final IntSettingFactory holder;
@ -26,8 +29,13 @@ public class IntSettingsGui extends AbstractSettingGui{
protected int now; protected int now;
protected int step; protected int step;
/**
* Create an int setting config gui.
* @param holder Configuration factory of this setting.
* @param now The defined value of this setting.
*/
protected IntSettingsGui(IntSettingFactory holder, int now) { protected IntSettingsGui(IntSettingFactory holder, int now) {
super(3, holder.title, holder.parent); super(3, holder.getTitle(), holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9; assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder; this.holder = holder;
this.before = now; this.before = now;
@ -50,6 +58,9 @@ public class IntSettingsGui extends AbstractSettingGui{
} }
protected GuiItem returnToDefault; protected GuiItem returnToDefault;
/**
* Prepare "return to default value" gui item.
*/
protected void prepareReturnToDefault(){ protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
@ -65,6 +76,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}, CustomAnvil.instance); }, CustomAnvil.instance);
} }
/**
* Update item using the setting value to match the new value.
*/
protected void updateValueDisplay(){ protected void updateValueDisplay(){
PatternPane pane = getPane(); PatternPane pane = getPane();
@ -123,6 +137,10 @@ public class IntSettingsGui extends AbstractSettingGui{
} }
/**
* @param planned Value to change current setting to.
* @return A consumer to update the current setting's value.
*/
protected Consumer<InventoryClickEvent> updateNowConsumer(int planned){ protected Consumer<InventoryClickEvent> updateNowConsumer(int planned){
return event->{ return event->{
event.setCancelled(true); event.setCancelled(true);
@ -132,6 +150,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}; };
} }
/**
* Initialise step items.
*/
protected void initStepsValue(){ protected void initStepsValue(){
// Put background glass on the background of 'a' to 'b' // Put background glass on the background of 'a' to 'b'
GuiItem background = GuiGlobalItems.backgroundItem(); GuiItem background = GuiGlobalItems.backgroundItem();
@ -143,11 +164,15 @@ public class IntSettingsGui extends AbstractSettingGui{
// Then update legit step values // Then update legit step values
updateStepValue(); updateStepValue();
} }
/**
* Update steps items value.
*/
protected void updateStepValue(){ protected void updateStepValue(){
if(holder.steps.length <= 1) return; if(holder.steps.length <= 1) return;
// We assume steps have a length of 2k+1 cause its more pretty // We assume steps have a length of 2k+1 cause its more pretty
char val = getMidStepChar(); char val = getMidStepChar();
// Offset // Offset to start (not the best way to do it)
val -= (char) ((holder.steps.length-1)/2); val -= (char) ((holder.steps.length-1)/2);
// Then place items // Then place items
@ -158,10 +183,19 @@ public class IntSettingsGui extends AbstractSettingGui{
} }
/**
* Step use lower case character from 'a' to a certain char.
* @return The middle value of the character set for steps.
*/
protected char getMidStepChar(){ protected char getMidStepChar(){
return 'e'; return 'e';
} }
/**
* Create a step item from a step value.
* @param stepIndex the index of the step item.
* @return A step item corresponding to its index value.
*/
protected GuiItem stepGuiItem(int stepIndex){ protected GuiItem stepGuiItem(int stepIndex){
int stepValue = holder.steps[stepIndex]; int stepValue = holder.steps[stepIndex];
@ -194,6 +228,10 @@ public class IntSettingsGui extends AbstractSettingGui{
return new GuiItem(item, clickEvent, CustomAnvil.instance); return new GuiItem(item, clickEvent, CustomAnvil.instance);
} }
/**
* @param stepValue Value to change current step to.
* @return A consumer to update the current step of this setting.
*/
protected Consumer<InventoryClickEvent> updateStepValue(int stepValue){ protected Consumer<InventoryClickEvent> updateStepValue(int stepValue){
return event -> { return event -> {
event.setCancelled(true); event.setCancelled(true);
@ -220,6 +258,21 @@ public class IntSettingsGui extends AbstractSettingGui{
return now != before; return now != before;
} }
/**
* Create an int setting factory from setting's parameters.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultVal Default value if not found on the config.
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 5 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
* @return A factory for an int setting gui.
*/
public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent, public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){ int min, int max, int defaultVal, int... steps){
@ -229,11 +282,27 @@ public class IntSettingsGui extends AbstractSettingGui{
min, max, defaultVal, steps); min, max, defaultVal, steps);
} }
/**
* A factory for an int setting gui that hold setting's information.
*/
public static class IntSettingFactory extends SettingGuiFactory{ public static class IntSettingFactory extends SettingGuiFactory{
@NotNull String title; ValueUpdatableGui parent; @NotNull String title; ValueUpdatableGui parent;
int min; int max; int defaultVal; int[] steps; int min; int max; int defaultVal; int[] steps;
/**
* Constructor for an int setting gui factory.
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param configPath Configuration path of this setting.
* @param config Configuration holder of this setting.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultVal Default value if not found on the config.
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 5 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
*/
protected IntSettingFactory( protected IntSettingFactory(
@NotNull String title, ValueUpdatableGui parent, @NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config, String configPath, ConfigHolder config,
@ -247,11 +316,17 @@ public class IntSettingsGui extends AbstractSettingGui{
this.steps = steps; this.steps = steps;
} }
/**
* @return Get setting's gui title
*/
@NotNull @NotNull
public String getTitle() { public String getTitle() {
return title; return title;
} }
/**
* @return The configured value for the associated setting.
*/
public int getConfiguredValue(){ public int getConfiguredValue(){
return this.config.getConfig().getInt(this.configPath, this.defaultVal); return this.config.getConfig().getInt(this.configPath, this.defaultVal);
} }

View file

@ -12,12 +12,27 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer; import java.util.function.Consumer;
/**
* A utility class to store function that create generic GUI actions.
*/
public class GuiGlobalActions { public class GuiGlobalActions {
public final static String NO_EDIT_PERM = "§cYou do not have permission to edit the config"; public final static 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.
*/
public final static Consumer<InventoryClickEvent> stayInPlace = (event) -> event.setCancelled(true); public final static Consumer<InventoryClickEvent> stayInPlace = (event) -> event.setCancelled(true);
/**
* Create a consumer to create and open a new GUI.
* Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
* @param clazz The class of the gui to open.
* It is assumed this class contain a constructor requiring arguments of argClass in the same order as argClass array.
* @param argClass Classes of the argument that will be passed to the constructor of the GUI class.
* @param args Arguments for the constructor the GUI class.
* @return A consumer to create a new gui and open it.
*/
public static @NotNull Consumer<InventoryClickEvent> openGuiAction( public static @NotNull Consumer<InventoryClickEvent> openGuiAction(
@NotNull Class<? extends Gui> clazz, @NotNull Class<? extends Gui> clazz,
@NotNull Class<?>[] argClass, @NotNull Class<?>[] argClass,
@ -44,11 +59,24 @@ public class GuiGlobalActions {
}; };
} }
/**
* Create a consumer to create and open a new GUI.
* Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
* @param clazz The class of the gui to open.
* It is assumed this class contain a constructor with no argument.
* @return A consumer to create a new gui and open it.
*/
public static @NotNull Consumer<InventoryClickEvent> openGuiAction( public static @NotNull Consumer<InventoryClickEvent> openGuiAction(
@NotNull Class<? extends Gui> clazz){ @NotNull Class<? extends Gui> clazz){
return openGuiAction(clazz, new Class<?>[0]); return openGuiAction(clazz, new Class<?>[0]);
} }
/**
* Create a consumer to open a setting gui from a setting GUI factory.
* Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
* @param factory The setting gui factory.
* @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(AbstractSettingGui.SettingGuiFactory factory){
return event -> { return event -> {
event.setCancelled(true); event.setCancelled(true);
@ -57,6 +85,12 @@ public class GuiGlobalActions {
}; };
} }
/**
* Create a consumer to open a global GUI.
* Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
* @param goal The gui to open when consumer is run.
* @return A consumer to open a global GUI.
*/
public static @NotNull Consumer<InventoryClickEvent> openGuiAction(@NotNull Gui goal) { public static @NotNull Consumer<InventoryClickEvent> openGuiAction(@NotNull Gui goal) {
return event -> { return event -> {
HumanEntity player = event.getWhoClicked(); HumanEntity player = event.getWhoClicked();
@ -71,6 +105,14 @@ public class GuiGlobalActions {
}; };
} }
/**
* Create a consumer to update and open an updatable GUI.
* Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
* This consumer check if the player who interacted with the item have the permission to save before saving.
* @param setting The gui that contain the modified setting.
* @param goal The gui to update and open when consumer is run.
* @return A consumer to open a global GUI.
*/
public static @NotNull Consumer<InventoryClickEvent> saveSettingAction( public static @NotNull Consumer<InventoryClickEvent> saveSettingAction(
@NotNull AbstractSettingGui setting, @NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal) { @NotNull ValueUpdatableGui goal) {
@ -86,12 +128,13 @@ public class GuiGlobalActions {
// Save setting // Save setting
if(!setting.onSave()){ if(!setting.onSave()){
player.sendMessage("\u00A7cSomething wrong happen while saving the change of value."); player.sendMessage("\u00A7cSomething went wrong while saving the change of value.");
} }
// Update gui for the one who have it open // Update gui for those who have it open.
goal.updateGuiValues(); goal.updateGuiValues();
// Then show // Then show
goal.show(player); goal.show(player);
}; };
} }
} }

View file

@ -12,36 +12,63 @@ 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.BoolSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.util.StringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Collections; import java.util.Collections;
// maybe use builder patern ? /**
* A utility class to store function that create generic GUI item.
*/
public class GuiGlobalItems { public class GuiGlobalItems {
// return // statically create default back itemstack
public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){ private static final ItemStack BACK_ITEM;
return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
}
// statically create back itemstack
private static final ItemStack BACK_ITEM = new ItemStack(Material.BARRIER);
static { static {
// todo add what I need to add to the back item BACK_ITEM = new ItemStack(Material.BARRIER);
ItemMeta meta = BACK_ITEM.getItemMeta(); ItemMeta meta = BACK_ITEM.getItemMeta();
meta.setDisplayName("\u00A7cBack"); meta.setDisplayName("\u00A7cBack");
BACK_ITEM.setItemMeta(meta); BACK_ITEM.setItemMeta(meta);
} }
/**
* Create a GuiItem that open the given GUi.
* @param item The item to display in the GUI.
* @param goal The GUI to open on click.
* @return An GuiItem that open goal on click.
*/
public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
}
/**
* Create back button item from default back GuiItem.
* The back item will open the goal inventory when clicked.
* @param goal The GUI to go back to.
* @return An GuiItem that go back to goal on click.
*/
public static GuiItem backItem(@NotNull Gui goal){ public static GuiItem backItem(@NotNull Gui goal){
// simple go back item
return goToGuiItem(BACK_ITEM, goal); return goToGuiItem(BACK_ITEM, goal);
} }
/**
* Add default back item to a GUI pattern with the reserved character key <strong>B</strong>.
* The back item will open the target inventory when clicked.
* @param target The pattern to add the back item.
* @param goal The GUI to go back to.
*/
public static void addBackItem(@NotNull PatternPane target, public static void addBackItem(@NotNull PatternPane target,
@NotNull Gui goal){ @NotNull Gui goal){
target.bindItem('B', backItem(goal)); target.bindItem('B', backItem(goal));
} }
private static final Material DEFAULT_BACKGROUND_MAT = Material.LIGHT_GRAY_STAINED_GLASS_PANE; private static final Material DEFAULT_BACKGROUND_MAT = Material.LIGHT_GRAY_STAINED_GLASS_PANE;
/**
* Get a background item with backgroundMat as the displayed material.
* A background item is a GuiItem that do nothing when interacted with and have an empty name.
* @param backgroundMat The material to which the background item should be made of.
* @return A background item with backgroundMat as material.
*/
public static GuiItem backgroundItem(Material backgroundMat){ public static GuiItem backgroundItem(Material backgroundMat){
ItemStack item = new ItemStack(backgroundMat); ItemStack item = new ItemStack(backgroundMat);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
@ -49,19 +76,46 @@ public class GuiGlobalItems {
item.setItemMeta(meta); item.setItemMeta(meta);
return new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance); return new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
} }
/**
* Get default background GuiItem.
* A background item is a GuiItem that do nothing when interacted with and have an empty name.
* @return A new instance of the default background item.
*/
public static GuiItem backgroundItem(){ public static GuiItem backgroundItem(){
return backgroundItem(DEFAULT_BACKGROUND_MAT); return backgroundItem(DEFAULT_BACKGROUND_MAT);
} }
/**
* Add default background item to a GUI pattern with the reserved character key <strong>0</strong>.
* A background item is a GuiItem that do nothing when interacted with and have an empty name.
* @param target The pattern to add the background item.
* @param backgroundMat The material of the background item.
*/
public static void addBackgroundItem(@NotNull PatternPane target, public static void addBackgroundItem(@NotNull PatternPane target,
@NotNull Material backgroundMat){ @NotNull Material backgroundMat){
target.bindItem('0', backgroundItem(backgroundMat)); target.bindItem('0', backgroundItem(backgroundMat));
} }
/**
* Add default background item to a GUI pattern with the reserved character key <strong>0</strong>.
* A background item is a GuiItem that do nothing when interacted with and have an empty name.
* @param target The pattern to add the background item.
*/
public static void addBackgroundItem(@NotNull PatternPane target){ public static void addBackgroundItem(@NotNull PatternPane target){
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT); addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
} }
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE; private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE;
private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE; private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE;
/**
* Create a new save setting GuiItem.
* A save setting item is a GuiItem that save a changed setting when clicked.
* This item also check if the player who interacted with the item have the permission to save before saving.
* @param setting The setting to change.
* @param goal Parent GUI of this setting GUI. as setting will be change the display of goal GUI will be updated.
* @return A save setting item.
*/
public static GuiItem saveItem( public static GuiItem saveItem(
@NotNull AbstractSettingGui setting, @NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal){ @NotNull ValueUpdatableGui goal){
@ -75,6 +129,7 @@ public class GuiGlobalItems {
CustomAnvil.instance); CustomAnvil.instance);
} }
// Create static non change item
private static final GuiItem NO_CHANGE_ITEM; private static final GuiItem NO_CHANGE_ITEM;
static { static {
ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM); ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM);
@ -84,10 +139,22 @@ public class GuiGlobalItems {
NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance); NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
} }
/**
* Get the global "no change" GuiItem.
* The no change item do nothing when interacted, only the title is change to show there is no change.
* @return The global "no change" item.
*/
public static GuiItem noChangeItem(){ public static GuiItem noChangeItem(){
return NO_CHANGE_ITEM; return NO_CHANGE_ITEM;
} }
/**
* Create a new "create and go to the setting GUI" GuiItem.
* This item will create and open a setting GUI from the factory.
* @param item The item that will be displayed.
* @param factory The setting's GUI factory.
* @return A formatted GuiItem that will create and open a GUI for the setting.
*/
public static GuiItem openSettingGuiItem( public static GuiItem openSettingGuiItem(
@NotNull ItemStack item, @NotNull ItemStack item,
@NotNull AbstractSettingGui.SettingGuiFactory factory @NotNull AbstractSettingGui.SettingGuiFactory factory
@ -95,8 +162,17 @@ public class GuiGlobalItems {
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance); return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
} }
// Prefix of the one line lore that will be added to setting's item.
public static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: "; public static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
/**
* Create a new Boolean setting GuiItem.
* This item will create and open a boolean setting GUI from the factory.
* The item will have its value written in the lore part of the item.
* @param factory The setting's GUI factory.
* @param name Name of the item.
* @return A formatted GuiItem that will create and open a GUI for the boolean setting.
*/
public static GuiItem boolSettingGuiItem( public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory, @NotNull BoolSettingsGui.BoolSettingFactory factory,
@NotNull String name @NotNull String name
@ -118,14 +194,30 @@ public class GuiGlobalItems {
return createGuiItemFromProperties(factory, itemMat, itemName, value); return createGuiItemFromProperties(factory, itemMat, itemName, value);
} }
/**
* Create a new boolean setting GuiItem.
* This item will create and open a boolean setting GUI from the factory.
* The item will have its value written in the lore part of the item.
* Item's name will be the factory set title.
* @param factory The setting's GUI factory.
* @return A formatted GuiItem that will create and open a GUI for the boolean setting.
*/
public static GuiItem boolSettingGuiItem( public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory @NotNull BoolSettingsGui.BoolSettingFactory factory
){ ){
String configPath = getConfigNameFromPath(factory.getConfigPath()); String configPath = getConfigNameFromPath(factory.getConfigPath());
return boolSettingGuiItem(factory, StringUtil.snakeToUpperSpacedCase(configPath)); return boolSettingGuiItem(factory, CasedStringUtil.snakeToUpperSpacedCase(configPath));
} }
/**
* Create a new int setting GuiItem.
* This item will create and open an int setting GUI from the factory.
* The item will have its value written in the lore part of the item.
* @param factory The setting's GUI factory.
* @param itemMat Displayed material of the item.
* @param name Name of the item.
* @return A formatted GuiItem that will create and open a GUI for the int setting.
*/
public static GuiItem intSettingGuiItem( public static GuiItem intSettingGuiItem(
@NotNull IntSettingsGui.IntSettingFactory factory, @NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat, @NotNull Material itemMat,
@ -138,21 +230,39 @@ public class GuiGlobalItems {
return createGuiItemFromProperties(factory, itemMat, itemName, value); return createGuiItemFromProperties(factory, itemMat, itemName, value);
} }
/**
* Create a new int setting GuiItem.
* This item will create and open an int setting GUI from the factory.
* The item will have its value written in the lore part of the item.
* Item's name will be the factory set title.
* @param factory The setting's GUI factory.
* @param itemMat Displayed material of the item.
* @return A formatted GuiItem that will create and open a GUI for the int setting.
*/
public static GuiItem intSettingGuiItem( public static GuiItem intSettingGuiItem(
@NotNull IntSettingsGui.IntSettingFactory factory, @NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat @NotNull Material itemMat
){ ){
String configPath = getConfigNameFromPath(factory.getConfigPath()); String configPath = getConfigNameFromPath(factory.getConfigPath());
return intSettingGuiItem(factory, itemMat, StringUtil.snakeToUpperSpacedCase(configPath)); return intSettingGuiItem(factory, itemMat, CasedStringUtil.snakeToUpperSpacedCase(configPath));
} }
/**
* Create an arbitrary GuiItem from a unique setting and item's property.
* @param factory The setting's GUI factory.
* @param itemMat Displayed material of the item.
* @param itemName Name of the item.
* @param value Value of the setting when the item is created.
* Will not update automatically, if the setting's value change, the item need to be created again.
* @return A formatted GuiItem that will create and open a GUI for the setting.
*/
private static GuiItem createGuiItemFromProperties( private static GuiItem createGuiItemFromProperties(
@NotNull AbstractSettingGui.SettingGuiFactory factory, @NotNull AbstractSettingGui.SettingGuiFactory factory,
@NotNull Material itemMat, @NotNull Material itemMat,
@NotNull StringBuilder itemName, @NotNull StringBuilder itemName,
@NotNull Object value @NotNull Object value
){ ){
// Create item // Create & initialise item
ItemStack item = new ItemStack(itemMat); ItemStack item = new ItemStack(itemMat);
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
@ -160,13 +270,20 @@ public class GuiGlobalItems {
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value)); itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
item.setItemMeta(itemMeta); item.setItemMeta(itemMeta);
// Create GuiItem
return openSettingGuiItem(item, factory); return openSettingGuiItem(item, factory);
} }
/**
* Get the setting name from the setting path.
* For example: "gui.command.name" will return "name".
* @param path The setting's path.
* @return The setting's name.
*/
public static String getConfigNameFromPath(String path){ public static String getConfigNameFromPath(String path){
// Get index of first dot
int indexOfDot = path.indexOf("."); int indexOfDot = path.indexOf(".");
//if(indexOfDot == -1) return path; // when indexOfDot == -1 (not fond), it is implied that indexOfDot+1 = 0. substring will keep the full path as expected
// indexOfDot == -1 (not fond) imply indexOfDot+1 = 0. substring will keep the full path as expected
return path.substring(indexOfDot+1); return path.substring(indexOfDot+1);
} }

View file

@ -1,8 +1,18 @@
package xyz.alexcrea.cuanvil.util; package xyz.alexcrea.cuanvil.util;
public class StringUtil { /**
* An incomplete cased string util
*/
public class CasedStringUtil {
//we assume snake_cased_string is in snake case /**
* Transform a snake cased string to an upper-cased spaced string.
* <p>
* for example: if we use "hello_world" as an input this function will return "Hello World".
* @param snake_cased_string The input string.
* This argument NEED to be a snake cased string, or it will not work
* @return The input as an upper-cased string with space separator.
*/
public static String snakeToUpperSpacedCase(String snake_cased_string){ public static String snakeToUpperSpacedCase(String snake_cased_string){
if(snake_cased_string.contentEquals("")) return ""; if(snake_cased_string.contentEquals("")) return "";
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();