Add javadoc for settings gui & minor change

This commit is contained in:
alexcrea 2024-03-12 14:45:18 +01:00
parent 180459a7b9
commit 2a3c085aaa
5 changed files with 257 additions and 23 deletions

View file

@ -41,7 +41,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity();
}catch (IllegalArgumentException ignored){}
return EnchantCostSettingsGui.enchFactory(prettyKey+" Level Cost", this,
return EnchantCostSettingsGui.enchentCostFactory(prettyKey+" Level Cost", this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
rarity.getItemValue(), rarity.getBookValue(),
1, 10, 50);

View file

@ -15,6 +15,9 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections;
import java.util.List;
/**
* An instance gui used to edit a setting.
*/
public abstract class AbstractSettingGui extends ChestGui {
// Temporary values, until I get something better.
@ -25,17 +28,34 @@ public abstract class AbstractSettingGui extends ChestGui {
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) {
super(rows, title, CustomAnvil.instance);
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) {
this(rows, StringHolder.of(title), parent);
}
protected GuiItem saveItem;
protected GuiItem noChangeItem;
/**
* Initialise and prepare value for this gui.
* @param parent Parent gui to go back when completed.
*/
private void initBase(ValueUpdatableGui parent){
Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
@ -57,35 +77,76 @@ public abstract class AbstractSettingGui extends ChestGui {
super.update();
}
/**
* Get main pane for this setting gui.
* @return Main pattern pain of this gui.
*/
protected PatternPane getPane() {
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();
/**
* 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.
* <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{
protected String configPath;
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){
this.configPath = configPath;
this.config = config;
}
/**
* @return Configuration path of this setting.
*/
public String getConfigPath() {
return configPath;
}
/**
* @return Configuration holder of this setting.
*/
public ConfigHolder getConfigHolder() {
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

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

View file

@ -19,6 +19,10 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Arrays;
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 {
protected final static String ITEM_PATH = ".item";
@ -27,6 +31,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
private int beforeBook;
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) {
super(holder, nowItem);
@ -35,6 +44,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
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
protected void initStepsValue() {
super.initStepsValue();
@ -53,6 +67,9 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
);
}
/**
* Initialise item that should not be updated late.
*/
private void initStaticItem() {
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){
return event->{
event.setCancelled(true);
@ -197,7 +218,24 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
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 enchentCostFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultItemVal, int defaultBookVal,
int... steps){
@ -207,11 +245,28 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
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 {
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(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
@ -224,16 +279,17 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
this.defaultBookVal = defaultBookVal;
}
@NotNull
public String getTitle() {
return title;
}
/**
* @return The configured value for the enchant setting item value.
*/
@Override
public int getConfiguredValue() {
return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal);
}
/**
* @return The configured value for the enchant setting book value.
*/
public int getConfiguredBookValue(){
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.function.Consumer;
/**
* An instance of a gui used to edit an int setting.
*/
public class IntSettingsGui extends AbstractSettingGui{
protected final IntSettingFactory holder;
@ -26,8 +29,13 @@ public class IntSettingsGui extends AbstractSettingGui{
protected int now;
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) {
super(3, holder.title, holder.parent);
super(3, holder.getTitle(), holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder;
this.before = now;
@ -50,6 +58,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}
protected GuiItem returnToDefault;
/**
* Prepare "return to default value" gui item.
*/
protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta();
@ -65,6 +76,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}, CustomAnvil.instance);
}
/**
* Update item using the setting value to match the new value.
*/
protected void updateValueDisplay(){
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){
return event->{
event.setCancelled(true);
@ -132,6 +150,9 @@ public class IntSettingsGui extends AbstractSettingGui{
};
}
/**
* Initialise step items.
*/
protected void initStepsValue(){
// Put background glass on the background of 'a' to 'b'
GuiItem background = GuiGlobalItems.backgroundItem();
@ -143,11 +164,15 @@ public class IntSettingsGui extends AbstractSettingGui{
// Then update legit step values
updateStepValue();
}
/**
* Update steps items value.
*/
protected void updateStepValue(){
if(holder.steps.length <= 1) return;
// We assume steps have a length of 2k+1 cause its more pretty
char val = getMidStepChar();
// Offset
// Offset to start (not the best way to do it)
val -= (char) ((holder.steps.length-1)/2);
// 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(){
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){
int stepValue = holder.steps[stepIndex];
@ -194,6 +228,10 @@ public class IntSettingsGui extends AbstractSettingGui{
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){
return event -> {
event.setCancelled(true);
@ -220,6 +258,21 @@ public class IntSettingsGui extends AbstractSettingGui{
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,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
@ -229,11 +282,27 @@ public class IntSettingsGui extends AbstractSettingGui{
min, max, defaultVal, steps);
}
/**
* A factory for an int setting gui that hold setting's information.
*/
public static class IntSettingFactory extends SettingGuiFactory{
@NotNull String title; ValueUpdatableGui parent;
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(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
@ -247,11 +316,17 @@ public class IntSettingsGui extends AbstractSettingGui{
this.steps = steps;
}
/**
* @return Get setting's gui title
*/
@NotNull
public String getTitle() {
return title;
}
/**
* @return The configured value for the associated setting.
*/
public int getConfiguredValue(){
return this.config.getConfig().getInt(this.configPath, this.defaultVal);
}