Compare commits

..

4 commits

10 changed files with 346 additions and 170 deletions

View file

@ -3,18 +3,16 @@ package xyz.alexcrea.cuanvil.api;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import kotlin.Triple; import kotlin.Triple;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
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.dependency.DependencyManager; import xyz.alexcrea.cuanvil.dependency.DependencyManager;
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
/** /**
* Custom Anvil api for unit repair. * Custom Anvil api for unit repair.
@ -22,7 +20,8 @@ import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class UnitRepairApi { public class UnitRepairApi {
private UnitRepairApi(){} private UnitRepairApi() {
}
private static Object saveChangeTask = null; private static Object saveChangeTask = null;
@ -39,6 +38,19 @@ public class UnitRepairApi {
return addUnitRepair(unit, repairable, 0.25, false); return addUnitRepair(unit, repairable, 0.25, false);
} }
/**
* Write and add a custom anvil unit repair recipe.
* Will not write the recipe if it already exists or was deleted.
* Set the value to minecraft default value (0.25 = 25%)
*
* @param unit The unit material used to repair the bellow item.
* @param repairable The item to be repaired.
* @return true if successful.
*/
public static boolean addUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable) {
return addUnitRepair(unit, repairable, 0.25, false);
}
/** /**
* Write and add a custom anvil unit repair recipe. * Write and add a custom anvil unit repair recipe.
* Will not write the recipe if it already exists or was deleted. * Will not write the recipe if it already exists or was deleted.
@ -63,8 +75,22 @@ public class UnitRepairApi {
* @return true if successful. * @return true if successful.
*/ */
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted) { public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted) {
return addUnitRepair(unit.getKey(), repairable.getKey(), value, overrideDeleted);
}
/**
* Write and add a custom anvil unit repair recipe.
* Will not write the recipe if it already exists.
*
* @param unit The unit material used to repair the bellow item.
* @param repairable The item to be repaired.
* @param value The amount to be repaired by every unit. (1% = 0.01)
* @param overrideDeleted If we should write even if the recipe was previously deleted.
* @return true if successful.
*/
public static boolean addUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable, double value, boolean overrideDeleted) {
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String path = unit.name().toLowerCase() + "." + repairable.name().toLowerCase(); String path = unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase();
if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false; if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false;
if (config.contains(path)) return false; if (config.contains(path)) return false;
@ -83,10 +109,23 @@ public class UnitRepairApi {
* @return true if successful. * @return true if successful.
*/ */
public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) { public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) {
return setUnitRepair(unit.getKey(), repairable.getKey(), value);
}
/**
* Write and add a custom anvil unit repair recipe.
* Do not check if it previously existed or exist.
*
* @param unit The unit material used to repair the bellow item.
* @param repairable The item to be repaired.
* @param value The amount to be repaired by every unit. (1% = 0.01)
* @return true if successful.
*/
public static boolean setUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable, double value) {
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String repairableName = repairable.name().toLowerCase(); String repairableName = repairable.toString().toLowerCase();
String path = unit.name().toLowerCase() + "." + repairableName; String path = unit.toString().toLowerCase() + "." + repairableName;
// Add to config then prepare save // Add to config then prepare save
config.set(path, value); config.set(path, value);
@ -97,7 +136,7 @@ public class UnitRepairApi {
if (repairConfigGui != null) { if (repairConfigGui != null) {
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
if(elementGui != null) elementGui.updateValueForGeneric(repairableName, true); if (elementGui != null) elementGui.updateValueForGeneric(repairable, true);
repairConfigGui.updateValueForGeneric(unit, true); repairConfigGui.updateValueForGeneric(unit, true);
} }
@ -112,38 +151,46 @@ public class UnitRepairApi {
* @return true if successful. * @return true if successful.
*/ */
public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable) { public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable) {
return removeUnitRepair(unit.getKey(), repairable.getKey());
}
/**
* Remove a custom anvil unit repair recipe.
*
* @param unit The unit material used to repair the bellow item.
* @param repairable The item used to be repaired.
* @return true if successful.
*/
public static boolean removeUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable) {
// Delete every possible variation and save to file // Delete every possible variation and save to file
String unitName = unit.name();
String repairableName = repairable.name();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
config.set(unitName.toLowerCase() + "." + repairableName.toUpperCase(), null); config.set(unit.getKey() + "." + repairable.getKey(), null);
config.set(unitName.toUpperCase() + "." + repairableName.toLowerCase(), null); config.set(unit.getKey() + "." + repairable, null);
config.set(unitName.toUpperCase() + "." + repairableName.toUpperCase(), null); config.set(unit + "." + repairable.getKey(), null);
config.set(unitName.toLowerCase() + "." + repairableName.toLowerCase(), null); config.set(unit + "." + repairable, null);
// Test if it was the last value of this section // Test if it was the last value of this section
boolean lastValue = false; boolean lastValue = false;
if(config.isConfigurationSection(unitName.toLowerCase())) { if (config.isConfigurationSection(unit.toString())) {
ConfigurationSection section = config.getConfigurationSection(unitName.toLowerCase()); ConfigurationSection section = config.getConfigurationSection(unit.toString());
if (section != null && section.getKeys(false).isEmpty()) { if (section != null && section.getKeys(false).isEmpty()) {
lastValue = true; lastValue = true;
config.set(unitName.toLowerCase(), null); config.set(unit.toString(), null);
} }
} else if (config.isConfigurationSection(unitName.toUpperCase())) { } else if (config.isConfigurationSection(unit.getKey())) {
ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase()); ConfigurationSection section = config.getConfigurationSection(unit.getKey());
if (section != null && section.getKeys(false).isEmpty()) { if (section != null && section.getKeys(false).isEmpty()) {
lastValue = true; lastValue = true;
config.set(unitName.toUpperCase(), null); config.set(unit.getKey(), null);
} }
} else lastValue = true; } else lastValue = true;
// We only need to "delete" as the lower case to be counted as deleted ConfigHolder.UNIT_REPAIR_HOLDER.delete(unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase());
ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + "." + repairableName.toLowerCase());
prepareSaveTask(); prepareSaveTask();
// Remove from gui // Remove from gui
@ -151,7 +198,7 @@ public class UnitRepairApi {
if (repairConfigGui != null) { if (repairConfigGui != null) {
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
if(elementGui != null) elementGui.removeGeneric(repairableName); if (elementGui != null) elementGui.removeGeneric(repairable);
if (lastValue) { if (lastValue) {
repairConfigGui.removeGeneric(unit); repairConfigGui.removeGeneric(unit);
} }
@ -174,6 +221,7 @@ public class UnitRepairApi {
/** /**
* Get every unit repair recipes. * Get every unit repair recipes.
*
* @return An immutable collection of unit repair recipes. * @return An immutable collection of unit repair recipes.
* <p> * <p>
* Each element of the provided triple represent a part of the recipe * Each element of the provided triple represent a part of the recipe
@ -182,7 +230,10 @@ public class UnitRepairApi {
* <li>Second object is the item to be repaired. * <li>Second object is the item to be repaired.
* <li>Last object is the amount to be repaired by every unit. (1% = 0.01) * <li>Last object is the amount to be repaired by every unit. (1% = 0.01)
* </ul> * </ul>
* @deprecated some may be missing. use {@link #getModernUnitRepairs()}
*
*/ */
@Deprecated
@NotNull @NotNull
public static List<Triple<Material, Material, Double>> getUnitRepairs() { public static List<Triple<Material, Material, Double>> getUnitRepairs() {
List<Triple<Material, Material, Double>> mutableList = new ArrayList<>(); List<Triple<Material, Material, Double>> mutableList = new ArrayList<>();
@ -215,4 +266,53 @@ public class UnitRepairApi {
return Collections.unmodifiableList(mutableList); return Collections.unmodifiableList(mutableList);
} }
/**
* Get every unit repair recipes.
*
* @return An immutable collection of unit repair recipes.
* <p>
* <li>First map contain a key the unit material used to repair the bellow item and value the second map
* <li>Second map contain as key the item to be repaired and as value the amount to be repaired by every unit. (1% = 0.01)
* </ul>
*/
@NotNull
public static Map<NamespacedKey, Map<NamespacedKey, Double>> getModernUnitRepairs() {
Map<NamespacedKey, Map<NamespacedKey, Double>> mutableList = new HashMap<>();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
for (String unitKey : config.getKeys(false)) {
// Test if config section exist
if (!config.isConfigurationSection(unitKey)) continue;
// Test if unit is a material
NamespacedKey unit = NamespacedKey.fromString(unitKey.toLowerCase());
if (unit == null) continue;
Map<NamespacedKey, Double> map;
if (!mutableList.containsKey(unit)) {
map = new HashMap<>();
mutableList.put(unit, map);
} else {
map = mutableList.get(unit);
}
// Iterate over reparable items
ConfigurationSection section = config.getConfigurationSection(unitKey);
for (String repairableKey : section.getKeys(false)) {
// Test if value section exist
if (!section.isDouble(repairableKey)) continue;
// Test if repairable is valid a material
NamespacedKey repairable = NamespacedKey.fromString(repairableKey.toLowerCase());
if (repairable == null) continue;
// Add the values
map.put(repairable, section.getDouble(repairableKey));
}
}
return mutableList;
}
} }

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.global; package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -43,6 +44,10 @@ public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, MappedG
init(); init();
} }
public GroupConfigGui(Gui parent) {
super("Group Config", parent);
}
@Override @Override
protected ItemStack createItemForGeneric(IncludeGroup group) { protected ItemStack createItemForGeneric(IncludeGroup group) {
ItemStack item = new ItemStack(group.getRepresentativeMaterial()); ItemStack item = new ItemStack(group.getRepresentativeMaterial());

View file

@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -18,7 +17,9 @@ import java.util.Collections;
public class ItemConfigGui extends ChestGui { public class ItemConfigGui extends ChestGui {
public EnchantConflictGui enchantConflictGui; public EnchantConflictGui enchantConflictGui;
public GroupConfigGui groupConfigGui;
public UnitRepairConfigGui unitRepairConfigGui;
public CustomRecipeConfigGui customRecipeConfigGui;
public ItemConfigGui(Material display, NamespacedKey material) { public ItemConfigGui(Material display, NamespacedKey material) {
super(3, material.getKey() + " Config", CustomAnvil.instance); super(3, material.getKey() + " Config", CustomAnvil.instance);
@ -62,7 +63,7 @@ public class ItemConfigGui extends ChestGui {
groupMeta.setLore(Collections.singletonList("§7Click here to open item group menu")); groupMeta.setLore(Collections.singletonList("§7Click here to open item group menu"));
groupItemstack.setItemMeta(groupMeta); groupItemstack.setItemMeta(groupMeta);
GuiItem groupConfigItem = GuiGlobalItems.goToGuiItem(groupItemstack, GroupConfigGui.getInstance()); GuiItem groupConfigItem = GuiGlobalItems.goToGuiItem(groupItemstack, getGroupConfigGui(material));
pane.bindItem('6', groupConfigItem); pane.bindItem('6', groupConfigItem);
@ -75,6 +76,7 @@ public class ItemConfigGui extends ChestGui {
unitRepairMeta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu")); unitRepairMeta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu"));
unirRepairItemstack.setItemMeta(unitRepairMeta); unirRepairItemstack.setItemMeta(unitRepairMeta);
//TODO
GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, UnitRepairConfigGui.getInstance()); GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, UnitRepairConfigGui.getInstance());
pane.bindItem('7', unitRepairItem); pane.bindItem('7', unitRepairItem);
@ -87,12 +89,12 @@ public class ItemConfigGui extends ChestGui {
customRecipeMeta.setLore(Collections.singletonList("§7Click here to open anvil custom recipe menu")); customRecipeMeta.setLore(Collections.singletonList("§7Click here to open anvil custom recipe menu"));
customRecipeItemstack.setItemMeta(customRecipeMeta); customRecipeItemstack.setItemMeta(customRecipeMeta);
//TODO
GuiItem customRecipeItem = GuiGlobalItems.goToGuiItem(customRecipeItemstack, CustomRecipeConfigGui.getInstance()); GuiItem customRecipeItem = GuiGlobalItems.goToGuiItem(customRecipeItemstack, CustomRecipeConfigGui.getInstance());
pane.bindItem('8', customRecipeItem); pane.bindItem('8', customRecipeItem);
} }
private Gui getEnchantConflictGui(NamespacedKey material) { private EnchantConflictGui getEnchantConflictGui(NamespacedKey material) {
if (enchantConflictGui == null) { if (enchantConflictGui == null) {
enchantConflictGui = new EnchantConflictGui(this); enchantConflictGui = new EnchantConflictGui(this);
enchantConflictGui.setFilter(group -> enchantConflictGui.setFilter(group ->
@ -103,4 +105,29 @@ public class ItemConfigGui extends ChestGui {
return enchantConflictGui; return enchantConflictGui;
} }
private GroupConfigGui getGroupConfigGui(NamespacedKey material) {
if (groupConfigGui == null) {
groupConfigGui = new GroupConfigGui(this);
groupConfigGui.setFilter(group ->
group.contain(material)
);
groupConfigGui.init();
}
return groupConfigGui;
}
/*private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) {
if (unitRepairConfigGui == null) {
unitRepairConfigGui = new UnitRepairConfigGui(this);
unitRepairConfigGui.setFilter(otherMat ->
group.contain(material) //TODO check material & what inside
);
unitRepairConfigGui.init();
}
return unitRepairConfigGui;
}*/
} }

View file

@ -1,9 +1,10 @@
package xyz.alexcrea.cuanvil.gui.config.global; package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,13 +14,14 @@ import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui;
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MaterialUtil;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
public class UnitRepairConfigGui extends public class UnitRepairConfigGui extends
MappedGuiListConfigGui<Material, MappedGuiListConfigGui.LazyElement<UnitRepairElementListGui>> { MappedGuiListConfigGui<NamespacedKey, MappedGuiListConfigGui.LazyElement<UnitRepairElementListGui>> {
private static UnitRepairConfigGui INSTANCE; private static UnitRepairConfigGui INSTANCE;
@ -41,8 +43,12 @@ public class UnitRepairConfigGui extends
init(); init();
} }
public UnitRepairConfigGui(Gui parent) {
super("Unit Repair Config", parent);
}
@Override @Override
protected LazyElement<UnitRepairElementListGui> newInstanceOfGui(Material material, GuiItem item) { protected LazyElement<UnitRepairElementListGui> newInstanceOfGui(NamespacedKey material, GuiItem item) {
return new LazyElement<>(item, () -> { return new LazyElement<>(item, () -> {
UnitRepairElementListGui element = new UnitRepairElementListGui(material, this); UnitRepairElementListGui element = new UnitRepairElementListGui(material, this);
element.init(); element.init();
@ -51,17 +57,28 @@ public class UnitRepairConfigGui extends
} }
@Override @Override
protected ItemStack createItemForGeneric(Material material) { protected ItemStack createItemForGeneric(@NotNull NamespacedKey material) {
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase()); var unitConfig = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()); var section = unitConfig.getConfigurationSection(material.toString().toLowerCase());
var legacySection = unitConfig.getConfigurationSection(material.toString().toLowerCase());
if(material.isAir()){ String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.getKey().toLowerCase());
material = Material.BARRIER;
var display = MaterialUtil.INSTANCE.getMatFromKey(material);
if (display == null || display.isAir()) {
display = Material.BARRIER;
} }
int reparableItemCount = materialSection == null ? 0 : materialSection.getKeys(false).size(); // Probably an expensive call but... why not var reparable = new HashSet<String>();
if (section != null)
reparable.addAll(section.getKeys(false));
if (legacySection != null)
reparable.addAll(legacySection.getKeys(false));
ItemStack item = new ItemStack(material); var reparableItemCount = reparable.size();
ItemStack item = new ItemStack(display);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
assert meta != null; assert meta != null;
@ -77,13 +94,16 @@ public class UnitRepairConfigGui extends
} }
@Override @Override
protected Collection<Material> getEveryInstanceOfGeneric() { protected Collection<NamespacedKey> getEveryInstanceOfGeneric() {
ArrayList<Material> materials = new ArrayList<>(); var materials = new HashSet<NamespacedKey>();
var config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
for (String matName : ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getKeys(false)) { for (String matName : config.getKeys(false)) {
Material mat = Material.getMaterial(matName.toUpperCase()); if(!config.isConfigurationSection(matName)) continue;
if(mat != null){
materials.add(mat); NamespacedKey material = NamespacedKey.fromString(matName.toLowerCase());
if (material != null) {
materials.add(material);
} }
} }
return materials; return materials;
@ -113,7 +133,7 @@ public class UnitRepairConfigGui extends
"§7You like to be an unit repair item", "§7You like to be an unit repair item",
this, this,
(itemStack, player) -> { (itemStack, player) -> {
Material type = itemStack.getType(); NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack);
// Add new material // Add new material
updateValueForGeneric(type, true); updateValueForGeneric(type, true);
@ -126,7 +146,7 @@ public class UnitRepairConfigGui extends
} }
@NotNull @NotNull
public LazyElement<UnitRepairElementListGui> getInstanceOrCreate(Material mat){ public LazyElement<UnitRepairElementListGui> getInstanceOrCreate(NamespacedKey mat) {
LazyElement<UnitRepairElementListGui> element = this.elementGuiMap.get(mat); LazyElement<UnitRepairElementListGui> element = this.elementGuiMap.get(mat);
if (element == null) { if (element == null) {
updateValueForGeneric(mat, false); updateValueForGeneric(mat, false);
@ -141,8 +161,10 @@ public class UnitRepairConfigGui extends
protected String genericDisplayedName() { protected String genericDisplayedName() {
return "this function Should not be used."; return "this function Should not be used.";
} }
@Override // Not used in this implementation. @Override // Not used in this implementation.
protected Material createAndSaveNewEmptyGeneric(String name) { protected NamespacedKey createAndSaveNewEmptyGeneric(String name) {
return null; return null;
} }
} }

View file

@ -3,12 +3,14 @@ package xyz.alexcrea.cuanvil.gui.config.list;
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;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui; import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui;
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
@ -17,26 +19,25 @@ import xyz.alexcrea.cuanvil.gui.config.settings.DoubleSettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MaterialUtil;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, DoubleSettingGui.DoubleSettingFactory> implements ElementMappedToListGui { public class UnitRepairElementListGui extends SettingGuiListConfigGui<NamespacedKey, DoubleSettingGui.DoubleSettingFactory> implements ElementMappedToListGui {
private final Material parentMaterial; private final NamespacedKey parentMaterial;
private final UnitRepairConfigGui parentGui; private final UnitRepairConfigGui parentGui;
private final String materialName; private final String materialName;
private boolean shouldWork = true; private boolean shouldWork = true;
public UnitRepairElementListGui(@NotNull Material parentMaterial,
public UnitRepairElementListGui(@NotNull NamespacedKey parentMaterial,
@NotNull UnitRepairConfigGui parentGui) { @NotNull UnitRepairConfigGui parentGui) {
super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()) + " §rUnit repair"); super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase()) + " §rUnit repair");
this.parentMaterial = parentMaterial; this.parentMaterial = parentMaterial;
this.parentGui = parentGui; this.parentGui = parentGui;
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()); this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase());
GuiGlobalItems.addBackItem(this.backgroundPane, parentGui); GuiGlobalItems.addBackItem(this.backgroundPane, parentGui);
} }
@ -66,28 +67,28 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
this, this,
(itemStack, player) -> { (itemStack, player) -> {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
Material type = itemStack.getType(); NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack);
if(!(meta instanceof Damageable) || (type.getMaxDurability() <= 0)) { if (!(meta instanceof Damageable)) {
player.sendMessage("§cThis item can't be damaged, so it can't be repaired."); player.sendMessage("§cThis item can't be damaged, so it can't be repaired.");
return; return;
} }
if(type == this.parentMaterial){ if (type.equals(this.parentMaterial)) {
player.sendMessage("§cItem can't repair something of the same type."); player.sendMessage("§cItem can't repair something of the same type.");
return; return;
} }
String materialName = type.name().toLowerCase(); String materialName = type.toString();
// Add new material // Add new material
ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.name().toLowerCase() + "." + materialName,0.25); ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.toString().toLowerCase() + "." + materialName, 0.25);
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
} }
// Update gui // Update gui
updateValueForGeneric(materialName, true); updateValueForGeneric(type, true);
this.parentGui.updateValueForGeneric(this.parentMaterial, true); this.parentGui.updateValueForGeneric(this.parentMaterial, true);
@ -106,14 +107,14 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
} }
@Override @Override
protected DoubleSettingGui.DoubleSettingFactory createFactory(String materialName) { protected DoubleSettingGui.DoubleSettingFactory createFactory(NamespacedKey materialName) {
String materialDisplayName = CasedStringUtil.snakeToUpperSpacedCase(materialName); String materialDisplayName = CasedStringUtil.snakeToUpperSpacedCase(materialName.getKey());
return new DoubleSettingGui.DoubleSettingFactory( return new DoubleSettingGui.DoubleSettingFactory(
"§0%§8" + materialDisplayName + " Repair", "§0%§8" + materialDisplayName + " Repair",
this, this,
ConfigHolder.UNIT_REPAIR_HOLDER, ConfigHolder.UNIT_REPAIR_HOLDER,
this.parentMaterial.name().toLowerCase()+"."+materialName, this.parentMaterial.toString().toLowerCase() + "." + materialName,
Arrays.asList( Arrays.asList(
"§7Click here to change how many §e% §7of §a" + materialDisplayName, "§7Click here to change how many §e% §7of §a" + materialDisplayName,
"§7Should get repaired by §e" + this.materialName "§7Should get repaired by §e" + this.materialName
@ -128,28 +129,39 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
} }
@Override @Override
protected GuiItem itemFromFactory(String materialName, DoubleSettingGui.DoubleSettingFactory factory) { protected GuiItem itemFromFactory(NamespacedKey materialName, DoubleSettingGui.DoubleSettingFactory factory) {
return factory.getItem(materialFromName(materialName), return factory.getItem(materialFromName(materialName),
"§7%§a" + CasedStringUtil.snakeToUpperSpacedCase(materialName)+ " §erepaired by §a" + this.materialName); "§7%§a" + CasedStringUtil.snakeToUpperSpacedCase(materialName.getKey()) + " §erepaired by §a" + this.materialName);
} }
@Override @Override
protected Collection<String> getEveryInstanceOfGeneric() { protected Collection<NamespacedKey> getEveryInstanceOfGeneric() {
ArrayList<String> keys = new ArrayList<>(); Set<NamespacedKey> keys = new HashSet<>();
if (!this.shouldWork) { if (!this.shouldWork) {
return keys; return keys;
} }
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.name().toLowerCase()); ConfigurationSection legacySection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.getKey().toLowerCase());
if(materialSection == null){ ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.toString().toLowerCase());
return keys;
} addAllKeys(legacySection, keys);
keys.addAll(materialSection.getKeys(false)); addAllKeys(materialSection, keys);
return keys; return keys;
} }
private Material materialFromName(String materialName){ private void addAllKeys(@Nullable ConfigurationSection section, @NotNull Set<NamespacedKey> keys) {
Material mat = Material.getMaterial(materialName.toUpperCase()); if (section == null) return;
for (var key : section.getKeys(false)) {
var material = NamespacedKey.fromString(key);
if (material == null) continue;
keys.add(material);
}
}
private Material materialFromName(NamespacedKey material) {
Material mat = MaterialUtil.INSTANCE.getMatFromKey(material);
if (mat == null || mat.isAir()) return Material.BARRIER; if (mat == null || mat.isAir()) return Material.BARRIER;
return mat; return mat;
} }
@ -163,7 +175,8 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
// ElementMappedToListGui methods // ElementMappedToListGui methods
@Override // Not used in this implementation @Override // Not used in this implementation
public void updateLocal() {} public void updateLocal() {
}
@Override @Override
public void cleanAndBeUnusable() { public void cleanAndBeUnusable() {

View file

@ -1,6 +1,5 @@
package xyz.alexcrea.cuanvil.update.minecraft; package xyz.alexcrea.cuanvil.update.minecraft;
import io.delilaheve.CustomAnvil;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.update.UpdateUtils; import xyz.alexcrea.cuanvil.update.UpdateUtils;
import xyz.alexcrea.cuanvil.update.Version; import xyz.alexcrea.cuanvil.update.Version;

View file

@ -38,26 +38,26 @@ public class Update_1_21_11 extends MCUpdate{
addAbsentToList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "spears"); addAbsentToList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "spears");
// Unit repair for spears // Unit repair for spears
unitConfig.set("gold_ingot.golden_spear", 0.25); unitConfig.set("minecraft:gold_ingot.minecraft:golden_spear", 0.25);
unitConfig.set("copper_ingot.copper_spear", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_spear", 0.25);
unitConfig.set("iron_ingot.iron_spear", 0.25); unitConfig.set("minecraft:iron_ingot.minecraft:iron_spear", 0.25);
unitConfig.set("diamond.diamond_spear", 0.25); unitConfig.set("minecraft:diamond.minecraft:diamond_spear", 0.25);
unitConfig.set("netherite_ingot.netherite_spear", 0.25); unitConfig.set("minecraft:netherite_ingot.minecraft:netherite_spear", 0.25);
unitConfig.set("cobblestone.stone_spear", 0.25); unitConfig.set("minecraft:cobblestone.stone_spear", 0.25);
unitConfig.set("cobbled_deepslate.stone_spear", 0.25); unitConfig.set("minecraft:cobbled_deepslate.stone_spear", 0.25);
unitConfig.set("oak_planks.wooden_spear", 0.25); unitConfig.set("minecraft:oak_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("spruce_planks.wooden_spear", 0.25); unitConfig.set("minecraft:spruce_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("birch_planks.wooden_spear", 0.25); unitConfig.set("minecraft:birch_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("jungle_planks.wooden_spear", 0.25); unitConfig.set("minecraft:jungle_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("acacia_planks.wooden_spear", 0.25); unitConfig.set("minecraft:acacia_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("dark_oak_planks.wooden_spear", 0.25); unitConfig.set("minecraft:dark_oak_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("mangrove_planks.wooden_spear", 0.25); unitConfig.set("minecraft:mangrove_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("cherry_planks.wooden_spear", 0.25); unitConfig.set("minecraft:cherry_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("bamboo_planks.wooden_spear", 0.25); unitConfig.set("minecraft:bamboo_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("crimson_planks.wooden_spear", 0.25); unitConfig.set("minecraft:crimson_planks.minecraft:wooden_spear", 0.25);
unitConfig.set("warped_planks.wooden_spear", 0.25); unitConfig.set("minecraft:warped_planks.minecraft:wooden_spear", 0.25);
// Create lunge enchant value and group // Create lunge enchant value and group
baseConfig.set("enchant_limits.minecraft:lunge", 3); baseConfig.set("enchant_limits.minecraft:lunge", 3);

View file

@ -49,16 +49,16 @@ public class Update_1_21_9 extends MCUpdate{
public static void addCopperUnitRepair(FileConfiguration unitConfig) { public static void addCopperUnitRepair(FileConfiguration unitConfig) {
// Add unit repair // Add unit repair
unitConfig.set("copper_ingot.copper_helmet", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_helmet", 0.25);
unitConfig.set("copper_ingot.copper_chestplate", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_chestplate", 0.25);
unitConfig.set("copper_ingot.copper_leggings", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_leggings", 0.25);
unitConfig.set("copper_ingot.copper_boots", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_boots", 0.25);
unitConfig.set("copper_ingot.copper_pickaxe", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_pickaxe", 0.25);
unitConfig.set("copper_ingot.copper_shovel", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_shovel", 0.25);
unitConfig.set("copper_ingot.copper_hoe", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_hoe", 0.25);
unitConfig.set("copper_ingot.copper_axe", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_axe", 0.25);
unitConfig.set("copper_ingot.copper_sword", 0.25); unitConfig.set("minecraft:copper_ingot.minecraft:copper_sword", 0.25);
} }
} }

View file

@ -6,6 +6,7 @@ import org.bukkit.NamespacedKey
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.dependency.plugins.EcoItemDependencyUtil import xyz.alexcrea.cuanvil.dependency.plugins.EcoItemDependencyUtil
import javax.annotation.Nullable
object MaterialUtil { object MaterialUtil {
@ -35,11 +36,13 @@ object MaterialUtil {
return this.type.key return this.type.key
} }
@Nullable
private fun bukkitMaterialFromKey(key: NamespacedKey): Material? { private fun bukkitMaterialFromKey(key: NamespacedKey): Material? {
//TODO on paper only transition Registry.MATERIAL.get(key) //TODO on paper only transition Registry.MATERIAL.get(key)
return Material.matchMaterial(key.toString()) return Material.matchMaterial(key.toString())
} }
@Nullable
fun getMatFromKey(key: NamespacedKey): Material? { fun getMatFromKey(key: NamespacedKey): Material? {
if(DependencyManager.ecoEnchantCompatibility != null) { if(DependencyManager.ecoEnchantCompatibility != null) {
val result = EcoItemDependencyUtil.ecoItemMaterialFromKey(key) val result = EcoItemDependencyUtil.ecoItemMaterialFromKey(key)

View file

@ -1,6 +1,8 @@
package xyz.alexcrea.cuanvil.util package xyz.alexcrea.cuanvil.util
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
@ -11,7 +13,7 @@ object UnitRepairUtil {
private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25 private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25
// Path to user default unit repair value // Path to user default unit repair value
private const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount" public const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount"
/** /**
* Get the % of repair by unit [other] will do to this [ItemStack]. * Get the % of repair by unit [other] will do to this [ItemStack].
@ -22,40 +24,45 @@ object UnitRepairUtil {
): Double? { ): Double? {
if (other == null) return null if (other == null) return null
val config = ConfigHolder.UNIT_REPAIR_HOLDER.config val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
// Get configuration section if exist
val otherName = other.customType.key.lowercase()
var section = config.getConfigurationSection(otherName)
if (section == null) {
section = config.getConfigurationSection(otherName.uppercase())
if (section == null) return null
} val result = findRepairValue(this, other, config) ?: return null
// Get repair amount
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) if(result > 0) return result
if (userDefault <= 0) {
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR // Get default
val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if (userDefault <= 0)
return DEFAULT_DEFAULT_UNIT_REPAIR
return userDefault
} }
return getRepairAmount(this, section, userDefault) private fun findRepairValue(
self: ItemStack,
other: ItemStack,
config: FileConfiguration
): Double? {
val material = other.customType
val selfType = self.customType
val result = checkSection(config, material.toString(), selfType)
if (result != null) return result
return checkSection(config, material.key, selfType)
} }
/** fun checkSection(
* Get the item % repaired by this configuration section of a unit repair. config: FileConfiguration,
* null if not found. path: String,
* If value is set to less than or equal to 0 then it will be set to default material: NamespacedKey
*/ ): Double? {
private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? { val section = config.getConfigurationSection(path) ?: return null
val itemName = item.customType.key.lowercase()
val repairValue = if (section.isDouble(itemName)) { if (section.isDouble(material.toString()))
section.getDouble(itemName) return section.getDouble(material.toString())
} else if (section.isDouble(itemName.uppercase())) { if (section.isDouble(material.key))
section.getDouble(itemName.uppercase()) return section.getDouble(material.key)
} else {
return null return null
} }
if (repairValue <= 0)
return default
return repairValue
}
} }