add enchant limit config gui.

also fixed some style & grammar issue
This commit is contained in:
alexcrea 2024-03-03 03:06:33 +01:00
parent 3abb1129ee
commit c0762d862e
10 changed files with 200 additions and 38 deletions

View file

@ -9,8 +9,9 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui; import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
@ -45,8 +46,19 @@ public class MainConfigGui extends ChestGui {
basicConfigMeta.setLore(Collections.singletonList("\u00A77Click here to open basic config menu")); basicConfigMeta.setLore(Collections.singletonList("\u00A77Click here to open basic config menu"));
basicConfigItemstack.setItemMeta(basicConfigMeta); basicConfigItemstack.setItemMeta(basicConfigMeta);
GuiItem placeholder1 = GuiGlobalItems.goToGuiItem(basicConfigItemstack, BasicConfigGui.INSTANCE); GuiItem basicConfigItem = GuiGlobalItems.goToGuiItem(basicConfigItemstack, BasicConfigGui.INSTANCE);
pane.bindItem('1', placeholder1); pane.bindItem('1', basicConfigItem);
// enchant level limit item
ItemStack enchantLimitItemstack = new ItemStack(Material.ENCHANTED_BOOK);
ItemMeta enchantLimitMeta = enchantLimitItemstack.getItemMeta();
enchantLimitMeta.setDisplayName("\u00A7aEnchantment Level Limit");
enchantLimitMeta.setLore(Collections.singletonList("\u00A77Click here to open enchantment level limit menu"));
enchantLimitItemstack.setItemMeta(enchantLimitMeta);
GuiItem enchantLimitItem = GuiGlobalItems.goToGuiItem(enchantLimitItemstack, EnchantLimitConfigGui.INSTANCE);
pane.bindItem('2', enchantLimitItem);
// WIP configuration items // WIP configuration items
ItemStack wipItemstack = new ItemStack(Material.BARRIER); ItemStack wipItemstack = new ItemStack(Material.BARRIER);
@ -54,14 +66,11 @@ public class MainConfigGui extends ChestGui {
wipMeta.setDisplayName("\u00A7cWIP"); wipMeta.setDisplayName("\u00A7cWIP");
wipItemstack.setItemMeta(wipMeta); wipItemstack.setItemMeta(wipMeta);
GuiItem wip2 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
GuiItem wip3 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip3 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
GuiItem wip4 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip4 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
GuiItem wip5 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip5 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
GuiItem wip6 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip6 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
pane.bindItem('2', wip2);
pane.bindItem('3', wip3); pane.bindItem('3', wip3);
pane.bindItem('4', wip4); pane.bindItem('4', wip4);
pane.bindItem('5', wip5); pane.bindItem('5', wip5);

View file

@ -14,8 +14,8 @@ import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
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.gui.utils.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
@ -28,7 +28,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
} }
private BasicConfigGui(){ private BasicConfigGui(){
super(3, "\u00A78Basic Config GUI", CustomAnvil.instance); super(3, "\u00A78Basic Config", CustomAnvil.instance);
} }
@ -85,13 +85,13 @@ public class BasicConfigGui extends ValueUpdatableGui {
// item repair cost // item repair cost
range = ConfigOptions.REPAIR_COST_RANGE; range = ConfigOptions.REPAIR_COST_RANGE;
this.itemRepairCost = IntSettingsGui.factory("\u00A78Item repair cost", this, this.itemRepairCost = IntSettingsGui.factory("\u00A78Item Repair Cost", this,
ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_ITEM_REPAIR_COST, ConfigOptions.DEFAULT_ITEM_REPAIR_COST,
1, 5, 10, 50, 100); 1, 5, 10, 50, 100);
// unit repair cost // unit repair cost
this.unitRepairCost = IntSettingsGui.factory("\u00A78Unit repair cost", this, this.unitRepairCost = IntSettingsGui.factory("\u00A78Unit Repair Cost", this,
ConfigOptions.UNIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), ConfigOptions.UNIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_UNIT_REPAIR_COST, ConfigOptions.DEFAULT_UNIT_REPAIR_COST,
1, 5, 10, 50, 100); 1, 5, 10, 50, 100);
@ -105,12 +105,11 @@ public class BasicConfigGui extends ValueUpdatableGui {
// sacrifice illegal enchant cost // sacrifice illegal enchant cost
range = ConfigOptions.SACRIFICE_ILLEGAL_COST_RANGE; range = ConfigOptions.SACRIFICE_ILLEGAL_COST_RANGE;
this.sacrificeIllegalEnchantCost = IntSettingsGui.factory("\u00A78Sacrifice Illegal enchant Cost", this, this.sacrificeIllegalEnchantCost = IntSettingsGui.factory("\u00A78Sacrifice Illegal Enchant Cost", this,
ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST, ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST,
1, 5, 10, 50, 100); 1, 5, 10, 50, 100);
} }
@Override @Override

View file

@ -0,0 +1,104 @@
package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.pane.Orientable;
import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
import com.github.stefvanschie.inventoryframework.pane.Pane;
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.enchantments.Enchantment;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class EnchantLimitConfigGui extends ValueUpdatableGui {
private final static String SECTION_NAME = "enchant_limits";
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
public final static EnchantLimitConfigGui INSTANCE = new EnchantLimitConfigGui();
static {
INSTANCE.init();
}
private EnchantLimitConfigGui(){
super(6, "\u00A78Enchantment Level Limit", CustomAnvil.instance);
}
PatternPane backItems;
OutlinePane filledEnchant;
private void init(){
// Back item panel
Pattern pattern = new Pattern(
"000000000",
"000000000",
"000000000",
"000000000",
"000000000",
"B11111111"
);
backItems = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern);
addPane(backItems);
GuiGlobalItems.addBackItem(backItems, MainConfigGui.INSTANCE);
GuiGlobalItems.addBackgroundItem(backItems);
backItems.bindItem('1', GuiGlobalItems.backgroundItem(SECONDARY_BACKGROUND_MATERIAL));
// enchant item panel
filledEnchant = new OutlinePane(0, 0, 9, 5);
filledEnchant.align(OutlinePane.Alignment.BEGIN);
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
addPane(filledEnchant);
prepareValues();
updateGuiValues();
}
private List<IntSettingsGui.IntSettingFactory> bookItemFactoryList;
protected void prepareValues(){
bookItemFactoryList = new ArrayList<>();
for (Enchantment enchant : Enchantment.values()) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
IntSettingsGui.IntSettingFactory factory = IntSettingsGui.factory(prettyKey, this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
enchant.getMaxLevel(),
1, 5, 10, 50, 100);
bookItemFactoryList.add(factory);
}
}
@Override
public void updateGuiValues() {
// probably not the most efficient but hey ! it do the work
// TODO optimise one day.. maybe
this.filledEnchant.clear();
for (IntSettingsGui.IntSettingFactory inventoryFactory : this.bookItemFactoryList) {
GuiItem item = GuiGlobalItems.intSettingGuiItem(inventoryFactory,
Material.ENCHANTED_BOOK,
inventoryFactory.getTitle());
this.filledEnchant.addItem(item);
}
update();
}
}

View file

@ -10,7 +10,7 @@ import io.delilaheve.CustomAnvil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;

View file

@ -11,7 +11,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -139,6 +139,11 @@ public class BoolSettingsGui extends AbstractSettingGui{
this.defaultVal = defaultVal; this.defaultVal = defaultVal;
} }
@NotNull
public String getTitle() {
return title;
}
public boolean getConfiguredValue(){ public boolean getConfiguredValue(){
return this.config.getConfig().getBoolean(this.configPath, this.defaultVal); return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
} }

View file

@ -11,8 +11,8 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -168,12 +168,12 @@ public class IntSettingsGui extends AbstractSettingGui{
if(stepValue == step){ if(stepValue == step){
stepMat = Material.GREEN_STAINED_GLASS_PANE; stepMat = Material.GREEN_STAINED_GLASS_PANE;
stepName.append('a'); stepName.append('a');
stepLore = Collections.singletonList("\u00A77Value is changing by a step of "+stepValue); stepLore = Collections.singletonList("\u00A77Value is changing by "+stepValue);
clickEvent = GuiGlobalActions.stayInPlace; clickEvent = GuiGlobalActions.stayInPlace;
}else{ }else{
stepMat = Material.RED_STAINED_GLASS_PANE; stepMat = Material.RED_STAINED_GLASS_PANE;
stepName.append('c'); stepName.append('c');
stepLore = Collections.singletonList("\u00A77Click here to change the value of a step by "+stepValue); stepLore = Collections.singletonList("\u00A77Click here to change the value by "+stepValue);
clickEvent = updateStepValue(stepValue); clickEvent = updateStepValue(stepValue);
} }
stepName.append("Step of: ").append(stepValue); stepName.append("Step of: ").append(stepValue);
@ -239,6 +239,11 @@ public class IntSettingsGui extends AbstractSettingGui{
this.steps = steps; this.steps = steps;
} }
@NotNull
public String getTitle() {
return title;
}
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

@ -1,4 +1,4 @@
package xyz.alexcrea.cuanvil.gui.utils; package xyz.alexcrea.cuanvil.gui.util;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;

View file

@ -1,4 +1,4 @@
package xyz.alexcrea.cuanvil.gui.utils; package xyz.alexcrea.cuanvil.gui.util;
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.gui.type.util.Gui;
@ -12,6 +12,7 @@ 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 java.util.Collections; import java.util.Collections;
@ -97,21 +98,44 @@ public class GuiGlobalItems {
private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: "; private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
public static GuiItem boolSettingGuiItem( public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory @NotNull BoolSettingsGui.BoolSettingFactory factory,
@NotNull String name
){ ){
// Get item properties // Get item properties
boolean value = factory.getConfiguredValue(); boolean value = factory.getConfiguredValue();
Material itemMat; Material itemMat;
StringBuilder partOfItemName = new StringBuilder("\u00A7"); StringBuilder itemName = new StringBuilder("\u00A7");
if(value){ if(value){
itemMat = Material.GREEN_TERRACOTTA; itemMat = Material.GREEN_TERRACOTTA;
partOfItemName.append("a"); itemName.append("a");
}else{ }else{
itemMat = Material.RED_TERRACOTTA; itemMat = Material.RED_TERRACOTTA;
partOfItemName.append("c"); itemName.append("c");
} }
return createGuiItemFromProperties(factory, itemMat, partOfItemName, value); itemName.append(name);
return createGuiItemFromProperties(factory, itemMat, itemName, value);
}
public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory
){
String configPath = getConfigNameFromPath(factory.getConfigPath());
return boolSettingGuiItem(factory, StringUtil.snakeToUpperSpacedCase(configPath));
}
public static GuiItem intSettingGuiItem(
@NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat,
@NotNull String name
){
// Get item properties
int value = factory.getConfiguredValue();
StringBuilder itemName = new StringBuilder("\u00A7a").append(name);
return createGuiItemFromProperties(factory, itemMat, itemName, value);
} }
@ -119,26 +143,20 @@ public class GuiGlobalItems {
@NotNull IntSettingsGui.IntSettingFactory factory, @NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat @NotNull Material itemMat
){ ){
// Get item properties String configPath = getConfigNameFromPath(factory.getConfigPath());
int value = factory.getConfiguredValue(); return intSettingGuiItem(factory, itemMat, StringUtil.snakeToUpperSpacedCase(configPath));
StringBuilder partOfItemName = new StringBuilder("\u00A7a");
return createGuiItemFromProperties(factory, itemMat, partOfItemName, value);
} }
private static GuiItem createGuiItemFromProperties( private static GuiItem createGuiItemFromProperties(
@NotNull AbstractSettingGui.SettingGuiFactory factory, @NotNull AbstractSettingGui.SettingGuiFactory factory,
@NotNull Material itemMat, @NotNull Material itemMat,
@NotNull StringBuilder partOfItemName, @NotNull StringBuilder itemName,
@NotNull Object value @NotNull Object value
){ ){
partOfItemName.append(getConfigNameFromPath(factory.getConfigPath()));
// Create item // Create item
ItemStack item = new ItemStack(itemMat); ItemStack item = new ItemStack(itemMat);
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(partOfItemName.toString()); itemMeta.setDisplayName(itemName.toString());
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value)); itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
item.setItemMeta(itemMeta); item.setItemMeta(itemMeta);
@ -151,4 +169,5 @@ public class GuiGlobalItems {
// indexOfDot == -1 (not fond) imply 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

@ -0,0 +1,21 @@
package xyz.alexcrea.cuanvil.util;
public class StringUtil {
//we assume snake_cased_string is in snake case
public static String snakeToUpperSpacedCase(String snake_cased_string){
if(snake_cased_string.contentEquals("")) return "";
StringBuilder result = new StringBuilder();
for (String word : snake_cased_string.split("_")) {
result.append(" ");
if(word.isEmpty()) continue;
char firstChar = word.charAt(0);
result.append(Character.toUpperCase(firstChar));
result.append(word.substring(1));
}
return result.substring(1);
}
}

View file

@ -6,7 +6,7 @@ import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.HumanEntity import org.bukkit.entity.HumanEntity
import xyz.alexcrea.cuanvil.gui.MainConfigGui import xyz.alexcrea.cuanvil.gui.MainConfigGui
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions
class EditConfigExecutor : CommandExecutor { class EditConfigExecutor : CommandExecutor {