Config gui part 2 (#2)

Add enchant level limit and cost config gui.
This commit is contained in:
alexcrea 2024-03-03 16:38:03 +00:00 committed by GitHub
commit 6df4938cc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 707 additions and 70 deletions

View file

@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
version = "1.3.0-alpha-1"
version = "1.3.0-alpha-2"
repositories {
mavenCentral()

View file

@ -138,6 +138,7 @@ public abstract class ConfigHolder {
@Override
public boolean reloadFromDisk(boolean hardFail) {
CustomAnvil.instance.saveDefaultConfig();
CustomAnvil.instance.reloadConfig();
this.configuration = CustomAnvil.instance.getConfig();
return true;

View file

@ -0,0 +1,57 @@
package xyz.alexcrea.cuanvil.enchant;
// to bind EnchantmentRarity to an enchantment...
public enum EnchantmentProperties {
AQUA_AFFINITY(EnchantmentRarity.RARE),
BANE_OF_ARTHROPODS(EnchantmentRarity.UNCOMMON),
BINDING_CURSE(EnchantmentRarity.VERY_RARE),
BLAST_PROTECTION(EnchantmentRarity.RARE),
CHANNELING(EnchantmentRarity.VERY_RARE),
DEPTH_STRIDER(EnchantmentRarity.RARE),
EFFICIENCY(EnchantmentRarity.COMMON),
FLAME(EnchantmentRarity.RARE),
FEATHER_FALLING(EnchantmentRarity.UNCOMMON),
FIRE_ASPECT(EnchantmentRarity.RARE),
FIRE_PROTECTION(EnchantmentRarity.UNCOMMON),
FORTUNE(EnchantmentRarity.RARE),
FROST_WALKER(EnchantmentRarity.RARE),
IMPALING(EnchantmentRarity.RARE),
INFINITY(EnchantmentRarity.VERY_RARE),
KNOCKBACK(EnchantmentRarity.UNCOMMON),
LOOTING(EnchantmentRarity.RARE),
LOYALTY(EnchantmentRarity.COMMON),
LUCK_OF_THE_SEA(EnchantmentRarity.RARE),
LURE(EnchantmentRarity.RARE),
MENDING(EnchantmentRarity.RARE),
MULTISHOT(EnchantmentRarity.RARE),
PIERCING(EnchantmentRarity.COMMON),
POWER(EnchantmentRarity.COMMON),
PROJECTILE_PROTECTION(EnchantmentRarity.UNCOMMON),
PROTECTION(EnchantmentRarity.COMMON),
PUNCH(EnchantmentRarity.RARE),
QUICK_CHARGE(EnchantmentRarity.UNCOMMON),
RESPIRATION(EnchantmentRarity.RARE),
RIPTIDE(EnchantmentRarity.RARE),
SILK_TOUCH(EnchantmentRarity.VERY_RARE),
SHARPNESS(EnchantmentRarity.COMMON),
SMITE(EnchantmentRarity.UNCOMMON),
SOUL_SPEED(EnchantmentRarity.VERY_RARE),
SWIFT_SNEAK(EnchantmentRarity.VERY_RARE),
SWEEPING(EnchantmentRarity.RARE),
THORNS(EnchantmentRarity.VERY_RARE),
UNBREAKING(EnchantmentRarity.UNCOMMON),
VANISHING_CURSE(EnchantmentRarity.VERY_RARE)
;
private final EnchantmentRarity rarity;
EnchantmentProperties(EnchantmentRarity rarity){
this.rarity = rarity;
}
public EnchantmentRarity getRarity() {
return rarity;
}
}

View file

@ -0,0 +1,33 @@
package xyz.alexcrea.cuanvil.enchant;
// because spigot (1.18) do not support enchantment rarity, I need to do it myself...
public enum EnchantmentRarity {
NO_RARITY(0, 0),
COMMON(1),
UNCOMMON(2),
RARE(4),
VERY_RARE(8)
;
private final int itemValue;
private final int bookValue;
EnchantmentRarity(int itemValue, int bookValue){
this.itemValue = itemValue;
this.bookValue = bookValue;
}
EnchantmentRarity(int itemValue){
this(itemValue, Math.max(1,itemValue/2));
}
public int getBookValue() {
return bookValue;
}
public int getItemValue() {
return itemValue;
}
}

View file

@ -9,8 +9,10 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.config.EnchantCostConfigGui;
import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections;
@ -45,8 +47,30 @@ public class MainConfigGui extends ChestGui {
basicConfigMeta.setLore(Collections.singletonList("\u00A77Click here to open basic config menu"));
basicConfigItemstack.setItemMeta(basicConfigMeta);
GuiItem placeholder1 = GuiGlobalItems.goToGuiItem(basicConfigItemstack, BasicConfigGui.INSTANCE);
pane.bindItem('1', placeholder1);
GuiItem basicConfigItem = GuiGlobalItems.goToGuiItem(basicConfigItemstack, BasicConfigGui.INSTANCE);
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);
// enchant cost item
ItemStack enchantCostItemstack = new ItemStack(Material.EXPERIENCE_BOTTLE);
ItemMeta enchantCostMeta = enchantCostItemstack.getItemMeta();
enchantCostMeta.setDisplayName("\u00A7aEnchantment Cost");
enchantCostMeta.setLore(Collections.singletonList("\u00A77Click here to open enchantment costs menu"));
enchantCostItemstack.setItemMeta(enchantCostMeta);
GuiItem enchantCostItem = GuiGlobalItems.goToGuiItem(enchantCostItemstack, EnchantCostConfigGui.INSTANCE);
pane.bindItem('3', enchantCostItem);
// WIP configuration items
ItemStack wipItemstack = new ItemStack(Material.BARRIER);
@ -54,15 +78,10 @@ public class MainConfigGui extends ChestGui {
wipMeta.setDisplayName("\u00A7cWIP");
wipItemstack.setItemMeta(wipMeta);
GuiItem wip2 = 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 wip5 = 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('4', wip4);
pane.bindItem('5', wip5);
pane.bindItem('6', wip6);

View file

@ -0,0 +1,105 @@
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.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public abstract class AbstractEnchantConfigGui<T extends AbstractSettingGui.SettingGuiFactory> extends ValueUpdatableGui {
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
protected AbstractEnchantConfigGui(String title){
super(6, title, CustomAnvil.instance);
}
PatternPane backItems;
OutlinePane filledEnchant;
protected 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<T> bookItemFactoryList;
protected void prepareValues(){
bookItemFactoryList = new ArrayList<>();
List<Enchantment> enchantments = Arrays.asList(Enchantment.values());
enchantments.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
for (Enchantment enchant : enchantments) {
//String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
//String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
T factory = getFactoryFromEnchant(enchant);
/*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 (T inventoryFactory : this.bookItemFactoryList) {
GuiItem item = getItemFromFactory(inventoryFactory);
/*GuiGlobalItems.intSettingGuiItem(inventoryFactory,
Material.ENCHANTED_BOOK,
inventoryFactory.getTitle());*/
this.filledEnchant.addItem(item);
}
update();
}
public abstract T getFactoryFromEnchant(Enchantment enchant);
public abstract GuiItem getItemFromFactory(T inventoryFactory);
}

View file

@ -14,8 +14,8 @@ import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
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;
@ -28,7 +28,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
}
private BasicConfigGui(){
super(3, "\u00A78Basic Config GUI", CustomAnvil.instance);
super(3, "\u00A78Basic Config", CustomAnvil.instance);
}
@ -60,12 +60,12 @@ public class BasicConfigGui extends ValueUpdatableGui {
protected void prepareValues(){
// limit repair item
this.limitRepairFactory = BoolSettingsGui.factory("\u00A78Limit Repair Cost ?",this,
this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?",this,
ConfigOptions.LIMIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, ConfigOptions.DEFAULT_LIMIT_REPAIR);
// rename cost item
IntRange range = ConfigOptions.REPAIR_LIMIT_RANGE;
this.repairCostFactory = IntSettingsGui.factory("\u00A78Repair Cost Limit", this,
this.repairCostFactory = IntSettingsGui.intFactory("\u00A78Repair Cost Limit", this,
ConfigOptions.LIMIT_REPAIR_VALUE, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_LIMIT_REPAIR_VALUE,
1, 5, 10);
@ -80,37 +80,36 @@ public class BasicConfigGui extends ValueUpdatableGui {
this.notNeededLimitValueItem = new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
// remove repair limit item
this.removeRepairLimit = BoolSettingsGui.factory("\u00A78Remove Repair Limit ?",this,
this.removeRepairLimit = BoolSettingsGui.boolFactory("\u00A78Remove Repair Limit ?",this,
ConfigOptions.REMOVE_REPAIR_LIMIT, ConfigHolder.DEFAULT_CONFIG, ConfigOptions.DEFAULT_REMOVE_LIMIT);
// item repair cost
range = ConfigOptions.REPAIR_COST_RANGE;
this.itemRepairCost = IntSettingsGui.factory("\u00A78Item repair cost", this,
this.itemRepairCost = IntSettingsGui.intFactory("\u00A78Item Repair Cost", this,
ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_ITEM_REPAIR_COST,
1, 5, 10, 50, 100);
// unit repair cost
this.unitRepairCost = IntSettingsGui.factory("\u00A78Unit repair cost", this,
this.unitRepairCost = IntSettingsGui.intFactory("\u00A78Unit Repair Cost", this,
ConfigOptions.UNIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_UNIT_REPAIR_COST,
1, 5, 10, 50, 100);
// item rename cost
range = ConfigOptions.ITEM_RENAME_COST_RANGE;
this.itemRenameCost = IntSettingsGui.factory("\u00A78Rename Cost", this,
this.itemRenameCost = IntSettingsGui.intFactory("\u00A78Rename Cost", this,
ConfigOptions.ITEM_RENAME_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_ITEM_RENAME_COST,
1, 5, 10, 50, 100);
// sacrifice illegal enchant cost
range = ConfigOptions.SACRIFICE_ILLEGAL_COST_RANGE;
this.sacrificeIllegalEnchantCost = IntSettingsGui.factory("\u00A78Sacrifice Illegal enchant Cost", this,
this.sacrificeIllegalEnchantCost = IntSettingsGui.intFactory("\u00A78Sacrifice Illegal Enchant Cost", this,
ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST,
1, 5, 10, 50, 100);
}
@Override

View file

@ -0,0 +1,71 @@
package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.StringUtil;
import java.util.Arrays;
import java.util.Locale;
public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSettingsGui.EnchantCostSettingFactory> {
private final static String SECTION_NAME = "enchant_values";
public final static EnchantCostConfigGui INSTANCE = new EnchantCostConfigGui();
static {
INSTANCE.init();
}
private EnchantCostConfigGui() {
super("\u00A78Enchantment Level Limit");
}
@Override
public EnchantCostSettingsGui.EnchantCostSettingFactory getFactoryFromEnchant(Enchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH);
String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
// try to find rarity. default to 0 if not found
EnchantmentRarity rarity = EnchantmentRarity.NO_RARITY;
try {
rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity();
}catch (IllegalArgumentException ignored){}
return EnchantCostSettingsGui.enchFactory(prettyKey+" Level Cost", this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
rarity.getItemValue(), rarity.getBookValue(),
1, 10, 50);
}
@Override
public GuiItem getItemFromFactory(EnchantCostSettingsGui.EnchantCostSettingFactory factory) {
// Get item properties
int itemCost = factory.getConfiguredValue();
int bookCost = factory.getConfiguredBookValue();
StringBuilder itemName = new StringBuilder("\u00A7a").append(factory.getTitle());
// Create item
ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
ItemMeta itemMeta = item.getItemMeta();
// Edit name and lore
itemMeta.setDisplayName(itemName.toString());
itemMeta.setLore(Arrays.asList(
"\u00A77Item Cost: " + itemCost,
"\u00A77Book Cost: " + bookCost));
item.setItemMeta(itemMeta);
return GuiGlobalItems.openSettingGuiItem(item, factory);
}
}

View file

@ -0,0 +1,46 @@
package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.StringUtil;
import java.util.Locale;
public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsGui.IntSettingFactory> {
private final static String SECTION_NAME = "enchant_limits";
public final static EnchantLimitConfigGui INSTANCE = new EnchantLimitConfigGui();
static {
INSTANCE.init();
}
private EnchantLimitConfigGui() {
super("\u00A78Enchantment Level Limit");
}
@Override
public IntSettingsGui.IntSettingFactory getFactoryFromEnchant(Enchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
return IntSettingsGui.intFactory(prettyKey+" Level Limit", this,
SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
enchant.getMaxLevel(),
1, 5, 10, 50, 100);
}
@Override
public GuiItem getItemFromFactory(IntSettingsGui.IntSettingFactory inventoryFactory) {
return GuiGlobalItems.intSettingGuiItem(inventoryFactory,
Material.ENCHANTED_BOOK,
inventoryFactory.getTitle());
}
}

View file

@ -10,7 +10,7 @@ import io.delilaheve.CustomAnvil;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
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.List;

View file

@ -11,7 +11,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
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.function.Consumer;
@ -22,7 +22,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
private final boolean before;
private boolean now;
private BoolSettingsGui(BoolSettingFactory holder, boolean now) {
protected BoolSettingsGui(BoolSettingFactory holder, boolean now) {
super(3, holder.title, holder.parent);
this.holder = holder;
this.before = now;
@ -115,7 +115,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
return now != before;
}
public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
boolean defaultVal){
return new BoolSettingFactory(
@ -129,7 +129,8 @@ public class BoolSettingsGui extends AbstractSettingGui{
@NotNull String title; ValueUpdatableGui parent;
boolean defaultVal;
private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
protected BoolSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
boolean defaultVal){
super(configPath, config);
@ -139,6 +140,11 @@ public class BoolSettingsGui extends AbstractSettingGui{
this.defaultVal = defaultVal;
}
@NotNull
public String getTitle() {
return title;
}
public boolean getConfiguredValue(){
return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
}

View file

@ -0,0 +1,250 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
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.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;
public class EnchantCostSettingsGui extends IntSettingsGui {
protected final static String ITEM_PATH = ".item";
protected final static String BOOK_PATH = ".book";
private int beforeBook;
private int nowBook;
protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) {
super(holder, nowItem);
this.step = holder.steps[0];
initStaticItem();
}
@Override
protected void initStepsValue() {
super.initStepsValue();
int nowBook = ((EnchantCostSettingFactory)this.holder).getConfiguredBookValue();
this.beforeBook = nowBook;
this.nowBook = nowBook;
}
@Override
public Pattern getGuiPattern() {
return new Pattern(
"abc13-v+0",
"D0012MVP0",
"B0010000S"
);
}
private void initStaticItem() {
PatternPane pane = getPane();
// book display
ItemStack bookItemstack = new ItemStack(Material.BOOK);
ItemMeta bookMeta = bookItemstack.getItemMeta();
bookMeta.setDisplayName("\u00A7aCost of an Enchantment by Book");
bookMeta.setLore(Arrays.asList(
"\u00A77Cost per result item level of an sacrifice enchantment",
"\u00A77Only apply if sacrificed item \u00A7cis \u00A77a book"));bookItemstack.setItemMeta(bookMeta);
// sword display
ItemStack swordItemstack = new ItemStack(Material.WOODEN_SWORD);
ItemMeta swordMeta = swordItemstack.getItemMeta();
swordMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
swordMeta.setDisplayName("\u00A7aCost of an Enchantment by Item");
swordMeta.setLore(Arrays.asList(
"\u00A77Cost per result item level of an sacrifice enchantment",
"\u00A77Only apply if sacrificed item \u00A7cis not \u00A77a book"));
swordItemstack.setItemMeta(swordMeta);
pane.bindItem('1', GuiGlobalItems.backgroundItem(Material.BLACK_STAINED_GLASS_PANE));
pane.bindItem('2', new GuiItem(bookItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance));
pane.bindItem('3', new GuiItem(swordItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance));
}
@Override
protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta();
// assume holder is an instance of EnchantCostSettingFactory
EnchantCostSettingFactory holder = (EnchantCostSettingFactory) this.holder;
meta.setDisplayName("\u00A7eReset to default value");
meta.setLore(Arrays.asList(
"\u00A77Default item value is: " + holder.defaultVal,
"\u00A77Default book value is: " + holder.defaultBookVal));
item.setItemMeta(meta);
returnToDefault = new GuiItem(item, event -> {
event.setCancelled(true);
nowBook = holder.defaultBookVal;
now = holder.defaultVal;
updateValueDisplay();
update();
}, CustomAnvil.instance);
}
@Override
protected void updateValueDisplay(){
super.updateValueDisplay();
PatternPane pane = getPane();
// assume holder is an instance of EnchantCostSettingFactory
EnchantCostSettingFactory holder = ((EnchantCostSettingFactory) this.holder);
int nowBook = this.nowBook;
// minus item
GuiItem minusItem;
if(nowBook > holder.min){
int planned = Math.max(holder.min, nowBook - step);
ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7c-"+(nowBook-planned)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
minusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance);
}else{
minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER);
}
pane.bindItem('M', minusItem);
//plus item
GuiItem plusItem;
if(nowBook < holder.max){
int planned = Math.min(holder.max, nowBook + step);
ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-nowBook)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
plusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance);
}else{
plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER);
}
pane.bindItem('P', plusItem);
// "result" display
ItemStack resultPaper = new ItemStack(Material.PAPER);
ItemMeta resultMeta = resultPaper.getItemMeta();
resultMeta.setDisplayName("\u00A7eValue: "+nowBook);
resultPaper.setItemMeta(resultMeta);
GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
pane.bindItem('V', resultItem);
// reset to default
GuiItem returnToDefault;
if(now != holder.defaultVal || nowBook != holder.defaultBookVal){
returnToDefault = this.returnToDefault;
}else{
returnToDefault = GuiGlobalItems.backgroundItem();
}
pane.bindItem('D', returnToDefault);
}
protected Consumer<InventoryClickEvent> updateNowBookConsumer(int planned){
return event->{
event.setCancelled(true);
nowBook = planned;
updateValueDisplay();
update();
};
}
@Override
protected char getMidStepChar() {
return 'b';
}
@Override
public boolean onSave() {
if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
holder.config.getConfig().set(holder.configPath+ITEM_PATH, now);
holder.config.getConfig().set(holder.configPath+BOOK_PATH, nowBook);
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
}
@Override
public boolean hadChange() {
return super.hadChange() || nowBook != beforeBook;
}
public static EnchantCostSettingFactory enchFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultItemVal, int defaultBookVal,
int... steps){
return new EnchantCostSettingFactory(
title,parent,
configPath, config,
min, max, defaultItemVal, defaultBookVal, steps);
}
public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory {
int defaultBookVal;
protected EnchantCostSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultItemVal, int defaultBookVal,
int... steps){
super(title,parent,
configPath, config,
min, max, defaultItemVal, steps);
this.defaultBookVal = defaultBookVal;
}
@NotNull
public String getTitle() {
return title;
}
@Override
public int getConfiguredValue() {
return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal);
}
public int getConfiguredBookValue(){
return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal);
}
@Override
public AbstractSettingGui create() {
// Get value or default
int nowItem = getConfiguredValue();
// create new gui
return new EnchantCostSettingsGui(this, nowItem);
}
}
}

View file

@ -11,8 +11,8 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
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.List;
@ -20,12 +20,12 @@ import java.util.function.Consumer;
public class IntSettingsGui extends AbstractSettingGui{
private final IntSettingFactory holder;
private final int before;
private int now;
private int step;
protected final IntSettingFactory holder;
protected final int before;
protected int now;
protected int step;
private IntSettingsGui(IntSettingFactory holder, int now) {
protected IntSettingsGui(IntSettingFactory holder, int now) {
super(3, holder.title, holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder;
@ -33,9 +33,9 @@ public class IntSettingsGui extends AbstractSettingGui{
this.now = now;
this.step = holder.steps[0];
initStepsValue();
prepareReturnToDefault();
updateValueDisplay();
initStepsValue();
}
@ -48,7 +48,7 @@ public class IntSettingsGui extends AbstractSettingGui{
);
}
GuiItem returnToDefault;
protected GuiItem returnToDefault;
protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta();
@ -136,7 +136,7 @@ public class IntSettingsGui extends AbstractSettingGui{
GuiItem background = GuiGlobalItems.backgroundItem();
PatternPane pane = getPane();
for (char i = 'a'; i < 'a'+9; i++) {
for (char i = 'a'; i < (getMidStepChar()-'a')*2+1; i++) {
pane.bindItem(i, background);
}
// Then update legit step values
@ -145,7 +145,7 @@ public class IntSettingsGui extends AbstractSettingGui{
protected void updateStepValue(){
if(holder.steps.length <= 1) return;
// We assume steps have a length of 2k+1 cause its more pretty
char val = 'e'; // e is the middle, maybe rework this part to remove magic number.
char val = getMidStepChar();
// Offset
val -= (char) ((holder.steps.length-1)/2);
@ -157,6 +157,10 @@ public class IntSettingsGui extends AbstractSettingGui{
}
protected char getMidStepChar(){
return 'e';
}
protected GuiItem stepGuiItem(int stepIndex){
int stepValue = holder.steps[stepIndex];
@ -168,12 +172,12 @@ public class IntSettingsGui extends AbstractSettingGui{
if(stepValue == step){
stepMat = Material.GREEN_STAINED_GLASS_PANE;
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;
}else{
stepMat = Material.RED_STAINED_GLASS_PANE;
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);
}
stepName.append("Step of: ").append(stepValue);
@ -213,7 +217,7 @@ public class IntSettingsGui extends AbstractSettingGui{
return now != before;
}
public static IntSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
return new IntSettingFactory(
@ -227,7 +231,8 @@ public class IntSettingsGui extends AbstractSettingGui{
@NotNull String title; ValueUpdatableGui parent;
int min; int max; int defaultVal; int[] steps;
private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent,
protected IntSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
super(configPath, config);
@ -239,6 +244,11 @@ public class IntSettingsGui extends AbstractSettingGui{
this.steps = steps;
}
@NotNull
public String getTitle() {
return title;
}
public int getConfiguredValue(){
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 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.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.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.util.StringUtil;
import java.util.Collections;
@ -94,24 +95,47 @@ public class GuiGlobalItems {
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
}
private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
public static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory
@NotNull BoolSettingsGui.BoolSettingFactory factory,
@NotNull String name
){
// Get item properties
boolean value = factory.getConfiguredValue();
Material itemMat;
StringBuilder partOfItemName = new StringBuilder("\u00A7");
StringBuilder itemName = new StringBuilder("\u00A7");
if(value){
itemMat = Material.GREEN_TERRACOTTA;
partOfItemName.append("a");
itemName.append("a");
}else{
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 Material itemMat
){
// Get item properties
int value = factory.getConfiguredValue();
StringBuilder partOfItemName = new StringBuilder("\u00A7a");
return createGuiItemFromProperties(factory, itemMat, partOfItemName, value);
String configPath = getConfigNameFromPath(factory.getConfigPath());
return intSettingGuiItem(factory, itemMat, StringUtil.snakeToUpperSpacedCase(configPath));
}
private static GuiItem createGuiItemFromProperties(
@NotNull AbstractSettingGui.SettingGuiFactory factory,
@NotNull Material itemMat,
@NotNull StringBuilder partOfItemName,
@NotNull StringBuilder itemName,
@NotNull Object value
){
partOfItemName.append(getConfigNameFromPath(factory.getConfigPath()));
// Create item
ItemStack item = new ItemStack(itemMat);
ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(partOfItemName.toString());
itemMeta.setDisplayName(itemName.toString());
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
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
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.entity.HumanEntity
import xyz.alexcrea.cuanvil.gui.MainConfigGui
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions
class EditConfigExecutor : CommandExecutor {

View file

@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
version: 1.3.0-alpha-1
version: 1.3.0-alpha-2
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD