Update items and inventory names.

Fix some value not updating after config changed.
This commit is contained in:
alexcrea 2024-04-06 16:49:46 +02:00
parent e440d05bb9
commit c55c1c8c6a
7 changed files with 67 additions and 80 deletions

View file

@ -89,6 +89,7 @@ public abstract class ConfigHolder {
// Save logic // Save logic
public boolean saveToDisk(boolean doBackup) { public boolean saveToDisk(boolean doBackup) {
CustomAnvil.Companion.log("Saving "+getConfigFileName());
if (doBackup) { if (doBackup) {
if (!saveBackup()) { if (!saveBackup()) {
CustomAnvil.instance.getLogger().severe("Could not save backup. see above."); CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
@ -110,6 +111,7 @@ public abstract class ConfigHolder {
return false; return false;
} }
CustomAnvil.Companion.log(getConfigFileName()+" saved successfully");
return true; return true;
} }

View file

@ -22,7 +22,7 @@ public class CustomRecipeConfigGui extends MappedElementListConfigGui<AnvilCusto
} }
private CustomRecipeConfigGui() { private CustomRecipeConfigGui() {
super("title"); super("Custom Recipe Config");
} }
@ -41,8 +41,14 @@ public class CustomRecipeConfigGui extends MappedElementListConfigGui<AnvilCusto
ItemMeta meta = displaydItem.getItemMeta(); ItemMeta meta = displaydItem.getItemMeta();
meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(recipe.getName()) + " \u00A7fCustom recipe"); meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(recipe.getName()) + " \u00A7fCustom recipe");
boolean shouldWork = recipe.validate();
meta.setLore(Arrays.asList( meta.setLore(Arrays.asList(
"TODO" "\u00A77Should work: \u00A7"+(shouldWork ? "aYes" : "cNo"),
"\u00A77Exact count: \u00A7"+(recipe.getExactCount() ? "aYes" : "cNo"),
"\u00A77Recipe Xp Cost: \u00A7e"+recipe.getXpCostPerCraft()
)); ));
displaydItem.setItemMeta(meta); displaydItem.setItemMeta(meta);

View file

@ -24,9 +24,11 @@ import java.util.UUID;
public abstract class ElementListGlobalConfigGui< T > extends ValueUpdatableGui { public abstract class ElementListGlobalConfigGui< T > extends ValueUpdatableGui {
private final String namePrefix;
public ElementListGlobalConfigGui(@NotNull String title) { public ElementListGlobalConfigGui(@NotNull String title) {
super(6, title, CustomAnvil.instance); super(6, title, CustomAnvil.instance);
this.namePrefix = title;
} }
@ -229,7 +231,12 @@ public abstract class ElementListGlobalConfigGui< T > extends ValueUpdatableGui
addPane(page); addPane(page);
// set title // set title
setTitle("Conflict Config (" + (pageID + 1) + "/" + (pages.size()) + ")"); StringBuilder title = new StringBuilder(this.namePrefix);
int pagesSize = this.pages.size();
if(pagesSize > 1){
title.append(" (").append(pageID + 1).append('/').append(pagesSize).append(')');
}
setTitle(title.toString());
super.show(humanEntity); super.show(humanEntity);

View file

@ -37,7 +37,7 @@ public class CustomRecipeSubSettingGui extends MappedToListSubSettingGui {
@NotNull CustomRecipeConfigGui parent, @NotNull CustomRecipeConfigGui parent,
@NotNull AnvilCustomRecipe anvilRecipe, @NotNull AnvilCustomRecipe anvilRecipe,
@NotNull GuiItem parentItemForThisGui) { @NotNull GuiItem parentItemForThisGui) {
super(parentItemForThisGui, 3, "title"); super(parentItemForThisGui, 3, "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(anvilRecipe.getName()) + " \u00A78Config");
this.parent = parent; this.parent = parent;
this.anvilRecipe = anvilRecipe; this.anvilRecipe = anvilRecipe;
@ -76,24 +76,24 @@ public class CustomRecipeSubSettingGui extends MappedToListSubSettingGui {
// Displayed item will be updated later // Displayed item will be updated later
IntRange costRange = AnvilCustomRecipe.Companion.getXP_COST_CONFIG_RANGE(); IntRange costRange = AnvilCustomRecipe.Companion.getXP_COST_CONFIG_RANGE();
exactCountFactory = BoolSettingsGui.boolFactory("title", this, this.exactCountFactory = BoolSettingsGui.boolFactory("\u00A78Exact count ?", this,
this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER, this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER,
AnvilCustomRecipe.Companion.getDEFAULT_EXACT_COUNT_CONFIG()); AnvilCustomRecipe.Companion.getDEFAULT_EXACT_COUNT_CONFIG());
xpCostFactory = IntSettingsGui.intFactory("title", this, this.xpCostFactory = IntSettingsGui.intFactory("\u00A78Recipe Xp Cost", this,
this.anvilRecipe.getName()+"."+AnvilCustomRecipe.XP_COST_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER, this.anvilRecipe.getName()+"."+AnvilCustomRecipe.XP_COST_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER,
costRange.getFirst(), costRange.getLast(), AnvilCustomRecipe.Companion.getDEFAULT_XP_COST_CONFIG(), 1, 5, 10); costRange.getFirst(), costRange.getLast(), AnvilCustomRecipe.Companion.getDEFAULT_XP_COST_CONFIG(), 1, 5, 10);
leftItemFactory = ItemSettingGui.itemFactory("title", this, this.leftItemFactory = ItemSettingGui.itemFactory("\u00A78Recipe \u00A7eLeft \u00A78Item", this,
this.anvilRecipe.getName()+"."+AnvilCustomRecipe.LEFT_ITEM_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER, this.anvilRecipe.getName()+"."+AnvilCustomRecipe.LEFT_ITEM_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER,
AnvilCustomRecipe.Companion.getDEFAULT_LEFT_ITEM_CONFIG()); AnvilCustomRecipe.Companion.getDEFAULT_LEFT_ITEM_CONFIG());
rightItemFactory = ItemSettingGui.itemFactory("title", this, this.rightItemFactory = ItemSettingGui.itemFactory("\u00A78Recipe \u00A7eLeft \u00A78Item", this,
this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER, this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER,
AnvilCustomRecipe.Companion.getDEFAULT_RIGHT_ITEM_CONFIG()); AnvilCustomRecipe.Companion.getDEFAULT_RIGHT_ITEM_CONFIG());
resultItemFactory = ItemSettingGui.itemFactory("title", this, this.resultItemFactory = ItemSettingGui.itemFactory("\u00A78Recipe \u00A7aResult \u00A78Item", this,
this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER, this.anvilRecipe.getName()+"."+AnvilCustomRecipe.EXACT_COUNT_CONFIG, ConfigHolder.CUSTOM_RECIPE_HOLDER,
AnvilCustomRecipe.Companion.getDEFAULT_RESULT_ITEM_CONFIG()); AnvilCustomRecipe.Companion.getDEFAULT_RESULT_ITEM_CONFIG());
} }
@ -131,83 +131,23 @@ public class CustomRecipeSubSettingGui extends MappedToListSubSettingGui {
@Override @Override
public void updateGuiValues() { public void updateGuiValues() {
// update value from config to conflict
this.anvilRecipe.updateFromFile();
// Parent should call updateLocal with this call
this.parent.updateValueForGeneric(this.anvilRecipe, true); this.parent.updateValueForGeneric(this.anvilRecipe, true);
// Parent should call updateLocal
} }
public void updateLocal() { public void updateLocal() {
if (!this.shouldWork) return; if (!this.shouldWork) return;
/*// Prepare enchantment lore GuiItem exactCountItem = GuiGlobalItems.boolSettingGuiItem(this.exactCountFactory);
ArrayList<String> enchantLore = new ArrayList<>(); pane.bindItem('1', exactCountItem);
enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include");
Set<Enchantment> enchants = getSelectedEnchantments();
if (enchants.isEmpty()) {
enchantLore.add("\u00A77There is no included enchantment for this conflict.");
} else {
enchantLore.add("\u00A77List of included enchantment for this conflict:");
Iterator<Enchantment> enchantIterator = enchants.iterator();
boolean greaterThanMax = enchants.size() > 5; GuiItem xpCostItem = GuiGlobalItems.intSettingGuiItem(this.xpCostFactory, Material.EXPERIENCE_BOTTLE);
int maxindex = (greaterThanMax ? 4 : enchants.size()); pane.bindItem('2', xpCostItem);
for (int i = 0; i < maxindex; i++) {
// format string like "- Fire Protection"
String formattedName = CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey());
enchantLore.add("\u00A77- \u00A75" + formattedName);
}
if (greaterThanMax) {
enchantLore.add("\u00A77And " + (enchants.size() - 4) + " more...");
}
}
// Prepare group lore
ArrayList<String> groupLore = new ArrayList<>();
groupLore.add("\u00A77Allow you to select a list of \u00A73Groups \u00A77that this conflict should include");
Set<AbstractMaterialGroup> grouos = getSelectedGroups();
if (grouos.isEmpty()) {
groupLore.add("\u00A77There is no excluded groups for this conflict.");
} else {
groupLore.add("\u00A77List of excluded groups for this conflict:");
Iterator<AbstractMaterialGroup> groupIterator = grouos.iterator();
boolean greaterThanMax = grouos.size() > 5;
int maxindex = (greaterThanMax ? 4 : grouos.size());
for (int i = 0; i < maxindex; i++) {
// format string like "- Melee Weapons"
String formattedName = CasedStringUtil.snakeToUpperSpacedCase(groupIterator.next().getName());
groupLore.add("\u00A77- \u00A73" + formattedName);
}
if (greaterThanMax) {
groupLore.add("\u00A77And " + (grouos.size() - 4) + " more...");
}
}
// Configure enchant setting item
ItemStack enchantItem = this.enchantSettingItem.getItem();
ItemMeta enchantMeta = enchantItem.getItemMeta();
enchantMeta.setDisplayName("\u00A7aSelect included \u00A75Enchantments \u00A7aSettings");
enchantMeta.setLore(enchantLore);
enchantItem.setItemMeta(enchantMeta);
this.enchantSettingItem.setItem(enchantItem); // Just in case
// Configure group setting item
ItemStack groupItem = this.groupSettingItem.getItem();
ItemMeta groupMeta = groupItem.getItemMeta();
groupMeta.setDisplayName("\u00A7aSelect excluded \u00A73Groups \u00A7aSettings");
groupMeta.setLore(groupLore);
groupItem.setItemMeta(groupMeta);
this.groupSettingItem.setItem(groupItem); // Just in case
this.pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(this.minBeforeActiveSettingFactory, Material.COMMAND_BLOCK));*/
update(); update();
} }

View file

@ -148,8 +148,12 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
@Override @Override
public void updateGuiValues() { public void updateGuiValues() {
// update value from config to conflict
int minBeforeBlock = ConfigHolder.CONFLICT_HOLDER.getConfig().getInt(this.enchantConflict.getName()+'.'+EnchantConflictManager.ENCH_MAX_PATH, 0);
this.enchantConflict.setMinBeforeBlock(minBeforeBlock);
// Parent should call updateLocal with this call
this.parent.updateValueForGeneric(this.enchantConflict, true); this.parent.updateValueForGeneric(this.enchantConflict, true);
// Parent should call updateLocal
} }
@Override @Override

View file

@ -8,7 +8,7 @@ import xyz.alexcrea.cuanvil.interfaces.Named
class EnchantConflictGroup( class EnchantConflictGroup(
private val name: String, private val name: String,
private val cantConflict: AbstractMaterialGroup, private val cantConflict: AbstractMaterialGroup,
val minBeforeBlock: Int var minBeforeBlock: Int
): Named { ): Named {
private val enchantments = HashSet<Enchantment>() private val enchantments = HashSet<Enchantment>()

View file

@ -89,10 +89,38 @@ class AnvilCustomRecipe(
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); ConfigHolder.CUSTOM_RECIPE_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
} }
} }
fun updateFromFile(){
this.exactCount = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getBoolean(
"$name.$EXACT_COUNT_CONFIG",
DEFAULT_EXACT_COUNT_CONFIG
)
this.xpCostPerCraft = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getInt(
"$name.$XP_COST_CONFIG",
DEFAULT_XP_COST_CONFIG
)
// Update items
this.leftItem = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getItemStack(
"$name.$LEFT_ITEM_CONFIG",
DEFAULT_LEFT_ITEM_CONFIG
)
this.rightItem = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getItemStack(
"$name.$RIGHT_ITEM_CONFIG",
DEFAULT_RIGHT_ITEM_CONFIG
)
this.resultItem = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getItemStack(
"$name.$RESULT_ITEM_CONFIG",
DEFAULT_RESULT_ITEM_CONFIG
)
}
fun testItem(item1: ItemStack, item2: ItemStack?): Boolean { fun testItem(item1: ItemStack, item2: ItemStack?): Boolean {
// We assume this function can be call only if leftItem != null // We assume this function can be call only if leftItem != null