callable) {
+ super(chartId);
+ this.callable = callable;
+ }
+
+ @Override
+ protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
+ int value = callable.call();
+ if (value == 0) {
+ // Null = skip the chart
+ return null;
+ }
+ return new JsonObjectBuilder().appendField("value", value).build();
+ }
+ }
+
+ /**
+ * An extremely simple JSON builder.
+ *
+ * While this class is neither feature-rich nor the most performant one, it's sufficient enough
+ * for its use-case.
+ */
+ public static class JsonObjectBuilder {
+
+ private StringBuilder builder = new StringBuilder();
+
+ private boolean hasAtLeastOneField = false;
+
+ public JsonObjectBuilder() {
+ builder.append("{");
+ }
+
+ /**
+ * Appends a null field to the JSON.
+ *
+ * @param key The key of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendNull(String key) {
+ appendFieldUnescaped(key, "null");
+ return this;
+ }
+
+ /**
+ * Appends a string field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String value) {
+ if (value == null) {
+ throw new IllegalArgumentException("JSON value must not be null");
+ }
+ appendFieldUnescaped(key, "\"" + escape(value) + "\"");
+ return this;
+ }
+
+ /**
+ * Appends an integer field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int value) {
+ appendFieldUnescaped(key, String.valueOf(value));
+ return this;
+ }
+
+ /**
+ * Appends an object to the JSON.
+ *
+ * @param key The key of the field.
+ * @param object The object.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject object) {
+ if (object == null) {
+ throw new IllegalArgumentException("JSON object must not be null");
+ }
+ appendFieldUnescaped(key, object.toString());
+ return this;
+ }
+
+ /**
+ * Appends a string array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The string array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values)
+ .map(value -> "\"" + escape(value) + "\"")
+ .collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an integer array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an object array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends a field to the object.
+ *
+ * @param key The key of the field.
+ * @param escapedValue The escaped value of the field.
+ */
+ private void appendFieldUnescaped(String key, String escapedValue) {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ if (key == null) {
+ throw new IllegalArgumentException("JSON key must not be null");
+ }
+ if (hasAtLeastOneField) {
+ builder.append(",");
+ }
+ builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
+ hasAtLeastOneField = true;
+ }
+
+ /**
+ * Builds the JSON string and invalidates this builder.
+ *
+ * @return The built JSON string.
+ */
+ public JsonObject build() {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ JsonObject object = new JsonObject(builder.append("}").toString());
+ builder = null;
+ return object;
+ }
+
+ /**
+ * Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
+ *
+ *
This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
+ * Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
+ *
+ * @param value The value to escape.
+ * @return The escaped value.
+ */
+ private static String escape(String value) {
+ final StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < value.length(); i++) {
+ char c = value.charAt(i);
+ if (c == '"') {
+ builder.append("\\\"");
+ } else if (c == '\\') {
+ builder.append("\\\\");
+ } else if (c <= '\u000F') {
+ builder.append("\\u000").append(Integer.toHexString(c));
+ } else if (c <= '\u001F') {
+ builder.append("\\u00").append(Integer.toHexString(c));
+ } else {
+ builder.append(c);
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * A super simple representation of a JSON object.
+ *
+ *
This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
+ * allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
+ * JsonObject)}.
+ */
+ public static class JsonObject {
+
+ private final String value;
+
+ private JsonObject(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public String toString() {
+ return value;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 8c0e480..b3adc0b 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.UnsafeEnchants
name: UnsafeEnchantsPlus
prefix: UnsafeEnchants+
-version: 1.1.4
+version: 1.1.5
description: Allow custom illegal enchantment
api-version: 1.18
load: POSTWORLD
From 77c34e029f2466a4becf759fc9648637324d579e Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Wed, 7 Feb 2024 14:45:01 +0100
Subject: [PATCH 003/691] start new system with unit repair
---
.../io/delilaheve/AnvilEventListener.kt | 53 +++++++++++-------
.../kotlin/io/delilaheve/UnsafeEnchants.kt | 24 ++++----
.../kotlin/io/delilaheve/util/ItemUtil.kt | 9 +--
.../alexcrea/{group => }/util/Metrics.java | 2 +-
.../xyz/alexcrea/util/UnitRepairUtil.kt | 55 +++++++++++++++++++
src/main/resources/config.yml | 8 ++-
src/main/resources/unit_repair_item.yml | 1 +
7 files changed, 111 insertions(+), 41 deletions(-)
rename src/main/kotlin/xyz/alexcrea/{group => }/util/Metrics.java (99%)
create mode 100644 src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
create mode 100644 src/main/resources/unit_repair_item.yml
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index bb24bbe..f759ba0 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -41,6 +41,8 @@ class AnvilEventListener : Listener {
val inventory = event.inventory
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
val second = inventory.getItem(ANVIL_INPUT_RIGHT) ?: return
+
+ var anvilCost = 0
if (first.canMergeWith(second)) {
// Should find player
val player = event.view.player
@@ -50,7 +52,8 @@ class AnvilEventListener : Listener {
val resultItem = first.clone()
resultItem.setEnchantmentsUnsafe(newEnchants)
- var anvilCost = calculateCost(first, second, resultItem)
+ anvilCost = calculatePenalty(first, second, resultItem)
+ anvilCost+= getRightValues(second, resultItem)
if (!first.isBook() && !second.isBook()) {
// we only need to be concerned with repair when neither item is a book
val repaired = resultItem.repairFrom(first, second)
@@ -68,8 +71,8 @@ class AnvilEventListener : Listener {
if(!it.displayName.contentEquals(inventory.renameText)){
it.setDisplayName(inventory.renameText)
anvilCost += ConfigOptions.itemRenameCost
+ resultItem.itemMeta = it
}
- resultItem.itemMeta = it
}
if (ConfigOptions.limitRepairCost) {
@@ -112,18 +115,37 @@ class AnvilEventListener : Listener {
}
/**
- * Function to calculate most of the xp requirement for the anvil fuse
- * Change result work penalty for future use
+ * Function to calculate work penalty of anvil work
+ * Also change result work penalty
*/
- private fun calculateCost(left: ItemStack, right: ItemStack, result: ItemStack): Int{
+ private fun calculatePenalty(left: ItemStack, right: ItemStack, result: ItemStack): Int{
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
// Calculate work penality
val leftPenality = (left.itemMeta as? Repairable)?.repairCost ?: 0
val rightPenality = (right.itemMeta as? Repairable)?.repairCost ?: 0
+ // Try to set work penality for the result item
+ result.itemMeta?.let {
+ (it as? Repairable)?.repairCost = leftPenality*2+1
+ result.itemMeta = it
+ }
+
+ UnsafeEnchants.log("Calculated penality: " +
+ "leftPenality: $leftPenality, " +
+ "rightPenality: $rightPenality, " +
+ "result penality: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}")
+
+ return leftPenality + rightPenality
+ }
+
+ /**
+ * Function to calculate right enchantment values
+ * it include enchantment placed on final item and conflicting enchantment
+ */
+ private fun getRightValues(right: ItemStack, result:ItemStack) : Int {
// Calculate right value and illegal enchant penalty
- var rightValue = 0
var illegalPenalty = 0
+ var rightValue = 0
val rightIsFormBook = right.isBook()
val resultEnchs = result.findEnchantments()
@@ -147,25 +169,14 @@ class AnvilEventListener : Listener {
val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook)
val value = resultLevel * enchantmentMultiplier
UnsafeEnchants.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value")
- rightValue+=value
+ rightValue += value
}
-
- // Try to set work penality for the result item
- result.itemMeta?.let {
- (it as? Repairable)?.repairCost = leftPenality*2+1
- result.itemMeta = it
- }
-
- UnsafeEnchants.log("Calculated cost: " +
- "leftPenality: $leftPenality, " +
- "rightPenality: $rightPenality, " +
+ UnsafeEnchants.log("Calculated right values: " +
"rightValue: $rightValue, " +
- "illegalPenalty: $illegalPenalty," +
- "result penality: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}")
+ "illegalPenalty: $illegalPenalty")
- // We are missing [Renaming Cost] + [Refilling Durability] but it will be handled later
- return rightValue + leftPenality + rightPenality + illegalPenalty
+ return rightValue + illegalPenalty
}
}
diff --git a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
index b8c925c..3abcf12 100644
--- a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
+++ b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
@@ -6,8 +6,8 @@ import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
-import xyz.alexcrea.group.util.Metrics
-import xyz.alexcrea.group.util.Metrics.SimplePie
+import xyz.alexcrea.util.Metrics
+import xyz.alexcrea.util.Metrics.SimplePie
import java.io.File
import java.io.FileReader
@@ -29,15 +29,20 @@ class UnsafeEnchants : JavaPlugin() {
const val bypassLevelPermission = "ue.bypass.level"
// Item Grouping Configuration file name
- const val itemGroupingConfigName = "item_groups.yml"
+ const val itemGroupingConfigFilePath = "item_groups.yml"
// Conflict Configuration file name
- const val enchantConflicConfigName = "enchant_conflict.yml"
+ const val enchantConflicConfigFilePath = "enchant_conflict.yml"
+ // Unit Repair Configuration file name
+ const val unitRepairFilePath = "unit_repair_item.yml"
// Current plugin instance
lateinit var instance: UnsafeEnchants
// Current item grouping configuration instance
lateinit var conflictManager: EnchantConflictManager
+ // Configuration for unit repair
+ lateinit var unitRepairConfig: YamlConfiguration
+
/**
* Logging handler
*/
@@ -60,17 +65,20 @@ class UnsafeEnchants : JavaPlugin() {
addCustomMetric(metric)
// Load material grouping config
- val itemGroupConfig = reloadResource(itemGroupingConfigName) ?: return
+ val itemGroupConfig = reloadResource(itemGroupingConfigFilePath) ?: return
// Read material groups from config
val itemGroupsManager = ItemGroupManager()
itemGroupsManager.prepareGroups(itemGroupConfig)
// Load enchantment conflicts config
- val conflictConfig = reloadResource(enchantConflicConfigName) ?: return
+ val conflictConfig = reloadResource(enchantConflicConfigFilePath) ?: return
// Read conflicts from config and material group manager
conflictManager = EnchantConflictManager()
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
+ // Load unit repair config
+ unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
+
server.pluginManager.registerEvents(
AnvilEventListener(),
this
@@ -82,16 +90,12 @@ class UnsafeEnchants : JavaPlugin() {
metric.addCustomChart(SimplePie("item_rename_cost") {
ConfigOptions.itemRenameCost.toString()
})
- println("item_rename_cost: ${ConfigOptions.itemRenameCost}")
metric.addCustomChart(SimplePie("item_repair_cost") {
ConfigOptions.itemRepairCost.toString()
})
- println("item_repair_cost: ${ConfigOptions.itemRepairCost}")
metric.addCustomChart(SimplePie("sacrifice_illegal_enchant_cost") {
ConfigOptions.sacrificeIllegalCost.toString()
})
- println("sacrifice_illegal_enchant_cost: ${ConfigOptions.sacrificeIllegalCost}")
-
}
private fun reloadResource(resourceName: String,
diff --git a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
index 1f8f9c8..bd46167 100644
--- a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
@@ -25,13 +25,6 @@ object ItemUtil {
*/
private fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
- /**
- * Determine if this [ItemStack] can hold enchants, this should be sufficient for
- * detecting if an item is a tool/armour/etc... and not a carrot/potato/etc...
- */
- private fun ItemStack.canHoldEnchants() = Enchantment.values()
- .any { it.canEnchantItem(this) }
-
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
@@ -108,5 +101,5 @@ object ItemUtil {
*/
fun ItemStack.canMergeWith(
other: ItemStack
- ) = type == other.type || (canHoldEnchants() && other.isEnchantedBook())
+ ) = type == other.type || (other.isEnchantedBook())
}
diff --git a/src/main/kotlin/xyz/alexcrea/group/util/Metrics.java b/src/main/kotlin/xyz/alexcrea/util/Metrics.java
similarity index 99%
rename from src/main/kotlin/xyz/alexcrea/group/util/Metrics.java
rename to src/main/kotlin/xyz/alexcrea/util/Metrics.java
index da923c2..f8a1a9b 100644
--- a/src/main/kotlin/xyz/alexcrea/group/util/Metrics.java
+++ b/src/main/kotlin/xyz/alexcrea/util/Metrics.java
@@ -12,7 +12,7 @@
*
* Violations will result in a ban of your plugin and account from bStats.
*/
-package xyz.alexcrea.group.util;
+package xyz.alexcrea.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
diff --git a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
new file mode 100644
index 0000000..ad57d4c
--- /dev/null
+++ b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
@@ -0,0 +1,55 @@
+package xyz.alexcrea.util
+
+import io.delilaheve.UnsafeEnchants
+import org.bukkit.configuration.ConfigurationSection
+import org.bukkit.inventory.ItemStack
+
+object UnitRepairUtil {
+
+ // Default value for user set default unit repair %
+ private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25
+
+ /**
+ * Get the % of repair by unit [other] will do to this [ItemStack].
+ * null if can't unit repaired by [other]
+ */
+ fun ItemStack.getRepair(
+ other: ItemStack
+ ): Double? {
+ val config = UnsafeEnchants.unitRepairConfig
+ // Get configuration section if exist
+ val otherName = other.type.name.uppercase()
+ var section = config.getConfigurationSection(otherName)
+ if(section == null){
+ section = config.getConfigurationSection(otherName.lowercase())
+ if(section == null) return null
+ }
+ // Get repair amount
+ var userDefault = config.getDouble("default_repair_amount",DEFAULT_DEFAULT_UNIT_REPAIR)
+ if(userDefault <= 0){
+ userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
+ }
+
+ return getRepairAmount(this,section,userDefault)
+ }
+
+ /**
+ * Get the item % repaired by this configuration section of a unit repair.
+ * null if not found.
+ * If value is set to less than or equal to 0 then it will be set to default
+ */
+ private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double?{
+ val itemName = item.type.name.uppercase()
+ val repairValue = if(section.isDouble(itemName)){
+ section.getDouble(itemName)
+ }else if(section.isDouble(itemName.lowercase())){
+ section.getDouble(itemName.lowercase())
+ }else{
+ return null
+ }
+ if(repairValue <= 0)
+ return default
+ return repairValue
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 19a97ab..005dca3 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -13,11 +13,17 @@ limit_repair_value: 39
# The anvil will still visually display "too expensive" however the action will be completable
remove_repair_limit: false
-# Value added to the anvil when the item durability increase
+# Value added to the anvil when the item is repaired by another item of the same type
#
# Valid range of 0 - 255
item_repair_cost: 2
+# Value added to the anvil when the item is repaired by an "unit"
+# For example, a diamond on a diamond sword
+#
+# Valid range of 0 - 255
+unit_repair_cost: 1
+
# Value added to the anvil when the item is renamed
#
# Valid range of 0 - 255
diff --git a/src/main/resources/unit_repair_item.yml b/src/main/resources/unit_repair_item.yml
new file mode 100644
index 0000000..503fa1d
--- /dev/null
+++ b/src/main/resources/unit_repair_item.yml
@@ -0,0 +1 @@
+#TODO
\ No newline at end of file
From 80468f6add059f36010ef51e20d6ac3483d8c743 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Wed, 7 Feb 2024 20:29:09 +0100
Subject: [PATCH 004/691] add unit repair config & version up
---
build.gradle.kts | 2 +-
.../xyz/alexcrea/util/UnitRepairUtil.kt | 4 +-
src/main/resources/plugin.yml | 2 +-
src/main/resources/unit_repair_item.yml | 180 +++++++++++++++++-
4 files changed, 184 insertions(+), 4 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 3df289d..3a91a58 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.1.5"
+version = "1.2.0"
repositories {
mavenCentral()
diff --git a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
index ad57d4c..fd53415 100644
--- a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
@@ -8,6 +8,8 @@ object UnitRepairUtil {
// Default value for user set default unit repair %
private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25
+ // Path to user default unit repair value
+ private const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount"
/**
* Get the % of repair by unit [other] will do to this [ItemStack].
@@ -25,7 +27,7 @@ object UnitRepairUtil {
if(section == null) return null
}
// Get repair amount
- var userDefault = config.getDouble("default_repair_amount",DEFAULT_DEFAULT_UNIT_REPAIR)
+ var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH,DEFAULT_DEFAULT_UNIT_REPAIR)
if(userDefault <= 0){
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
}
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index b3adc0b..1d71ebc 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.UnsafeEnchants
name: UnsafeEnchantsPlus
prefix: UnsafeEnchants+
-version: 1.1.5
+version: 1.2.0
description: Allow custom illegal enchantment
api-version: 1.18
load: POSTWORLD
diff --git a/src/main/resources/unit_repair_item.yml b/src/main/resources/unit_repair_item.yml
index 503fa1d..2eb6e0d 100644
--- a/src/main/resources/unit_repair_item.yml
+++ b/src/main/resources/unit_repair_item.yml
@@ -1 +1,179 @@
-#TODO
\ No newline at end of file
+# Unit repair configuration
+#
+# This configuration is to make custom unit repair
+# A unit repair is, for example, a diamond to repair a diamond sword
+# In vanilla, a unit repair 25% of object durability
+# you can make a custom value here
+#
+# Item name should NOT combine caps and no caps (example: Stone)
+
+# Default value if the config is an invalid value (value <= 0 )
+# If value > 1 it will be treated as being = 1
+default_repair_amount: 0.25
+
+# Vanilla unit repair group is bellow
+
+diamond:
+ diamond_helmet: 0.25
+ diamond_chestplate: 0.25
+ diamond_leggings: 0.25
+ diamond_boots: 0.25
+ diamond_sword: 0.25
+ diamond_pickaxe: 0.25
+ diamond_axe: 0.25
+ diamond_shovel: 0.25
+ diamond_hoe: 0.25
+
+netherite_ingot:
+ netherite_helmet: 0.25
+ netherite_chestplate: 0.25
+ netherite_leggings: 0.25
+ netherite_boots: 0.25
+ netherite_sword: 0.25
+ netherite_pickaxe: 0.25
+ netherite_axe: 0.25
+ netherite_shovel: 0.25
+ netherite_hoe: 0.25
+
+gold_ingot:
+ golden_helmet: 0.25
+ golden_chestplate: 0.25
+ golden_leggings: 0.25
+ golden_boots: 0.25
+ golden_sword: 0.25
+ golden_pickaxe: 0.25
+ golden_axe: 0.25
+ golden_shovel: 0.25
+ golden_hoe: 0.25
+
+iron_ingot:
+ iron_helmet: 0.25
+ iron_chestplate: 0.25
+ iron_leggings: 0.25
+ iron_boots: 0.25
+ iron_sword: 0.25
+ iron_pickaxe: 0.25
+ iron_axe: 0.25
+ iron_shovel: 0.25
+ iron_hoe: 0.25
+
+cobblestone:
+ stone_sword: 0.25
+ stone_pickaxe: 0.25
+ stone_axe: 0.25
+ stone_shovel: 0.25
+ stone_hoe: 0.25
+
+cobbled_deepslate:
+ stone_sword: 0.25
+ stone_pickaxe: 0.25
+ stone_axe: 0.25
+ stone_shovel: 0.25
+ stone_hoe: 0.25
+
+blackstone:
+ stone_sword: 0.25
+ stone_pickaxe: 0.25
+ stone_axe: 0.25
+ stone_shovel: 0.25
+ stone_hoe: 0.25
+
+leather:
+ leather_helmet: 0.25
+ leather_chestplate: 0.25
+ leather_leggings: 0.25
+ leather_boots: 0.25
+
+phantom_membrane:
+ elytra: 0.25
+
+scute:
+ turtle_helmet: 0.25
+
+oak_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+spruce_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+birch_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+jungle_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+acacia_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+dark_oak_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+mangrove_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+cherry_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+bamboo_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+crimson_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
+
+warped_planks:
+ wooden_sword: 0.25
+ wooden_pickaxe: 0.25
+ wooden_axe: 0.25
+ wooden_shovel: 0.25
+ wooden_hoe: 0.25
+ shield: 0.25
From 4195add65509c4365ae22d419bbd7508181ab32e Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Fri, 9 Feb 2024 00:25:12 +0100
Subject: [PATCH 005/691] unit repair done
---
.../io/delilaheve/AnvilEventListener.kt | 201 ++++++++++++++----
.../io/delilaheve/util/ConfigOptions.kt | 20 +-
.../io/delilaheve/util/EnchantmentUtil.kt | 16 +-
.../kotlin/io/delilaheve/util/ItemUtil.kt | 43 ++--
.../xyz/alexcrea/util/UnitRepairUtil.kt | 7 +-
src/main/resources/unit_repair_item.yml | 8 +-
6 files changed, 230 insertions(+), 65 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index f759ba0..b6382fd 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -5,9 +5,12 @@ import io.delilaheve.util.EnchantmentUtil.combineWith
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import io.delilaheve.util.ItemUtil.canMergeWith
import io.delilaheve.util.ItemUtil.findEnchantments
-import io.delilaheve.util.ItemUtil.isBook
+import io.delilaheve.util.ItemUtil.isEnchantedBook
import io.delilaheve.util.ItemUtil.repairFrom
import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
+import io.delilaheve.util.ItemUtil.unitRepair
+import org.bukkit.Material
+import org.bukkit.entity.Player
import org.bukkit.event.Event
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority.HIGHEST
@@ -19,6 +22,7 @@ import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable
import xyz.alexcrea.group.ConflictType
+import xyz.alexcrea.util.UnitRepairUtil.getRepair
import kotlin.math.min
/**
@@ -40,61 +44,98 @@ class AnvilEventListener : Listener {
fun anvilCombineCheck(event: PrepareAnvilEvent) {
val inventory = event.inventory
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
- val second = inventory.getItem(ANVIL_INPUT_RIGHT) ?: return
+ val second = inventory.getItem(ANVIL_INPUT_RIGHT)
- var anvilCost = 0
+ // Should find player
+ val player = event.view.player
+ if(!player.hasPermission(UnsafeEnchants.unsafePermission)) return
+
+ // Test rename lonely item
+ if(second == null){
+ val resultItem = first.clone()
+ var anvilCost = handleRename(resultItem, inventory)
+ anvilCost+= calculatePenalty(first,null,resultItem)
+
+ // Test/stop if nothing changed.
+ if(first == resultItem){
+ event.result = null
+ return
+ }
+ // We do set item here as vanilla do all of our job (renaming)
+
+ handleDisplayedXp(inventory, event, anvilCost)
+ return
+ }
+
+ // Test for merge
if (first.canMergeWith(second)) {
- // Should find player
- val player = event.view.player
val newEnchants = first.findEnchantments()
.combineWith(second.findEnchantments(), first.type, player)
val resultItem = first.clone()
resultItem.setEnchantmentsUnsafe(newEnchants)
- anvilCost = calculatePenalty(first, second, resultItem)
+ var anvilCost = calculatePenalty(first, second, resultItem)
anvilCost+= getRightValues(second, resultItem)
- if (!first.isBook() && !second.isBook()) {
+ if (!first.isEnchantedBook() && !second.isEnchantedBook()) {
// we only need to be concerned with repair when neither item is a book
val repaired = resultItem.repairFrom(first, second)
anvilCost += if(repaired) ConfigOptions.itemRepairCost else 0
}
- // Test if nothing change and stop.
+ // Test/stop if nothing changed.
if(first == resultItem){
event.result = null
return
}
- // Rename item and add renaming cost
- resultItem.itemMeta?.let {
- if(!it.displayName.contentEquals(inventory.renameText)){
- it.setDisplayName(inventory.renameText)
- anvilCost += ConfigOptions.itemRenameCost
- resultItem.itemMeta = it
- }
- }
+ anvilCost+= handleRename(resultItem, inventory)
if (ConfigOptions.limitRepairCost) {
anvilCost = min(anvilCost, ConfigOptions.limitRepairValue)
}
-
event.result = resultItem
- /* Because Minecraft likes to have the final say in the repair cost displayed
- * we need to wait for the event to end before overriding it, this ensures that
- * we have the final say in the process. */
- UnsafeEnchants.instance
- .server
- .scheduler
- .runTask(UnsafeEnchants.instance, Runnable {
- if (ConfigOptions.removeRepairLimit) {
- inventory.maximumRepairCost = Int.MAX_VALUE
- }
- inventory.repairCost = anvilCost
- event.view.setProperty(REPAIR_COST, anvilCost)
- })
+ handleDisplayedXp(inventory, event, anvilCost)
+ return
}
+
+ // Test for unit repair
+ val unitRepairAmount = first.getRepair(second)
+ if(unitRepairAmount != null){
+ val resultItem = first.clone()
+ var anvilCost = handleRename(resultItem, inventory)
+ // We do not care about right item penalty for unit repair
+ anvilCost+= calculatePenalty(first,null,resultItem)
+
+ val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount)
+ if(repairAmount > 0){
+ anvilCost += repairAmount*ConfigOptions.unitRepairCost
+ }
+
+ // Test/stop if nothing changed.
+ if(first == resultItem){
+ event.result = null
+ return
+ }
+ event.result = resultItem
+
+ handleDisplayedXp(inventory, event, anvilCost)
+ }else{
+ event.result = null
+ }
+ }
+
+ private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory): Int{
+ // Rename item and add renaming cost
+ resultItem.itemMeta?.let {
+ if(!it.displayName.contentEquals(inventory.renameText)){
+ it.setDisplayName(inventory.renameText)
+ resultItem.itemMeta = it
+ return ConfigOptions.itemRenameCost
+ }
+ }
+ return 0
}
/**
@@ -102,27 +143,89 @@ class AnvilEventListener : Listener {
*/
@EventHandler(ignoreCancelled = true)
fun anvilExtractionCheck(event: InventoryClickEvent) {
- //val player = event.whoClicked as? Player ?: return
+ val player = event.whoClicked as? Player ?: return
+ if(!player.hasPermission(UnsafeEnchants.unsafePermission)) return
val inventory = event.inventory as? AnvilInventory ?: return
if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
- // Is true if there was no change. probably when there are conflict
- if(output == inventory.getItem(ANVIL_INPUT_LEFT)){
+ val leftItem = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
+ val rightItem = inventory.getItem(ANVIL_INPUT_RIGHT)
+
+ val canMerge = leftItem.canMergeWith(rightItem)
+ val unitRepairResult = leftItem.getRepair(rightItem)
+ val allowed = (rightItem == null)
+ || (canMerge)
+ || (unitRepairResult != null)
+ // True if there was no change or not allowed
+ if((output == inventory.getItem(ANVIL_INPUT_LEFT))
+ || !allowed){
+
event.result = Event.Result.DENY
return
}
- event.result = Event.Result.ALLOW
+ if(rightItem == null){
+ event.result = Event.Result.ALLOW
+ return
+ }
+ if(canMerge){
+ event.result = Event.Result.ALLOW
+ }else if(unitRepairResult != null){
+ val resultCopy = leftItem.clone()
+ val resultAmount = resultCopy.unitRepair(
+ rightItem.amount, unitRepairResult)
+
+ // To avoid vanilla, we cancel the event for unit repair
+ event.result = Event.Result.DENY
+ event.isCancelled = true
+ // And we give the item manually
+ // But first we check if we should give the item
+ if(player.itemOnCursor.type != Material.AIR) return
+ if(inventory.repairCost > player.level) return
+
+ // Get repairCost
+ var repairCost = 0
+ leftItem.itemMeta?.let { leftMeta ->
+ val leftName = leftMeta.displayName
+ output.itemMeta?.let {
+ if(!leftName.contentEquals(it.displayName)){
+ repairCost+= ConfigOptions.itemRenameCost
+ }
+ }
+ }
+
+ repairCost+= calculatePenalty(leftItem,null,resultCopy)
+ repairCost+= resultAmount*ConfigOptions.unitRepairCost
+
+ if((inventory.maximumRepairCost < repairCost)
+ || (player.level < repairCost)) return
+
+ // We remove what should be removed
+ inventory.setItem(ANVIL_INPUT_LEFT,null)
+ rightItem.amount-= resultAmount
+ inventory.setItem(ANVIL_INPUT_RIGHT,rightItem)
+ inventory.setItem(ANVIL_OUTPUT_SLOT, null)
+
+ UnsafeEnchants.log("repair cost: $repairCost")
+ player.level-= repairCost
+
+ // Finally, we add the item to the player
+ player.setItemOnCursor(output)
+
+ return
+ }
}
/**
* Function to calculate work penalty of anvil work
* Also change result work penalty
*/
- private fun calculatePenalty(left: ItemStack, right: ItemStack, result: ItemStack): Int{
+ private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int{
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
// Calculate work penality
val leftPenality = (left.itemMeta as? Repairable)?.repairCost ?: 0
- val rightPenality = (right.itemMeta as? Repairable)?.repairCost ?: 0
+ val rightPenality =
+ if(right == null){ 0 }
+ else{ (right.itemMeta as? Repairable)?.repairCost ?: 0 }
// Try to set work penality for the result item
result.itemMeta?.let {
@@ -147,7 +250,7 @@ class AnvilEventListener : Listener {
var illegalPenalty = 0
var rightValue = 0
- val rightIsFormBook = right.isBook()
+ val rightIsFormBook = right.isEnchantedBook()
val resultEnchs = result.findEnchantments()
val resultEnchsKeys = HashSet(resultEnchs.keys)
@@ -179,4 +282,28 @@ class AnvilEventListener : Listener {
return rightValue + illegalPenalty
}
+ /**
+ * Display xp needed for the work on the anvil inventory
+ */
+ private fun handleDisplayedXp(inventory: AnvilInventory,
+ event: PrepareAnvilEvent,
+ anvilCost: Int){
+ inventory.maximumRepairCost = Int.MAX_VALUE
+ inventory.repairCost = anvilCost
+ /* Because Minecraft likes to have the final say in the repair cost displayed
+ * we need to wait for the event to end before overriding it, this ensures that
+ * we have the final say in the process. */
+ UnsafeEnchants.instance
+ .server
+ .scheduler
+ .runTask(UnsafeEnchants.instance, Runnable {
+ if (ConfigOptions.removeRepairLimit) {
+ inventory.maximumRepairCost = Int.MAX_VALUE
+ }
+ inventory.repairCost = anvilCost
+
+ event.view.setProperty(REPAIR_COST, anvilCost)
+ })
+ }
+
}
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index c142495..0e7ddf8 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -17,6 +17,8 @@ object ConfigOptions {
private const val LIMIT_REPAIR_VALUE = "limit_repair_value"
// Path for level cost on item repair
private const val ITEM_REPAIR_COST = "item_repair_cost"
+ // Path for level cost on unit repair
+ private const val UNIT_REPAIR_COST = "unit_repair_cost"
// Path for level cost on item renaming
private const val ITEM_RENAME_COST = "item_rename_cost"
// Path for level cost on illegal enchantment on sacrifice
@@ -41,6 +43,8 @@ object ConfigOptions {
private const val DEFAULT_LIMIT_REPAIR_VALUE = 39
// Default value for level cost on item repair
private const val DEFAULT_ITEM_REPAIR_COST = 2
+ // Default value for level cost per unit repair
+ private const val DEFAULT_UNIT_REPAIR_COST = 1
// Default value for level cost on item renaming
private const val DEFAULT_ITEM_RENAME_COST = 1
// Default value for level cost on illegal enchantment on sacrifice
@@ -48,7 +52,7 @@ object ConfigOptions {
// Valid range for repair cost limit
private val REPAIR_LIMIT_RANGE = 1..39
// Valid range for repair cost
- private val ITEM_REPAIR_COST_RANGE = 0..255
+ private val REPAIR_COST_RANGE = 0..255
// Valid range for rename cost
private val ITEM_RENAME_COST_RANGE = 0..255
// Valid range for illegal enchantment conflict cost
@@ -102,10 +106,22 @@ object ConfigOptions {
return UnsafeEnchants.instance
.config
.getInt(ITEM_REPAIR_COST, DEFAULT_ITEM_REPAIR_COST)
- .takeIf { it in ITEM_REPAIR_COST_RANGE }
+ .takeIf { it in REPAIR_COST_RANGE }
?: DEFAULT_ITEM_REPAIR_COST
}
+ /**
+ * Value of an item repair
+ */
+ val unitRepairCost: Int
+ get() {
+ return UnsafeEnchants.instance
+ .config
+ .getInt(UNIT_REPAIR_COST, DEFAULT_UNIT_REPAIR_COST)
+ .takeIf { it in REPAIR_COST_RANGE }
+ ?: DEFAULT_UNIT_REPAIR_COST
+ }
+
/**
* Value of an item rename
*/
diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
index b60adf2..2df46ab 100644
--- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
@@ -31,17 +31,13 @@ object EnchantmentUtil {
other.forEach { (enchantment, level) ->
// Enchantment not yet in result list
if (!containsKey(enchantment)) {
- if(player.hasPermission(UnsafeEnchants.unsafePermission)){
- // Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
- this[enchantment] = level
- if(!player.hasPermission(UnsafeEnchants.bypassFusePermission) &&
- (UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
- this.remove(enchantment)
- }
- }else if(!keys.any { enchantment.conflictsWith(it) }){
-
- this[enchantment] = level
+ // Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
+ this[enchantment] = level
+ if(!player.hasPermission(UnsafeEnchants.bypassFusePermission) &&
+ (UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
+ this.remove(enchantment)
}
+
}
// Enchantment already in result list
else{
diff --git a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
index bd46167..3e8763b 100644
--- a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
@@ -1,13 +1,13 @@
package io.delilaheve.util
import io.delilaheve.UnsafeEnchants
-import org.bukkit.Material.BOOK
import org.bukkit.Material.ENCHANTED_BOOK
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.EnchantmentStorageMeta
-import org.bukkit.inventory.meta.ItemMeta
+import kotlin.math.ceil
+import kotlin.math.max
import kotlin.math.min
/**
@@ -15,20 +15,15 @@ import kotlin.math.min
*/
object ItemUtil {
- /**
- * Check if this [ItemStack] is a [BOOK] or [ENCHANTED_BOOK]
- */
- fun ItemStack.isBook() = type in listOf(BOOK, ENCHANTED_BOOK)
-
/**
* Check if this [ItemStack] is an [ENCHANTED_BOOK]
*/
- private fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
+ fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
- fun ItemStack.findEnchantments() = if (isBook()) {
+ fun ItemStack.findEnchantments() = if (isEnchantedBook()) {
(itemMeta as? EnchantmentStorageMeta)?.storedEnchants ?: emptyMap()
} else {
itemMeta?.enchants ?: emptyMap()
@@ -38,7 +33,7 @@ object ItemUtil {
* Apply an [enchantments] map to this [ItemStack]
*/
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map) {
- if (isBook()) {
+ if (isEnchantedBook()) {
/* For some god-forsaken reason, item meta is not mutable
* so, we have to get the instance, modify it, then set it
* back to the item... #BecauseMinecraft */
@@ -88,18 +83,40 @@ object ItemUtil {
val combinedDurability = firstDurability + secondDurability
val newDurability = min(combinedDurability, durability)
it.damage = durability - newDurability
- itemMeta = it as ItemMeta
+ itemMeta = it
return true
}
return false
}
+ fun ItemStack.unitRepair(
+ unitAmount: Int,
+ percentPerUnit: Double
+ ): Int {
+ (itemMeta as? Damageable)?.let {
+ val durability = type.maxDurability.toInt()
+ val firstDamage = it.damage
+ if( firstDamage == 0) return 0
+ var unitCount = 0
+ var damage = firstDamage
+ while((unitCount < unitAmount) && (damage > 0)){
+ unitCount++
+ damage = ceil(firstDamage - durability*percentPerUnit*unitCount).toInt()
+ }
+
+ it.damage = max(damage, 0)
+ itemMeta = it
+ return unitCount
+ }
+ return 0
+ }
+
/**
* Check that this [ItemStack] can merge with the [other]
*
* The two items should either be the same type, or, the [other] is a book
*/
fun ItemStack.canMergeWith(
- other: ItemStack
- ) = type == other.type || (other.isEnchantedBook())
+ other: ItemStack?
+ ) = (other != null) && (type == other.type || (other.isEnchantedBook()))
}
diff --git a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
index fd53415..7f1df1d 100644
--- a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
@@ -16,15 +16,18 @@ object UnitRepairUtil {
* null if can't unit repaired by [other]
*/
fun ItemStack.getRepair(
- other: ItemStack
+ other: ItemStack?
): Double? {
+ if(other == null) return null
val config = UnsafeEnchants.unitRepairConfig
// Get configuration section if exist
val otherName = other.type.name.uppercase()
var section = config.getConfigurationSection(otherName)
if(section == null){
section = config.getConfigurationSection(otherName.lowercase())
- if(section == null) return null
+ if(section == null) {
+ return null
+ }
}
// Get repair amount
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH,DEFAULT_DEFAULT_UNIT_REPAIR)
diff --git a/src/main/resources/unit_repair_item.yml b/src/main/resources/unit_repair_item.yml
index 2eb6e0d..0e96878 100644
--- a/src/main/resources/unit_repair_item.yml
+++ b/src/main/resources/unit_repair_item.yml
@@ -11,8 +11,14 @@
# If value > 1 it will be treated as being = 1
default_repair_amount: 0.25
-# Vanilla unit repair group is bellow
+# You can add custom unit repair
+# The example bellow make a shield repaired by 10% by sticks
+#stick:
+# shield: 0.10
+
+
+# Vanilla unit repair group is bellow
diamond:
diamond_helmet: 0.25
diamond_chestplate: 0.25
From d9c7a9376b1f35ce31dcd301d5d2980db6e25cc9 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Fri, 9 Feb 2024 15:06:56 +0100
Subject: [PATCH 006/691] add shift click for unit repair
---
.../io/delilaheve/AnvilEventListener.kt | 166 ++++++++++++------
1 file changed, 116 insertions(+), 50 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index b6382fd..76d9cf4 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -9,6 +9,7 @@ import io.delilaheve.util.ItemUtil.isEnchantedBook
import io.delilaheve.util.ItemUtil.repairFrom
import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
import io.delilaheve.util.ItemUtil.unitRepair
+import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.Event
@@ -25,6 +26,7 @@ import xyz.alexcrea.group.ConflictType
import xyz.alexcrea.util.UnitRepairUtil.getRepair
import kotlin.math.min
+
/**
* Listener for anvil events
*/
@@ -35,6 +37,9 @@ class AnvilEventListener : Listener {
private const val ANVIL_INPUT_LEFT = 0
private const val ANVIL_INPUT_RIGHT = 1
private const val ANVIL_OUTPUT_SLOT = 2
+ // static slot container
+ private val NO_SLOT = SlotContainer(SlotType.NO_SLOT,0)
+ private val CURSOR_SLOT = SlotContainer(SlotType.CURSOR,0)
}
/**
@@ -170,75 +175,127 @@ class AnvilEventListener : Listener {
if(canMerge){
event.result = Event.Result.ALLOW
}else if(unitRepairResult != null){
- val resultCopy = leftItem.clone()
- val resultAmount = resultCopy.unitRepair(
- rightItem.amount, unitRepairResult)
-
- // To avoid vanilla, we cancel the event for unit repair
- event.result = Event.Result.DENY
- event.isCancelled = true
- // And we give the item manually
- // But first we check if we should give the item
- if(player.itemOnCursor.type != Material.AIR) return
- if(inventory.repairCost > player.level) return
-
- // Get repairCost
- var repairCost = 0
- leftItem.itemMeta?.let { leftMeta ->
- val leftName = leftMeta.displayName
- output.itemMeta?.let {
- if(!leftName.contentEquals(it.displayName)){
- repairCost+= ConfigOptions.itemRenameCost
- }
- }
- }
-
- repairCost+= calculatePenalty(leftItem,null,resultCopy)
- repairCost+= resultAmount*ConfigOptions.unitRepairCost
-
- if((inventory.maximumRepairCost < repairCost)
- || (player.level < repairCost)) return
-
- // We remove what should be removed
- inventory.setItem(ANVIL_INPUT_LEFT,null)
- rightItem.amount-= resultAmount
- inventory.setItem(ANVIL_INPUT_RIGHT,rightItem)
- inventory.setItem(ANVIL_OUTPUT_SLOT, null)
-
- UnsafeEnchants.log("repair cost: $repairCost")
- player.level-= repairCost
-
- // Finally, we add the item to the player
- player.setItemOnCursor(output)
+ onUnitRepairExtract(leftItem, rightItem, output,
+ unitRepairResult, event, player, inventory)
return
}
}
+ private fun onUnitRepairExtract(leftItem: ItemStack,
+ rightItem: ItemStack,
+ output: ItemStack,
+ unitRepairResult: Double,
+ event: InventoryClickEvent,
+ player: Player,
+ inventory: AnvilInventory){
+ val resultCopy = leftItem.clone()
+ val resultAmount = resultCopy.unitRepair(
+ rightItem.amount, unitRepairResult)
+
+ // To avoid vanilla, we cancel the event for unit repair
+ event.result = Event.Result.DENY
+ event.isCancelled = true
+ // And we give the item manually
+ // But first we check if we should give the item
+ val slotDestination = getActionSlot(event,player)
+ if(slotDestination.type == SlotType.NO_SLOT) return
+ if(inventory.repairCost > player.level) return
+
+ // Get repairCost
+ var repairCost = 0
+ leftItem.itemMeta?.let { leftMeta ->
+ val leftName = leftMeta.displayName
+ output.itemMeta?.let {
+ if(!leftName.contentEquals(it.displayName)){
+ repairCost+= ConfigOptions.itemRenameCost
+ }
+ }
+ }
+
+ repairCost+= calculatePenalty(leftItem,null,resultCopy)
+ repairCost+= resultAmount*ConfigOptions.unitRepairCost
+
+ val ignoreXpCost = player.gameMode == GameMode.CREATIVE
+ if((!ignoreXpCost) && ((inventory.maximumRepairCost < repairCost)
+ || (player.level < repairCost))) return
+
+ // We remove what should be removed
+ inventory.setItem(ANVIL_INPUT_LEFT,null)
+ rightItem.amount-= resultAmount
+ inventory.setItem(ANVIL_INPUT_RIGHT,rightItem)
+ inventory.setItem(ANVIL_OUTPUT_SLOT, null)
+
+ if(!ignoreXpCost){
+ player.level-= repairCost
+ }
+
+ // Finally, we add the item to the player
+ if(slotDestination.type == SlotType.CURSOR){
+ player.setItemOnCursor(output)
+ }else{// We assume SlotType == SlotType.INVENTORY
+ player.inventory.setItem(slotDestination.slot,output)
+ }
+ }
+
+ /**
+ * Get the destination slot or "NO_SLOT" slot container if there is no slot available
+ */
+ private fun getActionSlot(event: InventoryClickEvent, player: Player): SlotContainer{
+ if(event.isShiftClick){
+ val inventory = player.inventory
+ val firstEmpty = inventory.firstEmpty()
+ if(firstEmpty == -1){
+ return NO_SLOT
+ }
+ //check hotbare full
+ var slotIndex = 8
+ while(slotIndex >= 0 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)){
+ slotIndex--
+ }
+ if(slotIndex >= 0){
+ return SlotContainer(SlotType.INVENTORY,slotIndex)
+ }
+ slotIndex = 35 //4*9 - 1 (max of player inventory)
+ while(slotIndex >= 9 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)){
+ slotIndex--
+ }
+ if(slotIndex < 9){
+ return NO_SLOT
+ }
+ return SlotContainer(SlotType.INVENTORY,slotIndex)
+ }else{
+ if(player.itemOnCursor.type != Material.AIR){
+ return NO_SLOT
+ }
+ return CURSOR_SLOT
+ }
+ }
+
/**
* Function to calculate work penalty of anvil work
* Also change result work penalty
*/
private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int{
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
- // Calculate work penality
- val leftPenality = (left.itemMeta as? Repairable)?.repairCost ?: 0
- val rightPenality =
+ // Calculate work penalty
+ val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0
+ val rightPenalty =
if(right == null){ 0 }
else{ (right.itemMeta as? Repairable)?.repairCost ?: 0 }
- // Try to set work penality for the result item
+ // Try to set work penalty for the result item
result.itemMeta?.let {
- (it as? Repairable)?.repairCost = leftPenality*2+1
+ (it as? Repairable)?.repairCost = leftPenalty*2+1
result.itemMeta = it
}
- UnsafeEnchants.log("Calculated penality: " +
- "leftPenality: $leftPenality, " +
- "rightPenality: $rightPenality, " +
- "result penality: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}")
+ UnsafeEnchants.log("Calculated penalty: " +
+ "leftPenalty: $leftPenalty, " +
+ "rightPenalty: $rightPenalty, " +
+ "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}")
- return leftPenality + rightPenality
+ return leftPenalty + rightPenalty
}
/**
@@ -307,3 +364,12 @@ class AnvilEventListener : Listener {
}
}
+
+
+private class SlotContainer(val type: SlotType, val slot: Int)
+private enum class SlotType{
+ CURSOR,
+ INVENTORY,
+ NO_SLOT
+
+}
From 2f051a93e391deb106e37ac5191ead76c03cfc75 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Sat, 10 Feb 2024 22:59:30 +0100
Subject: [PATCH 007/691] add reload config command
---
.../kotlin/io/delilaheve/UnsafeEnchants.kt | 35 ++++++++++++++-----
.../xyz/alexcrea/command/ReloadExecutor.kt | 35 +++++++++++++++++++
src/main/resources/plugin.yml | 9 ++++-
3 files changed, 70 insertions(+), 9 deletions(-)
create mode 100644 src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
diff --git a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
index 3abcf12..ac83f1c 100644
--- a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
+++ b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
@@ -4,6 +4,7 @@ import io.delilaheve.util.ConfigOptions
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
+import xyz.alexcrea.command.ReloadExecutor
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
import xyz.alexcrea.util.Metrics
@@ -27,6 +28,11 @@ class UnsafeEnchants : JavaPlugin() {
const val bypassFusePermission = "ue.bypass.fuse"
// Permission string required to bypass enchantment conflicts test
const val bypassLevelPermission = "ue.bypass.level"
+ // Permission string required to reload the config
+ const val commandReloadPermission = "ue.command.reload"
+
+ // Command Name to reload the config
+ const val commandReloadName = "reloadunsafeenchants"
// Item Grouping Configuration file name
const val itemGroupingConfigFilePath = "item_groups.yml"
@@ -60,10 +66,24 @@ class UnsafeEnchants : JavaPlugin() {
instance = this
// Load bstats
val metric = Metrics(this, bstatsPluginId)
- saveDefaultConfig()
+ reloadAllConfigs()
addCustomMetric(metric)
+ // Add command to reload the plugin
+ val command = getCommand(commandReloadName)
+ command?.setExecutor(ReloadExecutor())
+
+ server.pluginManager.registerEvents(
+ AnvilEventListener(),
+ this
+ )
+
+ }
+
+ fun reloadAllConfigs(){
+ saveDefaultConfig()
+
// Load material grouping config
val itemGroupConfig = reloadResource(itemGroupingConfigFilePath) ?: return
// Read material groups from config
@@ -73,17 +93,15 @@ class UnsafeEnchants : JavaPlugin() {
// Load enchantment conflicts config
val conflictConfig = reloadResource(enchantConflicConfigFilePath) ?: return
// Read conflicts from config and material group manager
- conflictManager = EnchantConflictManager()
+ val conflictManager = EnchantConflictManager()
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
// Load unit repair config
- unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
-
- server.pluginManager.registerEvents(
- AnvilEventListener(),
- this
- )
+ val unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
+ // Set the global variable
+ UnsafeEnchants.conflictManager = conflictManager
+ UnsafeEnchants.unitRepairConfig = unitRepairConfig
}
private fun addCustomMetric(metric: Metrics) {
@@ -96,6 +114,7 @@ class UnsafeEnchants : JavaPlugin() {
metric.addCustomChart(SimplePie("sacrifice_illegal_enchant_cost") {
ConfigOptions.sacrificeIllegalCost.toString()
})
+
}
private fun reloadResource(resourceName: String,
diff --git a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
new file mode 100644
index 0000000..c086ee6
--- /dev/null
+++ b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
@@ -0,0 +1,35 @@
+package xyz.alexcrea.command
+
+import io.delilaheve.UnsafeEnchants
+import org.bukkit.command.Command
+import org.bukkit.command.CommandExecutor
+import org.bukkit.command.CommandSender
+
+class ReloadExecutor : CommandExecutor {
+ override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean {
+ if(!sender.hasPermission(UnsafeEnchants.commandReloadPermission)) {
+ sender.sendMessage("§cYou do not have permission to reload the config")
+ return false
+ }
+ sender.sendMessage("§eReloading config...")
+ val commandSuccess = commandBody()
+ if(commandSuccess){
+ sender.sendMessage("§aConfig reloaded !")
+ }else{
+ sender.sendMessage("§cConfig was not able to be reloaded...")
+ }
+ return commandSuccess
+ }
+
+ /**
+ * Execute the command, return true if success or false otherwise
+ */
+ private fun commandBody(): Boolean{
+ try {
+ UnsafeEnchants.instance.reloadAllConfigs()
+ return true
+ }catch (ignored: Exception){
+ return false
+ }
+ }
+}
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 1d71ebc..38151f4 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -8,6 +8,10 @@ load: POSTWORLD
authors: [DelilahEve, alexcrea]
libraries:
- org.jetbrains.kotlin:kotlin-stdlib:1.6.21
+commands:
+ reloadunsafeenchants:
+ description: Reload every config of this plugin
+ permission: ue.command.reload
permissions:
ue.unsafe:
default: true
@@ -17,4 +21,7 @@ permissions:
description: Allow player to combine every "unsafe" enchants
ue.bypass.level:
default: false
- description: Allow player to bypass max level limit
\ No newline at end of file
+ description: Allow player to bypass max level limit
+ ue.command.reload:
+ default: op
+ description: Allow administrator to reload the plugin's config
\ No newline at end of file
From b36c1827a4f5d6c71a60baef2dc3cbc7dc22174c Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Sun, 11 Feb 2024 07:14:48 +0100
Subject: [PATCH 008/691] fix creative issues
---
.../io/delilaheve/AnvilEventListener.kt | 45 ++++++++++---------
1 file changed, 23 insertions(+), 22 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index 76d9cf4..11b583d 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -16,6 +16,7 @@ import org.bukkit.event.Event
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority.HIGHEST
import org.bukkit.event.Listener
+import org.bukkit.event.inventory.ClickType
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
@@ -200,33 +201,33 @@ class AnvilEventListener : Listener {
// But first we check if we should give the item
val slotDestination = getActionSlot(event,player)
if(slotDestination.type == SlotType.NO_SLOT) return
- if(inventory.repairCost > player.level) return
- // Get repairCost
+ // Test repair cost
var repairCost = 0
- leftItem.itemMeta?.let { leftMeta ->
- val leftName = leftMeta.displayName
- output.itemMeta?.let {
- if(!leftName.contentEquals(it.displayName)){
- repairCost+= ConfigOptions.itemRenameCost
+ if(player.gameMode != GameMode.CREATIVE){
+ // Get repairCost
+ leftItem.itemMeta?.let { leftMeta ->
+ val leftName = leftMeta.displayName
+ output.itemMeta?.let {
+ if(!leftName.contentEquals(it.displayName)){
+ repairCost+= ConfigOptions.itemRenameCost
+ }
}
}
+
+ repairCost+= calculatePenalty(leftItem,null,resultCopy)
+ repairCost+= resultAmount*ConfigOptions.unitRepairCost
+
+ if((inventory.maximumRepairCost < repairCost)
+ || (player.level < repairCost)) return
}
-
- repairCost+= calculatePenalty(leftItem,null,resultCopy)
- repairCost+= resultAmount*ConfigOptions.unitRepairCost
-
- val ignoreXpCost = player.gameMode == GameMode.CREATIVE
- if((!ignoreXpCost) && ((inventory.maximumRepairCost < repairCost)
- || (player.level < repairCost))) return
-
- // We remove what should be removed
- inventory.setItem(ANVIL_INPUT_LEFT,null)
- rightItem.amount-= resultAmount
- inventory.setItem(ANVIL_INPUT_RIGHT,rightItem)
- inventory.setItem(ANVIL_OUTPUT_SLOT, null)
-
- if(!ignoreXpCost){
+ // If not creative middle click...
+ if(event.click != ClickType.MIDDLE){
+ // We remove what should be removed
+ inventory.setItem(ANVIL_INPUT_LEFT,null)
+ rightItem.amount-= resultAmount
+ inventory.setItem(ANVIL_INPUT_RIGHT,rightItem)
+ inventory.setItem(ANVIL_OUTPUT_SLOT, null)
player.level-= repairCost
}
From 944e4b025ce26f73d7c4d5c46ebe72482777f858 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Mon, 12 Feb 2024 19:23:44 +0100
Subject: [PATCH 009/691] add more usefull metrics
---
.../kotlin/io/delilaheve/UnsafeEnchants.kt | 22 +---
.../io/delilaheve/util/ConfigOptions.kt | 20 ++-
.../kotlin/xyz/alexcrea/util/MetricsUtil.kt | 119 ++++++++++++++++++
3 files changed, 143 insertions(+), 18 deletions(-)
create mode 100644 src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
diff --git a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
index ac83f1c..d22ce58 100644
--- a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
+++ b/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
@@ -8,7 +8,7 @@ import xyz.alexcrea.command.ReloadExecutor
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
import xyz.alexcrea.util.Metrics
-import xyz.alexcrea.util.Metrics.SimplePie
+import xyz.alexcrea.util.MetricsUtil
import java.io.File
import java.io.FileReader
@@ -64,11 +64,11 @@ class UnsafeEnchants : JavaPlugin() {
*/
override fun onEnable() {
instance = this
- // Load bstats
- val metric = Metrics(this, bstatsPluginId)
reloadAllConfigs()
- addCustomMetric(metric)
+ // Load metrics
+ val metric = Metrics(this, bstatsPluginId)
+ MetricsUtil.addCustomMetric(metric)
// Add command to reload the plugin
val command = getCommand(commandReloadName)
@@ -102,19 +102,9 @@ class UnsafeEnchants : JavaPlugin() {
// Set the global variable
UnsafeEnchants.conflictManager = conflictManager
UnsafeEnchants.unitRepairConfig = unitRepairConfig
- }
-
- private fun addCustomMetric(metric: Metrics) {
- metric.addCustomChart(SimplePie("item_rename_cost") {
- ConfigOptions.itemRenameCost.toString()
- })
- metric.addCustomChart(SimplePie("item_repair_cost") {
- ConfigOptions.itemRepairCost.toString()
- })
- metric.addCustomChart(SimplePie("sacrifice_illegal_enchant_cost") {
- ConfigOptions.sacrificeIllegalCost.toString()
- })
+ // Test if default config
+ MetricsUtil.testIfConfigIsDefault(config, itemGroupConfig, conflictConfig, unitRepairConfig)
}
private fun reloadResource(resourceName: String,
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index 0e7ddf8..f01f7f6 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -26,9 +26,9 @@ object ConfigOptions {
// Path for removing repair cost limits
private const val REMOVE_REPAIR_LIMIT = "remove_repair_limit"
// Root path for enchantment limits
- private const val ENCHANT_LIMIT_ROOT = "enchant_limits"
+ const val ENCHANT_LIMIT_ROOT = "enchant_limits"
// Root path for enchantment values
- private const val ENCHANT_VALUES_ROOT = "enchant_values"
+ const val ENCHANT_VALUES_ROOT = "enchant_values"
// Keys for specific enchantment values
private const val KEY_BOOK = "book"
private const val KEY_ITEM = "item"
@@ -194,4 +194,20 @@ object ConfigOptions {
?: DEFAULT_ENCHANT_VALUE
}
+ /**
+ * Get an array of key of basic config options
+ */
+ fun getBasicConfigKeys(): Array{
+ return arrayOf(DEFAULT_LIMIT_PATH,
+ LIMIT_REPAIR_COST,
+ LIMIT_REPAIR_VALUE,
+ ITEM_REPAIR_COST,
+ UNIT_REPAIR_COST,
+ ITEM_RENAME_COST,
+ SACRIFICE_ILLEGAL_COST,
+ REMOVE_REPAIR_LIMIT
+ )
+ }
+
+
}
diff --git a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
new file mode 100644
index 0000000..17f8bac
--- /dev/null
+++ b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
@@ -0,0 +1,119 @@
+package xyz.alexcrea.util
+
+import io.delilaheve.UnsafeEnchants
+import io.delilaheve.util.ConfigOptions
+import org.bukkit.configuration.ConfigurationSection
+
+object MetricsUtil {
+
+ private const val baseConfigHash = -1527723485
+ private const val enchantLimitsConfigHash = 781312397
+ private const val enchantValuesConfigHash = 1072574774
+ private const val enchantConflictConfigHash = 1406650190
+ private const val itemGroupsConfigHash = -1014133828
+ private const val unitRepairItemConfigHash = 536871958
+ private const val baseConfigPieName = "isDefaultBaseConfig"
+ private const val enchantLimitsConfigPieName = "isDefaultEnchantLimitsConfig"
+ private const val enchantValuesConfigPieName = "isDefaultEnchantValuesConfig"
+ private const val enchantConflictConfigPieName = "isDefaultEnchantConflictConfig"
+ private const val itemGroupsConfigPieName = "isDefaultItemGroupsConfig"
+ private const val unitRepairItemConfigPieName = "isDefaultUnitRepairItemConfig"
+ private var isDefaultBaseConfig = true
+ private var isDefaultEnchantLimitsConfig = true
+ private var isDefaultEnchantValuesConfig = true
+ private var isDefaultEnchantConflictConfig = true
+ private var isDefaultItemGroupsConfig = true
+ private var isDefaultUnitRepairItemConfig = true
+
+ /**
+ * Get hash of a key, value a pair of a configuration section
+ */
+ private fun getHashFromKey(section: ConfigurationSection, key: String): Int {
+ // Key is assumend to exist
+ val resultHash: Int
+ if(section.isConfigurationSection(key)){
+ val sectionResult = getConfigurationHash(section.getConfigurationSection(key)!!)
+ resultHash = key.hashCode() xor sectionResult
+ }else{
+ resultHash = key.hashCode() xor section.getString(key).hashCode()
+ }
+ return resultHash.hashCode()
+ }
+
+ /**
+ * Get hash of a configuration section
+ */
+ private fun getConfigurationHash(section: ConfigurationSection): Int {
+ var resultHash = 0
+ for (key in section.getKeys(false)) {
+ resultHash = resultHash xor getHashFromKey(section,key)
+ }
+ return resultHash
+ }
+
+ /**
+ * Get hash value of the default config
+ */
+ private fun testBaseConfig(defaultConfig: ConfigurationSection): Int{
+ var result = 0
+ for (key in ConfigOptions.getBasicConfigKeys()) {
+ result = result xor getHashFromKey(defaultConfig,key)
+ }
+ return result
+ }
+
+ /**
+ * Test if the used configuration is the default config
+ */
+ fun testIfConfigIsDefault(defaultConfig: ConfigurationSection,
+ enchantConflictConfig: ConfigurationSection,
+ itemGroupsConfig: ConfigurationSection,
+ unitRepairItemConfig: ConfigurationSection){
+ // Calculate hash of config
+ val baseConfig = testBaseConfig(defaultConfig)
+ val limitEnchantConfig = getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_LIMIT_ROOT)
+ val enchantValueConfig =getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_VALUES_ROOT)
+ val enchantConflictConfig2 = getConfigurationHash(enchantConflictConfig)
+ val itemGroupConfig = getConfigurationHash(itemGroupsConfig)
+ val unitRepairConfig = getConfigurationHash(unitRepairItemConfig)
+ // Test if default
+ isDefaultBaseConfig = baseConfigHash == baseConfig
+ isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig
+ isDefaultEnchantValuesConfig = enchantValuesConfigHash == enchantValueConfig
+ isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig2
+ isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig
+ isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
+ // If not default and debug flag active, print the hash.
+ if(ConfigOptions.debugLog){
+ if(!isDefaultBaseConfig){UnsafeEnchants.log("baseConfig: $baseConfig")}
+ if(!isDefaultEnchantLimitsConfig){UnsafeEnchants.log("limitEnchantConfig: $limitEnchantConfig")}
+ if(!isDefaultEnchantValuesConfig){UnsafeEnchants.log("enchantValueConfig: $enchantValueConfig")}
+ if(!isDefaultEnchantConflictConfig){UnsafeEnchants.log("enchantConflictConfig: $enchantConflictConfig")}
+ if(!isDefaultItemGroupsConfig){UnsafeEnchants.log("itemGroupConfig: $itemGroupConfig")}
+ if(!isDefaultUnitRepairItemConfig){UnsafeEnchants.log("unitRepairConfig: $unitRepairConfig")}
+ }
+
+ }
+
+ fun addCustomMetric(metric: Metrics) {
+ metric.addCustomChart(Metrics.SimplePie(baseConfigPieName) {
+ isDefaultBaseConfig.toString()
+ })
+ metric.addCustomChart(Metrics.SimplePie(enchantLimitsConfigPieName) {
+ isDefaultEnchantLimitsConfig.toString()
+ })
+ metric.addCustomChart(Metrics.SimplePie(enchantValuesConfigPieName) {
+ isDefaultEnchantValuesConfig.toString()
+ })
+ metric.addCustomChart(Metrics.SimplePie(enchantConflictConfigPieName) {
+ isDefaultEnchantConflictConfig.toString()
+ })
+ metric.addCustomChart(Metrics.SimplePie(itemGroupsConfigPieName) {
+ isDefaultItemGroupsConfig.toString()
+ })
+ metric.addCustomChart(Metrics.SimplePie(unitRepairItemConfigPieName) {
+ isDefaultUnitRepairItemConfig.toString()
+ })
+
+ }
+}
\ No newline at end of file
From c366b45e16d32d643e8a178965f8f351a66103b1 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Wed, 14 Feb 2024 01:28:48 +0100
Subject: [PATCH 010/691] renamed the plugin
---
README.md | 54 +++++++++++--------
.../io/delilaheve/AnvilEventListener.kt | 16 +++---
.../{UnsafeEnchants.kt => CustomAnvil.kt} | 27 ++++++----
.../io/delilaheve/util/ConfigOptions.kt | 24 ++++-----
.../io/delilaheve/util/EnchantmentUtil.kt | 12 ++---
.../kotlin/io/delilaheve/util/ItemUtil.kt | 4 +-
.../xyz/alexcrea/command/ReloadExecutor.kt | 9 ++--
.../alexcrea/group/EnchantConflictManager.kt | 12 ++---
.../xyz/alexcrea/group/ItemGroupManager.kt | 12 ++---
.../kotlin/xyz/alexcrea/util/MetricsUtil.kt | 14 ++---
.../xyz/alexcrea/util/UnitRepairUtil.kt | 4 +-
src/main/resources/plugin.yml | 37 ++++++++-----
12 files changed, 125 insertions(+), 100 deletions(-)
rename src/main/kotlin/io/delilaheve/{UnsafeEnchants.kt => CustomAnvil.kt} (82%)
diff --git a/README.md b/README.md
index 85fcbf0..3d38489 100644
--- a/README.md
+++ b/README.md
@@ -1,40 +1,48 @@
-# UnsafeEnchants+
+# Custom Anvil
-**UnsafeEnchants+** is a fully configurable plugin for bukkit, spigot, and paper minecraft servers
-allowing custom enchantment limits and customising combination restrictions.
+**Custom Anvil** is a plugin to allow admin to customise evey aspect of the anvil's mechanics.
+It is expected to work on 1.18 to 1.20.4 minecraft servers running spigot or paper.
-**UnsafeEnchants+** is based on [UnsafeEnchants](https://github.com/DelilahEve/UnsafeEnchants). You can find it on
+**Custom Anvil** was previously named **Unsafe Enchants+**.
+It was renamed because it now affects every anvil aspect and not only unsafe enchants
+
+**Custom Anvil** is based on [Unsafe Enchants](https://github.com/DelilahEve/UnsafeEnchants) by DelilahEve. You can find it on
[GitHub](https://github.com/DelilahEve/UnsafeEnchants/releases/latest),
[Spigot](https://www.spigotmc.org/resources/unsafe-enchants.104708/) or
[CurseForge](https://www.curseforge.com/minecraft/bukkit-plugins/unsafe-enchants/files/all)
-
-**UnsafeEnchants+** add the following to [UnsafeEnchants](https://github.com/DelilahEve/UnsafeEnchants):
-- Make default configuration more vanilla like
-- Fix a xp bug with enchanted book
-- Custom enchantment restriction configuration
----
-### Know issue:
-There is non known issue, if you find one please report the issue.
-
----
-
### Download Locations:
-the plugin can be downloaded on the
-[Spigot site](https://www.spigotmc.org/resources/unsafe-enchants.114884/)
+the plugin can be downloaded on the
+[Spigot site](https://www.spigotmc.org/resources/unsafe-enchants.114884/)
or [on GitHub](https://github.com/alexcrea/UnsafeEnchantsPlus/releases/latest)
-
+---
+**Custom Anvil** have the following features:
+- Vanilla like default configuration
+- Custom enchantment level limit
+- Custom enchant restrictions (allow unsafe enchantment only for a group of item or create new restriction)
+- Custom items of unit repairs (repair damaged with unit of "material", for example the repair of diamond sword by diamonds)
+- Custom XP cost for every aspect of the anvil
+- Permissions to bypass level limit or enchantment restriction.
+---
### Permissions:
```yml
-ue.unsafe: Allows use of custom restriction rules
-ue.bypass.fuse: Bypass every enchantment restriction check. Including custom restrictions
-ue.bypass.level: Bypass max level check. Including custom max level
+ca.affected: Player with this permission will be affected by the plugin
+ca.bypass.fuse: Allow player to combine every enchantments to every item (no custom limit)
+ca.bypass.level: Allow player to bypass every level limit (no custom limit)
+ca.command.reload: Allow administrator to reload the plugin's configs
```
-
+### Commands
+```yml
+anvilconfigreload or carl: Reload every config of this plugin
+```
+---
### Default Configuration:
Default configuration can be found on following links:
- [config.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/config.yml)
- [enchant_conflict.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/enchant_conflict.yml)
- [item_groups.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/item_groups.yml)
-
+- [unit_repair_item.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/unit_repair_item.yml)
+---
+### Know issue:
+There is non known issue, if you find one please report the issue.
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index 11b583d..7efde5f 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -54,7 +54,7 @@ class AnvilEventListener : Listener {
// Should find player
val player = event.view.player
- if(!player.hasPermission(UnsafeEnchants.unsafePermission)) return
+ if(!player.hasPermission(CustomAnvil.unsafePermission)) return
// Test rename lonely item
if(second == null){
@@ -150,7 +150,7 @@ class AnvilEventListener : Listener {
@EventHandler(ignoreCancelled = true)
fun anvilExtractionCheck(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return
- if(!player.hasPermission(UnsafeEnchants.unsafePermission)) return
+ if(!player.hasPermission(CustomAnvil.unsafePermission)) return
val inventory = event.inventory as? AnvilInventory ?: return
if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
@@ -291,7 +291,7 @@ class AnvilEventListener : Listener {
result.itemMeta = it
}
- UnsafeEnchants.log("Calculated penalty: " +
+ CustomAnvil.log("Calculated penalty: " +
"leftPenalty: $leftPenalty, " +
"rightPenalty: $rightPenalty, " +
"result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}")
@@ -316,7 +316,7 @@ class AnvilEventListener : Listener {
// count enchant as illegal enchant if it conflicts with another enchant or not in result
if((enchantment.key !in resultEnchsKeys)){
resultEnchsKeys.add(enchantment.key)
- val conflictType = UnsafeEnchants.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
+ val conflictType = CustomAnvil.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
resultEnchsKeys.remove(enchantment.key)
if(ConflictType.BIG_CONFLICT == conflictType){
@@ -329,11 +329,11 @@ class AnvilEventListener : Listener {
val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook)
val value = resultLevel * enchantmentMultiplier
- UnsafeEnchants.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value")
+ CustomAnvil.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value")
rightValue += value
}
- UnsafeEnchants.log("Calculated right values: " +
+ CustomAnvil.log("Calculated right values: " +
"rightValue: $rightValue, " +
"illegalPenalty: $illegalPenalty")
@@ -351,10 +351,10 @@ class AnvilEventListener : Listener {
/* Because Minecraft likes to have the final say in the repair cost displayed
* we need to wait for the event to end before overriding it, this ensures that
* we have the final say in the process. */
- UnsafeEnchants.instance
+ CustomAnvil.instance
.server
.scheduler
- .runTask(UnsafeEnchants.instance, Runnable {
+ .runTask(CustomAnvil.instance, Runnable {
if (ConfigOptions.removeRepairLimit) {
inventory.maximumRepairCost = Int.MAX_VALUE
}
diff --git a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
similarity index 82%
rename from src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
rename to src/main/kotlin/io/delilaheve/CustomAnvil.kt
index d22ce58..42e7286 100644
--- a/src/main/kotlin/io/delilaheve/UnsafeEnchants.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -16,23 +16,23 @@ import java.io.FileReader
* Bukkit/Spigot/Paper plugin to alter enchantment max
* levels and allow unsafe enchantment combinations
*/
-class UnsafeEnchants : JavaPlugin() {
+class CustomAnvil : JavaPlugin() {
companion object {
// bstats plugin id
private const val bstatsPluginId = 20923
// Permission string required to use the plugin's features
- const val unsafePermission = "ue.unsafe"
+ const val unsafePermission = "ca.unsafe"
// Permission string required to bypass enchantment conflicts test
- const val bypassFusePermission = "ue.bypass.fuse"
+ const val bypassFusePermission = "ca.bypass.fuse"
// Permission string required to bypass enchantment conflicts test
- const val bypassLevelPermission = "ue.bypass.level"
+ const val bypassLevelPermission = "ca.bypass.level"
// Permission string required to reload the config
- const val commandReloadPermission = "ue.command.reload"
+ const val commandReloadPermission = "ca.command.reload"
// Command Name to reload the config
- const val commandReloadName = "reloadunsafeenchants"
+ const val commandReloadName = "anvilconfigreload"
// Item Grouping Configuration file name
const val itemGroupingConfigFilePath = "item_groups.yml"
@@ -42,7 +42,7 @@ class UnsafeEnchants : JavaPlugin() {
const val unitRepairFilePath = "unit_repair_item.yml"
// Current plugin instance
- lateinit var instance: UnsafeEnchants
+ lateinit var instance: CustomAnvil
// Current item grouping configuration instance
lateinit var conflictManager: EnchantConflictManager
@@ -65,6 +65,14 @@ class UnsafeEnchants : JavaPlugin() {
override fun onEnable() {
instance = this
+ // Disable old plugin name if exist
+ val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus")
+ if(potentialPlugin != null){
+ Bukkit.getPluginManager().disablePlugin(potentialPlugin)
+ logger.warning("An old version of this plugin was detected")
+ logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
+ }
+
reloadAllConfigs()
// Load metrics
val metric = Metrics(this, bstatsPluginId)
@@ -78,7 +86,6 @@ class UnsafeEnchants : JavaPlugin() {
AnvilEventListener(),
this
)
-
}
fun reloadAllConfigs(){
@@ -100,8 +107,8 @@ class UnsafeEnchants : JavaPlugin() {
val unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
// Set the global variable
- UnsafeEnchants.conflictManager = conflictManager
- UnsafeEnchants.unitRepairConfig = unitRepairConfig
+ CustomAnvil.conflictManager = conflictManager
+ CustomAnvil.unitRepairConfig = unitRepairConfig
// Test if default config
MetricsUtil.testIfConfigIsDefault(config, itemGroupConfig, conflictConfig, unitRepairConfig)
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index f01f7f6..1a235b2 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -1,6 +1,6 @@
package io.delilaheve.util
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import org.bukkit.enchantments.Enchantment
@@ -71,7 +71,7 @@ object ConfigOptions {
*/
private val defaultEnchantLimit: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(DEFAULT_LIMIT_PATH, DEFAULT_ENCHANT_LIMIT)
}
@@ -81,7 +81,7 @@ object ConfigOptions {
*/
val limitRepairCost: Boolean
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getBoolean(LIMIT_REPAIR_COST, DEFAULT_LIMIT_REPAIR)
}
@@ -91,7 +91,7 @@ object ConfigOptions {
*/
val limitRepairValue: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(LIMIT_REPAIR_VALUE, DEFAULT_LIMIT_REPAIR_VALUE)
.takeIf { it in REPAIR_LIMIT_RANGE }
@@ -103,7 +103,7 @@ object ConfigOptions {
*/
val itemRepairCost: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(ITEM_REPAIR_COST, DEFAULT_ITEM_REPAIR_COST)
.takeIf { it in REPAIR_COST_RANGE }
@@ -115,7 +115,7 @@ object ConfigOptions {
*/
val unitRepairCost: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(UNIT_REPAIR_COST, DEFAULT_UNIT_REPAIR_COST)
.takeIf { it in REPAIR_COST_RANGE }
@@ -127,7 +127,7 @@ object ConfigOptions {
*/
val itemRenameCost: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(ITEM_RENAME_COST, DEFAULT_ITEM_RENAME_COST)
.takeIf { it in ITEM_RENAME_COST_RANGE }
@@ -139,7 +139,7 @@ object ConfigOptions {
*/
val sacrificeIllegalCost: Int
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(SACRIFICE_ILLEGAL_COST, DEFAULT_SACRIFICE_ILLEGAL_COST)
.takeIf { it in SACRIFICE_ILLEGAL_COST_RANGE }
@@ -150,7 +150,7 @@ object ConfigOptions {
*/
val removeRepairLimit: Boolean
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getBoolean(REMOVE_REPAIR_LIMIT, DEFAULT_REMOVE_LIMIT)
}
@@ -160,7 +160,7 @@ object ConfigOptions {
*/
val debugLog: Boolean
get() {
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getBoolean(DEBUG_LOGGING, DEFAULT_DEBUG_LOG)
}
@@ -170,7 +170,7 @@ object ConfigOptions {
*/
fun enchantLimit(enchantment: Enchantment): Int {
val path = "${ENCHANT_LIMIT_ROOT}.${enchantment.enchantmentName}"
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(path, defaultEnchantLimit)
.takeIf { it in ENCHANT_LIMIT_RANGE }
@@ -187,7 +187,7 @@ object ConfigOptions {
): Int {
val typeKey = if (isFromBook) KEY_BOOK else KEY_ITEM
val path = "${ENCHANT_VALUES_ROOT}.${enchantment.enchantmentName}.$typeKey"
- return UnsafeEnchants.instance
+ return CustomAnvil.instance
.config
.getInt(path, DEFAULT_ENCHANT_VALUE)
.takeIf { it >= DEFAULT_ENCHANT_VALUE }
diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
index 2df46ab..07874d9 100644
--- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
@@ -1,6 +1,6 @@
package io.delilaheve.util
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.HumanEntity
@@ -33,8 +33,8 @@ object EnchantmentUtil {
if (!containsKey(enchantment)) {
// Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
this[enchantment] = level
- if(!player.hasPermission(UnsafeEnchants.bypassFusePermission) &&
- (UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
+ if(!player.hasPermission(CustomAnvil.bypassFusePermission) &&
+ (CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
this.remove(enchantment)
}
@@ -42,8 +42,8 @@ object EnchantmentUtil {
// Enchantment already in result list
else{
// ... and they are conflicting
- if((UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
- && !player.hasPermission(UnsafeEnchants.bypassFusePermission)){
+ if((CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
+ && !player.hasPermission(CustomAnvil.bypassFusePermission)){
return@forEach
}
@@ -58,7 +58,7 @@ object EnchantmentUtil {
// try to increase the enchantment level by 1
var newLevel = this[enchantment]!! +1
// Get max level or 255 if player can bypass
- val maxLevel = if(player.hasPermission(UnsafeEnchants.bypassLevelPermission)){
+ val maxLevel = if(player.hasPermission(CustomAnvil.bypassLevelPermission)){
255
}else{
ConfigOptions.enchantLimit(enchantment)
diff --git a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
index 3e8763b..f438fae 100644
--- a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt
@@ -1,6 +1,6 @@
package io.delilaheve.util
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.Material.ENCHANTED_BOOK
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
@@ -59,7 +59,7 @@ object ItemUtil {
}
enchantments.forEach { (enchant, level) ->
val added = addStoredEnchant(enchant, level, true)
- UnsafeEnchants.log("${enchant.key} added to item? $added")
+ CustomAnvil.log("${enchant.key} added to item? $added")
}
}
diff --git a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
index c086ee6..2d9e83f 100644
--- a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
@@ -1,13 +1,13 @@
package xyz.alexcrea.command
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
class ReloadExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean {
- if(!sender.hasPermission(UnsafeEnchants.commandReloadPermission)) {
+ if(!sender.hasPermission(CustomAnvil.commandReloadPermission)) {
sender.sendMessage("§cYou do not have permission to reload the config")
return false
}
@@ -26,9 +26,10 @@ class ReloadExecutor : CommandExecutor {
*/
private fun commandBody(): Boolean{
try {
- UnsafeEnchants.instance.reloadAllConfigs()
+ CustomAnvil.instance.reloadAllConfigs()
return true
- }catch (ignored: Exception){
+ }catch (e: Exception){
+ e.printStackTrace()
return false
}
}
diff --git a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt
index b285989..0b2a271 100644
--- a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt
@@ -1,6 +1,6 @@
package xyz.alexcrea.group
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
@@ -69,7 +69,7 @@ class EnchantConflictManager {
val enchant = Enchantment.getByKey(enchantKey)
if(enchant == null){
if(!futureUse){
- UnsafeEnchants.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName")
+ CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName")
}
continue
}
@@ -77,7 +77,7 @@ class EnchantConflictManager {
}
if(conflict.getEnchants().size == 0){
if(!futureUse){
- UnsafeEnchants.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not work")
+ CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not work")
}
return null
}
@@ -92,8 +92,8 @@ class EnchantConflictManager {
var minBeforeBlock = section.getInt(ENCH_MAX_PATH,0)
if(minBeforeBlock < 0){
minBeforeBlock = 0
- UnsafeEnchants.instance.logger.warning("Conflict $conflictName have an invalid value of $ENCH_MAX_PATH")
- UnsafeEnchants.instance.logger.warning("It should be more or equal to 0. default to 0")
+ CustomAnvil.instance.logger.warning("Conflict $conflictName have an invalid value of $ENCH_MAX_PATH")
+ CustomAnvil.instance.logger.warning("It should be more or equal to 0. default to 0")
}
// Find or create the selected group for the conflict
val groupList = section.getStringList(CONFLICT_GROUP_PATH)
@@ -115,7 +115,7 @@ class EnchantConflictManager {
private fun findGroup(groupName: String,itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup {
val group = itemManager.get(groupName)
if(group == null){
- UnsafeEnchants.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName")
+ CustomAnvil.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName")
return DEFAULT_EMPTY_GROUP
}
diff --git a/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt
index 7c02d47..dc67389 100644
--- a/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt
@@ -1,6 +1,6 @@
package xyz.alexcrea.group
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
@@ -48,7 +48,7 @@ class ItemGroupManager {
}else {
group = IncludeGroup(key)
if(!GroupType.INCLUDE.equal(groupType)){
- UnsafeEnchants.instance.logger.warning("Group $key have an invalid group type. default to Include.")
+ CustomAnvil.instance.logger.warning("Group $key have an invalid group type. default to Include.")
}
}
@@ -70,7 +70,7 @@ class ItemGroupManager {
if(material == null){
// Check if we should warn the user
if(materialName !in FUTURE_MATERIAL){
- UnsafeEnchants.instance.logger.warning(
+ CustomAnvil.instance.logger.warning(
"Unknown material $materialTemp on group ${group.getName()}")
}
@@ -84,7 +84,7 @@ class ItemGroupManager {
val groupList = groupSection.getStringList(GROUP_LIST_PATH)
for (groupName in groupList) {
if(groupName !in keys){
- UnsafeEnchants.instance.logger.warning(
+ CustomAnvil.instance.logger.warning(
"Group $groupName do not exist but is included in group ${group.getName()}")
continue
}
@@ -96,9 +96,9 @@ class ItemGroupManager {
}
// Avoid self reference or it will create an infinite loop
if(otherGroup.isReferencing(group)){
- UnsafeEnchants.instance.logger.warning(
+ CustomAnvil.instance.logger.warning(
"Group $groupName is on a reference loop with group ${group.getName()} !")
- UnsafeEnchants.instance.logger.warning(
+ CustomAnvil.instance.logger.warning(
"Please fix it in your item_groups config or the plugin will probably not work as expected.")
continue
}
diff --git a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
index 17f8bac..7a9338b 100644
--- a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
@@ -1,6 +1,6 @@
package xyz.alexcrea.util
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import org.bukkit.configuration.ConfigurationSection
@@ -85,12 +85,12 @@ object MetricsUtil {
isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
// If not default and debug flag active, print the hash.
if(ConfigOptions.debugLog){
- if(!isDefaultBaseConfig){UnsafeEnchants.log("baseConfig: $baseConfig")}
- if(!isDefaultEnchantLimitsConfig){UnsafeEnchants.log("limitEnchantConfig: $limitEnchantConfig")}
- if(!isDefaultEnchantValuesConfig){UnsafeEnchants.log("enchantValueConfig: $enchantValueConfig")}
- if(!isDefaultEnchantConflictConfig){UnsafeEnchants.log("enchantConflictConfig: $enchantConflictConfig")}
- if(!isDefaultItemGroupsConfig){UnsafeEnchants.log("itemGroupConfig: $itemGroupConfig")}
- if(!isDefaultUnitRepairItemConfig){UnsafeEnchants.log("unitRepairConfig: $unitRepairConfig")}
+ if(!isDefaultBaseConfig){CustomAnvil.log("baseConfig: $baseConfig")}
+ if(!isDefaultEnchantLimitsConfig){CustomAnvil.log("limitEnchantConfig: $limitEnchantConfig")}
+ if(!isDefaultEnchantValuesConfig){CustomAnvil.log("enchantValueConfig: $enchantValueConfig")}
+ if(!isDefaultEnchantConflictConfig){CustomAnvil.log("enchantConflictConfig: $enchantConflictConfig")}
+ if(!isDefaultItemGroupsConfig){CustomAnvil.log("itemGroupConfig: $itemGroupConfig")}
+ if(!isDefaultUnitRepairItemConfig){CustomAnvil.log("unitRepairConfig: $unitRepairConfig")}
}
}
diff --git a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
index 7f1df1d..19b531d 100644
--- a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
@@ -1,6 +1,6 @@
package xyz.alexcrea.util
-import io.delilaheve.UnsafeEnchants
+import io.delilaheve.CustomAnvil
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.ItemStack
@@ -19,7 +19,7 @@ object UnitRepairUtil {
other: ItemStack?
): Double? {
if(other == null) return null
- val config = UnsafeEnchants.unitRepairConfig
+ val config = CustomAnvil.unitRepairConfig
// Get configuration section if exist
val otherName = other.type.name.uppercase()
var section = config.getConfigurationSection(otherName)
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 38151f4..0185c7b 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,27 +1,36 @@
-main: io.delilaheve.UnsafeEnchants
-name: UnsafeEnchantsPlus
-prefix: UnsafeEnchants+
+main: io.delilaheve.CustomAnvil
+name: CustomAnvil
+prefix: "Custom Anvil"
version: 1.2.0
-description: Allow custom illegal enchantment
+description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
authors: [DelilahEve, alexcrea]
libraries:
- org.jetbrains.kotlin:kotlin-stdlib:1.6.21
commands:
- reloadunsafeenchants:
+ anvilconfigreload:
description: Reload every config of this plugin
- permission: ue.command.reload
+ permission: ca.command.reload
+ aliases:
+ #- acreload # anvil config reload
+ #- careload # custom anvil reload
+ - carl # custom anvil reload
permissions:
- ue.unsafe:
+ ca.affected:
default: true
- description: Allow player to combine allowed "unsafe" enchants
- ue.bypass.fuse:
+ description: Player with this permission will be affected by the plugin
+ ca.bypass.fuse:
default: false
- description: Allow player to combine every "unsafe" enchants
- ue.bypass.level:
+ description: Allow player to combine every enchantments to every item (no custom limit)
+ ca.bypass.level:
default: false
- description: Allow player to bypass max level limit
- ue.command.reload:
+ description: Allow player to bypass every level limit (no custom limit)
+ ca.command.reload:
default: op
- description: Allow administrator to reload the plugin's config
\ No newline at end of file
+ description: Allow administrator to reload the plugin's configs
+
+# soft depend on old name so I can disable it if it is on the same server
+# as it is the old name for this plugin
+softdepend:
+ - UnsafeEnchantsPlus
\ No newline at end of file
From 1c802bc1c0c43f678d774735813d19c88862ba91 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Wed, 14 Feb 2024 01:56:37 +0100
Subject: [PATCH 011/691] fix issue about xp and item display
---
.../io/delilaheve/AnvilEventListener.kt | 48 +++++++++++--------
1 file changed, 29 insertions(+), 19 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index 7efde5f..eec1a10 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -60,16 +60,18 @@ class AnvilEventListener : Listener {
if(second == null){
val resultItem = first.clone()
var anvilCost = handleRename(resultItem, inventory)
- anvilCost+= calculatePenalty(first,null,resultItem)
// Test/stop if nothing changed.
if(first == resultItem){
event.result = null
return
}
- // We do set item here as vanilla do all of our job (renaming)
+ // We don't manually set item here as vanilla do it (renaming)
+ //event.result = null
- handleDisplayedXp(inventory, event, anvilCost)
+ anvilCost+= calculatePenalty(first,null,resultItem)
+
+ handleAnvilXp(inventory, event, anvilCost)
return
}
@@ -81,8 +83,9 @@ class AnvilEventListener : Listener {
val resultItem = first.clone()
resultItem.setEnchantmentsUnsafe(newEnchants)
- var anvilCost = calculatePenalty(first, second, resultItem)
- anvilCost+= getRightValues(second, resultItem)
+ // Calculate enchantment cost
+ var anvilCost = getRightValues(second, resultItem)
+ // Calculate repair cost
if (!first.isEnchantedBook() && !second.isEnchantedBook()) {
// we only need to be concerned with repair when neither item is a book
val repaired = resultItem.repairFrom(first, second)
@@ -94,15 +97,15 @@ class AnvilEventListener : Listener {
event.result = null
return
}
-
+ // As calculatePenalty edit result, we need to calculate penalty after checking equality
+ anvilCost+= calculatePenalty(first, second, resultItem)
+ // Calculate rename cost
anvilCost+= handleRename(resultItem, inventory)
- if (ConfigOptions.limitRepairCost) {
- anvilCost = min(anvilCost, ConfigOptions.limitRepairValue)
- }
+ // Finally, we set result
event.result = resultItem
- handleDisplayedXp(inventory, event, anvilCost)
+ handleAnvilXp(inventory, event, anvilCost)
return
}
@@ -111,13 +114,13 @@ class AnvilEventListener : Listener {
if(unitRepairAmount != null){
val resultItem = first.clone()
var anvilCost = handleRename(resultItem, inventory)
- // We do not care about right item penalty for unit repair
- anvilCost+= calculatePenalty(first,null,resultItem)
val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount)
if(repairAmount > 0){
anvilCost += repairAmount*ConfigOptions.unitRepairCost
}
+ // We do not care about right item penalty for unit repair
+ anvilCost+= calculatePenalty(first,null,resultItem)
// Test/stop if nothing changed.
if(first == resultItem){
@@ -126,7 +129,7 @@ class AnvilEventListener : Listener {
}
event.result = resultItem
- handleDisplayedXp(inventory, event, anvilCost)
+ handleAnvilXp(inventory, event, anvilCost)
}else{
event.result = null
}
@@ -162,6 +165,7 @@ class AnvilEventListener : Listener {
val allowed = (rightItem == null)
|| (canMerge)
|| (unitRepairResult != null)
+
// True if there was no change or not allowed
if((output == inventory.getItem(ANVIL_INPUT_LEFT))
|| !allowed){
@@ -343,11 +347,17 @@ class AnvilEventListener : Listener {
/**
* Display xp needed for the work on the anvil inventory
*/
- private fun handleDisplayedXp(inventory: AnvilInventory,
- event: PrepareAnvilEvent,
- anvilCost: Int){
- inventory.maximumRepairCost = Int.MAX_VALUE
- inventory.repairCost = anvilCost
+ private fun handleAnvilXp(inventory: AnvilInventory,
+ event: PrepareAnvilEvent,
+ anvilCost: Int){
+ // Test repair cost limit
+ val finalAnvilCost: Int
+ if (ConfigOptions.limitRepairCost) {
+ finalAnvilCost = min(anvilCost, ConfigOptions.limitRepairValue)
+ }else{
+ finalAnvilCost = anvilCost
+ }
+
/* Because Minecraft likes to have the final say in the repair cost displayed
* we need to wait for the event to end before overriding it, this ensures that
* we have the final say in the process. */
@@ -358,7 +368,7 @@ class AnvilEventListener : Listener {
if (ConfigOptions.removeRepairLimit) {
inventory.maximumRepairCost = Int.MAX_VALUE
}
- inventory.repairCost = anvilCost
+ inventory.repairCost = finalAnvilCost
event.view.setProperty(REPAIR_COST, anvilCost)
})
From db1bfd957e63425249ea718a00233bc152a23910 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Wed, 14 Feb 2024 01:36:31 +0000
Subject: [PATCH 012/691] Update README.md
---
README.md | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 3d38489..47941b8 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Custom Anvil
-**Custom Anvil** is a plugin to allow admin to customise evey aspect of the anvil's mechanics.
+**Custom Anvil** is a plugin that allows server administrators to customise every aspect of the anvil's mechanics.
It is expected to work on 1.18 to 1.20.4 minecraft servers running spigot or paper.
**Custom Anvil** was previously named **Unsafe Enchants+**.
@@ -13,8 +13,8 @@ It was renamed because it now affects every anvil aspect and not only unsafe enc
### Download Locations:
the plugin can be downloaded on the
-[Spigot site](https://www.spigotmc.org/resources/unsafe-enchants.114884/)
-or [on GitHub](https://github.com/alexcrea/UnsafeEnchantsPlus/releases/latest)
+[Spigot site](https://www.spigotmc.org/resources/custom-anvil.114884)
+or [on GitHub](https://github.com/alexcrea/CustomAnvil/releases/latest)
---
**Custom Anvil** have the following features:
- Vanilla like default configuration
@@ -39,10 +39,10 @@ anvilconfigreload or carl: Reload every config of this plugin
### Default Configuration:
Default configuration can be found on following links:
-- [config.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/config.yml)
-- [enchant_conflict.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/enchant_conflict.yml)
-- [item_groups.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/item_groups.yml)
-- [unit_repair_item.yml](https://github.com/alexcrea/UnsafeEnchantsPlus/blob/master/src/main/resources/unit_repair_item.yml)
+- [config.yml](https://github.com/alexcrea/CustomAnvil/blob/master/src/main/resources/config.yml)
+- [enchant_conflict.yml](https://github.com/alexcrea/CustomAnvil/blob/master/src/main/resources/enchant_conflict.yml)
+- [item_groups.yml](https://github.com/alexcrea/CustomAnvil/blob/master/src/main/resources/item_groups.yml)
+- [unit_repair_item.yml](https://github.com/alexcrea/CustomAnvil/blob/master/src/main/resources/unit_repair_item.yml)
---
### Know issue:
There is non known issue, if you find one please report the issue.
From 729b3535bd7399e2b088f32c45eaae2088764526 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Thu, 15 Feb 2024 16:09:19 +0100
Subject: [PATCH 013/691] fix hard fail on reload command
---
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 15 +++++++++------
.../kotlin/xyz/alexcrea/command/ReloadExecutor.kt | 11 +++++++----
2 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 42e7286..f554075 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -73,7 +73,9 @@ class CustomAnvil : JavaPlugin() {
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
}
- reloadAllConfigs()
+ val success = reloadAllConfigs(true)
+ if(!success) return
+
// Load metrics
val metric = Metrics(this, bstatsPluginId)
MetricsUtil.addCustomMetric(metric)
@@ -88,30 +90,31 @@ class CustomAnvil : JavaPlugin() {
)
}
- fun reloadAllConfigs(){
+ fun reloadAllConfigs(hardFailSafe: Boolean): Boolean{
saveDefaultConfig()
// Load material grouping config
- val itemGroupConfig = reloadResource(itemGroupingConfigFilePath) ?: return
+ val itemGroupConfig = reloadResource(itemGroupingConfigFilePath, hardFailSafe) ?: return false
// Read material groups from config
val itemGroupsManager = ItemGroupManager()
itemGroupsManager.prepareGroups(itemGroupConfig)
// Load enchantment conflicts config
- val conflictConfig = reloadResource(enchantConflicConfigFilePath) ?: return
+ val conflictConfig = reloadResource(enchantConflicConfigFilePath, hardFailSafe) ?: return false
// Read conflicts from config and material group manager
val conflictManager = EnchantConflictManager()
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
// Load unit repair config
- val unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
+ val unitRepairConfig = reloadResource(unitRepairFilePath, hardFailSafe) ?: return false
// Set the global variable
CustomAnvil.conflictManager = conflictManager
CustomAnvil.unitRepairConfig = unitRepairConfig
- // Test if default config
+ // Test if is default config
MetricsUtil.testIfConfigIsDefault(config, itemGroupConfig, conflictConfig, unitRepairConfig)
+ return true
}
private fun reloadResource(resourceName: String,
diff --git a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
index 2d9e83f..4893343 100644
--- a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
@@ -12,11 +12,15 @@ class ReloadExecutor : CommandExecutor {
return false
}
sender.sendMessage("§eReloading config...")
- val commandSuccess = commandBody()
+ val hardfail = args.isNotEmpty() && ("hard".equals(args[0],true))
+ val commandSuccess = commandBody(hardfail)
if(commandSuccess){
sender.sendMessage("§aConfig reloaded !")
}else{
sender.sendMessage("§cConfig was not able to be reloaded...")
+ if(hardfail){
+ sender.sendMessage("§4Hard fail, plugin disabled")
+ }
}
return commandSuccess
}
@@ -24,10 +28,9 @@ class ReloadExecutor : CommandExecutor {
/**
* Execute the command, return true if success or false otherwise
*/
- private fun commandBody(): Boolean{
+ private fun commandBody(hardfail: Boolean): Boolean{
try {
- CustomAnvil.instance.reloadAllConfigs()
- return true
+ return CustomAnvil.instance.reloadAllConfigs(hardfail)
}catch (e: Exception){
e.printStackTrace()
return false
From dda152280f11795d34e65eca270918202ff9d6a6 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sat, 17 Feb 2024 14:53:07 +0100
Subject: [PATCH 014/691] fix reload of config.yml & version up
---
build.gradle.kts | 2 +-
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 1 +
src/main/resources/plugin.yml | 2 +-
3 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 3a91a58..d929515 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.2.0"
+version = "1.2.1"
repositories {
mavenCentral()
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index f554075..1b80747 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -92,6 +92,7 @@ class CustomAnvil : JavaPlugin() {
fun reloadAllConfigs(hardFailSafe: Boolean): Boolean{
saveDefaultConfig()
+ reloadConfig()
// Load material grouping config
val itemGroupConfig = reloadResource(itemGroupingConfigFilePath, hardFailSafe) ?: return false
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 0185c7b..8a591ae 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
-version: 1.2.0
+version: 1.2.1
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
From a44315b235d61110241388bea9a298453ca91c1e Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 18:43:56 +0100
Subject: [PATCH 015/691] add gui library
---
build.gradle.kts | 6 +++++-
src/main/resources/plugin.yml | 2 ++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index d929515..6780b90 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -17,6 +17,10 @@ dependencies {
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")
+ // Gui library
+ compileOnly("com.github.stefvanschie.inventoryframework:IF:0.10.13")
+
+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
@@ -30,7 +34,7 @@ tasks.getByName("test") {
// Fat-jar builder
val fatJar = tasks.register("fatJar") {
manifest {
- attributes.apply { put("Main-Class", "io.delilaheve.UnsafeEnchants") }
+ attributes.apply { put("Main-Class", "io.delilaheve.CustomAnvil") }
}
archiveFileName.set("${rootProject.name}-${archiveVersion}.jar")
exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 8a591ae..639910e 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -8,6 +8,8 @@ load: POSTWORLD
authors: [DelilahEve, alexcrea]
libraries:
- org.jetbrains.kotlin:kotlin-stdlib:1.6.21
+ - com.github.stefvanschie.inventoryframework:IF:0.10.13
+
commands:
anvilconfigreload:
description: Reload every config of this plugin
From ae3971d14a71fd4d9ea28ab1696345e9fc5bd654 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 18:52:42 +0100
Subject: [PATCH 016/691] edit metric package
---
.../alexcrea => java/xyz/alexcrea/cuanvil}/util/Metrics.java | 2 +-
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 2 +-
src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt | 1 +
3 files changed, 3 insertions(+), 2 deletions(-)
rename src/main/{kotlin/xyz/alexcrea => java/xyz/alexcrea/cuanvil}/util/Metrics.java (99%)
diff --git a/src/main/kotlin/xyz/alexcrea/util/Metrics.java b/src/main/java/xyz/alexcrea/cuanvil/util/Metrics.java
similarity index 99%
rename from src/main/kotlin/xyz/alexcrea/util/Metrics.java
rename to src/main/java/xyz/alexcrea/cuanvil/util/Metrics.java
index f8a1a9b..fe1ffb7 100644
--- a/src/main/kotlin/xyz/alexcrea/util/Metrics.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/util/Metrics.java
@@ -12,7 +12,7 @@
*
* Violations will result in a ban of your plugin and account from bStats.
*/
-package xyz.alexcrea.util;
+package xyz.alexcrea.cuanvil.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 1b80747..8535ae7 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -7,7 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.command.ReloadExecutor
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
-import xyz.alexcrea.util.Metrics
+import xyz.alexcrea.cuanvil.util.Metrics
import xyz.alexcrea.util.MetricsUtil
import java.io.File
import java.io.FileReader
diff --git a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
index 7a9338b..e6db164 100644
--- a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
@@ -3,6 +3,7 @@ package xyz.alexcrea.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import org.bukkit.configuration.ConfigurationSection
+import xyz.alexcrea.cuanvil.util.Metrics
object MetricsUtil {
From 6259eaa2cdf1a3c20f7e0525578c5cf0ad70769c Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 18:56:24 +0100
Subject: [PATCH 017/691] start of the guis
---
.../cuanvil/gui/gui/GuiGlobalActions.java | 45 ++++++++++++++++
.../cuanvil/gui/gui/GuiGlobalItems.java | 34 ++++++++++++
.../cuanvil/gui/gui/MainConfigGui.java | 53 +++++++++++++++++++
.../gui/gui/config/BasicConfigGui.java | 29 ++++++++++
4 files changed, 161 insertions(+)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java
new file mode 100644
index 0000000..8edc3cc
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java
@@ -0,0 +1,45 @@
+package xyz.alexcrea.cuanvil.gui.gui;
+
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+import org.bukkit.event.inventory.InventoryClickEvent;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.function.Consumer;
+
+public class GuiGlobalActions {
+
+ public static Consumer stayInPlace = (event) -> event.setCancelled(true);
+
+
+ public static Consumer openGuiFactory(
+ Class extends Gui> clazz,
+ Class>[] argClass,
+ Object... args){
+ return event -> {
+ event.setCancelled(true);
+ try {
+ Constructor extends Gui> constructor = clazz.getConstructor(argClass);
+ // Assume constructor is accessible
+ Gui gui = constructor.newInstance(args);
+ gui.show(event.getWhoClicked());
+
+ } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException |
+ InstantiationException e) {
+ throw new RuntimeException(e);
+ }
+ };
+ }
+
+ public static Consumer openGuiFactory(
+ Class extends Gui> clazz){
+ return openGuiFactory(clazz, new Class>[0]);
+ }
+
+ public static Consumer openGuiFactory(Gui goal) {
+ return event -> {
+ event.setCancelled(true);
+ goal.show(event.getWhoClicked());
+ };
+ }
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
new file mode 100644
index 0000000..6060c70
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
@@ -0,0 +1,34 @@
+package xyz.alexcrea.cuanvil.gui.gui;
+
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+import com.github.stefvanschie.inventoryframework.pane.PatternPane;
+import io.delilaheve.CustomAnvil;
+import org.bukkit.Material;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
+
+public class GuiGlobalItems {
+
+ // return
+ public static GuiItem toGuiItem(ItemStack item, Gui goal){
+ return new GuiItem(item, GuiGlobalActions.openGuiFactory(goal), CustomAnvil.instance);
+ }
+
+ // statically create back itemstack
+ private static final ItemStack BACK_ITEM = new ItemStack(Material.BARRIER);
+ static {
+ // todo add what I need to add to the back item
+ ItemMeta meta = BACK_ITEM.getItemMeta();
+ meta.setDisplayName("§cBack");
+ BACK_ITEM.setItemMeta(meta);
+ }
+ public static GuiItem backItem(Gui goal){
+ // simple go back item
+ return toGuiItem(BACK_ITEM, goal);
+ }
+ public static void addBackItem(PatternPane target, Gui goal){
+ target.bindItem('B', backItem(goal));
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java
new file mode 100644
index 0000000..33359b8
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java
@@ -0,0 +1,53 @@
+package xyz.alexcrea.cuanvil.gui.gui;
+
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
+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.inventory.ItemStack;
+import xyz.alexcrea.cuanvil.gui.gui.config.BasicConfigGui;
+
+public class MainConfigGui extends ChestGui {
+
+ public final static MainConfigGui INSTANCE = new MainConfigGui();
+
+ private MainConfigGui() {
+ super(3, "§8Anvil Config", CustomAnvil.instance);
+
+ Pattern pattern = new Pattern(
+ "111111111",
+ "112345611",
+ "111111111"
+ );
+ PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
+ addPane(pane);
+
+ ItemStack backgroundBukkit = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
+
+ GuiItem background = new GuiItem(backgroundBukkit,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+
+
+ pane.bindItem('1', background);
+
+
+ ItemStack stonePlaceholder = new ItemStack(Material.STONE);
+ GuiItem placeholder1 = GuiGlobalItems.toGuiItem(stonePlaceholder, BasicConfigGui.INSTANCE);
+ GuiItem placeholder2 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
+ GuiItem placeholder3 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
+ GuiItem placeholder4 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
+ GuiItem placeholder5 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
+
+
+ pane.bindItem('2', placeholder1);
+ pane.bindItem('3', placeholder2);
+ pane.bindItem('4', placeholder3);
+ pane.bindItem('5', placeholder4);
+ pane.bindItem('6', placeholder5);
+
+ }
+
+
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java
new file mode 100644
index 0000000..c9e6353
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java
@@ -0,0 +1,29 @@
+package xyz.alexcrea.cuanvil.gui.gui.config;
+
+import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
+import com.github.stefvanschie.inventoryframework.pane.PatternPane;
+import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
+import io.delilaheve.CustomAnvil;
+import xyz.alexcrea.cuanvil.gui.gui.GuiGlobalItems;
+import xyz.alexcrea.cuanvil.gui.gui.MainConfigGui;
+
+public class BasicConfigGui extends ChestGui {
+
+ public final static BasicConfigGui INSTANCE = new BasicConfigGui();
+
+ private BasicConfigGui(){
+ super(3, "Basic Config GUI", CustomAnvil.instance);
+
+ Pattern pattern = new Pattern(
+ "111111111",
+ "111111111",
+ "B11111111"
+ );
+ PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
+ addPane(pane);
+
+ GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
+
+ }
+
+}
From 379eb12940fe0f4d853a4ac6e14f7624cd53d013 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 18:56:37 +0100
Subject: [PATCH 018/691] add test command
---
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 18 ++++++++++++++----
.../xyz/alexcrea/command/TestExecutor.kt | 19 +++++++++++++++++++
src/main/resources/plugin.yml | 3 +++
3 files changed, 36 insertions(+), 4 deletions(-)
create mode 100644 src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 8535ae7..73f34f2 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -5,9 +5,10 @@ import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.command.ReloadExecutor
+import xyz.alexcrea.command.TestExecutor
+import xyz.alexcrea.cuanvil.util.Metrics
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
-import xyz.alexcrea.cuanvil.util.Metrics
import xyz.alexcrea.util.MetricsUtil
import java.io.File
import java.io.FileReader
@@ -33,6 +34,8 @@ class CustomAnvil : JavaPlugin() {
// Command Name to reload the config
const val commandReloadName = "anvilconfigreload"
+ // Test command name
+ const val commandTestName = "test"
// Item Grouping Configuration file name
const val itemGroupingConfigFilePath = "item_groups.yml"
@@ -80,9 +83,8 @@ class CustomAnvil : JavaPlugin() {
val metric = Metrics(this, bstatsPluginId)
MetricsUtil.addCustomMetric(metric)
- // Add command to reload the plugin
- val command = getCommand(commandReloadName)
- command?.setExecutor(ReloadExecutor())
+ // Add commands to reload the plugin
+ prepareCommand()
server.pluginManager.registerEvents(
AnvilEventListener(),
@@ -145,4 +147,12 @@ class CustomAnvil : JavaPlugin() {
return yamlConfig
}
+ fun prepareCommand(): Unit {
+ var command = getCommand(commandReloadName)
+ command?.setExecutor(ReloadExecutor())
+
+ command = getCommand(commandTestName)
+ command?.setExecutor(TestExecutor())
+ }
+
}
diff --git a/src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt b/src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt
new file mode 100644
index 0000000..aa80055
--- /dev/null
+++ b/src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt
@@ -0,0 +1,19 @@
+package xyz.alexcrea.command
+
+import org.bukkit.command.Command
+import org.bukkit.command.CommandExecutor
+import org.bukkit.command.CommandSender
+import org.bukkit.entity.HumanEntity
+import xyz.alexcrea.cuanvil.gui.gui.MainConfigGui
+
+class TestExecutor : CommandExecutor {
+
+ override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean {
+
+ if(sender !is HumanEntity) return false
+ MainConfigGui.INSTANCE.show(sender)
+
+ return true
+ }
+
+}
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 639910e..ccce05b 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -18,6 +18,9 @@ commands:
#- acreload # anvil config reload
#- careload # custom anvil reload
- carl # custom anvil reload
+ test:
+ description: test cmd
+
permissions:
ca.affected:
default: true
From 60ef3781a6c04be90424ec88b1b88b5d1e2f1dad Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 19:08:03 +0100
Subject: [PATCH 019/691] moved pakage xyz.alexcrea to xyz.alexcrea.cuanvil
---
src/main/kotlin/io/delilaheve/AnvilEventListener.kt | 4 ++--
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 12 ++++++------
.../kotlin/io/delilaheve/util/EnchantmentUtil.kt | 2 +-
.../alexcrea/{ => cuanvil}/command/ReloadExecutor.kt | 2 +-
.../alexcrea/{ => cuanvil}/command/TestExecutor.kt | 2 +-
.../{ => cuanvil}/group/AbstractMaterialGroup.kt | 2 +-
.../{ => cuanvil}/group/EnchantConflictGroup.kt | 2 +-
.../{ => cuanvil}/group/EnchantConflictManager.kt | 10 +++++-----
.../xyz/alexcrea/{ => cuanvil}/group/ExcludeGroup.kt | 2 +-
.../xyz/alexcrea/{ => cuanvil}/group/IncludeGroup.kt | 2 +-
.../alexcrea/{ => cuanvil}/group/ItemGroupManager.kt | 6 +++---
.../xyz/alexcrea/{ => cuanvil}/util/MetricsUtil.kt | 5 ++---
.../alexcrea/{ => cuanvil}/util/UnitRepairUtil.kt | 4 ++--
13 files changed, 27 insertions(+), 28 deletions(-)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/command/ReloadExecutor.kt (97%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/command/TestExecutor.kt (92%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/AbstractMaterialGroup.kt (96%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/EnchantConflictGroup.kt (96%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/EnchantConflictManager.kt (94%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/ExcludeGroup.kt (95%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/IncludeGroup.kt (95%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/group/ItemGroupManager.kt (96%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/util/MetricsUtil.kt (97%)
rename src/main/kotlin/xyz/alexcrea/{ => cuanvil}/util/UnitRepairUtil.kt (96%)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index eec1a10..110a651 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -23,8 +23,8 @@ import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable
-import xyz.alexcrea.group.ConflictType
-import xyz.alexcrea.util.UnitRepairUtil.getRepair
+import xyz.alexcrea.cuanvil.group.ConflictType
+import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import kotlin.math.min
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 73f34f2..1fee6ea 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -4,12 +4,12 @@ import io.delilaheve.util.ConfigOptions
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
-import xyz.alexcrea.command.ReloadExecutor
-import xyz.alexcrea.command.TestExecutor
+import xyz.alexcrea.cuanvil.command.ReloadExecutor
+import xyz.alexcrea.cuanvil.command.TestExecutor
import xyz.alexcrea.cuanvil.util.Metrics
-import xyz.alexcrea.group.EnchantConflictManager
-import xyz.alexcrea.group.ItemGroupManager
-import xyz.alexcrea.util.MetricsUtil
+import xyz.alexcrea.cuanvil.group.EnchantConflictManager
+import xyz.alexcrea.cuanvil.group.ItemGroupManager
+import xyz.alexcrea.cuanvil.util.MetricsUtil
import java.io.File
import java.io.FileReader
@@ -147,7 +147,7 @@ class CustomAnvil : JavaPlugin() {
return yamlConfig
}
- fun prepareCommand(): Unit {
+ fun prepareCommand() {
var command = getCommand(commandReloadName)
command?.setExecutor(ReloadExecutor())
diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
index 07874d9..ff61cbb 100644
--- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
@@ -4,7 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.HumanEntity
-import xyz.alexcrea.group.ConflictType
+import xyz.alexcrea.cuanvil.group.ConflictType
import kotlin.math.max
import kotlin.math.min
diff --git a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
similarity index 97%
rename from src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index 4893343..b92b569 100644
--- a/src/main/kotlin/xyz/alexcrea/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.command
+package xyz.alexcrea.cuanvil.command
import io.delilaheve.CustomAnvil
import org.bukkit.command.Command
diff --git a/src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
similarity index 92%
rename from src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
index aa80055..9140edc 100644
--- a/src/main/kotlin/xyz/alexcrea/command/TestExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.command
+package xyz.alexcrea.cuanvil.command
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
diff --git a/src/main/kotlin/xyz/alexcrea/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
similarity index 96%
rename from src/main/kotlin/xyz/alexcrea/group/AbstractMaterialGroup.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
index b253da9..fbc007a 100644
--- a/src/main/kotlin/xyz/alexcrea/group/AbstractMaterialGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import org.bukkit.Material
import java.util.EnumSet
diff --git a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt
similarity index 96%
rename from src/main/kotlin/xyz/alexcrea/group/EnchantConflictGroup.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt
index adef2fe..08b1e02 100644
--- a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
diff --git a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
similarity index 94%
rename from src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
index 0b2a271..add081f 100644
--- a/src/main/kotlin/xyz/alexcrea/group/EnchantConflictManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import io.delilaheve.CustomAnvil
import org.bukkit.Material
@@ -86,8 +86,8 @@ class EnchantConflictManager {
}
private fun createConflictObject(section: ConfigurationSection,
- itemManager: ItemGroupManager,
- conflictName: String): EnchantConflictGroup {
+ itemManager: ItemGroupManager,
+ conflictName: String): EnchantConflictGroup {
// Get the maximum number of enchantment before validating the conflict
var minBeforeBlock = section.getInt(ENCH_MAX_PATH,0)
if(minBeforeBlock < 0){
@@ -112,7 +112,7 @@ class EnchantConflictManager {
return EnchantConflictGroup(finalGroup, minBeforeBlock)
}
- private fun findGroup(groupName: String,itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup {
+ private fun findGroup(groupName: String, itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup {
val group = itemManager.get(groupName)
if(group == null){
CustomAnvil.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName")
@@ -122,7 +122,7 @@ class EnchantConflictManager {
return group
}
- fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): ConflictType{
+ fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): ConflictType {
val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT
var result = ConflictType.NO_CONFLICT
diff --git a/src/main/kotlin/xyz/alexcrea/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
similarity index 95%
rename from src/main/kotlin/xyz/alexcrea/group/ExcludeGroup.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
index e929346..fe70cb6 100644
--- a/src/main/kotlin/xyz/alexcrea/group/ExcludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import org.bukkit.Material
import java.util.*
diff --git a/src/main/kotlin/xyz/alexcrea/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
similarity index 95%
rename from src/main/kotlin/xyz/alexcrea/group/IncludeGroup.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
index d349821..8f97b94 100644
--- a/src/main/kotlin/xyz/alexcrea/group/IncludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import org.bukkit.Material
import java.util.EnumSet
diff --git a/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
similarity index 96%
rename from src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
index dc67389..f157870 100644
--- a/src/main/kotlin/xyz/alexcrea/group/ItemGroupManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.group
+package xyz.alexcrea.cuanvil.group
import io.delilaheve.CustomAnvil
import org.bukkit.Material
@@ -20,7 +20,7 @@ class ItemGroupManager {
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
}
- private lateinit var groupMap : HashMap
+ private lateinit var groupMap : HashMap
// Read and create material groups
fun prepareGroups(config: YamlConfiguration){
@@ -37,7 +37,7 @@ class ItemGroupManager {
// Create group by key
private fun createGroup(config: YamlConfiguration,
keys: Set,
- key: String): AbstractMaterialGroup{
+ key: String): AbstractMaterialGroup {
val groupSection = config.getConfigurationSection(key)!!
val groupType = groupSection.getString(GROUP_TYPE_PATH,null)
diff --git a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
similarity index 97%
rename from src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
index e6db164..7e56db2 100644
--- a/src/main/kotlin/xyz/alexcrea/util/MetricsUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
@@ -1,9 +1,8 @@
-package xyz.alexcrea.util
+package xyz.alexcrea.cuanvil.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import org.bukkit.configuration.ConfigurationSection
-import xyz.alexcrea.cuanvil.util.Metrics
object MetricsUtil {
@@ -73,7 +72,7 @@ object MetricsUtil {
// Calculate hash of config
val baseConfig = testBaseConfig(defaultConfig)
val limitEnchantConfig = getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_LIMIT_ROOT)
- val enchantValueConfig =getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_VALUES_ROOT)
+ val enchantValueConfig = getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_VALUES_ROOT)
val enchantConflictConfig2 = getConfigurationHash(enchantConflictConfig)
val itemGroupConfig = getConfigurationHash(itemGroupsConfig)
val unitRepairConfig = getConfigurationHash(unitRepairItemConfig)
diff --git a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
similarity index 96%
rename from src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
index 19b531d..b01d37f 100644
--- a/src/main/kotlin/xyz/alexcrea/util/UnitRepairUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
@@ -1,4 +1,4 @@
-package xyz.alexcrea.util
+package xyz.alexcrea.cuanvil.util
import io.delilaheve.CustomAnvil
import org.bukkit.configuration.ConfigurationSection
@@ -30,7 +30,7 @@ object UnitRepairUtil {
}
}
// Get repair amount
- var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH,DEFAULT_DEFAULT_UNIT_REPAIR)
+ var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if(userDefault <= 0){
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
}
From 7154748455551240a24ce4d3ed54994bb65e6e87 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 19:09:40 +0100
Subject: [PATCH 020/691] version up
---
build.gradle.kts | 2 +-
src/main/resources/plugin.yml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 6780b90..e0e1a4b 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.2.1"
+version = "1.3"
repositories {
mavenCentral()
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index ccce05b..773da9b 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
-version: 1.2.1
+version: 1.3
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
From 69dde2152906dc32ba6dc3e60bbfa94f2b53e391 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 27 Feb 2024 21:52:50 +0100
Subject: [PATCH 021/691] add utils and fix package
---
.../gui/{gui => }/GuiGlobalActions.java | 17 +++---
.../alexcrea/cuanvil/gui/GuiGlobalItems.java | 56 +++++++++++++++++++
.../cuanvil/gui/{gui => }/MainConfigGui.java | 25 +++++----
.../gui/{gui => }/config/BasicConfigGui.java | 24 +++++---
.../cuanvil/gui/gui/GuiGlobalItems.java | 34 -----------
.../alexcrea/cuanvil/command/TestExecutor.kt | 2 +-
6 files changed, 95 insertions(+), 63 deletions(-)
rename src/main/java/xyz/alexcrea/cuanvil/gui/{gui => }/GuiGlobalActions.java (69%)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
rename src/main/java/xyz/alexcrea/cuanvil/gui/{gui => }/MainConfigGui.java (74%)
rename src/main/java/xyz/alexcrea/cuanvil/gui/{gui => }/config/BasicConfigGui.java (59%)
delete mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
similarity index 69%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
index 8edc3cc..de1fb6a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
@@ -1,7 +1,8 @@
-package xyz.alexcrea.cuanvil.gui.gui;
+package xyz.alexcrea.cuanvil.gui;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import org.bukkit.event.inventory.InventoryClickEvent;
+import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@@ -12,10 +13,10 @@ public class GuiGlobalActions {
public static Consumer stayInPlace = (event) -> event.setCancelled(true);
- public static Consumer openGuiFactory(
- Class extends Gui> clazz,
- Class>[] argClass,
- Object... args){
+ public static @NotNull Consumer openGuiFactory(
+ @NotNull Class extends Gui> clazz,
+ @NotNull Class>[] argClass,
+ @NotNull Object... args){
return event -> {
event.setCancelled(true);
try {
@@ -31,12 +32,12 @@ public class GuiGlobalActions {
};
}
- public static Consumer openGuiFactory(
- Class extends Gui> clazz){
+ public static @NotNull Consumer openGuiFactory(
+ @NotNull Class extends Gui> clazz){
return openGuiFactory(clazz, new Class>[0]);
}
- public static Consumer openGuiFactory(Gui goal) {
+ public static @NotNull Consumer openGuiFactory(@NotNull Gui goal) {
return event -> {
event.setCancelled(true);
goal.show(event.getWhoClicked());
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
new file mode 100644
index 0000000..42ecaa3
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
@@ -0,0 +1,56 @@
+package xyz.alexcrea.cuanvil.gui;
+
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+import com.github.stefvanschie.inventoryframework.pane.PatternPane;
+import io.delilaheve.CustomAnvil;
+import org.bukkit.Material;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.jetbrains.annotations.NotNull;
+
+// maybe use builder patern ?
+public class GuiGlobalItems {
+
+ // return
+ public static GuiItem toGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
+ return new GuiItem(item, GuiGlobalActions.openGuiFactory(goal), CustomAnvil.instance);
+ }
+
+ // statically create back itemstack
+ private static final ItemStack BACK_ITEM = new ItemStack(Material.BARRIER);
+ static {
+ // todo add what I need to add to the back item
+ ItemMeta meta = BACK_ITEM.getItemMeta();
+ meta.setDisplayName("\u00A7cBack");
+ BACK_ITEM.setItemMeta(meta);
+ }
+ public static GuiItem backItem(@NotNull Gui goal){
+ // simple go back item
+ return toGuiItem(BACK_ITEM, goal);
+ }
+ public static void addBackItem(@NotNull PatternPane target,
+ @NotNull Gui goal){
+ target.bindItem('B', backItem(goal));
+ }
+
+ private static final Material DEFAULT_BACKGROUND_MAT = Material.LIGHT_GRAY_STAINED_GLASS_PANE;
+ public static GuiItem backgroundItem(Material backgroundMat){
+ ItemStack item = new ItemStack(backgroundMat);
+ ItemMeta meta = item.getItemMeta();
+ meta.setDisplayName("\u00A7c");
+ item.setItemMeta(meta);
+ return new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+ }
+ public static GuiItem backgroundItem(){
+ return backgroundItem(DEFAULT_BACKGROUND_MAT);
+ }
+ public static void addBackgroundItem(@NotNull PatternPane target,
+ @NotNull Material backgroundMat){
+ target.bindItem('0', backgroundItem(backgroundMat));
+ }
+ public static void addBackgroundItem(@NotNull PatternPane target){
+ addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
similarity index 74%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index 33359b8..c2214ac 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -1,4 +1,4 @@
-package xyz.alexcrea.cuanvil.gui.gui;
+package xyz.alexcrea.cuanvil.gui;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
@@ -7,29 +7,30 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
-import xyz.alexcrea.cuanvil.gui.gui.config.BasicConfigGui;
+import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui;
public class MainConfigGui extends ChestGui {
public final static MainConfigGui INSTANCE = new MainConfigGui();
+ static {
+ INSTANCE.init();
+ }
private MainConfigGui() {
- super(3, "§8Anvil Config", CustomAnvil.instance);
+ super(3, "\u00A7cAnvil Config", CustomAnvil.instance);
+ }
+
+ private void init(){
Pattern pattern = new Pattern(
- "111111111",
- "112345611",
- "111111111"
+ "000000000",
+ "001234500",
+ "000000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
addPane(pane);
- ItemStack backgroundBukkit = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
-
- GuiItem background = new GuiItem(backgroundBukkit,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
-
-
- pane.bindItem('1', background);
+ GuiGlobalItems.addBackgroundItem(pane);
ItemStack stonePlaceholder = new ItemStack(Material.STONE);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
similarity index 59%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index c9e6353..6a4aa9b 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -1,28 +1,36 @@
-package xyz.alexcrea.cuanvil.gui.gui.config;
+package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
-import xyz.alexcrea.cuanvil.gui.gui.GuiGlobalItems;
-import xyz.alexcrea.cuanvil.gui.gui.MainConfigGui;
+import xyz.alexcrea.cuanvil.gui.MainConfigGui;
+import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
public class BasicConfigGui extends ChestGui {
public final static BasicConfigGui INSTANCE = new BasicConfigGui();
- private BasicConfigGui(){
- super(3, "Basic Config GUI", CustomAnvil.instance);
+ static {
+ INSTANCE.init();
+ }
+ private BasicConfigGui(){
+ super(3, "\u00A7cBasic Config GUI", CustomAnvil.instance);
+
+ }
+
+ private void init(){
Pattern pattern = new Pattern(
- "111111111",
- "111111111",
- "B11111111"
+ "000000000",
+ "000000000",
+ "B00000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
addPane(pane);
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
+ GuiGlobalItems.addBackgroundItem(pane);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
deleted file mode 100644
index 6060c70..0000000
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/gui/GuiGlobalItems.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package xyz.alexcrea.cuanvil.gui.gui;
-
-import com.github.stefvanschie.inventoryframework.gui.GuiItem;
-import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
-import com.github.stefvanschie.inventoryframework.pane.PatternPane;
-import io.delilaheve.CustomAnvil;
-import org.bukkit.Material;
-import org.bukkit.inventory.ItemStack;
-import org.bukkit.inventory.meta.ItemMeta;
-
-public class GuiGlobalItems {
-
- // return
- public static GuiItem toGuiItem(ItemStack item, Gui goal){
- return new GuiItem(item, GuiGlobalActions.openGuiFactory(goal), CustomAnvil.instance);
- }
-
- // statically create back itemstack
- private static final ItemStack BACK_ITEM = new ItemStack(Material.BARRIER);
- static {
- // todo add what I need to add to the back item
- ItemMeta meta = BACK_ITEM.getItemMeta();
- meta.setDisplayName("§cBack");
- BACK_ITEM.setItemMeta(meta);
- }
- public static GuiItem backItem(Gui goal){
- // simple go back item
- return toGuiItem(BACK_ITEM, goal);
- }
- public static void addBackItem(PatternPane target, Gui goal){
- target.bindItem('B', backItem(goal));
- }
-
-}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
index 9140edc..fbe1506 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
@@ -4,7 +4,7 @@ import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.HumanEntity
-import xyz.alexcrea.cuanvil.gui.gui.MainConfigGui
+import xyz.alexcrea.cuanvil.gui.MainConfigGui
class TestExecutor : CommandExecutor {
From c0a4dd080feff3fcd09642de6b6c493b2ea62a8a Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 28 Feb 2024 19:03:16 +0100
Subject: [PATCH 022/691] progress on int setting gui.
---
.../cuanvil/gui/GuiGlobalActions.java | 29 +++-
.../alexcrea/cuanvil/gui/GuiGlobalItems.java | 24 ++-
.../alexcrea/cuanvil/gui/MainConfigGui.java | 22 ++-
.../cuanvil/gui/config/BasicConfigGui.java | 15 +-
.../config/settings/AbstractSettingGui.java | 50 ++++++
.../gui/config/settings/IntSettingsGui.java | 147 ++++++++++++++++++
6 files changed, 272 insertions(+), 15 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
index de1fb6a..936b75b 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
@@ -3,6 +3,7 @@ package xyz.alexcrea.cuanvil.gui;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@@ -13,7 +14,7 @@ public class GuiGlobalActions {
public static Consumer stayInPlace = (event) -> event.setCancelled(true);
- public static @NotNull Consumer openGuiFactory(
+ public static @NotNull Consumer openGuiAction(
@NotNull Class extends Gui> clazz,
@NotNull Class>[] argClass,
@NotNull Object... args){
@@ -32,15 +33,35 @@ public class GuiGlobalActions {
};
}
- public static @NotNull Consumer openGuiFactory(
+ public static @NotNull Consumer openGuiAction(
@NotNull Class extends Gui> clazz){
- return openGuiFactory(clazz, new Class>[0]);
+ return openGuiAction(clazz, new Class>[0]);
}
- public static @NotNull Consumer openGuiFactory(@NotNull Gui goal) {
+ public static @NotNull Consumer openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory){
+ return event -> {
+ event.setCancelled(true);
+ Gui gui = factory.create();
+ gui.show(event.getWhoClicked());
+ };
+ }
+
+ public static @NotNull Consumer openGuiAction(@NotNull Gui goal) {
return event -> {
event.setCancelled(true);
goal.show(event.getWhoClicked());
};
}
+
+ public static @NotNull Consumer saveSettingAction(
+ @NotNull AbstractSettingGui setting,
+ @NotNull Gui goal) {
+ return event -> {
+ event.setCancelled(true);
+ // Save setting
+ setting.onSave();
+ // Then show
+ goal.show(event.getWhoClicked());
+ };
+ }
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
index 42ecaa3..2a588d4 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
@@ -8,13 +8,14 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
// maybe use builder patern ?
public class GuiGlobalItems {
// return
public static GuiItem toGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
- return new GuiItem(item, GuiGlobalActions.openGuiFactory(goal), CustomAnvil.instance);
+ return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
}
// statically create back itemstack
@@ -53,4 +54,25 @@ public class GuiGlobalItems {
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
}
+ private static final Material DEFAULT_SAVE_ITEM = Material.LIME_TERRACOTTA;
+ public static GuiItem saveItem(
+ @NotNull AbstractSettingGui setting,
+ @NotNull Gui goal){
+
+ ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM);
+ ItemMeta meta = item.getItemMeta();
+ meta.setDisplayName("\u00A7aSave");
+ item.setItemMeta(meta);
+ return new GuiItem(item,
+ GuiGlobalActions.saveSettingAction(setting, goal),
+ CustomAnvil.instance);
+ }
+
+ public static GuiItem openSettingGuiItem(
+ @NotNull ItemStack item,
+ @NotNull AbstractSettingGui.SettingGuiFactory factory
+ ){
+ return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
+ }
+
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index c2214ac..9282432 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -25,7 +25,7 @@ public class MainConfigGui extends ChestGui {
Pattern pattern = new Pattern(
"000000000",
"001234500",
- "000000000"
+ "Q00000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
addPane(pane);
@@ -41,14 +41,20 @@ public class MainConfigGui extends ChestGui {
GuiItem placeholder5 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
- pane.bindItem('2', placeholder1);
- pane.bindItem('3', placeholder2);
- pane.bindItem('4', placeholder3);
- pane.bindItem('5', placeholder4);
- pane.bindItem('6', placeholder5);
+ pane.bindItem('1', placeholder1);
+ pane.bindItem('2', placeholder2);
+ pane.bindItem('3', placeholder3);
+ pane.bindItem('4', placeholder4);
+ pane.bindItem('5', placeholder5);
+
+ // quit item
+ ItemStack quitItemstack = new ItemStack(Material.BARRIER);
+ GuiItem quitItem = new GuiItem(quitItemstack, event -> {
+ event.setCancelled(true);
+ event.getWhoClicked().closeInventory();
+ },CustomAnvil.instance);
+ pane.bindItem('Q', quitItem);
}
-
-
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 6a4aa9b..0922825 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -1,11 +1,16 @@
package xyz.alexcrea.cuanvil.gui.config;
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
-import xyz.alexcrea.cuanvil.gui.MainConfigGui;
+import org.bukkit.Material;
+import org.bukkit.inventory.ItemStack;
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+import xyz.alexcrea.cuanvil.gui.MainConfigGui;
+import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
+import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
public class BasicConfigGui extends ChestGui {
@@ -23,7 +28,7 @@ public class BasicConfigGui extends ChestGui {
private void init(){
Pattern pattern = new Pattern(
"000000000",
- "000000000",
+ "010000000",
"B00000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
@@ -32,6 +37,12 @@ public class BasicConfigGui extends ChestGui {
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
GuiGlobalItems.addBackgroundItem(pane);
+ ItemStack setting1Item = new ItemStack(Material.STONE);
+ AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", CustomAnvil.instance.getConfig(), 0,42,2,1);
+ GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
+ pane.bindItem('1', setting1);
+
+
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
new file mode 100644
index 0000000..0e4b010
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -0,0 +1,50 @@
+package xyz.alexcrea.cuanvil.gui.config.settings;
+
+import com.github.stefvanschie.inventoryframework.adventuresupport.StringHolder;
+import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder;
+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.util.Pattern;
+import io.delilaheve.CustomAnvil;
+import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+
+public abstract class AbstractSettingGui extends ChestGui {
+
+ public AbstractSettingGui(int rows, @NotNull TextHolder title, Gui parent) {
+ super(rows, title, CustomAnvil.instance);
+ initBase(parent);
+ }
+
+ public AbstractSettingGui(int rows, @NotNull String title, Gui parent) {
+ this(rows, StringHolder.of(title), parent);
+ }
+
+ private PatternPane pane;
+ private void initBase(Gui parent){
+ Pattern pattern = getGuiPattern();
+ pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
+ addPane(pane);
+
+ GuiGlobalItems.addBackItem(pane, parent);
+ GuiGlobalItems.addBackgroundItem(pane);
+
+ pane.bindItem('S', GuiGlobalItems.saveItem(this, parent));
+
+ }
+
+ protected PatternPane getPane() {
+ return pane;
+ }
+
+ // S, b, 0 is used by: save, back, background
+ protected abstract Pattern getGuiPattern();
+
+ public abstract void onSave();
+
+
+ public abstract static class SettingGuiFactory{
+ public abstract AbstractSettingGui create();
+ };
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
new file mode 100644
index 0000000..5275104
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -0,0 +1,147 @@
+package xyz.alexcrea.cuanvil.gui.config.settings;
+
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+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.configuration.ConfigurationSection;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.GuiGlobalActions;
+import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+
+import java.util.function.Consumer;
+
+public class IntSettingsGui extends AbstractSettingGui{
+
+ private final IntSettingFactory holder;
+ private int now;
+ private int step;
+
+ private IntSettingsGui(IntSettingFactory holder, int now) {
+ super(3, holder.title, holder.parent);
+ assert holder.steps.length > 0 && holder.steps.length <= 9;
+ this.holder = holder;
+ this.now = now;
+ this.step = holder.steps[0];
+
+ updateValueDisplay();
+ }
+
+
+ @Override
+ public Pattern getGuiPattern() {
+ return new Pattern(
+ "000000000",
+ "00-0v0+00",
+ "B0000000S"
+ );
+ }
+
+ protected void updateValueDisplay(){
+
+ PatternPane pane = getPane();
+
+ // minus item
+ GuiItem minusItem;
+ if(now > holder.min){
+ int planned = Math.max(holder.min, now - step);
+ ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
+ ItemMeta meta = item.getItemMeta();
+ meta.setDisplayName(planned + " (-"+(now-planned)+")"); //TODO add color
+ item.setItemMeta(meta);
+
+ minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
+ }else{
+ minusItem = GuiGlobalItems.backgroundItem();
+ }
+ pane.bindItem('-', minusItem);
+
+ //plus item
+ // may do a function to generalise ?
+ GuiItem plusItem;
+ if(now < holder.max){
+ int planned = Math.min(holder.max, now + step);
+ ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
+ ItemMeta meta = item.getItemMeta();
+ meta.setDisplayName(planned + " (+"+(planned-now)+")"); //TODO add color
+ item.setItemMeta(meta);
+
+ plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
+ }else{
+ plusItem = GuiGlobalItems.backgroundItem();
+ }
+ pane.bindItem('+', plusItem);
+
+ // "result" display
+ ItemStack resultPaper = new ItemStack(Material.PAPER);
+ ItemMeta resultMeta = resultPaper.getItemMeta();
+ resultMeta.setDisplayName(""+now); //TODO color and text
+ resultPaper.setItemMeta(resultMeta);
+ GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+
+ pane.bindItem('v', resultItem);
+
+ }
+
+ protected Consumer updateNowConsumer(int planned){
+ return event->{
+ event.setCancelled(true);
+ now = planned;
+ updateValueDisplay();
+ update();
+ };
+ }
+
+ @Override
+ public void onSave() {
+ holder.section.set(holder.configPath, now);
+ // TODO SAVE (also backup before)
+
+ }
+
+ public static SettingGuiFactory factory(@NotNull String title, Gui parent,
+ String configPath, ConfigurationSection section,
+ int min, int max, int defaultVal, int... steps){
+ return new IntSettingFactory(
+ title,parent,
+ configPath, section,
+ min, max, defaultVal, steps);
+ }
+
+
+ public static class IntSettingFactory extends SettingGuiFactory{
+ @NotNull String title; Gui parent;
+ String configPath; ConfigurationSection section;
+ int min; int max; int defaultVal; int[] steps;
+
+ private IntSettingFactory(@NotNull String title, Gui parent,
+ String configPath, ConfigurationSection section,
+ int min, int max, int defaultVal, int... steps){
+ this.title = title;
+ this.parent = parent;
+ this.configPath = configPath;
+ this.section = section;
+ this.min = min;
+ this.max = max;
+ this.defaultVal = defaultVal;
+ this.steps = steps;
+ }
+
+ @Override
+ public AbstractSettingGui create() {
+ // Get value or default
+ //TODO maybe get section dynamically (and maybe same for save ?)
+ int now = section.getInt(this.configPath, this.defaultVal);
+ // create new gui
+ return new IntSettingsGui(this, now);
+ }
+
+ }
+
+}
+
From f23d818cf301d67c62491b84908d20bf9f5ad667 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 28 Feb 2024 19:25:17 +0100
Subject: [PATCH 023/691] boolean setting gui mostly done.
---
.../cuanvil/gui/config/BasicConfigGui.java | 8 +-
.../gui/config/settings/BoolSettingsGui.java | 120 ++++++++++++++++++
2 files changed, 126 insertions(+), 2 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 0922825..337c401 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -10,6 +10,7 @@ import org.bukkit.inventory.ItemStack;
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
+import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
public class BasicConfigGui extends ChestGui {
@@ -28,7 +29,7 @@ public class BasicConfigGui extends ChestGui {
private void init(){
Pattern pattern = new Pattern(
"000000000",
- "010000000",
+ "012000000",
"B00000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
@@ -42,7 +43,10 @@ public class BasicConfigGui extends ChestGui {
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
pane.bindItem('1', setting1);
-
+ ItemStack setting2Item = new ItemStack(Material.STONE);
+ AbstractSettingGui.SettingGuiFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
+ GuiItem setting2 = GuiGlobalItems.openSettingGuiItem(setting2Item, factory2);
+ pane.bindItem('2', setting2);
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
new file mode 100644
index 0000000..4074517
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -0,0 +1,120 @@
+package xyz.alexcrea.cuanvil.gui.config.settings;
+
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+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.configuration.ConfigurationSection;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collections;
+import java.util.function.Consumer;
+
+public class BoolSettingsGui extends AbstractSettingGui{
+
+ private final BoolSettingFactory holder;
+ private boolean now;
+
+ private BoolSettingsGui(BoolSettingFactory holder, boolean now) {
+ super(3, holder.title, holder.parent);
+ this.holder = holder;
+ this.now = now;
+
+ updateValueDisplay();
+ }
+
+
+ @Override
+ public Pattern getGuiPattern() {
+ return new Pattern(
+ "000000000",
+ "00-0v0+00",
+ "B0000000S"
+ );
+ }
+
+ protected void updateValueDisplay(){
+
+ PatternPane pane = getPane();
+
+ // Get displayed value for this config.
+ String displayedName;
+ Material displayedMat;
+ if(now){
+ displayedName = "\u00A7aTrue";
+ displayedMat = Material.GREEN_TERRACOTTA;
+ }else{
+ displayedName = "\u00A7cFalse";
+ displayedMat = Material.RED_TERRACOTTA;
+ }
+
+ ItemStack valueItemStack = new ItemStack(displayedMat);
+ ItemMeta valueMeta = valueItemStack.getItemMeta();
+ valueMeta.setDisplayName(displayedName);
+ valueMeta.setLore(Collections.singletonList("\u00A77Click Here to change the value"));
+ valueItemStack.setItemMeta(valueMeta);
+ GuiItem resultItem = new GuiItem(valueItemStack, inverseNowConsumer(), CustomAnvil.instance);
+
+ pane.bindItem('v', resultItem);
+
+ }
+
+ protected Consumer inverseNowConsumer(){
+ return event->{
+ event.setCancelled(true);
+ now = !now;
+ updateValueDisplay();
+ update();
+ };
+ }
+
+ @Override
+ public void onSave() {
+ holder.section.set(holder.configPath, now);
+ // TODO SAVE (also backup before)
+
+ }
+
+ public static SettingGuiFactory factory(@NotNull String title, Gui parent,
+ String configPath, ConfigurationSection section,
+ boolean defaultVal){
+ return new BoolSettingFactory(
+ title,parent,
+ configPath, section,
+ defaultVal);
+ }
+
+
+ public static class BoolSettingFactory extends SettingGuiFactory{
+ @NotNull String title; Gui parent;
+ String configPath; ConfigurationSection section;
+ boolean defaultVal;
+
+ private BoolSettingFactory(@NotNull String title, Gui parent,
+ String configPath, ConfigurationSection section,
+ boolean defaultVal){
+ this.title = title;
+ this.parent = parent;
+ this.configPath = configPath;
+ this.section = section;
+ this.defaultVal = defaultVal;
+ }
+
+ @Override
+ public AbstractSettingGui create() {
+ // Get value or default
+ //TODO maybe get section dynamically (and maybe same for save ?)
+ boolean now = section.getBoolean(this.configPath, this.defaultVal);
+ // create new gui
+ return new BoolSettingsGui(this, now);
+ }
+
+ }
+
+}
+
From 48f60bc38e1c2e4883a32aa1fab5f852c922fbb0 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 28 Feb 2024 19:42:15 +0100
Subject: [PATCH 024/691] add lore & message to int setting & more
---
src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java | 5 +++++
.../cuanvil/gui/config/settings/AbstractSettingGui.java | 5 +++++
.../cuanvil/gui/config/settings/BoolSettingsGui.java | 3 +--
.../cuanvil/gui/config/settings/IntSettingsGui.java | 8 +++++---
4 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index 9282432..e92a9a9 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -7,6 +7,7 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui;
public class MainConfigGui extends ChestGui {
@@ -49,6 +50,10 @@ public class MainConfigGui extends ChestGui {
// quit item
ItemStack quitItemstack = new ItemStack(Material.BARRIER);
+ ItemMeta quitMeta = quitItemstack.getItemMeta();
+ quitMeta.setDisplayName("\u00A7cQuit");
+ quitItemstack.setItemMeta(quitMeta);
+
GuiItem quitItem = new GuiItem(quitItemstack, event -> {
event.setCancelled(true);
event.getWhoClicked().closeInventory();
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index 0e4b010..b7aeb3a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -10,8 +10,13 @@ import io.delilaheve.CustomAnvil;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+import java.util.Collections;
+import java.util.List;
+
public abstract class AbstractSettingGui extends ChestGui {
+ protected final static List CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value");
+
public AbstractSettingGui(int rows, @NotNull TextHolder title, Gui parent) {
super(rows, title, CustomAnvil.instance);
initBase(parent);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 4074517..2015c90 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -12,7 +12,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
-import java.util.Collections;
import java.util.function.Consumer;
public class BoolSettingsGui extends AbstractSettingGui{
@@ -56,7 +55,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
ItemStack valueItemStack = new ItemStack(displayedMat);
ItemMeta valueMeta = valueItemStack.getItemMeta();
valueMeta.setDisplayName(displayedName);
- valueMeta.setLore(Collections.singletonList("\u00A77Click Here to change the value"));
+ valueMeta.setLore(AbstractSettingGui.CLICK_LORE);
valueItemStack.setItemMeta(valueMeta);
GuiItem resultItem = new GuiItem(valueItemStack, inverseNowConsumer(), CustomAnvil.instance);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 5275104..5541ed4 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -52,7 +52,8 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.max(holder.min, now - step);
ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(planned + " (-"+(now-planned)+")"); //TODO add color
+ meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)");
+ meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
@@ -68,7 +69,8 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.min(holder.max, now + step);
ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(planned + " (+"+(planned-now)+")"); //TODO add color
+ meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)");
+ meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
@@ -80,7 +82,7 @@ public class IntSettingsGui extends AbstractSettingGui{
// "result" display
ItemStack resultPaper = new ItemStack(Material.PAPER);
ItemMeta resultMeta = resultPaper.getItemMeta();
- resultMeta.setDisplayName(""+now); //TODO color and text
+ resultMeta.setDisplayName("\u00A7e"+now);
resultPaper.setItemMeta(resultMeta);
GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
From 0b11f5b66fc90feed34b663864bb0c3161032681 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 29 Feb 2024 00:45:19 +0100
Subject: [PATCH 025/691] add custom item to boolean setting button
---
.../alexcrea/cuanvil/gui/GuiGlobalItems.java | 38 +++++++++++++++++++
.../cuanvil/gui/UpdatableGlobalGui.java | 7 ++++
.../cuanvil/gui/config/BasicConfigGui.java | 5 +--
.../config/settings/AbstractSettingGui.java | 20 +++++++++-
.../gui/config/settings/BoolSettingsGui.java | 13 ++++---
.../gui/config/settings/IntSettingsGui.java | 10 +++--
6 files changed, 80 insertions(+), 13 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
index 2a588d4..82cf3c9 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
@@ -9,6 +9,9 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
+import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
+
+import java.util.Collections;
// maybe use builder patern ?
public class GuiGlobalItems {
@@ -75,4 +78,39 @@ public class GuiGlobalItems {
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
}
+ private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
+ public static GuiItem boolSettingGuiItem(
+ @NotNull BoolSettingsGui.BoolSettingFactory factory
+ ){
+ // Get item properties
+ boolean value = factory.getConfiguredValue();
+
+ Material itemMat;
+ StringBuilder itemName = new StringBuilder("\u00A7");
+ if(value){
+ itemMat = Material.GREEN_TERRACOTTA;
+ itemName.append("a");
+ }else{
+ itemMat = Material.RED_TERRACOTTA;
+ itemName.append("c");
+ }
+ itemName.append(getConfigNameFromPath(factory.getConfigPath()));
+
+ // Create item
+ ItemStack item = new ItemStack(itemMat);
+ ItemMeta itemMeta = item.getItemMeta();
+
+ itemMeta.setDisplayName(itemName.toString());
+ itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
+
+ item.setItemMeta(itemMeta);
+ return openSettingGuiItem(item, factory);
+ }
+
+ public static String getConfigNameFromPath(String path){
+ int indexOfDot = path.indexOf(".");
+ //if(indexOfDot == -1) return path;
+ // indexOfDot == -1 (not fond) imply indexOfDot+1 = 0. substring will keep the full path as expected
+ return path.substring(indexOfDot+1);
+ }
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
new file mode 100644
index 0000000..54cafd7
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
@@ -0,0 +1,7 @@
+package xyz.alexcrea.cuanvil.gui;
+
+public interface UpdatableGlobalGui {
+
+ void updateGuiForAll();
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 337c401..0134580 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -43,9 +43,8 @@ public class BasicConfigGui extends ChestGui {
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
pane.bindItem('1', setting1);
- ItemStack setting2Item = new ItemStack(Material.STONE);
- AbstractSettingGui.SettingGuiFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
- GuiItem setting2 = GuiGlobalItems.openSettingGuiItem(setting2Item, factory2);
+ BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
+ GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
pane.bindItem('2', setting2);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index b7aeb3a..f0c2e3b 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -7,6 +7,7 @@ import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
+import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
@@ -50,6 +51,23 @@ public abstract class AbstractSettingGui extends ChestGui {
public abstract static class SettingGuiFactory{
+ protected String configPath;
+ protected ConfigurationSection section;
+
+ protected SettingGuiFactory(String configPath, ConfigurationSection section){
+ this.configPath = configPath;
+ this.section = section;
+ }
+
+ public String getConfigPath() {
+ return configPath;
+ }
+
+ public ConfigurationSection getSection() {
+ return section;
+ }
+
+
public abstract AbstractSettingGui create();
- };
+ }
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 2015c90..e2c1372 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -79,7 +79,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
}
- public static SettingGuiFactory factory(@NotNull String title, Gui parent,
+ public static BoolSettingFactory factory(@NotNull String title, Gui parent,
String configPath, ConfigurationSection section,
boolean defaultVal){
return new BoolSettingFactory(
@@ -91,24 +91,27 @@ public class BoolSettingsGui extends AbstractSettingGui{
public static class BoolSettingFactory extends SettingGuiFactory{
@NotNull String title; Gui parent;
- String configPath; ConfigurationSection section;
boolean defaultVal;
private BoolSettingFactory(@NotNull String title, Gui parent,
String configPath, ConfigurationSection section,
boolean defaultVal){
+ super(configPath, section);
this.title = title;
this.parent = parent;
- this.configPath = configPath;
- this.section = section;
+
this.defaultVal = defaultVal;
}
+ public boolean getConfiguredValue(){
+ return this.section.getBoolean(this.configPath, this.defaultVal);
+ }
+
@Override
public AbstractSettingGui create() {
// Get value or default
//TODO maybe get section dynamically (and maybe same for save ?)
- boolean now = section.getBoolean(this.configPath, this.defaultVal);
+ boolean now = getConfiguredValue();
// create new gui
return new BoolSettingsGui(this, now);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 5541ed4..2ddfeb9 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -118,27 +118,29 @@ public class IntSettingsGui extends AbstractSettingGui{
public static class IntSettingFactory extends SettingGuiFactory{
@NotNull String title; Gui parent;
- String configPath; ConfigurationSection section;
int min; int max; int defaultVal; int[] steps;
private IntSettingFactory(@NotNull String title, Gui parent,
String configPath, ConfigurationSection section,
int min, int max, int defaultVal, int... steps){
+ super(configPath, section);
this.title = title;
this.parent = parent;
- this.configPath = configPath;
- this.section = section;
this.min = min;
this.max = max;
this.defaultVal = defaultVal;
this.steps = steps;
}
+ public int getConfiguredValue(){
+ return this.section.getInt(this.configPath, this.defaultVal);
+ }
+
@Override
public AbstractSettingGui create() {
// Get value or default
//TODO maybe get section dynamically (and maybe same for save ?)
- int now = section.getInt(this.configPath, this.defaultVal);
+ int now = getConfiguredValue();
// create new gui
return new IntSettingsGui(this, now);
}
From 3afe786b386701da21bca3e417e1915f70e792f6 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 29 Feb 2024 01:55:08 +0100
Subject: [PATCH 026/691] update globaly visible menu on value update
---
.../alexcrea/cuanvil/gui/MainConfigGui.java | 1 +
.../cuanvil/gui/UpdatableGlobalGui.java | 7 -------
.../cuanvil/gui/ValueUpdatableGui.java | 20 +++++++++++++++++++
.../cuanvil/gui/config/BasicConfigGui.java | 17 ++++++++++++----
.../config/settings/AbstractSettingGui.java | 13 ++++++------
.../gui/config/settings/BoolSettingsGui.java | 8 ++++----
.../gui/config/settings/IntSettingsGui.java | 12 +++++------
.../gui/{ => utils}/GuiGlobalActions.java | 7 +++++--
.../gui/{ => utils}/GuiGlobalItems.java | 5 +++--
9 files changed, 59 insertions(+), 31 deletions(-)
delete mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/ValueUpdatableGui.java
rename src/main/java/xyz/alexcrea/cuanvil/gui/{ => utils}/GuiGlobalActions.java (90%)
rename src/main/java/xyz/alexcrea/cuanvil/gui/{ => utils}/GuiGlobalItems.java (96%)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index e92a9a9..d11e1ca 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -9,6 +9,7 @@ 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.GuiGlobalItems;
public class MainConfigGui extends ChestGui {
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
deleted file mode 100644
index 54cafd7..0000000
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/UpdatableGlobalGui.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package xyz.alexcrea.cuanvil.gui;
-
-public interface UpdatableGlobalGui {
-
- void updateGuiForAll();
-
-}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/ValueUpdatableGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/ValueUpdatableGui.java
new file mode 100644
index 0000000..3cf65f0
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/ValueUpdatableGui.java
@@ -0,0 +1,20 @@
+package xyz.alexcrea.cuanvil.gui;
+
+import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder;
+import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+
+public abstract class ValueUpdatableGui extends ChestGui {
+
+ public ValueUpdatableGui(int rows, @NotNull String title, @NotNull Plugin plugin) {
+ super(rows, title, plugin);
+ }
+
+ public ValueUpdatableGui(int rows, @NotNull TextHolder title, @NotNull Plugin plugin) {
+ super(rows, title, plugin);
+ }
+
+ public abstract void updateGuiValues();
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 0134580..25c0a92 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -1,19 +1,19 @@
package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
-import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
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.inventory.ItemStack;
-import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
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.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
+import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
-public class BasicConfigGui extends ChestGui {
+public class BasicConfigGui extends ValueUpdatableGui {
public final static BasicConfigGui INSTANCE = new BasicConfigGui();
@@ -26,18 +26,25 @@ public class BasicConfigGui extends ChestGui {
}
+ PatternPane pane;
private void init(){
Pattern pattern = new Pattern(
"000000000",
"012000000",
"B00000000"
);
- PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
+ pane = new PatternPane(0, 0, 9, 3, pattern);
addPane(pane);
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
GuiGlobalItems.addBackgroundItem(pane);
+ updateGuiValues();
+ }
+
+ @Override
+ public void updateGuiValues() {
+ // Update item with value
ItemStack setting1Item = new ItemStack(Material.STONE);
AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", CustomAnvil.instance.getConfig(), 0,42,2,1);
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
@@ -46,6 +53,8 @@ public class BasicConfigGui extends ChestGui {
BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
pane.bindItem('2', setting2);
+
+ update();
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index f0c2e3b..6df0cb5 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -3,13 +3,13 @@ package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.adventuresupport.StringHolder;
import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder;
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.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
-import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
+import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
import java.util.Collections;
import java.util.List;
@@ -18,17 +18,18 @@ public abstract class AbstractSettingGui extends ChestGui {
protected final static List CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value");
- public AbstractSettingGui(int rows, @NotNull TextHolder title, Gui parent) {
+ private PatternPane pane;
+
+ public AbstractSettingGui(int rows, @NotNull TextHolder title, ValueUpdatableGui parent) {
super(rows, title, CustomAnvil.instance);
initBase(parent);
}
- public AbstractSettingGui(int rows, @NotNull String title, Gui parent) {
+ public AbstractSettingGui(int rows, @NotNull String title, ValueUpdatableGui parent) {
this(rows, StringHolder.of(title), parent);
}
- private PatternPane pane;
- private void initBase(Gui parent){
+ private void initBase(ValueUpdatableGui parent){
Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
addPane(pane);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index e2c1372..3bf81c6 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -1,7 +1,6 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
-import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
@@ -11,6 +10,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import java.util.function.Consumer;
@@ -79,7 +79,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
}
- public static BoolSettingFactory factory(@NotNull String title, Gui parent,
+ public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section,
boolean defaultVal){
return new BoolSettingFactory(
@@ -90,10 +90,10 @@ public class BoolSettingsGui extends AbstractSettingGui{
public static class BoolSettingFactory extends SettingGuiFactory{
- @NotNull String title; Gui parent;
+ @NotNull String title; ValueUpdatableGui parent;
boolean defaultVal;
- private BoolSettingFactory(@NotNull String title, Gui parent,
+ private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section,
boolean defaultVal){
super(configPath, section);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 2ddfeb9..07c1659 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -1,7 +1,6 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
-import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
@@ -11,8 +10,9 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
-import xyz.alexcrea.cuanvil.gui.GuiGlobalActions;
-import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
+import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
+import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
import java.util.function.Consumer;
@@ -106,7 +106,7 @@ public class IntSettingsGui extends AbstractSettingGui{
}
- public static SettingGuiFactory factory(@NotNull String title, Gui parent,
+ public static SettingGuiFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section,
int min, int max, int defaultVal, int... steps){
return new IntSettingFactory(
@@ -117,10 +117,10 @@ public class IntSettingsGui extends AbstractSettingGui{
public static class IntSettingFactory extends SettingGuiFactory{
- @NotNull String title; Gui parent;
+ @NotNull String title; ValueUpdatableGui parent;
int min; int max; int defaultVal; int[] steps;
- private IntSettingFactory(@NotNull String title, Gui parent,
+ private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section,
int min, int max, int defaultVal, int... steps){
super(configPath, section);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
similarity index 90%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
index 936b75b..711d9d5 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
@@ -1,8 +1,9 @@
-package xyz.alexcrea.cuanvil.gui;
+package xyz.alexcrea.cuanvil.gui.utils;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import java.lang.reflect.Constructor;
@@ -55,11 +56,13 @@ public class GuiGlobalActions {
public static @NotNull Consumer saveSettingAction(
@NotNull AbstractSettingGui setting,
- @NotNull Gui goal) {
+ @NotNull ValueUpdatableGui goal) {
return event -> {
event.setCancelled(true);
// Save setting
setting.onSave();
+ // Update gui for the one who have it open
+ goal.updateGuiValues();
// Then show
goal.show(event.getWhoClicked());
};
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
similarity index 96%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
index 82cf3c9..3e07848 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
@@ -1,4 +1,4 @@
-package xyz.alexcrea.cuanvil.gui;
+package xyz.alexcrea.cuanvil.gui.utils;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
@@ -8,6 +8,7 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
@@ -60,7 +61,7 @@ public class GuiGlobalItems {
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_TERRACOTTA;
public static GuiItem saveItem(
@NotNull AbstractSettingGui setting,
- @NotNull Gui goal){
+ @NotNull ValueUpdatableGui goal){
ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM);
ItemMeta meta = item.getItemMeta();
From 643487e1a929c6ef5254832c7a842aa40c5063d9 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 29 Feb 2024 16:34:52 +0100
Subject: [PATCH 027/691] add config holder.
---
.../alexcrea/cuanvil/config/ConfigHolder.java | 239 ++++++++++++++++++
.../io/delilaheve/AnvilEventListener.kt | 3 +-
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 48 +---
.../io/delilaheve/util/ConfigOptions.kt | 19 +-
.../io/delilaheve/util/EnchantmentUtil.kt | 5 +-
.../cuanvil/command/ReloadExecutor.kt | 3 +-
.../cuanvil/group/EnchantConflictManager.kt | 6 +-
.../cuanvil/group/ItemGroupManager.kt | 8 +-
.../alexcrea/cuanvil/util/UnitRepairUtil.kt | 9 +-
9 files changed, 271 insertions(+), 69 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
new file mode 100644
index 0000000..0ad0d61
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
@@ -0,0 +1,239 @@
+package xyz.alexcrea.cuanvil.config;
+
+import com.google.common.io.Files;
+import io.delilaheve.CustomAnvil;
+import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
+import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
+import xyz.alexcrea.cuanvil.group.ItemGroupManager;
+
+import java.io.File;
+import java.io.IOException;
+
+public abstract class ConfigHolder {
+
+ // Available configuration:
+ public static DefaultConfigHolder DEFAULT_CONFIG;
+ public static ItemGroupConfigHolder ITEM_GROUP_HOLDER;
+ public static ConflictConfigHolder CONFLICT_HOLDER;
+ public static UnitRepairHolder UNIT_REPAIR_HOLDER;
+
+ public static boolean loadConfig(){
+ DEFAULT_CONFIG = new DefaultConfigHolder();
+ ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
+ CONFLICT_HOLDER = new ConflictConfigHolder();
+ UNIT_REPAIR_HOLDER = new UnitRepairHolder();
+
+ return reloadAllFromDisk(true);
+ }
+
+ public static boolean reloadAllFromDisk(boolean hardfail){
+
+ boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail);
+ if(!sucess) return false;
+ sucess = ITEM_GROUP_HOLDER.reloadFromDisk(hardfail);
+ if(!sucess) return false;
+ sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
+ if(!sucess) return false;
+ sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail);
+ return sucess;
+ }
+
+
+ // usefull part of the file
+ private static final File BACKUP_FOLDER = new File(CustomAnvil.instance.getDataFolder(), "backup");
+
+ protected FileConfiguration configuration;
+ protected ConfigHolder(){
+
+ }
+
+ public abstract boolean reloadFromDisk(boolean hardFail);
+ public abstract void reload();
+ public FileConfiguration getConfig(){
+ return configuration;
+ }
+
+ // Config name and files
+ protected abstract String getConfigFileName();
+
+ protected String getConfigFileExtension(){
+ return ".yml";
+ }
+ protected File getConfigFile(){
+ return new File(CustomAnvil.instance.getDataFolder(), getConfigFileName()+getConfigFileExtension());
+ }
+ protected File getFirstBackup(){
+ return new File(BACKUP_FOLDER, getConfigFileName()+"-first"+getConfigFileExtension());
+ }
+ protected File getLastBackup(){
+ return new File(BACKUP_FOLDER, getConfigFileName()+"-latest"+getConfigFileExtension());
+ }
+
+ // Save logic
+ public boolean saveToDisk(boolean doBackup){
+ if(!doBackup){
+ if(!saveBackup()){
+ CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
+ return false;
+ }
+ }
+ File base = getConfigFile();
+ // if file exist and can't be deleted the file, then we gave up.
+ if(base.exists() && !base.delete()) {
+ CustomAnvil.instance.getLogger().severe("Could not save config: can't delete existing file.");
+ return false;
+ }
+ FileConfiguration config = getConfig();
+ try {
+ config.save(base);
+ } catch (IOException e) {
+ e.printStackTrace();
+ CustomAnvil.instance.getLogger().severe("Could not save config...");
+ return false;
+ }
+
+
+ return true;
+ }
+
+ public boolean saveBackup(){
+ File base = getConfigFile();
+ if(!base.exists()) return true; // We did back up everything we had to (nothing in this case)
+ boolean sufficientSuccess = false;
+
+ BACKUP_FOLDER.mkdirs();
+ // save first backup if do not exist
+ File firstBackup = getFirstBackup();
+ if(!firstBackup.exists()){
+ try {
+ Files.copy(base, firstBackup);
+ sufficientSuccess = true;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ // save last backup
+ File lastBackup = getLastBackup();
+ // if file exist and can't be deleted the file, then we gave up.
+ if(lastBackup.exists() && !lastBackup.delete()){
+ return sufficientSuccess;
+ }
+
+ try {
+ Files.move(base, lastBackup);
+ sufficientSuccess = true;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return sufficientSuccess;
+ }
+
+ public static class DefaultConfigHolder extends ConfigHolder{
+
+ @Override
+ protected String getConfigFileName() {
+ return "config";
+ }
+
+ @Override
+ public boolean reloadFromDisk(boolean hardFail) {
+ CustomAnvil.instance.reloadConfig();
+ this.configuration = CustomAnvil.instance.getConfig();
+ return true;
+ }
+
+ @Override
+ public void reload() {}// Nothing to do
+
+ }
+
+ // Abstract class for non default config
+ public abstract static class ResourceConfigHolder extends ConfigHolder{
+
+ String resourceName;
+ private ResourceConfigHolder(String resourceName){
+ this.resourceName = resourceName;
+ }
+
+ @Override
+ protected String getConfigFileName() {
+ return resourceName;
+ }
+
+ @Override
+ public boolean reloadFromDisk(boolean hardFail) {
+ YamlConfiguration configuration = CustomAnvil.instance.reloadResource(
+ getConfigFileName()+getConfigFileExtension(), hardFail);
+ if(configuration == null) return false;
+ this.configuration = configuration;
+ reload();
+ return true;
+ }
+
+ }
+
+ // Class for itemGroupsManager config
+ public static class ItemGroupConfigHolder extends ResourceConfigHolder{
+ private final static String FILE_NAME = "item_groups";
+
+ ItemGroupManager itemGroupsManager;
+ private ItemGroupConfigHolder() {
+ super(FILE_NAME);
+ }
+
+ public ItemGroupManager getItemGroupsManager() {
+ return itemGroupsManager;
+ }
+
+ @Override
+ public void reload() {
+ // not the most efficient way for in game reload TODO optimise
+ this.itemGroupsManager = new ItemGroupManager();
+ this.itemGroupsManager.prepareGroups(this.configuration);
+
+ if(CONFLICT_HOLDER.getConfig() != null){
+ CONFLICT_HOLDER.reload();
+ }
+ }
+
+ }
+
+ // Class for enchant conflict config
+ public static class ConflictConfigHolder extends ResourceConfigHolder{
+ private final static String FILE_NAME = "enchant_conflict";
+
+ EnchantConflictManager conflictManager;
+ private ConflictConfigHolder() {
+ super(FILE_NAME);
+ }
+
+ public EnchantConflictManager getConflictManager() {
+ return conflictManager;
+ }
+
+ // We assume this is called after item group manager reload;,
+ @Override
+ public void reload() {
+ // not the most efficient way for in game reload TODO optimise
+ this.conflictManager = new EnchantConflictManager();
+ this.conflictManager.prepareConflicts(this.configuration, ITEM_GROUP_HOLDER.getItemGroupsManager());
+ }
+
+ }
+
+ // Class for unit repair config
+ public static class UnitRepairHolder extends ResourceConfigHolder{
+ private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item";
+
+ private UnitRepairHolder() {
+ super(ITEM_GROUP_FILE_NAME);
+ }
+ @Override
+ public void reload() {} // Do nothing
+
+ }
+
+
+}
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index 110a651..d6d3cf9 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -23,6 +23,7 @@ import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable
+import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.group.ConflictType
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import kotlin.math.min
@@ -320,7 +321,7 @@ class AnvilEventListener : Listener {
// count enchant as illegal enchant if it conflicts with another enchant or not in result
if((enchantment.key !in resultEnchsKeys)){
resultEnchsKeys.add(enchantment.key)
- val conflictType = CustomAnvil.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
+ val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
resultEnchsKeys.remove(enchantment.key)
if(ConflictType.BIG_CONFLICT == conflictType){
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 1fee6ea..1bb48f7 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -6,9 +6,8 @@ import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.cuanvil.command.ReloadExecutor
import xyz.alexcrea.cuanvil.command.TestExecutor
+import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.util.Metrics
-import xyz.alexcrea.cuanvil.group.EnchantConflictManager
-import xyz.alexcrea.cuanvil.group.ItemGroupManager
import xyz.alexcrea.cuanvil.util.MetricsUtil
import java.io.File
import java.io.FileReader
@@ -37,17 +36,8 @@ class CustomAnvil : JavaPlugin() {
// Test command name
const val commandTestName = "test"
- // Item Grouping Configuration file name
- const val itemGroupingConfigFilePath = "item_groups.yml"
- // Conflict Configuration file name
- const val enchantConflicConfigFilePath = "enchant_conflict.yml"
- // Unit Repair Configuration file name
- const val unitRepairFilePath = "unit_repair_item.yml"
-
// Current plugin instance
lateinit var instance: CustomAnvil
- // Current item grouping configuration instance
- lateinit var conflictManager: EnchantConflictManager
// Configuration for unit repair
lateinit var unitRepairConfig: YamlConfiguration
@@ -76,7 +66,9 @@ class CustomAnvil : JavaPlugin() {
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
}
- val success = reloadAllConfigs(true)
+ // Load config
+
+ val success = ConfigHolder.loadConfig();
if(!success) return
// Load metrics
@@ -92,36 +84,8 @@ class CustomAnvil : JavaPlugin() {
)
}
- fun reloadAllConfigs(hardFailSafe: Boolean): Boolean{
- saveDefaultConfig()
- reloadConfig()
-
- // Load material grouping config
- val itemGroupConfig = reloadResource(itemGroupingConfigFilePath, hardFailSafe) ?: return false
- // Read material groups from config
- val itemGroupsManager = ItemGroupManager()
- itemGroupsManager.prepareGroups(itemGroupConfig)
-
- // Load enchantment conflicts config
- val conflictConfig = reloadResource(enchantConflicConfigFilePath, hardFailSafe) ?: return false
- // Read conflicts from config and material group manager
- val conflictManager = EnchantConflictManager()
- conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
-
- // Load unit repair config
- val unitRepairConfig = reloadResource(unitRepairFilePath, hardFailSafe) ?: return false
-
- // Set the global variable
- CustomAnvil.conflictManager = conflictManager
- CustomAnvil.unitRepairConfig = unitRepairConfig
-
- // Test if is default config
- MetricsUtil.testIfConfigIsDefault(config, itemGroupConfig, conflictConfig, unitRepairConfig)
- return true
- }
-
- private fun reloadResource(resourceName: String,
- hardFailSafe:Boolean = true): YamlConfiguration?{
+ fun reloadResource(resourceName: String,
+ hardFailSafe:Boolean = true): YamlConfiguration?{
// Save default resource
val file = File(dataFolder,resourceName)
if(!file.exists()){
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index 1a235b2..2761d4d 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -3,6 +3,7 @@ package io.delilaheve.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import org.bukkit.enchantments.Enchantment
+import xyz.alexcrea.cuanvil.config.ConfigHolder
/**
* Config option accessors
@@ -71,7 +72,7 @@ object ConfigOptions {
*/
private val defaultEnchantLimit: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(DEFAULT_LIMIT_PATH, DEFAULT_ENCHANT_LIMIT)
}
@@ -81,7 +82,7 @@ object ConfigOptions {
*/
val limitRepairCost: Boolean
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getBoolean(LIMIT_REPAIR_COST, DEFAULT_LIMIT_REPAIR)
}
@@ -91,7 +92,7 @@ object ConfigOptions {
*/
val limitRepairValue: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(LIMIT_REPAIR_VALUE, DEFAULT_LIMIT_REPAIR_VALUE)
.takeIf { it in REPAIR_LIMIT_RANGE }
@@ -103,7 +104,7 @@ object ConfigOptions {
*/
val itemRepairCost: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(ITEM_REPAIR_COST, DEFAULT_ITEM_REPAIR_COST)
.takeIf { it in REPAIR_COST_RANGE }
@@ -115,7 +116,7 @@ object ConfigOptions {
*/
val unitRepairCost: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(UNIT_REPAIR_COST, DEFAULT_UNIT_REPAIR_COST)
.takeIf { it in REPAIR_COST_RANGE }
@@ -127,7 +128,7 @@ object ConfigOptions {
*/
val itemRenameCost: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(ITEM_RENAME_COST, DEFAULT_ITEM_RENAME_COST)
.takeIf { it in ITEM_RENAME_COST_RANGE }
@@ -139,7 +140,7 @@ object ConfigOptions {
*/
val sacrificeIllegalCost: Int
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(SACRIFICE_ILLEGAL_COST, DEFAULT_SACRIFICE_ILLEGAL_COST)
.takeIf { it in SACRIFICE_ILLEGAL_COST_RANGE }
@@ -150,7 +151,7 @@ object ConfigOptions {
*/
val removeRepairLimit: Boolean
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getBoolean(REMOVE_REPAIR_LIMIT, DEFAULT_REMOVE_LIMIT)
}
@@ -160,7 +161,7 @@ object ConfigOptions {
*/
val debugLog: Boolean
get() {
- return CustomAnvil.instance
+ return ConfigHolder.DEFAULT_CONFIG
.config
.getBoolean(DEBUG_LOGGING, DEFAULT_DEBUG_LOG)
}
diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
index ff61cbb..f2f90d6 100644
--- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
+++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt
@@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.HumanEntity
+import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.group.ConflictType
import kotlin.math.max
import kotlin.math.min
@@ -34,7 +35,7 @@ object EnchantmentUtil {
// Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
this[enchantment] = level
if(!player.hasPermission(CustomAnvil.bypassFusePermission) &&
- (CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
+ (ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
this.remove(enchantment)
}
@@ -42,7 +43,7 @@ object EnchantmentUtil {
// Enchantment already in result list
else{
// ... and they are conflicting
- if((CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
+ if((ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
&& !player.hasPermission(CustomAnvil.bypassFusePermission)){
return@forEach
}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index b92b569..9b20c0f 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
+import xyz.alexcrea.cuanvil.config.ConfigHolder
class ReloadExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean {
@@ -30,7 +31,7 @@ class ReloadExecutor : CommandExecutor {
*/
private fun commandBody(hardfail: Boolean): Boolean{
try {
- return CustomAnvil.instance.reloadAllConfigs(hardfail)
+ return ConfigHolder.reloadAllFromDisk(hardfail);
}catch (e: Exception){
e.printStackTrace()
return false
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
index add081f..4ebc510 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt
@@ -4,9 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
-import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.enchantments.Enchantment
-import kotlin.collections.ArrayList
class EnchantConflictManager {
@@ -29,7 +27,7 @@ class EnchantConflictManager {
private lateinit var conflictMap: HashMap>
// Read and prepare all conflict
- fun prepareConflicts(config: YamlConfiguration, itemManager: ItemGroupManager){
+ fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager){
conflictMap = HashMap()
val keys = config.getKeys(false)
@@ -140,7 +138,7 @@ class EnchantConflictManager {
}
-enum class ConflictType(){
+enum class ConflictType{
NO_CONFLICT,
SMALL_CONFLICT,
BIG_CONFLICT
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
index f157870..a7c927a 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
@@ -3,9 +3,7 @@ package xyz.alexcrea.cuanvil.group
import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
-import org.bukkit.configuration.file.YamlConfiguration
import java.util.*
-import kotlin.collections.HashMap
class ItemGroupManager {
@@ -23,7 +21,7 @@ class ItemGroupManager {
private lateinit var groupMap : HashMap
// Read and create material groups
- fun prepareGroups(config: YamlConfiguration){
+ fun prepareGroups(config: ConfigurationSection){
groupMap = HashMap()
val keys = config.getKeys(false)
@@ -35,7 +33,7 @@ class ItemGroupManager {
}
// Create group by key
- private fun createGroup(config: YamlConfiguration,
+ private fun createGroup(config: ConfigurationSection,
keys: Set,
key: String): AbstractMaterialGroup {
val groupSection = config.getConfigurationSection(key)!!
@@ -60,7 +58,7 @@ class ItemGroupManager {
// Read Group elements
private fun readGroup(group: AbstractMaterialGroup,
groupSection: ConfigurationSection,
- config: YamlConfiguration,
+ config: ConfigurationSection,
keys: Set){
// Read material to include in this group policy
val materialList = groupSection.getStringList(MATERIAL_LIST_PATH)
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
index b01d37f..a71d912 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt
@@ -1,8 +1,8 @@
package xyz.alexcrea.cuanvil.util
-import io.delilaheve.CustomAnvil
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.ItemStack
+import xyz.alexcrea.cuanvil.config.ConfigHolder
object UnitRepairUtil {
@@ -19,15 +19,14 @@ object UnitRepairUtil {
other: ItemStack?
): Double? {
if(other == null) return null
- val config = CustomAnvil.unitRepairConfig
+ val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
// Get configuration section if exist
val otherName = other.type.name.uppercase()
var section = config.getConfigurationSection(otherName)
if(section == null){
section = config.getConfigurationSection(otherName.lowercase())
- if(section == null) {
- return null
- }
+ if(section == null) return null
+
}
// Get repair amount
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
From c5dbbeb67c69ef5cbdcb24c5f997d90c14b03684 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 29 Feb 2024 19:36:42 +0100
Subject: [PATCH 028/691] add save to disk & backup to int & boolean setting
gui.
---
.../alexcrea/cuanvil/config/ConfigHolder.java | 5 ++--
.../cuanvil/gui/config/BasicConfigGui.java | 5 ++--
.../config/settings/AbstractSettingGui.java | 19 ++++++++-------
.../gui/config/settings/BoolSettingsGui.java | 23 ++++++++++---------
.../gui/config/settings/IntSettingsGui.java | 23 ++++++++++---------
.../cuanvil/gui/utils/GuiGlobalActions.java | 4 +++-
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 5 +---
.../cuanvil/command/ReloadExecutor.kt | 6 ++---
8 files changed, 47 insertions(+), 43 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
index 0ad0d61..40d8f82 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
@@ -72,7 +72,7 @@ public abstract class ConfigHolder {
// Save logic
public boolean saveToDisk(boolean doBackup){
- if(!doBackup){
+ if(doBackup){
if(!saveBackup()){
CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
return false;
@@ -93,11 +93,10 @@ public abstract class ConfigHolder {
return false;
}
-
return true;
}
- public boolean saveBackup(){
+ protected boolean saveBackup(){
File base = getConfigFile();
if(!base.exists()) return true; // We did back up everything we had to (nothing in this case)
boolean sufficientSuccess = false;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 25c0a92..6f0da4c 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -6,6 +6,7 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
+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.AbstractSettingGui;
@@ -46,11 +47,11 @@ public class BasicConfigGui extends ValueUpdatableGui {
public void updateGuiValues() {
// Update item with value
ItemStack setting1Item = new ItemStack(Material.STONE);
- AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", CustomAnvil.instance.getConfig(), 0,42,2,1);
+ AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,42,2,1);
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
pane.bindItem('1', setting1);
- BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
+ BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", ConfigHolder.DEFAULT_CONFIG, false);
GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
pane.bindItem('2', setting2);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index 6df0cb5..9ca931a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -6,8 +6,8 @@ import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
-import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
+import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
@@ -16,6 +16,10 @@ import java.util.List;
public abstract class AbstractSettingGui extends ChestGui {
+ // Temporary values, until I get something better.
+ public static final boolean TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE = true;
+ public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true;
+
protected final static List CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value");
private PatternPane pane;
@@ -48,27 +52,26 @@ public abstract class AbstractSettingGui extends ChestGui {
// S, b, 0 is used by: save, back, background
protected abstract Pattern getGuiPattern();
- public abstract void onSave();
+ public abstract boolean onSave();
public abstract static class SettingGuiFactory{
protected String configPath;
- protected ConfigurationSection section;
+ protected ConfigHolder config;
- protected SettingGuiFactory(String configPath, ConfigurationSection section){
+ protected SettingGuiFactory(String configPath, ConfigHolder config){
this.configPath = configPath;
- this.section = section;
+ this.config = config;
}
public String getConfigPath() {
return configPath;
}
- public ConfigurationSection getSection() {
- return section;
+ public ConfigHolder getConfigHolder() {
+ return config;
}
-
public abstract AbstractSettingGui create();
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 3bf81c6..677e551 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -5,11 +5,11 @@ 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.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent;
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 java.util.function.Consumer;
@@ -73,18 +73,20 @@ public class BoolSettingsGui extends AbstractSettingGui{
}
@Override
- public void onSave() {
- holder.section.set(holder.configPath, now);
- // TODO SAVE (also backup before)
-
+ public boolean onSave() {
+ holder.config.getConfig().set(holder.configPath, now);
+ if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
+ return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
+ }
+ return true;
}
public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigurationSection section,
+ String configPath, ConfigHolder config,
boolean defaultVal){
return new BoolSettingFactory(
title,parent,
- configPath, section,
+ configPath, config,
defaultVal);
}
@@ -94,9 +96,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
boolean defaultVal;
private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigurationSection section,
+ String configPath, ConfigHolder config,
boolean defaultVal){
- super(configPath, section);
+ super(configPath, config);
this.title = title;
this.parent = parent;
@@ -104,13 +106,12 @@ public class BoolSettingsGui extends AbstractSettingGui{
}
public boolean getConfiguredValue(){
- return this.section.getBoolean(this.configPath, this.defaultVal);
+ return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
}
@Override
public AbstractSettingGui create() {
// Get value or default
- //TODO maybe get section dynamically (and maybe same for save ?)
boolean now = getConfiguredValue();
// create new gui
return new BoolSettingsGui(this, now);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 07c1659..387d086 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -5,11 +5,11 @@ 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.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent;
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.utils.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
@@ -100,18 +100,20 @@ public class IntSettingsGui extends AbstractSettingGui{
}
@Override
- public void onSave() {
- holder.section.set(holder.configPath, now);
- // TODO SAVE (also backup before)
-
+ public boolean onSave() {
+ if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
+ holder.config.getConfig().set(holder.configPath, now);
+ return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
+ }
+ return true;
}
public static SettingGuiFactory factory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigurationSection section,
+ String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
return new IntSettingFactory(
title,parent,
- configPath, section,
+ configPath, config,
min, max, defaultVal, steps);
}
@@ -121,9 +123,9 @@ public class IntSettingsGui extends AbstractSettingGui{
int min; int max; int defaultVal; int[] steps;
private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigurationSection section,
+ String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
- super(configPath, section);
+ super(configPath, config);
this.title = title;
this.parent = parent;
this.min = min;
@@ -133,13 +135,12 @@ public class IntSettingsGui extends AbstractSettingGui{
}
public int getConfiguredValue(){
- return this.section.getInt(this.configPath, this.defaultVal);
+ return this.config.getConfig().getInt(this.configPath, this.defaultVal);
}
@Override
public AbstractSettingGui create() {
// Get value or default
- //TODO maybe get section dynamically (and maybe same for save ?)
int now = getConfiguredValue();
// create new gui
return new IntSettingsGui(this, now);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
index 711d9d5..69989ed 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
@@ -60,7 +60,9 @@ public class GuiGlobalActions {
return event -> {
event.setCancelled(true);
// Save setting
- setting.onSave();
+ if(!setting.onSave()){
+ event.getWhoClicked().sendMessage("\u00A7cSomething wrong happen while saving the change of value.");
+ }
// Update gui for the one who have it open
goal.updateGuiValues();
// Then show
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 1bb48f7..9b6bdf5 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -39,9 +39,6 @@ class CustomAnvil : JavaPlugin() {
// Current plugin instance
lateinit var instance: CustomAnvil
- // Configuration for unit repair
- lateinit var unitRepairConfig: YamlConfiguration
-
/**
* Logging handler
*/
@@ -68,7 +65,7 @@ class CustomAnvil : JavaPlugin() {
// Load config
- val success = ConfigHolder.loadConfig();
+ val success = ConfigHolder.loadConfig()
if(!success) return
// Load metrics
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index 9b20c0f..7819b6a 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -30,11 +30,11 @@ class ReloadExecutor : CommandExecutor {
* Execute the command, return true if success or false otherwise
*/
private fun commandBody(hardfail: Boolean): Boolean{
- try {
- return ConfigHolder.reloadAllFromDisk(hardfail);
+ return try {
+ ConfigHolder.reloadAllFromDisk(hardfail)
}catch (e: Exception){
e.printStackTrace()
- return false
+ false
}
}
}
From f0ec2151b36a51aaa8821ea6e42d45a57150ef64 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 02:02:52 +0100
Subject: [PATCH 029/691] add step selector for int setting gui.
---
.../cuanvil/gui/config/BasicConfigGui.java | 2 +-
.../gui/config/settings/IntSettingsGui.java | 77 ++++++++++++++++++-
2 files changed, 75 insertions(+), 4 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 6f0da4c..46f2298 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -47,7 +47,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
public void updateGuiValues() {
// Update item with value
ItemStack setting1Item = new ItemStack(Material.STONE);
- AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,42,2,1);
+ AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,255,2,1, 5, 10, 50, 100);
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
pane.bindItem('1', setting1);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 387d086..da60bf0 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -14,6 +14,8 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
+import java.util.Collections;
+import java.util.List;
import java.util.function.Consumer;
public class IntSettingsGui extends AbstractSettingGui{
@@ -30,13 +32,14 @@ public class IntSettingsGui extends AbstractSettingGui{
this.step = holder.steps[0];
updateValueDisplay();
+ initStepsValue();
}
@Override
public Pattern getGuiPattern() {
return new Pattern(
- "000000000",
+ "abcdefghi",
"00-0v0+00",
"B0000000S"
);
@@ -58,7 +61,7 @@ public class IntSettingsGui extends AbstractSettingGui{
minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
}else{
- minusItem = GuiGlobalItems.backgroundItem();
+ minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER);
}
pane.bindItem('-', minusItem);
@@ -75,7 +78,7 @@ public class IntSettingsGui extends AbstractSettingGui{
plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance);
}else{
- plusItem = GuiGlobalItems.backgroundItem();
+ plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER);
}
pane.bindItem('+', plusItem);
@@ -99,6 +102,74 @@ public class IntSettingsGui extends AbstractSettingGui{
};
}
+ protected void initStepsValue(){
+ // Put background glass on the background of 'a' to 'b'
+ GuiItem background = GuiGlobalItems.backgroundItem();
+ PatternPane pane = getPane();
+
+ for (char i = 'a'; i < 'a'+9; i++) {
+ pane.bindItem(i, background);
+ }
+ // Then update legit step values
+ updateStepValue();
+ }
+ 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.
+ // Offset
+ val -= (char) ((holder.steps.length-1)/2);
+
+ // Then place items
+ PatternPane pane = getPane();
+ for (int i = 0; i < holder.steps.length; i++) {
+ pane.bindItem(val+i, stepGuiItem(i));
+ }
+
+ }
+
+ protected GuiItem stepGuiItem(int stepIndex){
+ int stepValue = holder.steps[stepIndex];
+
+ // Get material properties
+ Material stepMat;
+ StringBuilder stepName = new StringBuilder("\u00A7");
+ List stepLore;
+ Consumer clickEvent;
+ if(stepValue == step){
+ stepMat = Material.GREEN_STAINED_GLASS_PANE;
+ stepName.append('a');
+ stepLore = Collections.singletonList("\u00A77Value is changing by a step of "+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);
+ clickEvent = updateStepValue(stepValue);
+ }
+ stepName.append("Step of: ").append(stepValue);
+
+ // Create item stack then gui item
+ ItemStack item = new ItemStack(stepMat);
+ ItemMeta meta = item.getItemMeta();
+
+ meta.setDisplayName(stepName.toString());
+ meta.setLore(stepLore);
+ item.setItemMeta(meta);
+
+ return new GuiItem(item, clickEvent, CustomAnvil.instance);
+ }
+
+ protected Consumer updateStepValue(int stepValue){
+ return event -> {
+ event.setCancelled(true);
+ this.step = stepValue;
+ updateStepValue();
+ updateValueDisplay();
+ update();
+ };
+ }
+
@Override
public boolean onSave() {
if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
From 065a3a13d8dc7a631c58e7123913633df92118bd Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 06:29:08 +0100
Subject: [PATCH 030/691] Add int value item.
---
.../cuanvil/gui/config/BasicConfigGui.java | 7 ++--
.../gui/config/settings/IntSettingsGui.java | 2 +-
.../cuanvil/gui/utils/GuiGlobalItems.java | 33 ++++++++++++++++---
.../cuanvil/command/ReloadExecutor.kt | 11 +++++--
4 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 46f2298..1b2fd1e 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -5,11 +5,9 @@ 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.inventory.ItemStack;
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.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
@@ -46,9 +44,8 @@ public class BasicConfigGui extends ValueUpdatableGui {
@Override
public void updateGuiValues() {
// Update item with value
- ItemStack setting1Item = new ItemStack(Material.STONE);
- AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,255,2,1, 5, 10, 50, 100);
- GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
+ IntSettingsGui.IntSettingFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,255,2,1, 5, 10, 50, 100);
+ GuiItem setting1 = GuiGlobalItems.intSettingGuiItem(factory1, Material.COMMAND_BLOCK);
pane.bindItem('1', setting1);
BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", ConfigHolder.DEFAULT_CONFIG, false);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index da60bf0..6f4cf97 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -179,7 +179,7 @@ public class IntSettingsGui extends AbstractSettingGui{
return true;
}
- public static SettingGuiFactory factory(@NotNull String title, ValueUpdatableGui parent,
+ public static IntSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
return new IntSettingFactory(
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
index 3e07848..44f0b86 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
@@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull;
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 java.util.Collections;
@@ -80,6 +81,7 @@ public class GuiGlobalItems {
}
private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
+
public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory
){
@@ -87,21 +89,42 @@ public class GuiGlobalItems {
boolean value = factory.getConfiguredValue();
Material itemMat;
- StringBuilder itemName = new StringBuilder("\u00A7");
+ StringBuilder partOfItemName = new StringBuilder("\u00A7");
if(value){
itemMat = Material.GREEN_TERRACOTTA;
- itemName.append("a");
+ partOfItemName.append("a");
}else{
itemMat = Material.RED_TERRACOTTA;
- itemName.append("c");
+ partOfItemName.append("c");
}
- itemName.append(getConfigNameFromPath(factory.getConfigPath()));
+ return createGuiItemFromProperties(factory, itemMat, partOfItemName, value);
+ }
+
+
+ public static GuiItem intSettingGuiItem(
+ @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);
+ }
+
+ private static GuiItem createGuiItemFromProperties(
+ @NotNull AbstractSettingGui.SettingGuiFactory factory,
+ @NotNull Material itemMat,
+ @NotNull StringBuilder partOfItemName,
+ @NotNull Object value
+ ){
+ partOfItemName.append(getConfigNameFromPath(factory.getConfigPath()));
// Create item
ItemStack item = new ItemStack(itemMat);
ItemMeta itemMeta = item.getItemMeta();
- itemMeta.setDisplayName(itemName.toString());
+ itemMeta.setDisplayName(partOfItemName.toString());
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
item.setItemMeta(itemMeta);
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index 7819b6a..2d6e506 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -5,6 +5,7 @@ import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import xyz.alexcrea.cuanvil.config.ConfigHolder
+import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui
class ReloadExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean {
@@ -30,11 +31,15 @@ class ReloadExecutor : CommandExecutor {
* Execute the command, return true if success or false otherwise
*/
private fun commandBody(hardfail: Boolean): Boolean{
- return try {
- ConfigHolder.reloadAllFromDisk(hardfail)
+ try {
+ if(!ConfigHolder.reloadAllFromDisk(hardfail)) return false;
+
+ // Then update all global gui containing value from config
+ BasicConfigGui.INSTANCE.updateGuiValues();
+ return true;
}catch (e: Exception){
e.printStackTrace()
- false
+ return false
}
}
}
From 321a2b1cc8521062b4641cfdf383f7eddfd35e18 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 15:09:06 +0100
Subject: [PATCH 031/691] add/change item of basic config menu.
---
.../alexcrea/cuanvil/gui/MainConfigGui.java | 44 +++++--
.../cuanvil/gui/config/BasicConfigGui.java | 114 ++++++++++++++++--
.../cuanvil/gui/utils/GuiGlobalItems.java | 4 +-
.../io/delilaheve/util/ConfigOptions.kt | 43 ++++---
.../cuanvil/command/ReloadExecutor.kt | 6 +-
5 files changed, 165 insertions(+), 46 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index d11e1ca..c70f39c 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -9,8 +9,11 @@ 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 java.util.Collections;
+
public class MainConfigGui extends ChestGui {
public final static MainConfigGui INSTANCE = new MainConfigGui();
@@ -19,14 +22,14 @@ public class MainConfigGui extends ChestGui {
INSTANCE.init();
}
private MainConfigGui() {
- super(3, "\u00A7cAnvil Config", CustomAnvil.instance);
+ super(3, "\u00A78Anvil Config", CustomAnvil.instance);
}
private void init(){
Pattern pattern = new Pattern(
"000000000",
- "001234500",
+ "012304560",
"Q00000000"
);
PatternPane pane = new PatternPane(0, 0, 9, 3, pattern);
@@ -34,20 +37,35 @@ public class MainConfigGui extends ChestGui {
GuiGlobalItems.addBackgroundItem(pane);
+ // Basic config item
+ ItemStack basicConfigItemstack = new ItemStack(Material.COMMAND_BLOCK);
+ ItemMeta basicConfigMeta = basicConfigItemstack.getItemMeta();
- ItemStack stonePlaceholder = new ItemStack(Material.STONE);
- GuiItem placeholder1 = GuiGlobalItems.toGuiItem(stonePlaceholder, BasicConfigGui.INSTANCE);
- GuiItem placeholder2 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
- GuiItem placeholder3 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
- GuiItem placeholder4 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
- GuiItem placeholder5 = new GuiItem(stonePlaceholder,CustomAnvil.instance);
-
+ basicConfigMeta.setDisplayName("\u00A7aBasic Config Menu");
+ 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);
- pane.bindItem('2', placeholder2);
- pane.bindItem('3', placeholder3);
- pane.bindItem('4', placeholder4);
- pane.bindItem('5', placeholder5);
+
+ // WIP configuration items
+ ItemStack wipItemstack = new ItemStack(Material.BARRIER);
+ ItemMeta wipMeta = wipItemstack.getItemMeta();
+ 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);
// quit item
ItemStack quitItemstack = new ItemStack(Material.BARRIER);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 1b2fd1e..e5a07d4 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -4,14 +4,21 @@ 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 io.delilaheve.util.ConfigOptions;
+import kotlin.ranges.IntRange;
import org.bukkit.Material;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
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.BoolSettingsGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
+import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
+import java.util.Collections;
+
public class BasicConfigGui extends ValueUpdatableGui {
public final static BasicConfigGui INSTANCE = new BasicConfigGui();
@@ -21,7 +28,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
}
private BasicConfigGui(){
- super(3, "\u00A7cBasic Config GUI", CustomAnvil.instance);
+ super(3, "\u00A78Basic Config GUI", CustomAnvil.instance);
}
@@ -29,7 +36,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
private void init(){
Pattern pattern = new Pattern(
"000000000",
- "012000000",
+ "012345670",
"B00000000"
);
pane = new PatternPane(0, 0, 9, 3, pattern);
@@ -38,19 +45,108 @@ public class BasicConfigGui extends ValueUpdatableGui {
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
GuiGlobalItems.addBackgroundItem(pane);
+ prepareValues();
updateGuiValues();
}
+ private BoolSettingsGui.BoolSettingFactory limitRepairFactory;
+ private IntSettingsGui.IntSettingFactory repairCostFactory;
+ private GuiItem notNeededLimitValueItem;
+ private BoolSettingsGui.BoolSettingFactory removeRepairLimit;
+ private IntSettingsGui.IntSettingFactory itemRepairCost;
+ private IntSettingsGui.IntSettingFactory unitRepairCost;
+ private IntSettingsGui.IntSettingFactory itemRenameCost;
+ private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost;
+
+ protected void prepareValues(){
+ // limit repair item
+ this.limitRepairFactory = BoolSettingsGui.factory("\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,
+ ConfigOptions.LIMIT_REPAIR_VALUE, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
+ ConfigOptions.DEFAULT_LIMIT_REPAIR_VALUE,
+ 1, 5, 10, 50, 100);
+
+ // rename cost not needed
+ ItemStack item = new ItemStack(Material.BARRIER);
+ ItemMeta meta = item.getItemMeta();
+
+ meta.setDisplayName("\u00A7cRepair Cost Limit");
+ meta.setLore(Collections.singletonList("\u00A77Please, enable repair cost limit for this variable to be editable."));
+ item.setItemMeta(meta);
+ this.notNeededLimitValueItem = new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+
+ // remove repair limit item
+ this.removeRepairLimit = BoolSettingsGui.factory("\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,
+ ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
+ ConfigOptions.DEFAULT_ITEM_REPAIR_COST,
+ 1, 5, 10);
+
+ // unit repair cost
+ this.unitRepairCost = IntSettingsGui.factory("\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,
+ 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,
+ ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
+ ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST,
+ 1, 5, 10, 50, 100);
+
+
+ }
+
@Override
public void updateGuiValues() {
- // Update item with value
- IntSettingsGui.IntSettingFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,255,2,1, 5, 10, 50, 100);
- GuiItem setting1 = GuiGlobalItems.intSettingGuiItem(factory1, Material.COMMAND_BLOCK);
- pane.bindItem('1', setting1);
+ // limit repair item
+ GuiItem limitRepairItem = GuiGlobalItems.boolSettingGuiItem(this.limitRepairFactory);
+ pane.bindItem('1', limitRepairItem);
- BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", ConfigHolder.DEFAULT_CONFIG, false);
- GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
- pane.bindItem('2', setting2);
+ // rename cost item
+ GuiItem limitRepairValueItem;
+ if(this.limitRepairFactory.getConfiguredValue()){
+ limitRepairValueItem = GuiGlobalItems.intSettingGuiItem(this.repairCostFactory, Material.EXPERIENCE_BOTTLE);
+ }else{
+ limitRepairValueItem = this.notNeededLimitValueItem;
+ }
+ pane.bindItem('2', limitRepairValueItem);
+
+ // remove repair limit item
+ GuiItem removeRepairLimitItem = GuiGlobalItems.boolSettingGuiItem(this.removeRepairLimit);
+ pane.bindItem('3', removeRepairLimitItem);
+
+ // item repair cost
+ GuiItem itemRepairCostItem = GuiGlobalItems.intSettingGuiItem(this.itemRepairCost, Material.ANVIL);
+ pane.bindItem('4', itemRepairCostItem);
+
+ // unit repair cost
+ GuiItem unitRepairCostItem = GuiGlobalItems.intSettingGuiItem(this.unitRepairCost, Material.DIAMOND);
+ pane.bindItem('5', unitRepairCostItem);
+
+ // item rename cost
+ GuiItem itemRenameCost = GuiGlobalItems.intSettingGuiItem(this.itemRenameCost, Material.NAME_TAG);
+ pane.bindItem('6', itemRenameCost);
+
+ // sacrifice illegal enchant cost
+ GuiItem illegalCostItem = GuiGlobalItems.intSettingGuiItem(this.sacrificeIllegalEnchantCost, Material.ENCHANTED_BOOK);
+ pane.bindItem('7', illegalCostItem);
update();
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
index 44f0b86..e15fbbf 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
@@ -19,7 +19,7 @@ import java.util.Collections;
public class GuiGlobalItems {
// return
- public static GuiItem toGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
+ public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
}
@@ -33,7 +33,7 @@ public class GuiGlobalItems {
}
public static GuiItem backItem(@NotNull Gui goal){
// simple go back item
- return toGuiItem(BACK_ITEM, goal);
+ return goToGuiItem(BACK_ITEM, goal);
}
public static void addBackItem(@NotNull PatternPane target,
@NotNull Gui goal){
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index 2761d4d..88846ee 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -13,19 +13,19 @@ object ConfigOptions {
// Path for default enchantment limits
private const val DEFAULT_LIMIT_PATH = "default_limit"
// Path for limiting repair cost
- private const val LIMIT_REPAIR_COST = "limit_repair_cost"
+ const val LIMIT_REPAIR_COST = "limit_repair_cost"
// Path for repair value limit
- private const val LIMIT_REPAIR_VALUE = "limit_repair_value"
+ const val LIMIT_REPAIR_VALUE = "limit_repair_value"
// Path for level cost on item repair
- private const val ITEM_REPAIR_COST = "item_repair_cost"
+ const val ITEM_REPAIR_COST = "item_repair_cost"
// Path for level cost on unit repair
- private const val UNIT_REPAIR_COST = "unit_repair_cost"
+ const val UNIT_REPAIR_COST = "unit_repair_cost"
// Path for level cost on item renaming
- private const val ITEM_RENAME_COST = "item_rename_cost"
+ const val ITEM_RENAME_COST = "item_rename_cost"
// Path for level cost on illegal enchantment on sacrifice
- private const val SACRIFICE_ILLEGAL_COST = "sacrifice_illegal_enchant_cost"
+ const val SACRIFICE_ILLEGAL_COST = "sacrifice_illegal_enchant_cost"
// Path for removing repair cost limits
- private const val REMOVE_REPAIR_LIMIT = "remove_repair_limit"
+ const val REMOVE_REPAIR_LIMIT = "remove_repair_limit"
// Root path for enchantment limits
const val ENCHANT_LIMIT_ROOT = "enchant_limits"
// Root path for enchantment values
@@ -39,29 +39,34 @@ object ConfigOptions {
// Default value for enchantment limits
private const val DEFAULT_ENCHANT_LIMIT = 5
// Default value for limiting repair cost
- private const val DEFAULT_LIMIT_REPAIR = true
+ const val DEFAULT_LIMIT_REPAIR = true
// Default value for repair cost limit
- private const val DEFAULT_LIMIT_REPAIR_VALUE = 39
+ const val DEFAULT_LIMIT_REPAIR_VALUE = 39
// Default value for level cost on item repair
- private const val DEFAULT_ITEM_REPAIR_COST = 2
+ const val DEFAULT_ITEM_REPAIR_COST = 2
// Default value for level cost per unit repair
- private const val DEFAULT_UNIT_REPAIR_COST = 1
+ const val DEFAULT_UNIT_REPAIR_COST = 1
// Default value for level cost on item renaming
- private const val DEFAULT_ITEM_RENAME_COST = 1
+ const val DEFAULT_ITEM_RENAME_COST = 1
// Default value for level cost on illegal enchantment on sacrifice
- private const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1
+ const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1
// Valid range for repair cost limit
- private val REPAIR_LIMIT_RANGE = 1..39
+ @JvmField
+ val REPAIR_LIMIT_RANGE = 1..39
// Valid range for repair cost
- private val REPAIR_COST_RANGE = 0..255
+ @JvmField
+ val REPAIR_COST_RANGE = 0..255
// Valid range for rename cost
- private val ITEM_RENAME_COST_RANGE = 0..255
+ @JvmField
+ val ITEM_RENAME_COST_RANGE = 0..255
// Valid range for illegal enchantment conflict cost
- private val SACRIFICE_ILLEGAL_COST_RANGE = 0..255
+ @JvmField
+ val SACRIFICE_ILLEGAL_COST_RANGE = 0..255
// Default for removing repair cost limits
- private const val DEFAULT_REMOVE_LIMIT = false
+ const val DEFAULT_REMOVE_LIMIT = false
// Valid range for an enchantment limit
- private val ENCHANT_LIMIT_RANGE = 1..255
+ @JvmField
+ val ENCHANT_LIMIT_RANGE = 1..255
// Default value for an enchantment multiplier
private const val DEFAULT_ENCHANT_VALUE = 0
// Default value for debug logging
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index 2d6e506..7315c03 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -32,11 +32,11 @@ class ReloadExecutor : CommandExecutor {
*/
private fun commandBody(hardfail: Boolean): Boolean{
try {
- if(!ConfigHolder.reloadAllFromDisk(hardfail)) return false;
+ if(!ConfigHolder.reloadAllFromDisk(hardfail)) return false
// Then update all global gui containing value from config
- BasicConfigGui.INSTANCE.updateGuiValues();
- return true;
+ BasicConfigGui.INSTANCE.updateGuiValues()
+ return true
}catch (e: Exception){
e.printStackTrace()
return false
From 6e0d1037bc3e4bf4cc73d46f3e26f0f033440fb9 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 15:31:28 +0100
Subject: [PATCH 032/691] add config menu/command name & permission. also edit
version number.
---
README.md | 2 ++
build.gradle.kts | 2 +-
.../cuanvil/gui/utils/GuiGlobalActions.java | 35 ++++++++++++++++---
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 8 +++--
...{TestExecutor.kt => EditConfigExecutor.kt} | 9 +++--
src/main/resources/plugin.yml | 12 +++++--
6 files changed, 54 insertions(+), 14 deletions(-)
rename src/main/kotlin/xyz/alexcrea/cuanvil/command/{TestExecutor.kt => EditConfigExecutor.kt} (61%)
diff --git a/README.md b/README.md
index 47941b8..4dde28b 100644
--- a/README.md
+++ b/README.md
@@ -30,10 +30,12 @@ ca.affected: Player with this permission will be affected by the plugin
ca.bypass.fuse: Allow player to combine every enchantments to every item (no custom limit)
ca.bypass.level: Allow player to bypass every level limit (no custom limit)
ca.command.reload: Allow administrator to reload the plugin's configs
+ca.config.edit: Allow administrator to reload the plugin's configs in game
```
### Commands
```yml
anvilconfigreload or carl: Reload every config of this plugin
+customanvilconfig: open a menu for administrator to edit plugin's config in game
```
---
### Default Configuration:
diff --git a/build.gradle.kts b/build.gradle.kts
index e0e1a4b..5f6dcb5 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.3"
+version = "1.3.0-alpha-1"
repositories {
mavenCentral()
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
index 69989ed..b641c50 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
@@ -1,6 +1,8 @@
package xyz.alexcrea.cuanvil.gui.utils;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
+import io.delilaheve.CustomAnvil;
+import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
@@ -12,7 +14,8 @@ import java.util.function.Consumer;
public class GuiGlobalActions {
- public static Consumer stayInPlace = (event) -> event.setCancelled(true);
+ public final static String NO_EDIT_PERM = "§cYou do not have permission to edit the config";
+ public final static Consumer stayInPlace = (event) -> event.setCancelled(true);
public static @NotNull Consumer openGuiAction(
@@ -21,11 +24,18 @@ public class GuiGlobalActions {
@NotNull Object... args){
return event -> {
event.setCancelled(true);
+ HumanEntity player = event.getWhoClicked();
+ // Do not allow to open inventory if player do not have edit configuration permission
+ if(!player.hasPermission(CustomAnvil.editConfigPermission)) {
+ player.closeInventory();
+ player.sendMessage(NO_EDIT_PERM);
+ return;
+ }
try {
Constructor extends Gui> constructor = clazz.getConstructor(argClass);
// Assume constructor is accessible
Gui gui = constructor.newInstance(args);
- gui.show(event.getWhoClicked());
+ gui.show(player);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException |
InstantiationException e) {
@@ -49,8 +59,15 @@ public class GuiGlobalActions {
public static @NotNull Consumer openGuiAction(@NotNull Gui goal) {
return event -> {
+ HumanEntity player = event.getWhoClicked();
+ // Do not allow to open inventory if player do not have edit configuration permission
+ if(!player.hasPermission(CustomAnvil.editConfigPermission)) {
+ player.closeInventory();
+ player.sendMessage(NO_EDIT_PERM);
+ return;
+ }
event.setCancelled(true);
- goal.show(event.getWhoClicked());
+ goal.show(player);
};
}
@@ -59,14 +76,22 @@ public class GuiGlobalActions {
@NotNull ValueUpdatableGui goal) {
return event -> {
event.setCancelled(true);
+ HumanEntity player = event.getWhoClicked();
+ // Do not allow to save configuration if player do not have edit configuration permission
+ if(!player.hasPermission(CustomAnvil.editConfigPermission)) {
+ player.closeInventory();
+ player.sendMessage(NO_EDIT_PERM);
+ return;
+ }
+
// Save setting
if(!setting.onSave()){
- event.getWhoClicked().sendMessage("\u00A7cSomething wrong happen while saving the change of value.");
+ player.sendMessage("\u00A7cSomething wrong happen while saving the change of value.");
}
// Update gui for the one who have it open
goal.updateGuiValues();
// Then show
- goal.show(event.getWhoClicked());
+ goal.show(player);
};
}
}
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index 9b6bdf5..4172392 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -5,7 +5,7 @@ import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.cuanvil.command.ReloadExecutor
-import xyz.alexcrea.cuanvil.command.TestExecutor
+import xyz.alexcrea.cuanvil.command.EditConfigExecutor
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.util.Metrics
import xyz.alexcrea.cuanvil.util.MetricsUtil
@@ -30,11 +30,13 @@ class CustomAnvil : JavaPlugin() {
const val bypassLevelPermission = "ca.bypass.level"
// Permission string required to reload the config
const val commandReloadPermission = "ca.command.reload"
+ // Permission string required to edit the plugin's config
+ const val editConfigPermission = "ca.config.edit"
// Command Name to reload the config
const val commandReloadName = "anvilconfigreload"
// Test command name
- const val commandTestName = "test"
+ const val commandTestName = "customanvilconfig"
// Current plugin instance
lateinit var instance: CustomAnvil
@@ -113,7 +115,7 @@ class CustomAnvil : JavaPlugin() {
command?.setExecutor(ReloadExecutor())
command = getCommand(commandTestName)
- command?.setExecutor(TestExecutor())
+ command?.setExecutor(EditConfigExecutor())
}
}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
similarity index 61%
rename from src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
rename to src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
index fbe1506..604911f 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/TestExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
@@ -1,15 +1,20 @@
package xyz.alexcrea.cuanvil.command
+import io.delilaheve.CustomAnvil
import org.bukkit.command.Command
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
-class TestExecutor : CommandExecutor {
+class EditConfigExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean {
-
+ if(!sender.hasPermission(CustomAnvil.editConfigPermission)) {
+ sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM)
+ return false
+ }
if(sender !is HumanEntity) return false
MainConfigGui.INSTANCE.show(sender)
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 773da9b..daa728f 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
-version: 1.3
+version: 1.3.0-alpha-1
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
@@ -18,8 +18,11 @@ commands:
#- acreload # anvil config reload
#- careload # custom anvil reload
- carl # custom anvil reload
- test:
- description: test cmd
+ customanvilconfig:
+ description: open a menu for administrator to edit plugin's config in game
+ permission: ca.config.edit
+ aliases:
+ - configanvil
permissions:
ca.affected:
@@ -34,6 +37,9 @@ permissions:
ca.command.reload:
default: op
description: Allow administrator to reload the plugin's configs
+ ca.config.edit:
+ default: op
+ description: Allow administrator to reload the plugin's configs in game
# soft depend on old name so I can disable it if it is on the same server
# as it is the old name for this plugin
From 75e75d92e6d2cb5fa69f3ebee7cfd4705b04857e Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 21:09:05 +0100
Subject: [PATCH 033/691] save buton now only display when change happen.
---
settings.gradle.kts | 2 +-
.../gui/config/settings/AbstractSettingGui.java | 16 +++++++++++++++-
.../gui/config/settings/BoolSettingsGui.java | 9 ++++++++-
.../gui/config/settings/IntSettingsGui.java | 15 +++++++++++----
.../cuanvil/gui/utils/GuiGlobalItems.java | 16 +++++++++++++++-
5 files changed, 50 insertions(+), 8 deletions(-)
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 6ffcb1a..03fe07e 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -1,2 +1,2 @@
-rootProject.name = "UnsafeEnchants"
+rootProject.name = "CustomAnvil"
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index 9ca931a..68ab60d 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.adventuresupport.StringHolder;
import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder;
+import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
@@ -33,6 +34,8 @@ public abstract class AbstractSettingGui extends ChestGui {
this(rows, StringHolder.of(title), parent);
}
+ protected GuiItem saveItem;
+ protected GuiItem noChangeItem;
private void initBase(ValueUpdatableGui parent){
Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
@@ -41,8 +44,17 @@ public abstract class AbstractSettingGui extends ChestGui {
GuiGlobalItems.addBackItem(pane, parent);
GuiGlobalItems.addBackgroundItem(pane);
- pane.bindItem('S', GuiGlobalItems.saveItem(this, parent));
+ saveItem = GuiGlobalItems.saveItem(this, parent);
+ noChangeItem = GuiGlobalItems.noChangeItem();
+ pane.bindItem('S', noChangeItem);
+
+ }
+
+ @Override
+ public void update() {
+ pane.bindItem('S', hadChange() ? saveItem : noChangeItem);
+ super.update();
}
protected PatternPane getPane() {
@@ -54,6 +66,8 @@ public abstract class AbstractSettingGui extends ChestGui {
public abstract boolean onSave();
+ public abstract boolean hadChange();
+
public abstract static class SettingGuiFactory{
protected String configPath;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 677e551..d50ba63 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -17,11 +17,13 @@ import java.util.function.Consumer;
public class BoolSettingsGui extends AbstractSettingGui{
private final BoolSettingFactory holder;
+ private final boolean before;
private boolean now;
private BoolSettingsGui(BoolSettingFactory holder, boolean now) {
super(3, holder.title, holder.parent);
this.holder = holder;
+ this.before = now;
this.now = now;
updateValueDisplay();
@@ -33,7 +35,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
return new Pattern(
"000000000",
"00-0v0+00",
- "B0000000S"
+ "BD000000S"
);
}
@@ -81,6 +83,11 @@ public class BoolSettingsGui extends AbstractSettingGui{
return true;
}
+ @Override
+ public boolean hadChange() {
+ return now != before;
+ }
+
public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
boolean defaultVal){
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 6f4cf97..a0080ea 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -21,6 +21,7 @@ import java.util.function.Consumer;
public class IntSettingsGui extends AbstractSettingGui{
private final IntSettingFactory holder;
+ private final int before;
private int now;
private int step;
@@ -28,6 +29,7 @@ public class IntSettingsGui extends AbstractSettingGui{
super(3, holder.title, holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder;
+ this.before =now;
this.now = now;
this.step = holder.steps[0];
@@ -41,7 +43,7 @@ public class IntSettingsGui extends AbstractSettingGui{
return new Pattern(
"abcdefghi",
"00-0v0+00",
- "B0000000S"
+ "BD000000S"
);
}
@@ -55,7 +57,7 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.max(holder.min, now - step);
ItemStack item = new ItemStack(Material.RED_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
- meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)");
+ meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
@@ -72,7 +74,7 @@ public class IntSettingsGui extends AbstractSettingGui{
int planned = Math.min(holder.max, now + step);
ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA);
ItemMeta meta = item.getItemMeta();
- meta.setDisplayName("\u00A7e"+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)");
+ meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)");
meta.setLore(AbstractSettingGui.CLICK_LORE);
item.setItemMeta(meta);
@@ -85,7 +87,7 @@ public class IntSettingsGui extends AbstractSettingGui{
// "result" display
ItemStack resultPaper = new ItemStack(Material.PAPER);
ItemMeta resultMeta = resultPaper.getItemMeta();
- resultMeta.setDisplayName("\u00A7e"+now);
+ resultMeta.setDisplayName("\u00A7eValue: "+now);
resultPaper.setItemMeta(resultMeta);
GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
@@ -179,6 +181,11 @@ public class IntSettingsGui extends AbstractSettingGui{
return true;
}
+ @Override
+ public boolean hadChange() {
+ return now != before;
+ }
+
public static IntSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
index e15fbbf..8b68eb8 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
@@ -59,7 +59,8 @@ public class GuiGlobalItems {
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
}
- private static final Material DEFAULT_SAVE_ITEM = Material.LIME_TERRACOTTA;
+ private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE;
+ private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE;
public static GuiItem saveItem(
@NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal){
@@ -73,6 +74,19 @@ public class GuiGlobalItems {
CustomAnvil.instance);
}
+ private static final GuiItem NO_CHANGE_ITEM;
+ static {
+ ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM);
+ ItemMeta meta = item.getItemMeta();
+ meta.setDisplayName("\u00A77No change. can't save.");
+ item.setItemMeta(meta);
+ NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+ }
+
+ public static GuiItem noChangeItem(){
+ return NO_CHANGE_ITEM;
+ }
+
public static GuiItem openSettingGuiItem(
@NotNull ItemStack item,
@NotNull AbstractSettingGui.SettingGuiFactory factory
From 432e4fbfbd330f04d3d425eabcc35ff009a497c1 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Fri, 1 Mar 2024 22:08:54 +0100
Subject: [PATCH 034/691] add return to default value item for boolean & int
setting gui
---
.../cuanvil/gui/config/BasicConfigGui.java | 6 ++--
.../gui/config/settings/BoolSettingsGui.java | 31 +++++++++++++++++--
.../gui/config/settings/IntSettingsGui.java | 31 +++++++++++++++++--
3 files changed, 61 insertions(+), 7 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index e5a07d4..5b973f3 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -68,13 +68,13 @@ public class BasicConfigGui extends ValueUpdatableGui {
this.repairCostFactory = IntSettingsGui.factory("\u00A78Repair Cost Limit", this,
ConfigOptions.LIMIT_REPAIR_VALUE, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_LIMIT_REPAIR_VALUE,
- 1, 5, 10, 50, 100);
+ 1, 5, 10);
// rename cost not needed
ItemStack item = new ItemStack(Material.BARRIER);
ItemMeta meta = item.getItemMeta();
- meta.setDisplayName("\u00A7cRepair Cost Limit");
+ meta.setDisplayName("\u00A7cRepair Cost Value");
meta.setLore(Collections.singletonList("\u00A77Please, enable repair cost limit for this variable to be editable."));
item.setItemMeta(meta);
this.notNeededLimitValueItem = new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
@@ -88,7 +88,7 @@ public class BasicConfigGui extends ValueUpdatableGui {
this.itemRepairCost = IntSettingsGui.factory("\u00A78Item repair cost", this,
ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(),
ConfigOptions.DEFAULT_ITEM_REPAIR_COST,
- 1, 5, 10);
+ 1, 5, 10, 50, 100);
// unit repair cost
this.unitRepairCost = IntSettingsGui.factory("\u00A78Unit repair cost", this,
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index d50ba63..d524b78 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -11,7 +11,9 @@ 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 java.util.Collections;
import java.util.function.Consumer;
public class BoolSettingsGui extends AbstractSettingGui{
@@ -26,6 +28,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
this.before = now;
this.now = now;
+ prepareReturnToDefault();
updateValueDisplay();
}
@@ -34,10 +37,25 @@ public class BoolSettingsGui extends AbstractSettingGui{
public Pattern getGuiPattern() {
return new Pattern(
"000000000",
- "00-0v0+00",
- "BD000000S"
+ "D0-0v0+00",
+ "B0000000S"
);
}
+ GuiItem returnToDefault;
+ protected void prepareReturnToDefault(){
+ ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
+ ItemMeta meta = item.getItemMeta();
+
+ meta.setDisplayName("\u00A7eReset to default value");
+ meta.setLore(Collections.singletonList("\u00A77Default value is: "+holder.defaultVal));
+ item.setItemMeta(meta);
+ returnToDefault = new GuiItem(item, event -> {
+ event.setCancelled(true);
+ now = holder.defaultVal;
+ updateValueDisplay();
+ update();
+ }, CustomAnvil.instance);
+ }
protected void updateValueDisplay(){
@@ -63,6 +81,15 @@ public class BoolSettingsGui extends AbstractSettingGui{
pane.bindItem('v', resultItem);
+ // reset to default
+ GuiItem returnToDefault;
+ if(now != holder.defaultVal){
+ returnToDefault = this.returnToDefault;
+ }else{
+ returnToDefault = GuiGlobalItems.backgroundItem();
+ }
+ pane.bindItem('D', returnToDefault);
+
}
protected Consumer inverseNowConsumer(){
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index a0080ea..c723ed1 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -33,6 +33,7 @@ public class IntSettingsGui extends AbstractSettingGui{
this.now = now;
this.step = holder.steps[0];
+ prepareReturnToDefault();
updateValueDisplay();
initStepsValue();
}
@@ -42,11 +43,27 @@ public class IntSettingsGui extends AbstractSettingGui{
public Pattern getGuiPattern() {
return new Pattern(
"abcdefghi",
- "00-0v0+00",
- "BD000000S"
+ "D0-0v0+00",
+ "B0000000S"
);
}
+ GuiItem returnToDefault;
+ protected void prepareReturnToDefault(){
+ ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
+ ItemMeta meta = item.getItemMeta();
+
+ meta.setDisplayName("\u00A7eReset to default value");
+ meta.setLore(Collections.singletonList("\u00A77Default value is: "+holder.defaultVal));
+ item.setItemMeta(meta);
+ returnToDefault = new GuiItem(item, event -> {
+ event.setCancelled(true);
+ now = holder.defaultVal;
+ updateValueDisplay();
+ update();
+ }, CustomAnvil.instance);
+ }
+
protected void updateValueDisplay(){
PatternPane pane = getPane();
@@ -93,6 +110,16 @@ public class IntSettingsGui extends AbstractSettingGui{
pane.bindItem('v', resultItem);
+ // reset to default
+ GuiItem returnToDefault;
+ if(now != holder.defaultVal){
+ returnToDefault = this.returnToDefault;
+ }else{
+ returnToDefault = GuiGlobalItems.backgroundItem();
+ }
+ pane.bindItem('D', returnToDefault);
+
+
}
protected Consumer updateNowConsumer(int planned){
From 8b80403d21ce022e413e19425efa7325912880e9 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sat, 2 Mar 2024 20:25:07 +0100
Subject: [PATCH 035/691] add info item.
---
.../xyz/alexcrea/cuanvil/gui/MainConfigGui.java | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index c70f39c..891e011 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -28,7 +28,7 @@ public class MainConfigGui extends ChestGui {
private void init(){
Pattern pattern = new Pattern(
- "000000000",
+ "I00000000",
"012304560",
"Q00000000"
);
@@ -79,6 +79,17 @@ public class MainConfigGui extends ChestGui {
},CustomAnvil.instance);
pane.bindItem('Q', quitItem);
+ // create & bind "info" item
+ ItemStack infoItemstack = new ItemStack(Material.PAPER);
+ ItemMeta infoMeta = infoItemstack.getItemMeta();
+
+ infoMeta.setDisplayName("\u00A7eThis is a alpha version of the gui !");
+ infoMeta.setLore(Collections.singletonList("\u00A77If you have feedback or idea you can send them to the dev !"));
+ infoItemstack.setItemMeta(infoMeta);
+
+ GuiItem infoItem = new GuiItem(infoItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
+ pane.bindItem('I', infoItem);
+
}
}
From f4e676339dfccf46205107d02303552cc659ca53 Mon Sep 17 00:00:00 2001
From: alexcrea <42614139+alexcrea@users.noreply.github.com>
Date: Sat, 2 Mar 2024 20:49:00 +0100
Subject: [PATCH 036/691] Update README.md
Add config gui in plugin's features.
---
README.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/README.md b/README.md
index 4dde28b..56ccb30 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@ or [on GitHub](https://github.com/alexcrea/CustomAnvil/releases/latest)
- Custom items of unit repairs (repair damaged with unit of "material", for example the repair of diamond sword by diamonds)
- Custom XP cost for every aspect of the anvil
- Permissions to bypass level limit or enchantment restriction.
+- Gui to configure the plugin in game (in alpha: can only edit basic config. next versions will focus on this feature)
---
### Permissions:
```yml
From 3abb1129eec572550b2703682472bb1ac64ed7be Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sat, 2 Mar 2024 20:59:56 +0100
Subject: [PATCH 037/691] Fix minor typo
---
README.md | 2 +-
src/main/resources/plugin.yml | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 56ccb30..4019f00 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@ ca.affected: Player with this permission will be affected by the plugin
ca.bypass.fuse: Allow player to combine every enchantments to every item (no custom limit)
ca.bypass.level: Allow player to bypass every level limit (no custom limit)
ca.command.reload: Allow administrator to reload the plugin's configs
-ca.config.edit: Allow administrator to reload the plugin's configs in game
+ca.config.edit: Allow administrator to edit the plugin's config in game
```
### Commands
```yml
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index daa728f..13eb3a6 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -39,9 +39,9 @@ permissions:
description: Allow administrator to reload the plugin's configs
ca.config.edit:
default: op
- description: Allow administrator to reload the plugin's configs in game
+ description: Allow administrator to edit the plugin's config in game
-# soft depend on old name so I can disable it if it is on the same server
+# soft depend on old name, so I can disable it if it is on the same server
# as it is the old name for this plugin
softdepend:
- UnsafeEnchantsPlus
\ No newline at end of file
From c0762d862e15361d912afa8ade0773b4ff00e557 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 03:06:33 +0100
Subject: [PATCH 038/691] add enchant limit config gui. also fixed some style &
grammar issue
---
.../alexcrea/cuanvil/gui/MainConfigGui.java | 23 ++--
.../cuanvil/gui/config/BasicConfigGui.java | 13 +--
.../gui/config/EnchantLimitConfigGui.java | 104 ++++++++++++++++++
.../config/settings/AbstractSettingGui.java | 2 +-
.../gui/config/settings/BoolSettingsGui.java | 7 +-
.../gui/config/settings/IntSettingsGui.java | 13 ++-
.../gui/{utils => util}/GuiGlobalActions.java | 2 +-
.../gui/{utils => util}/GuiGlobalItems.java | 51 ++++++---
.../xyz/alexcrea/cuanvil/util/StringUtil.java | 21 ++++
.../cuanvil/command/EditConfigExecutor.kt | 2 +-
10 files changed, 200 insertions(+), 38 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
rename src/main/java/xyz/alexcrea/cuanvil/gui/{utils => util}/GuiGlobalActions.java (98%)
rename src/main/java/xyz/alexcrea/cuanvil/gui/{utils => util}/GuiGlobalItems.java (80%)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index 891e011..8dd42a5 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -9,8 +9,9 @@ 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.EnchantLimitConfigGui;
+import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
+import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections;
@@ -45,8 +46,19 @@ 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);
// WIP configuration items
ItemStack wipItemstack = new ItemStack(Material.BARRIER);
@@ -54,14 +66,11 @@ 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);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 5b973f3..d87381a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -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);
}
@@ -85,13 +85,13 @@ public class BasicConfigGui extends ValueUpdatableGui {
// item repair cost
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.DEFAULT_ITEM_REPAIR_COST,
1, 5, 10, 50, 100);
// 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.DEFAULT_UNIT_REPAIR_COST,
1, 5, 10, 50, 100);
@@ -105,12 +105,11 @@ public class BasicConfigGui extends ValueUpdatableGui {
// sacrifice illegal enchant cost
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.DEFAULT_SACRIFICE_ILLEGAL_COST,
1, 5, 10, 50, 100);
-
}
@Override
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
new file mode 100644
index 0000000..69a00c4
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
@@ -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 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();
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
index 68ab60d..4ef2781 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java
@@ -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;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index d524b78..12953e8 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -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;
@@ -139,6 +139,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);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index c723ed1..9fcb5c6 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -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;
@@ -168,12 +168,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);
@@ -239,6 +239,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);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java
similarity index 98%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java
index b641c50..500f975 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalActions.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java
@@ -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;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
similarity index 80%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
index 8b68eb8..aadcde3 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/utils/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
@@ -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;
@@ -97,21 +98,44 @@ public class GuiGlobalItems {
private 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);
}
+
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java b/src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java
new file mode 100644
index 0000000..9a36bdb
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java
@@ -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);
+ }
+
+}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
index 604911f..0d4ebd0 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt
@@ -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 {
From 1e3bde650c4c8813c2cf73689d2dfb45178f549d Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 03:14:34 +0100
Subject: [PATCH 039/691] version up
---
build.gradle.kts | 2 +-
src/main/resources/plugin.yml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 5f6dcb5..53c492e 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.3.0-alpha-1"
+version = "1.3.0-alpha-2"
repositories {
mavenCentral()
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 13eb3a6..1dd9c01 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -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
From cfa029e2ca65d7696d2a3068a7dac05d92268c7f Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 03:36:23 +0100
Subject: [PATCH 040/691] alphabetically order enchantments
---
.../cuanvil/gui/config/EnchantLimitConfigGui.java | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
index 69a00c4..cf90e1d 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
@@ -16,9 +16,7 @@ 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;
+import java.util.*;
public class EnchantLimitConfigGui extends ValueUpdatableGui {
@@ -70,7 +68,10 @@ public class EnchantLimitConfigGui extends ValueUpdatableGui {
protected void prepareValues(){
bookItemFactoryList = new ArrayList<>();
- for (Enchantment enchant : Enchantment.values()) {
+ List 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);
From f8b41dd86cc835284a2fec3a06b016c038c992d6 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 04:10:47 +0100
Subject: [PATCH 041/691] add enchant cost setting gui.
---
.../gui/config/settings/BoolSettingsGui.java | 9 +-
.../settings/EnchantCostSettingsGui.java | 223 ++++++++++++++++++
.../gui/config/settings/IntSettingsGui.java | 29 ++-
3 files changed, 245 insertions(+), 16 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 12953e8..9e04c5a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -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;
@@ -129,9 +129,10 @@ public class BoolSettingsGui extends AbstractSettingGui{
@NotNull String title; ValueUpdatableGui parent;
boolean defaultVal;
- private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigHolder config,
- boolean defaultVal){
+ protected BoolSettingFactory(
+ @NotNull String title, ValueUpdatableGui parent,
+ String configPath, ConfigHolder config,
+ boolean defaultVal){
super(configPath, config);
this.title = title;
this.parent = parent;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
new file mode 100644
index 0000000..28ae717
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
@@ -0,0 +1,223 @@
+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.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.function.Consumer;
+
+public class EnchantCostSettingsGui extends IntSettingsGui {
+
+ protected final static String ITEM_PATH = ".item";
+ protected final static String BOOK_PATH = ".book";
+
+ private int beforeBook = -1;
+ private int nowBook = -1;
+
+ protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem, int nowBook) {
+ super(holder, nowItem);
+
+ this.beforeBook = nowBook;
+ this.nowBook = nowBook;
+
+ this.step = holder.steps[0];
+
+ updateValueDisplay();
+ initStepsValue();
+ }
+
+
+ @Override
+ public Pattern getGuiPattern() {
+ return new Pattern(
+ "abc1bMVP0",
+ "D001s-v+0",
+ "B0010000S"
+ );
+ }
+
+ @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 book value is: " + holder.defaultBookVal,
+ "\u00A77Default item value is: " + holder.defaultVal));
+ 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;
+ if(this.nowBook == -1){
+ nowBook = holder.getConfiguredBookValue();
+ }else{
+ 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 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, now);
+ return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean hadChange() {
+ return super.hadChange() || nowBook != beforeBook;
+ }
+
+ public static EnchantCostSettingFactory factory(@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();
+ // Get value or default
+ int nowBook = getConfiguredValue();
+ // create new gui
+ return new EnchantCostSettingsGui(this, nowItem, nowBook);
+ }
+
+ }
+
+}
+
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 9fcb5c6..c01a9ff 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -20,16 +20,16 @@ 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;
- this.before =now;
+ this.before = now;
this.now = now;
this.step = holder.steps[0];
@@ -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];
@@ -227,9 +231,10 @@ 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,
- String configPath, ConfigHolder config,
- int min, int max, int defaultVal, int... steps){
+ protected IntSettingFactory(
+ @NotNull String title, ValueUpdatableGui parent,
+ String configPath, ConfigHolder config,
+ int min, int max, int defaultVal, int... steps){
super(configPath, config);
this.title = title;
this.parent = parent;
From f7841ce562b3bd2283430fef22fd6e6acbadc18d Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 13:48:51 +0100
Subject: [PATCH 042/691] progress on enchantment cost config gui
---
.../alexcrea/cuanvil/gui/MainConfigGui.java | 14 ++-
.../gui/config/AbstractEnchantConfigGui.java | 105 ++++++++++++++++++
.../cuanvil/gui/config/BasicConfigGui.java | 14 +--
.../gui/config/EnchantCostConfigGui.java | 63 +++++++++++
.../gui/config/EnchantLimitConfigGui.java | 93 +++-------------
.../gui/config/settings/BoolSettingsGui.java | 6 +-
.../settings/EnchantCostSettingsGui.java | 48 ++++++--
.../gui/config/settings/IntSettingsGui.java | 6 +-
.../cuanvil/gui/util/GuiGlobalItems.java | 2 +-
9 files changed, 248 insertions(+), 103 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index 8dd42a5..3e17b56 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -9,6 +9,7 @@ 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.config.EnchantCostConfigGui;
import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
@@ -60,18 +61,27 @@ public class MainConfigGui extends ChestGui {
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);
ItemMeta wipMeta = wipItemstack.getItemMeta();
wipMeta.setDisplayName("\u00A7cWIP");
wipItemstack.setItemMeta(wipMeta);
- 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('3', wip3);
pane.bindItem('4', wip4);
pane.bindItem('5', wip5);
pane.bindItem('6', wip6);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
new file mode 100644
index 0000000..a249be1
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
@@ -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 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 bookItemFactoryList;
+ protected void prepareValues(){
+ bookItemFactoryList = new ArrayList<>();
+
+ List 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);
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index d87381a..8f1d1a5 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -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,32 +80,32 @@ 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);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
new file mode 100644
index 0000000..9a0d58d
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -0,0 +1,63 @@
+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.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 {
+
+ 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.ROOT);
+ String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
+
+ return EnchantCostSettingsGui.enchFactory(prettyKey+" Level Cost", this,
+ SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
+ enchant.getMaxLevel(),4,
+ 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(
+ "\u00A77Book Cost: " + bookCost,
+ "\u00A77Item Cost: " + itemCost));
+
+ item.setItemMeta(itemMeta);
+
+ return GuiGlobalItems.openSettingGuiItem(item, factory);
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
index cf90e1d..bda77b7 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
@@ -1,27 +1,18 @@
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.*;
+import java.util.Locale;
-public class EnchantLimitConfigGui extends ValueUpdatableGui {
+public class EnchantLimitConfigGui extends AbstractEnchantConfigGui {
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();
@@ -29,77 +20,27 @@ public class EnchantLimitConfigGui extends ValueUpdatableGui {
INSTANCE.init();
}
- private EnchantLimitConfigGui(){
- super(6, "\u00A78Enchantment Level Limit", CustomAnvil.instance);
+ private EnchantLimitConfigGui() {
+ super("\u00A78Enchantment Level Limit");
}
- 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 bookItemFactoryList;
- protected void prepareValues(){
- bookItemFactoryList = new ArrayList<>();
-
- List 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);
-
- 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
+ public IntSettingsGui.IntSettingFactory getFactoryFromEnchant(Enchantment enchant) {
+ String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
+ String prettyKey = StringUtil.snakeToUpperSpacedCase(key);
- this.filledEnchant.clear();
+ return IntSettingsGui.intFactory(prettyKey+" Level Limit", this,
+ SECTION_NAME+'.'+key, ConfigHolder.DEFAULT_CONFIG, 0, 255,
+ enchant.getMaxLevel(),
+ 1, 5, 10, 50, 100);
+ }
- for (IntSettingsGui.IntSettingFactory inventoryFactory : this.bookItemFactoryList) {
-
- GuiItem item = GuiGlobalItems.intSettingGuiItem(inventoryFactory,
- Material.ENCHANTED_BOOK,
- inventoryFactory.getTitle());
- this.filledEnchant.addItem(item);
- }
-
- update();
+ @Override
+ public GuiItem getItemFromFactory(IntSettingsGui.IntSettingFactory inventoryFactory) {
+ return GuiGlobalItems.intSettingGuiItem(inventoryFactory,
+ Material.ENCHANTED_BOOK,
+ inventoryFactory.getTitle());
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 9e04c5a..b6369c2 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -115,9 +115,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
return now != before;
}
- public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigHolder config,
- boolean defaultVal){
+ public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent,
+ String configPath, ConfigHolder config,
+ boolean defaultVal){
return new BoolSettingFactory(
title,parent,
configPath, config,
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
index 28ae717..5cf52ad 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
@@ -6,6 +6,7 @@ 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;
@@ -15,6 +16,7 @@ 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 {
@@ -33,20 +35,43 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
this.step = holder.steps[0];
- updateValueDisplay();
- initStepsValue();
+ initStaticItem();
}
-
@Override
public Pattern getGuiPattern() {
return new Pattern(
- "abc1bMVP0",
- "D001s-v+0",
+ "abc12MVP0",
+ "D0013-v+0",
"B0010000S"
);
}
+ private void initStaticItem() {
+ PatternPane pane = getPane();
+
+ // book display
+ ItemStack bookItemstack = new ItemStack(Material.BOOK);
+ ItemMeta bookMeta = bookItemstack.getItemMeta();
+
+ bookMeta.setDisplayName("\u00A7aEnchantment by Book Cost");
+ bookMeta.setLore(Collections.singletonList("\u00A77Value on the right represent cost of enchantment for every level if combined by a book"));
+ bookItemstack.setItemMeta(bookMeta);
+
+ // sword display
+ ItemStack swordItemstack = new ItemStack(Material.WOODEN_SWORD);
+ ItemMeta swordMeta = swordItemstack.getItemMeta();
+ swordMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
+
+ swordMeta.setDisplayName("\u00A7aEnchantment by Sword Cost");
+ swordMeta.setLore(Collections.singletonList("\u00A77Value on the right represent cost of enchantment for every level if combined by an item other than a 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);
@@ -153,9 +178,10 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
@Override
public boolean onSave() {
+ System.out.println("ON SAVE");
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, now);
+ holder.config.getConfig().set(holder.configPath+BOOK_PATH, nowBook);
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
@@ -166,10 +192,10 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
return super.hadChange() || nowBook != beforeBook;
}
- public static EnchantCostSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigHolder config,
- int min, int max, int defaultItemVal, int defaultBookVal,
- int... steps){
+ 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,
@@ -212,7 +238,7 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
// Get value or default
int nowItem = getConfiguredValue();
// Get value or default
- int nowBook = getConfiguredValue();
+ int nowBook = getConfiguredBookValue();
// create new gui
return new EnchantCostSettingsGui(this, nowItem, nowBook);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index c01a9ff..b217838 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -217,9 +217,9 @@ public class IntSettingsGui extends AbstractSettingGui{
return now != before;
}
- public static IntSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
- String configPath, ConfigHolder config,
- int min, int max, int defaultVal, int... steps){
+ public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent,
+ String configPath, ConfigHolder config,
+ int min, int max, int defaultVal, int... steps){
return new IntSettingFactory(
title,parent,
configPath, config,
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
index aadcde3..68df6df 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
@@ -95,7 +95,7 @@ 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,
From 47d8efda78814be6de47eb221103d0d61f166104 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 13:49:29 +0100
Subject: [PATCH 043/691] fix config.yml not saving default when absent
---
src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
index 40d8f82..9374505 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
@@ -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;
From 1fd8501461dabd4110920fb3f6fb1eaf8e9789bd Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 14:02:25 +0100
Subject: [PATCH 044/691] fix nowBook display value
---
.../settings/EnchantCostSettingsGui.java | 30 +++++++++----------
.../gui/config/settings/IntSettingsGui.java | 2 +-
2 files changed, 15 insertions(+), 17 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
index 5cf52ad..efbb906 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
@@ -24,20 +24,26 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
protected final static String ITEM_PATH = ".item";
protected final static String BOOK_PATH = ".book";
- private int beforeBook = -1;
- private int nowBook = -1;
+ private int beforeBook;
+ private int nowBook;
- protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem, int nowBook) {
+ protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) {
super(holder, nowItem);
- this.beforeBook = nowBook;
- this.nowBook = nowBook;
-
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(
@@ -102,12 +108,7 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
// assume holder is an instance of EnchantCostSettingFactory
EnchantCostSettingFactory holder = ((EnchantCostSettingFactory) this.holder);
- int nowBook;
- if(this.nowBook == -1){
- nowBook = holder.getConfiguredBookValue();
- }else{
- nowBook = this.nowBook;
- }
+ int nowBook = this.nowBook;
// minus item
GuiItem minusItem;
@@ -178,7 +179,6 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
@Override
public boolean onSave() {
- System.out.println("ON SAVE");
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);
@@ -237,10 +237,8 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
public AbstractSettingGui create() {
// Get value or default
int nowItem = getConfiguredValue();
- // Get value or default
- int nowBook = getConfiguredBookValue();
// create new gui
- return new EnchantCostSettingsGui(this, nowItem, nowBook);
+ return new EnchantCostSettingsGui(this, nowItem);
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index b217838..9fbd4b9 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -33,9 +33,9 @@ public class IntSettingsGui extends AbstractSettingGui{
this.now = now;
this.step = holder.steps[0];
+ initStepsValue();
prepareReturnToDefault();
updateValueDisplay();
- initStepsValue();
}
From 3dc272ec9772655b17fbd44d1093508c623dfb88 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 17:09:13 +0100
Subject: [PATCH 045/691] add default value for enchantment cost config
---
.../enchant/EnchantmentProperties.java | 57 +++++++++++++++++++
.../cuanvil/enchant/EnchantmentRarity.java | 33 +++++++++++
.../gui/config/EnchantCostConfigGui.java | 12 +++-
3 files changed, 100 insertions(+), 2 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java
new file mode 100644
index 0000000..cb481df
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java
@@ -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;
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java
new file mode 100644
index 0000000..e1ff834
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java
@@ -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;
+ }
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
index 9a0d58d..83980f7 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -6,6 +6,8 @@ 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;
@@ -30,12 +32,18 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui
Date: Sun, 3 Mar 2024 17:17:58 +0100
Subject: [PATCH 046/691] stylised menu
---
.../gui/config/EnchantCostConfigGui.java | 4 ++--
.../settings/EnchantCostSettingsGui.java | 21 +++++++++++--------
2 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
index 83980f7..1f6ee75 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -60,8 +60,8 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui {
event.setCancelled(true);
From 1f05e6ef595deefba2072f0fb21500140c841ae0 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 17:44:34 +0100
Subject: [PATCH 047/691] remove comment-out code & optimise import
---
.../gui/config/AbstractEnchantConfigGui.java | 12 +-------
.../settings/EnchantCostSettingsGui.java | 1 -
.../xyz/alexcrea/cuanvil/util/Metrics.java | 28 ++++++-------------
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 2 +-
.../cuanvil/group/AbstractMaterialGroup.kt | 2 +-
.../alexcrea/cuanvil/group/ExcludeGroup.kt | 1 -
.../alexcrea/cuanvil/group/IncludeGroup.kt | 2 +-
7 files changed, 13 insertions(+), 35 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
index a249be1..c1e57c6 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
@@ -65,14 +65,7 @@ public abstract class AbstractEnchantConfigGui 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);
}
@@ -86,11 +79,8 @@ public abstract class AbstractEnchantConfigGui {
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
index 8f97b94..bd962c2 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
@@ -1,7 +1,7 @@
package xyz.alexcrea.cuanvil.group
import org.bukkit.Material
-import java.util.EnumSet
+import java.util.*
class IncludeGroup(name: String): AbstractMaterialGroup(name) {
override fun createDefaultSet(): EnumSet {
From af892aabe0b14f2a7c5cc0c4cde1f604289149f2 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 18:43:11 +0100
Subject: [PATCH 048/691] make default config more vanilla like by disabling
level limiting.
---
src/main/kotlin/io/delilaheve/util/ConfigOptions.kt | 2 +-
src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt | 2 +-
src/main/resources/config.yml | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
index 88846ee..7708568 100644
--- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
+++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt
@@ -39,7 +39,7 @@ object ConfigOptions {
// Default value for enchantment limits
private const val DEFAULT_ENCHANT_LIMIT = 5
// Default value for limiting repair cost
- const val DEFAULT_LIMIT_REPAIR = true
+ const val DEFAULT_LIMIT_REPAIR = false
// Default value for repair cost limit
const val DEFAULT_LIMIT_REPAIR_VALUE = 39
// Default value for level cost on item repair
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
index 7e56db2..76b0e0b 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
@@ -6,7 +6,7 @@ import org.bukkit.configuration.ConfigurationSection
object MetricsUtil {
- private const val baseConfigHash = -1527723485
+ private const val baseConfigHash = -1592940914
private const val enchantLimitsConfigHash = 781312397
private const val enchantValuesConfigHash = 1072574774
private const val enchantConflictConfigHash = 1406650190
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 005dca3..2de074b 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -1,7 +1,7 @@
# Whether all anvil actions should be capped
#
# If true, all anvil repairs will max out at the value of limit_repair_value
-limit_repair_cost: true
+limit_repair_cost: false
# Value to limit repair costs to when limit_repair_cost is true
#
From 180459a7b9d4c842750cd9b33c75cc9b41cddefa Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 3 Mar 2024 18:43:40 +0100
Subject: [PATCH 049/691] fix metrics
---
.../alexcrea/cuanvil/config/ConfigHolder.java | 7 +++-
.../gui/config/settings/BoolSettingsGui.java | 3 ++
.../settings/EnchantCostSettingsGui.java | 7 +++-
.../gui/config/settings/IntSettingsGui.java | 5 ++-
.../cuanvil/command/ReloadExecutor.kt | 9 ++++
.../xyz/alexcrea/cuanvil/util/MetricsUtil.kt | 42 +++++++++++++------
6 files changed, 56 insertions(+), 17 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
index 9374505..6c25230 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
@@ -6,6 +6,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
import xyz.alexcrea.cuanvil.group.ItemGroupManager;
+import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.io.File;
import java.io.IOException;
@@ -24,7 +25,11 @@ public abstract class ConfigHolder {
CONFLICT_HOLDER = new ConflictConfigHolder();
UNIT_REPAIR_HOLDER = new UnitRepairHolder();
- return reloadAllFromDisk(true);
+ boolean result = reloadAllFromDisk(true);
+ if(result){
+ MetricsUtil.INSTANCE.testIfConfigIsDefault();
+ }
+ return result;
}
public static boolean reloadAllFromDisk(boolean hardfail){
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index b6369c2..793b171 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
+import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Collections;
import java.util.function.Consumer;
@@ -104,6 +105,8 @@ public class BoolSettingsGui extends AbstractSettingGui{
@Override
public boolean onSave() {
holder.config.getConfig().set(holder.configPath, now);
+
+ MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath);
if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
index e0bfe2e..bd70b64 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
@@ -14,6 +14,7 @@ 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 xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Arrays;
import java.util.function.Consumer;
@@ -181,9 +182,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
@Override
public boolean onSave() {
+ holder.config.getConfig().set(holder.configPath+ITEM_PATH, now);
+ holder.config.getConfig().set(holder.configPath+BOOK_PATH, nowBook);
+
+ MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath);
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;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index 9fbd4b9..ef3c7e6 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -13,6 +13,7 @@ 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 xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Collections;
import java.util.List;
@@ -205,8 +206,10 @@ public class IntSettingsGui extends AbstractSettingGui{
@Override
public boolean onSave() {
+ holder.config.getConfig().set(holder.configPath, now);
+
+ MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath);
if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
- holder.config.getConfig().set(holder.configPath, now);
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index 7315c03..fdcae7d 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -6,6 +6,9 @@ import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui
+import xyz.alexcrea.cuanvil.gui.config.EnchantCostConfigGui
+import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui
+import xyz.alexcrea.cuanvil.util.MetricsUtil
class ReloadExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean {
@@ -36,6 +39,12 @@ class ReloadExecutor : CommandExecutor {
// Then update all global gui containing value from config
BasicConfigGui.INSTANCE.updateGuiValues()
+ EnchantCostConfigGui.INSTANCE.updateGuiValues()
+ EnchantLimitConfigGui.INSTANCE.updateGuiValues()
+
+ // & update metric
+ MetricsUtil.testIfConfigIsDefault()
+
return true
}catch (e: Exception){
e.printStackTrace()
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
index 76b0e0b..8d6b246 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt
@@ -3,14 +3,15 @@ package xyz.alexcrea.cuanvil.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions
import org.bukkit.configuration.ConfigurationSection
+import xyz.alexcrea.cuanvil.config.ConfigHolder
object MetricsUtil {
private const val baseConfigHash = -1592940914
- private const val enchantLimitsConfigHash = 781312397
+ private const val enchantLimitsConfigHash = -1014133828
private const val enchantValuesConfigHash = 1072574774
private const val enchantConflictConfigHash = 1406650190
- private const val itemGroupsConfigHash = -1014133828
+ private const val itemGroupsConfigHash = 1406650190
private const val unitRepairItemConfigHash = 536871958
private const val baseConfigPieName = "isDefaultBaseConfig"
private const val enchantLimitsConfigPieName = "isDefaultEnchantLimitsConfig"
@@ -65,22 +66,19 @@ object MetricsUtil {
/**
* Test if the used configuration is the default config
*/
- fun testIfConfigIsDefault(defaultConfig: ConfigurationSection,
- enchantConflictConfig: ConfigurationSection,
- itemGroupsConfig: ConfigurationSection,
- unitRepairItemConfig: ConfigurationSection){
+ fun testIfConfigIsDefault(){
// Calculate hash of config
- val baseConfig = testBaseConfig(defaultConfig)
- val limitEnchantConfig = getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_LIMIT_ROOT)
- val enchantValueConfig = getHashFromKey(defaultConfig, ConfigOptions.ENCHANT_VALUES_ROOT)
- val enchantConflictConfig2 = getConfigurationHash(enchantConflictConfig)
- val itemGroupConfig = getConfigurationHash(itemGroupsConfig)
- val unitRepairConfig = getConfigurationHash(unitRepairItemConfig)
+ val baseConfig = testBaseConfig(ConfigHolder.DEFAULT_CONFIG.config)
+ val limitEnchantConfig = getHashFromKey(ConfigHolder.DEFAULT_CONFIG.config, ConfigOptions.ENCHANT_LIMIT_ROOT)
+ val enchantValueConfig = getHashFromKey(ConfigHolder.DEFAULT_CONFIG.config, ConfigOptions.ENCHANT_VALUES_ROOT)
+ val enchantConflictConfig = getConfigurationHash(ConfigHolder.CONFLICT_HOLDER.config)
+ val itemGroupConfig = getConfigurationHash(ConfigHolder.ITEM_GROUP_HOLDER.config)
+ val unitRepairConfig = getConfigurationHash(ConfigHolder.UNIT_REPAIR_HOLDER.config)
// Test if default
isDefaultBaseConfig = baseConfigHash == baseConfig
isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig
isDefaultEnchantValuesConfig = enchantValuesConfigHash == enchantValueConfig
- isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig2
+ isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig
isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig
isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
// If not default and debug flag active, print the hash.
@@ -95,6 +93,24 @@ object MetricsUtil {
}
+ fun notifyChange(holder: ConfigHolder, path: String){
+ if(ConfigHolder.DEFAULT_CONFIG.equals(holder)){
+ if(path.startsWith(ConfigOptions.ENCHANT_LIMIT_ROOT+".")){
+ isDefaultEnchantLimitsConfig = false;
+ }else if(path.startsWith(ConfigOptions.ENCHANT_VALUES_ROOT+".")){
+ isDefaultEnchantValuesConfig = false;
+ }else{
+ isDefaultBaseConfig = false;
+ }
+ }else if(ConfigHolder.CONFLICT_HOLDER.equals(holder)){
+ isDefaultEnchantConflictConfig = false;
+ }else if(ConfigHolder.ITEM_GROUP_HOLDER.equals(holder)){
+ isDefaultItemGroupsConfig = false;
+ }else if(ConfigHolder.UNIT_REPAIR_HOLDER.equals(holder)){
+ isDefaultUnitRepairItemConfig = false;
+ }
+ }
+
fun addCustomMetric(metric: Metrics) {
metric.addCustomChart(Metrics.SimplePie(baseConfigPieName) {
isDefaultBaseConfig.toString()
From 2a3c085aaa41b78f1266640009b4243b3b6e788e Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 12 Mar 2024 14:45:18 +0100
Subject: [PATCH 050/691] Add javadoc for settings gui & minor change
---
.../gui/config/EnchantCostConfigGui.java | 2 +-
.../config/settings/AbstractSettingGui.java | 65 ++++++++++++++-
.../gui/config/settings/BoolSettingsGui.java | 53 ++++++++++--
.../settings/EnchantCostSettingsGui.java | 79 +++++++++++++++---
.../gui/config/settings/IntSettingsGui.java | 81 ++++++++++++++++++-
5 files changed, 257 insertions(+), 23 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
index 1f6ee75..d0bec63 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -41,7 +41,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui
+ * S: save setting button.
+ * B: "back to previous gui" button.
+ * 0: default background item.
+ *
+ * @return The gui's pattern.
+ */
protected abstract Pattern getGuiPattern();
+ /**
+ * Called when the associated setting need to be saved.
+ * @return true if the save was successful. false otherwise
+ */
public abstract boolean onSave();
+ /**
+ * If this function return true
+ * the gui assume the associated setting can be saved.
+ * @return true if there is a change to the setting. false otherwise
+ */
public abstract boolean hadChange();
-
+ /**
+ * Most of the time a setting gui will be called from a global gui.
+ *
+ * It is better to keep a factory that hold setting data than find what parameters to use every time.
+ */
public abstract static class SettingGuiFactory{
protected String configPath;
protected ConfigHolder config;
+ /**
+ * Constructor for settings gui factory
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ */
protected SettingGuiFactory(String configPath, ConfigHolder config){
this.configPath = configPath;
this.config = config;
}
+ /**
+ * @return Configuration path of this setting.
+ */
public String getConfigPath() {
return configPath;
}
+ /**
+ * @return Configuration holder of this setting.
+ */
public ConfigHolder getConfigHolder() {
return config;
}
+ /**
+ * Create a gui using setting parameters and current setting value.
+ * @return A new instance of the implemented setting gui.
+ */
public abstract AbstractSettingGui create();
}
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
index 793b171..d3c56be 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java
@@ -17,14 +17,22 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Collections;
import java.util.function.Consumer;
+/**
+ * An instance of a gui used to edit a boolean setting.
+ */
public class BoolSettingsGui extends AbstractSettingGui{
private final BoolSettingFactory holder;
private final boolean before;
private boolean now;
+ /**
+ * Create a boolean setting config gui.
+ * @param holder Configuration factory of this setting.
+ * @param now The defined value of this setting.
+ */
protected BoolSettingsGui(BoolSettingFactory holder, boolean now) {
- super(3, holder.title, holder.parent);
+ super(3, holder.getTitle(), holder.parent);
this.holder = holder;
this.before = now;
this.now = now;
@@ -42,7 +50,12 @@ public class BoolSettingsGui extends AbstractSettingGui{
"B0000000S"
);
}
- GuiItem returnToDefault;
+
+ protected GuiItem returnToDefault;
+
+ /**
+ * Prepare "return to default value" gui item.
+ */
protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta();
@@ -58,8 +71,10 @@ public class BoolSettingsGui extends AbstractSettingGui{
}, CustomAnvil.instance);
}
+ /**
+ * Update item using the setting value to match the new value
+ */
protected void updateValueDisplay(){
-
PatternPane pane = getPane();
// Get displayed value for this config.
@@ -93,6 +108,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
}
+ /**
+ * @return A consumer to update the current setting's value.
+ */
protected Consumer inverseNowConsumer(){
return event->{
event.setCancelled(true);
@@ -118,6 +136,15 @@ public class BoolSettingsGui extends AbstractSettingGui{
return now != before;
}
+ /**
+ * Create a bool setting factory from setting's parameters.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param defaultVal Default value if not found on the config.
+ * @return A factory for a boolean setting gui.
+ */
public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
boolean defaultVal){
@@ -127,11 +154,21 @@ public class BoolSettingsGui extends AbstractSettingGui{
defaultVal);
}
-
+ /**
+ * A factory for a boolean setting gui that hold setting's information.
+ */
public static class BoolSettingFactory extends SettingGuiFactory{
@NotNull String title; ValueUpdatableGui parent;
boolean defaultVal;
+ /**
+ * Constructor for a boolean setting gui factory.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param defaultVal Default value if not found on the config.
+ */
protected BoolSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
@@ -143,18 +180,24 @@ public class BoolSettingsGui extends AbstractSettingGui{
this.defaultVal = defaultVal;
}
+ /**
+ * @return Get setting's gui title.
+ */
@NotNull
public String getTitle() {
return title;
}
+ /**
+ * @return The configured value for the associated setting.
+ */
public boolean getConfiguredValue(){
return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
}
@Override
public AbstractSettingGui create() {
- // Get value or default
+ // Get current value or default
boolean now = getConfiguredValue();
// create new gui
return new BoolSettingsGui(this, now);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
index bd70b64..7ecc1e5 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java
@@ -19,6 +19,10 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.Arrays;
import java.util.function.Consumer;
+/**
+ * An instance of a gui used to edit an enchantment cost setting.
+ * May be considered as a 2 int setting.
+ */
public class EnchantCostSettingsGui extends IntSettingsGui {
protected final static String ITEM_PATH = ".item";
@@ -27,6 +31,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
private int beforeBook;
private int nowBook;
+ /**
+ * Create an enchantment cost setting config gui.
+ * @param holder Configuration factory of this setting.
+ * @param nowItem The defined value of this setting item's value.
+ */
protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) {
super(holder, nowItem);
@@ -35,6 +44,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
initStaticItem();
}
+ /**
+ * Initialise step items.
+ * Also bypassed init sequence to initialise books value
+ * as it used as one of the first call from the init sequence of the gui
+ */
@Override
protected void initStepsValue() {
super.initStepsValue();
@@ -53,6 +67,9 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
);
}
+ /**
+ * Initialise item that should not be updated late.
+ */
private void initStaticItem() {
PatternPane pane = getPane();
@@ -166,6 +183,10 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
}
+ /**
+ * @param planned Value to change current book cost setting to.
+ * @return A consumer to update the current book cost setting's value.
+ */
protected Consumer updateNowBookConsumer(int planned){
return event->{
event.setCancelled(true);
@@ -197,21 +218,55 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
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){
+ /**
+ * Create an int setting factory from setting's parameters.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param min Minimum value of this setting.
+ * @param max Maximum value of this setting.
+ * @param defaultItemVal Default item value if not found on the config.
+ * @param defaultBookVal Default book value if not found on the config.
+ * @param steps List of step the value can increment/decrement.
+ * List's size should be between 1 (included) and 3 (included).
+ * it is visually preferable to have an odd number of step.
+ * If step only contain 1 value, no step item should be displayed.
+ * @return A factory for an enchant cost setting gui.
+ */
+ public static EnchantCostSettingFactory enchentCostFactory(
+ @NotNull String title, ValueUpdatableGui parent,
+ String configPath, ConfigHolder config,
+ int min, int max, int defaultItemVal, int defaultBookVal,
+ int... steps){
return new EnchantCostSettingFactory(
title,parent,
configPath, config,
min, max, defaultItemVal, defaultBookVal, steps);
}
-
+ /**
+ * A factory for an enchantment cost setting gui that hold setting's information.
+ */
public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory {
int defaultBookVal;
+ /**
+ * Constructor for an enchantment cost setting gui factory.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param min Minimum value of this setting.
+ * @param max Maximum value of this setting.
+ * @param defaultItemVal Default item value if not found on the config.
+ * @param defaultBookVal Default book value if not found on the config.
+ * @param steps List of step the value can increment/decrement.
+ * List's size should be between 1 (included) and 3 (included).
+ * it is visually preferable to have an odd number of step.
+ * If step only contain 1 value, no step item should be displayed.
+ */
protected EnchantCostSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
@@ -224,16 +279,17 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
this.defaultBookVal = defaultBookVal;
}
- @NotNull
- public String getTitle() {
- return title;
- }
-
+ /**
+ * @return The configured value for the enchant setting item value.
+ */
@Override
public int getConfiguredValue() {
return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal);
}
+ /**
+ * @return The configured value for the enchant setting book value.
+ */
public int getConfiguredBookValue(){
return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal);
}
@@ -248,5 +304,4 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
}
-}
-
+}
\ No newline at end of file
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
index ef3c7e6..f8ef20a 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java
@@ -19,6 +19,9 @@ import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
+/**
+ * An instance of a gui used to edit an int setting.
+ */
public class IntSettingsGui extends AbstractSettingGui{
protected final IntSettingFactory holder;
@@ -26,8 +29,13 @@ public class IntSettingsGui extends AbstractSettingGui{
protected int now;
protected int step;
+ /**
+ * Create an int setting config gui.
+ * @param holder Configuration factory of this setting.
+ * @param now The defined value of this setting.
+ */
protected IntSettingsGui(IntSettingFactory holder, int now) {
- super(3, holder.title, holder.parent);
+ super(3, holder.getTitle(), holder.parent);
assert holder.steps.length > 0 && holder.steps.length <= 9;
this.holder = holder;
this.before = now;
@@ -50,6 +58,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}
protected GuiItem returnToDefault;
+ /**
+ * Prepare "return to default value" gui item.
+ */
protected void prepareReturnToDefault(){
ItemStack item = new ItemStack(Material.COMMAND_BLOCK);
ItemMeta meta = item.getItemMeta();
@@ -65,6 +76,9 @@ public class IntSettingsGui extends AbstractSettingGui{
}, CustomAnvil.instance);
}
+ /**
+ * Update item using the setting value to match the new value.
+ */
protected void updateValueDisplay(){
PatternPane pane = getPane();
@@ -123,6 +137,10 @@ public class IntSettingsGui extends AbstractSettingGui{
}
+ /**
+ * @param planned Value to change current setting to.
+ * @return A consumer to update the current setting's value.
+ */
protected Consumer updateNowConsumer(int planned){
return event->{
event.setCancelled(true);
@@ -132,6 +150,9 @@ public class IntSettingsGui extends AbstractSettingGui{
};
}
+ /**
+ * Initialise step items.
+ */
protected void initStepsValue(){
// Put background glass on the background of 'a' to 'b'
GuiItem background = GuiGlobalItems.backgroundItem();
@@ -143,11 +164,15 @@ public class IntSettingsGui extends AbstractSettingGui{
// Then update legit step values
updateStepValue();
}
+
+ /**
+ * Update steps items value.
+ */
protected void updateStepValue(){
if(holder.steps.length <= 1) return;
// We assume steps have a length of 2k+1 cause its more pretty
char val = getMidStepChar();
- // Offset
+ // Offset to start (not the best way to do it)
val -= (char) ((holder.steps.length-1)/2);
// Then place items
@@ -158,10 +183,19 @@ public class IntSettingsGui extends AbstractSettingGui{
}
+ /**
+ * Step use lower case character from 'a' to a certain char.
+ * @return The middle value of the character set for steps.
+ */
protected char getMidStepChar(){
return 'e';
}
+ /**
+ * Create a step item from a step value.
+ * @param stepIndex the index of the step item.
+ * @return A step item corresponding to its index value.
+ */
protected GuiItem stepGuiItem(int stepIndex){
int stepValue = holder.steps[stepIndex];
@@ -194,6 +228,10 @@ public class IntSettingsGui extends AbstractSettingGui{
return new GuiItem(item, clickEvent, CustomAnvil.instance);
}
+ /**
+ * @param stepValue Value to change current step to.
+ * @return A consumer to update the current step of this setting.
+ */
protected Consumer updateStepValue(int stepValue){
return event -> {
event.setCancelled(true);
@@ -220,6 +258,21 @@ public class IntSettingsGui extends AbstractSettingGui{
return now != before;
}
+ /**
+ * Create an int setting factory from setting's parameters.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param min Minimum value of this setting.
+ * @param max Maximum value of this setting.
+ * @param defaultVal Default value if not found on the config.
+ * @param steps List of step the value can increment/decrement.
+ * List's size should be between 1 (included) and 5 (included).
+ * it is visually preferable to have an odd number of step.
+ * If step only contain 1 value, no step item should be displayed.
+ * @return A factory for an int setting gui.
+ */
public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){
@@ -229,11 +282,27 @@ public class IntSettingsGui extends AbstractSettingGui{
min, max, defaultVal, steps);
}
-
+ /**
+ * A factory for an int setting gui that hold setting's information.
+ */
public static class IntSettingFactory extends SettingGuiFactory{
@NotNull String title; ValueUpdatableGui parent;
int min; int max; int defaultVal; int[] steps;
+ /**
+ * Constructor for an int setting gui factory.
+ * @param title The title of the gui.
+ * @param parent Parent gui to go back when completed.
+ * @param configPath Configuration path of this setting.
+ * @param config Configuration holder of this setting.
+ * @param min Minimum value of this setting.
+ * @param max Maximum value of this setting.
+ * @param defaultVal Default value if not found on the config.
+ * @param steps List of step the value can increment/decrement.
+ * List's size should be between 1 (included) and 5 (included).
+ * it is visually preferable to have an odd number of step.
+ * If step only contain 1 value, no step item should be displayed.
+ */
protected IntSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigHolder config,
@@ -247,11 +316,17 @@ public class IntSettingsGui extends AbstractSettingGui{
this.steps = steps;
}
+ /**
+ * @return Get setting's gui title
+ */
@NotNull
public String getTitle() {
return title;
}
+ /**
+ * @return The configured value for the associated setting.
+ */
public int getConfiguredValue(){
return this.config.getConfig().getInt(this.configPath, this.defaultVal);
}
From 2bb23e75bfaf77d5c2de02929d885b0f04449bb7 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 13 Mar 2024 19:37:07 +0100
Subject: [PATCH 051/691] Add javadoc to global gui
---
.../gui/config/AbstractEnchantConfigGui.java | 51 +++++++++++++++----
.../cuanvil/gui/config/BasicConfigGui.java | 14 ++++-
.../gui/config/EnchantCostConfigGui.java | 6 +++
.../gui/config/EnchantLimitConfigGui.java | 6 +++
4 files changed, 65 insertions(+), 12 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
index c1e57c6..ae04fb2 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
@@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
+import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.Orientable;
import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
import com.github.stefvanschie.inventoryframework.pane.Pane;
@@ -19,16 +20,40 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
+/**
+ * Abstract Global Config gui for enchantment setting configuration.
+ * @param Type of the factory of the type of setting the gui should edit.
+ */
public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui {
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
- protected AbstractEnchantConfigGui(String title){
+ private final Gui backGui;
+
+ /**
+ * Constructor for a gui displaying available enchantment to edit a enchantment setting.
+ * @param title Title of the gui.
+ * @param backGui Gui to go back on click on the "back" button.
+ */
+ protected AbstractEnchantConfigGui(String title, Gui backGui){
super(6, title, CustomAnvil.instance);
+ this.backGui = backGui;
+ }
+ /**
+ * Constructor for a gui displaying available enchantment to edit a enchantment setting.
+ * @param title Title of the gui.
+ */
+ protected AbstractEnchantConfigGui(String title){
+ this(title, MainConfigGui.INSTANCE);
}
- PatternPane backItems;
+ PatternPane backgroundItems;
OutlinePane filledEnchant;
+
+ // Why is called like it is rn
+ /**
+ * Initialise value updatable gui pattern
+ */
protected void init(){
// Back item panel
Pattern pattern = new Pattern(
@@ -39,25 +64,29 @@ public abstract class AbstractEnchantConfigGui bookItemFactoryList;
+
+ /**
+ * Prepare enchantment config gui displayed items factory.
+ */
protected void prepareValues(){
bookItemFactoryList = new ArrayList<>();
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
index 8f1d1a5..7b39d8f 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
@@ -19,6 +19,9 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import java.util.Collections;
+/**
+ * Global config to edit basic basic settings.
+ */
public class BasicConfigGui extends ValueUpdatableGui {
public final static BasicConfigGui INSTANCE = new BasicConfigGui();
@@ -27,12 +30,18 @@ public class BasicConfigGui extends ValueUpdatableGui {
INSTANCE.init();
}
+ /**
+ * Constructor of this Global gui for basic settings.
+ */
private BasicConfigGui(){
super(3, "\u00A78Basic Config", CustomAnvil.instance);
-
}
PatternPane pane;
+
+ /**
+ * Initialise Basic gui
+ */
private void init(){
Pattern pattern = new Pattern(
"000000000",
@@ -58,6 +67,9 @@ public class BasicConfigGui extends ValueUpdatableGui {
private IntSettingsGui.IntSettingFactory itemRenameCost;
private IntSettingsGui.IntSettingFactory sacrificeIllegalEnchantCost;
+ /**
+ * Prepare basic gui displayed items factory and static items..
+ */
protected void prepareValues(){
// limit repair item
this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?",this,
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
index d0bec63..5104c63 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -15,6 +15,9 @@ import xyz.alexcrea.cuanvil.util.StringUtil;
import java.util.Arrays;
import java.util.Locale;
+/**
+ * Global Config gui for enchantment cost settings.
+ */
public class EnchantCostConfigGui extends AbstractEnchantConfigGui {
private final static String SECTION_NAME = "enchant_values";
@@ -25,6 +28,9 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui {
private final static String SECTION_NAME = "enchant_limits";
@@ -20,6 +23,9 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui
Date: Thu, 14 Mar 2024 02:03:54 +0100
Subject: [PATCH 052/691] Add javadoc & refactor of utils class related to
config.
---
.../gui/config/EnchantCostConfigGui.java | 6 +-
.../gui/config/EnchantLimitConfigGui.java | 4 +-
.../settings/EnchantCostSettingsGui.java | 2 +-
.../cuanvil/gui/util/GuiGlobalActions.java | 49 +++++-
.../cuanvil/gui/util/GuiGlobalItems.java | 155 +++++++++++++++---
.../{StringUtil.java => CasedStringUtil.java} | 14 +-
6 files changed, 200 insertions(+), 30 deletions(-)
rename src/main/java/xyz/alexcrea/cuanvil/util/{StringUtil.java => CasedStringUtil.java} (54%)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
index 5104c63..b5a2a50 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
@@ -10,7 +10,7 @@ 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 xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Arrays;
import java.util.Locale;
@@ -39,7 +39,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui stayInPlace = (event) -> event.setCancelled(true);
-
+ /**
+ * Create a consumer to create and open a new GUI.
+ * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
+ * @param clazz The class of the gui to open.
+ * It is assumed this class contain a constructor requiring arguments of argClass in the same order as argClass array.
+ * @param argClass Classes of the argument that will be passed to the constructor of the GUI class.
+ * @param args Arguments for the constructor the GUI class.
+ * @return A consumer to create a new gui and open it.
+ */
public static @NotNull Consumer openGuiAction(
@NotNull Class extends Gui> clazz,
@NotNull Class>[] argClass,
@@ -44,11 +59,24 @@ public class GuiGlobalActions {
};
}
+ /**
+ * Create a consumer to create and open a new GUI.
+ * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
+ * @param clazz The class of the gui to open.
+ * It is assumed this class contain a constructor with no argument.
+ * @return A consumer to create a new gui and open it.
+ */
public static @NotNull Consumer openGuiAction(
@NotNull Class extends Gui> clazz){
return openGuiAction(clazz, new Class>[0]);
}
+ /**
+ * Create a consumer to open a setting gui from a setting GUI factory.
+ * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
+ * @param factory The setting gui factory.
+ * @return A consumer to create and open a new setting GUI.
+ */
public static @NotNull Consumer openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory){
return event -> {
event.setCancelled(true);
@@ -57,6 +85,12 @@ public class GuiGlobalActions {
};
}
+ /**
+ * Create a consumer to open a global GUI.
+ * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
+ * @param goal The gui to open when consumer is run.
+ * @return A consumer to open a global GUI.
+ */
public static @NotNull Consumer openGuiAction(@NotNull Gui goal) {
return event -> {
HumanEntity player = event.getWhoClicked();
@@ -71,6 +105,14 @@ public class GuiGlobalActions {
};
}
+ /**
+ * Create a consumer to update and open an updatable GUI.
+ * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem.
+ * This consumer check if the player who interacted with the item have the permission to save before saving.
+ * @param setting The gui that contain the modified setting.
+ * @param goal The gui to update and open when consumer is run.
+ * @return A consumer to open a global GUI.
+ */
public static @NotNull Consumer saveSettingAction(
@NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal) {
@@ -86,12 +128,13 @@ public class GuiGlobalActions {
// Save setting
if(!setting.onSave()){
- player.sendMessage("\u00A7cSomething wrong happen while saving the change of value.");
+ player.sendMessage("\u00A7cSomething went wrong while saving the change of value.");
}
- // Update gui for the one who have it open
+ // Update gui for those who have it open.
goal.updateGuiValues();
// Then show
goal.show(player);
};
}
+
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
index 68df6df..bc959fb 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java
@@ -12,36 +12,63 @@ 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 xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Collections;
-// maybe use builder patern ?
+/**
+ * A utility class to store function that create generic GUI item.
+ */
public class GuiGlobalItems {
- // return
- public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
- return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
- }
-
- // statically create back itemstack
- private static final ItemStack BACK_ITEM = new ItemStack(Material.BARRIER);
+ // statically create default back itemstack
+ private static final ItemStack BACK_ITEM;
static {
- // todo add what I need to add to the back item
+ BACK_ITEM = new ItemStack(Material.BARRIER);
ItemMeta meta = BACK_ITEM.getItemMeta();
meta.setDisplayName("\u00A7cBack");
BACK_ITEM.setItemMeta(meta);
}
+
+ /**
+ * Create a GuiItem that open the given GUi.
+ * @param item The item to display in the GUI.
+ * @param goal The GUI to open on click.
+ * @return An GuiItem that open goal on click.
+ */
+ public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){
+ return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance);
+ }
+
+ /**
+ * Create back button item from default back GuiItem.
+ * The back item will open the goal inventory when clicked.
+ * @param goal The GUI to go back to.
+ * @return An GuiItem that go back to goal on click.
+ */
public static GuiItem backItem(@NotNull Gui goal){
- // simple go back item
return goToGuiItem(BACK_ITEM, goal);
}
+
+ /**
+ * Add default back item to a GUI pattern with the reserved character key B.
+ * The back item will open the target inventory when clicked.
+ * @param target The pattern to add the back item.
+ * @param goal The GUI to go back to.
+ */
public static void addBackItem(@NotNull PatternPane target,
@NotNull Gui goal){
target.bindItem('B', backItem(goal));
}
private static final Material DEFAULT_BACKGROUND_MAT = Material.LIGHT_GRAY_STAINED_GLASS_PANE;
+
+ /**
+ * Get a background item with backgroundMat as the displayed material.
+ * A background item is a GuiItem that do nothing when interacted with and have an empty name.
+ * @param backgroundMat The material to which the background item should be made of.
+ * @return A background item with backgroundMat as material.
+ */
public static GuiItem backgroundItem(Material backgroundMat){
ItemStack item = new ItemStack(backgroundMat);
ItemMeta meta = item.getItemMeta();
@@ -49,19 +76,46 @@ public class GuiGlobalItems {
item.setItemMeta(meta);
return new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance);
}
+ /**
+ * Get default background GuiItem.
+ * A background item is a GuiItem that do nothing when interacted with and have an empty name.
+ * @return A new instance of the default background item.
+ */
public static GuiItem backgroundItem(){
return backgroundItem(DEFAULT_BACKGROUND_MAT);
}
+
+ /**
+ * Add default background item to a GUI pattern with the reserved character key 0.
+ * A background item is a GuiItem that do nothing when interacted with and have an empty name.
+ * @param target The pattern to add the background item.
+ * @param backgroundMat The material of the background item.
+ */
public static void addBackgroundItem(@NotNull PatternPane target,
- @NotNull Material backgroundMat){
+ @NotNull Material backgroundMat){
target.bindItem('0', backgroundItem(backgroundMat));
}
+
+ /**
+ * Add default background item to a GUI pattern with the reserved character key 0.
+ * A background item is a GuiItem that do nothing when interacted with and have an empty name.
+ * @param target The pattern to add the background item.
+ */
public static void addBackgroundItem(@NotNull PatternPane target){
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
}
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE;
private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE;
+
+ /**
+ * Create a new save setting GuiItem.
+ * A save setting item is a GuiItem that save a changed setting when clicked.
+ * This item also check if the player who interacted with the item have the permission to save before saving.
+ * @param setting The setting to change.
+ * @param goal Parent GUI of this setting GUI. as setting will be change the display of goal GUI will be updated.
+ * @return A save setting item.
+ */
public static GuiItem saveItem(
@NotNull AbstractSettingGui setting,
@NotNull ValueUpdatableGui goal){
@@ -75,6 +129,7 @@ public class GuiGlobalItems {
CustomAnvil.instance);
}
+ // Create static non change item
private static final GuiItem NO_CHANGE_ITEM;
static {
ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM);
@@ -84,10 +139,22 @@ public class GuiGlobalItems {
NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance);
}
+ /**
+ * Get the global "no change" GuiItem.
+ * The no change item do nothing when interacted, only the title is change to show there is no change.
+ * @return The global "no change" item.
+ */
public static GuiItem noChangeItem(){
return NO_CHANGE_ITEM;
}
+ /**
+ * Create a new "create and go to the setting GUI" GuiItem.
+ * This item will create and open a setting GUI from the factory.
+ * @param item The item that will be displayed.
+ * @param factory The setting's GUI factory.
+ * @return A formatted GuiItem that will create and open a GUI for the setting.
+ */
public static GuiItem openSettingGuiItem(
@NotNull ItemStack item,
@NotNull AbstractSettingGui.SettingGuiFactory factory
@@ -95,8 +162,17 @@ public class GuiGlobalItems {
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
}
+ // Prefix of the one line lore that will be added to setting's item.
public static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
+ /**
+ * Create a new Boolean setting GuiItem.
+ * This item will create and open a boolean setting GUI from the factory.
+ * The item will have its value written in the lore part of the item.
+ * @param factory The setting's GUI factory.
+ * @param name Name of the item.
+ * @return A formatted GuiItem that will create and open a GUI for the boolean setting.
+ */
public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory,
@NotNull String name
@@ -118,14 +194,30 @@ public class GuiGlobalItems {
return createGuiItemFromProperties(factory, itemMat, itemName, value);
}
+ /**
+ * Create a new boolean setting GuiItem.
+ * This item will create and open a boolean setting GUI from the factory.
+ * The item will have its value written in the lore part of the item.
+ * Item's name will be the factory set title.
+ * @param factory The setting's GUI factory.
+ * @return A formatted GuiItem that will create and open a GUI for the boolean setting.
+ */
public static GuiItem boolSettingGuiItem(
@NotNull BoolSettingsGui.BoolSettingFactory factory
){
String configPath = getConfigNameFromPath(factory.getConfigPath());
- return boolSettingGuiItem(factory, StringUtil.snakeToUpperSpacedCase(configPath));
+ return boolSettingGuiItem(factory, CasedStringUtil.snakeToUpperSpacedCase(configPath));
}
-
+ /**
+ * Create a new int setting GuiItem.
+ * This item will create and open an int setting GUI from the factory.
+ * The item will have its value written in the lore part of the item.
+ * @param factory The setting's GUI factory.
+ * @param itemMat Displayed material of the item.
+ * @param name Name of the item.
+ * @return A formatted GuiItem that will create and open a GUI for the int setting.
+ */
public static GuiItem intSettingGuiItem(
@NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat,
@@ -138,21 +230,39 @@ public class GuiGlobalItems {
return createGuiItemFromProperties(factory, itemMat, itemName, value);
}
-
+ /**
+ * Create a new int setting GuiItem.
+ * This item will create and open an int setting GUI from the factory.
+ * The item will have its value written in the lore part of the item.
+ * Item's name will be the factory set title.
+ * @param factory The setting's GUI factory.
+ * @param itemMat Displayed material of the item.
+ * @return A formatted GuiItem that will create and open a GUI for the int setting.
+ */
public static GuiItem intSettingGuiItem(
@NotNull IntSettingsGui.IntSettingFactory factory,
@NotNull Material itemMat
){
String configPath = getConfigNameFromPath(factory.getConfigPath());
- return intSettingGuiItem(factory, itemMat, StringUtil.snakeToUpperSpacedCase(configPath));
+ return intSettingGuiItem(factory, itemMat, CasedStringUtil.snakeToUpperSpacedCase(configPath));
}
+
+ /**
+ * Create an arbitrary GuiItem from a unique setting and item's property.
+ * @param factory The setting's GUI factory.
+ * @param itemMat Displayed material of the item.
+ * @param itemName Name of the item.
+ * @param value Value of the setting when the item is created.
+ * Will not update automatically, if the setting's value change, the item need to be created again.
+ * @return A formatted GuiItem that will create and open a GUI for the setting.
+ */
private static GuiItem createGuiItemFromProperties(
@NotNull AbstractSettingGui.SettingGuiFactory factory,
@NotNull Material itemMat,
@NotNull StringBuilder itemName,
@NotNull Object value
){
- // Create item
+ // Create & initialise item
ItemStack item = new ItemStack(itemMat);
ItemMeta itemMeta = item.getItemMeta();
@@ -160,13 +270,20 @@ public class GuiGlobalItems {
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
item.setItemMeta(itemMeta);
+ // Create GuiItem
return openSettingGuiItem(item, factory);
}
+ /**
+ * Get the setting name from the setting path.
+ * For example: "gui.command.name" will return "name".
+ * @param path The setting's path.
+ * @return The setting's name.
+ */
public static String getConfigNameFromPath(String path){
+ // Get index of first dot
int indexOfDot = path.indexOf(".");
- //if(indexOfDot == -1) return path;
- // indexOfDot == -1 (not fond) imply indexOfDot+1 = 0. substring will keep the full path as expected
+ // when indexOfDot == -1 (not fond), it is implied that indexOfDot+1 = 0. substring will keep the full path as expected
return path.substring(indexOfDot+1);
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java
similarity index 54%
rename from src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java
rename to src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java
index 9a36bdb..28525f8 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/util/StringUtil.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java
@@ -1,8 +1,18 @@
package xyz.alexcrea.cuanvil.util;
-public class StringUtil {
+/**
+ * An incomplete cased string util
+ */
+public class CasedStringUtil {
- //we assume snake_cased_string is in snake case
+ /**
+ * Transform a snake cased string to an upper-cased spaced string.
+ *
+ * for example: if we use "hello_world" as an input this function will return "Hello World".
+ * @param snake_cased_string The input string.
+ * This argument NEED to be a snake cased string, or it will not work
+ * @return The input as an upper-cased string with space separator.
+ */
public static String snakeToUpperSpacedCase(String snake_cased_string){
if(snake_cased_string.contentEquals("")) return "";
StringBuilder result = new StringBuilder();
From d3f7776908ebbe9be42d3ef74edcaf2025e7dfa6 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 19 Mar 2024 02:54:49 +0100
Subject: [PATCH 053/691] Update wrong permission being used.
---
src/main/kotlin/io/delilaheve/AnvilEventListener.kt | 4 ++--
src/main/kotlin/io/delilaheve/CustomAnvil.kt | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index d6d3cf9..379dee1 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -55,7 +55,7 @@ class AnvilEventListener : Listener {
// Should find player
val player = event.view.player
- if(!player.hasPermission(CustomAnvil.unsafePermission)) return
+ if(!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
// Test rename lonely item
if(second == null){
@@ -154,7 +154,7 @@ class AnvilEventListener : Listener {
@EventHandler(ignoreCancelled = true)
fun anvilExtractionCheck(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return
- if(!player.hasPermission(CustomAnvil.unsafePermission)) return
+ if(!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
val inventory = event.inventory as? AnvilInventory ?: return
if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
index dbdca63..2f4fe43 100644
--- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt
+++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt
@@ -23,7 +23,7 @@ class CustomAnvil : JavaPlugin() {
private const val bstatsPluginId = 20923
// Permission string required to use the plugin's features
- const val unsafePermission = "ca.unsafe"
+ const val affectedByPluginPermission = "ca.affected"
// Permission string required to bypass enchantment conflicts test
const val bypassFusePermission = "ca.bypass.fuse"
// Permission string required to bypass enchantment conflicts test
From b6f3c4b40c9295021fa5f1d5ba1047a3ed7bc757 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Tue, 19 Mar 2024 03:02:59 +0100
Subject: [PATCH 054/691] version up
---
build.gradle.kts | 2 +-
src/main/resources/plugin.yml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 53c492e..c8e521a 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.3.0-alpha-2"
+version = "1.3.1-A1"
repositories {
mavenCentral()
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 1dd9c01..089c764 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
-version: 1.3.0-alpha-2
+version: 1.3.1-A1
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
From a7517974ef19287e3a6cb80ebe025c8c4c70cc03 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Sun, 24 Mar 2024 23:06:44 +0100
Subject: [PATCH 055/691] edit README.md
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index 4019f00..0f7c529 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,8 @@ It was renamed because it now affects every anvil aspect and not only unsafe enc
[GitHub](https://github.com/DelilahEve/UnsafeEnchants/releases/latest),
[Spigot](https://www.spigotmc.org/resources/unsafe-enchants.104708/) or
[CurseForge](https://www.curseforge.com/minecraft/bukkit-plugins/unsafe-enchants/files/all)
+
+/!\ version under 1.3.1 may not work from an issue in the main permission.
### Download Locations:
the plugin can be downloaded on the
From 53d2ea262312ae8eec2c6943d757882c6a02e3c5 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Mon, 25 Mar 2024 00:08:26 +0100
Subject: [PATCH 056/691] fix bug repair limit not working.
---
src/main/kotlin/io/delilaheve/AnvilEventListener.kt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
index 379dee1..26a27bd 100644
--- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
+++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt
@@ -371,7 +371,7 @@ class AnvilEventListener : Listener {
}
inventory.repairCost = finalAnvilCost
- event.view.setProperty(REPAIR_COST, anvilCost)
+ event.view.setProperty(REPAIR_COST, finalAnvilCost)
})
}
From c55c58cd5145a1cb95ba83d54f37d5e8f172b688 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Mon, 25 Mar 2024 00:23:43 +0100
Subject: [PATCH 057/691] version up: 1.3.2-A1
---
build.gradle.kts | 2 +-
src/main/resources/plugin.yml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index c8e521a..8d9b438 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "xyz.alexcrea"
-version = "1.3.1-A1"
+version = "1.3.2-A1"
repositories {
mavenCentral()
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 089c764..3b2a340 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil
name: CustomAnvil
prefix: "Custom Anvil"
-version: 1.3.1-A1
+version: 1.3.2-A1
description: Allow to customise anvil mechanics
api-version: 1.18
load: POSTWORLD
From de8821e817ca1dbb99532c8cbd73cde6ef581058 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Mon, 25 Mar 2024 00:42:15 +0100
Subject: [PATCH 058/691] better README.md
---
README.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 0f7c529..0ccbaa3 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,6 @@ It was renamed because it now affects every anvil aspect and not only unsafe enc
[Spigot](https://www.spigotmc.org/resources/unsafe-enchants.104708/) or
[CurseForge](https://www.curseforge.com/minecraft/bukkit-plugins/unsafe-enchants/files/all)
-/!\ version under 1.3.1 may not work from an issue in the main permission.
### Download Locations:
the plugin can be downloaded on the
@@ -35,6 +34,9 @@ ca.bypass.level: Allow player to bypass every level limit (no custom limit)
ca.command.reload: Allow administrator to reload the plugin's configs
ca.config.edit: Allow administrator to edit the plugin's config in game
```
+/!\ version under 1.3.1 use other permission. from 1.2.0 to 1.3.1-A1 use ua.unsafe instead of ca.affected
+under 1.2.0 replace ca prefix by ue and use ue.unsafe. some permission/features may not exist before the last version.
+
### Commands
```yml
anvilconfigreload or carl: Reload every config of this plugin
From b4cb6c2848d3373d72b6c21cb7b8116cd276e3af Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 20 Mar 2024 18:22:27 +0100
Subject: [PATCH 059/691] move openable groups and add interface for future
gui.
---
.../xyz/alexcrea/cuanvil/gui/MainConfigGui.java | 6 +++---
.../gui/config/SelectEnchantmentContainer.java | 16 ++++++++++++++++
.../cuanvil/gui/config/SelectGroupContainer.java | 15 +++++++++++++++
.../gui/config/SelectMaterialContainer.java | 16 ++++++++++++++++
.../config/{ => openable}/BasicConfigGui.java | 2 +-
.../{ => openable}/EnchantCostConfigGui.java | 3 ++-
.../{ => openable}/EnchantLimitConfigGui.java | 3 ++-
.../alexcrea/cuanvil/command/ReloadExecutor.kt | 6 +++---
8 files changed, 58 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/BasicConfigGui.java (99%)
rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/EnchantCostConfigGui.java (96%)
rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/EnchantLimitConfigGui.java (93%)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
index 3e17b56..1b8cc54 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java
@@ -8,9 +8,9 @@ import io.delilaheve.CustomAnvil;
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.config.EnchantCostConfigGui;
-import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui;
+import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui;
+import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui;
+import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
new file mode 100644
index 0000000..9a468de
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
@@ -0,0 +1,16 @@
+package xyz.alexcrea.cuanvil.gui.config;
+
+import org.bukkit.enchantments.Enchantment;
+import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
+
+import java.util.List;
+import java.util.Set;
+
+public interface SelectEnchantmentContainer {
+
+ List getSelectedEnchantments();
+ void setSelectedEnchantments(List enchantments);
+
+ Set illegalEnchantments();
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
new file mode 100644
index 0000000..7574f85
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
@@ -0,0 +1,15 @@
+package xyz.alexcrea.cuanvil.gui.config;
+
+import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
+
+import java.util.List;
+import java.util.Set;
+
+public interface SelectGroupContainer {
+
+ List getSelectedGroups();
+ void setSelectedGroups(List groups);
+
+ Set illegalGroups();
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
new file mode 100644
index 0000000..a510371
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
@@ -0,0 +1,16 @@
+package xyz.alexcrea.cuanvil.gui.config;
+
+import javafx.scene.paint.Material;
+import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
+
+import java.util.List;
+import java.util.Set;
+
+public interface SelectMaterialContainer {
+
+ List getSelectedMaterials();
+ void setSelectedMaterials(List materials);
+
+ Set illegalMaterials();
+
+}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java
similarity index 99%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java
index 7b39d8f..3e86948 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java
@@ -1,4 +1,4 @@
-package xyz.alexcrea.cuanvil.gui.config;
+package xyz.alexcrea.cuanvil.gui.config.openable;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java
similarity index 96%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java
index b5a2a50..7ecd771 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java
@@ -1,4 +1,4 @@
-package xyz.alexcrea.cuanvil.gui.config;
+package xyz.alexcrea.cuanvil.gui.config.openable;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import org.bukkit.Material;
@@ -8,6 +8,7 @@ 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.AbstractEnchantConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java
similarity index 93%
rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java
index fa63937..92b7833 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java
@@ -1,9 +1,10 @@
-package xyz.alexcrea.cuanvil.gui.config;
+package xyz.alexcrea.cuanvil.gui.config.openable;
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.AbstractEnchantConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
index fdcae7d..e8d201d 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt
@@ -5,9 +5,9 @@ import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import xyz.alexcrea.cuanvil.config.ConfigHolder
-import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui
-import xyz.alexcrea.cuanvil.gui.config.EnchantCostConfigGui
-import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui
+import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui
+import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui
+import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui
import xyz.alexcrea.cuanvil.util.MetricsUtil
class ReloadExecutor : CommandExecutor {
From 41235d30245436751a6349a14ebacd555d21d7b3 Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Wed, 20 Mar 2024 22:32:44 +0100
Subject: [PATCH 060/691] Add group select gui and edit group logic to be able
to handle group set.
---
.../config/SelectEnchantmentContainer.java | 3 +-
.../gui/config/SelectGroupContainer.java | 5 +-
.../gui/config/SelectMaterialContainer.java | 13 +-
.../settings/GroupSelectSettingGui.java | 140 ++++++++++++++++++
.../cuanvil/group/AbstractMaterialGroup.kt | 17 ++-
.../alexcrea/cuanvil/group/ExcludeGroup.kt | 27 +++-
.../alexcrea/cuanvil/group/IncludeGroup.kt | 28 +++-
.../cuanvil/group/ItemGroupManager.kt | 2 +-
8 files changed, 216 insertions(+), 19 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
index 9a468de..668aace 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
@@ -9,7 +9,8 @@ import java.util.Set;
public interface SelectEnchantmentContainer {
List getSelectedEnchantments();
- void setSelectedEnchantments(List enchantments);
+
+ boolean setSelectedEnchantments(List enchantments);
Set illegalEnchantments();
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
index 7574f85..33d76f9 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java
@@ -7,8 +7,9 @@ import java.util.Set;
public interface SelectGroupContainer {
- List getSelectedGroups();
- void setSelectedGroups(List groups);
+ Set getSelectedGroups();
+
+ boolean setSelectedGroups(Set groups);
Set illegalGroups();
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
index a510371..bd78db4 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java
@@ -1,16 +1,15 @@
package xyz.alexcrea.cuanvil.gui.config;
-import javafx.scene.paint.Material;
-import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
+import org.bukkit.Material;
-import java.util.List;
-import java.util.Set;
+import java.util.EnumSet;
public interface SelectMaterialContainer {
- List getSelectedMaterials();
- void setSelectedMaterials(List materials);
+ EnumSet getSelectedMaterials();
- Set illegalMaterials();
+ boolean setSelectedMaterials(EnumSet materials);
+
+ EnumSet illegalMaterials();
}
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
new file mode 100644
index 0000000..5ed1a6d
--- /dev/null
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
@@ -0,0 +1,140 @@
+package xyz.alexcrea.cuanvil.gui.config.settings;
+
+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.util.Pattern;
+import io.delilaheve.CustomAnvil;
+import org.bukkit.Material;
+import org.bukkit.enchantments.Enchantment;
+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.group.AbstractMaterialGroup;
+import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
+import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer;
+import xyz.alexcrea.cuanvil.util.CasedStringUtil;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+
+public class GroupSelectSettingGui extends AbstractSettingGui{
+
+ SelectGroupContainer groupContainer;
+ int page;
+
+ HashSet selectedGroups;
+
+ public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) {
+ super(6, title, parent);
+ this.groupContainer = groupContainer;
+ //Not used but planned
+ this.page = page;
+
+ this.selectedGroups = new HashSet<>(groupContainer.getSelectedGroups());
+
+ initGroups();
+ }
+
+ @Override
+ protected Pattern getGuiPattern() {
+ return new Pattern(
+ "000000000",
+ "000000000",
+ "000000000",
+ "000000000",
+ "000000000",
+ "B1111111S"
+ );
+ }
+
+ protected void initGroups(){
+ // Add enchantment gui item
+ OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5);
+ filledEnchant.setPriority(Pane.Priority.HIGH);
+ filledEnchant.align(OutlinePane.Alignment.BEGIN);
+ filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
+
+ Set illegalGroup = this.groupContainer.illegalGroups();
+ ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().forEach((name, group)->{
+ if(illegalGroup.contains(group)) {
+ return;
+ }
+ filledEnchant.addItem(getGuiItemFromGroup(group));
+
+ });
+ addPane(filledEnchant);
+
+ }
+
+ private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){
+ boolean isIn = this.selectedGroups.contains(group);
+
+ Material usedMaterial = Material.PAPER;
+ ItemStack item = new ItemStack(usedMaterial);
+
+ setGroupItemMeta(item, group.getName(), isIn);
+
+ GuiItem guiItem = new GuiItem(item, CustomAnvil.instance);
+ guiItem.setAction(getGrpupItemConsumer(group, guiItem));
+ return guiItem;
+ }
+
+ private static final List TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected");
+ private static final List FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected");
+
+ public void setGroupItemMeta(ItemStack item, String name, boolean isIn){
+ ItemMeta meta = item.getItemMeta();
+
+ meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name));
+ if(isIn){
+ meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true);
+ meta.setLore(TRUE_LORE);
+ }else{
+ meta.removeEnchant(Enchantment.DAMAGE_UNDEAD);
+ meta.setLore(FALSE_LORE);
+ }
+ meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS);
+
+ item.setItemMeta(meta);
+ }
+
+ private Consumer getGrpupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){
+ return event -> {
+ event.setCancelled(true);
+
+ boolean isIn = this.selectedGroups.contains(group);
+ if(isIn){
+ this.selectedGroups.remove(group);
+ }else{
+ this.selectedGroups.add(group);
+ }
+
+ ItemStack item = guiItem.getItem();
+ setGroupItemMeta(item, group.getName(), !isIn);
+ guiItem.setItem(item);// Just in case
+
+ update();
+ };
+ }
+
+ @Override
+ public boolean onSave() {
+ return this.groupContainer.setSelectedGroups(this.selectedGroups);
+ }
+
+ @Override
+ public boolean hadChange() {
+ Set baseGroup = this.groupContainer.getSelectedGroups();
+ return baseGroup.size() != this.selectedGroups.size() ||
+ !baseGroup.containsAll(this.selectedGroups);
+ }
+
+}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
index ffcaab4..2030741 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
@@ -5,6 +5,7 @@ import java.util.*
abstract class AbstractMaterialGroup(private val name: String) {
protected val includedMaterial by lazy {createDefaultSet()}
+ protected var groupChangeNotified = false
/**
* Get the group default set
@@ -33,6 +34,11 @@ abstract class AbstractMaterialGroup(private val name: String) {
*/
abstract fun addToPolicy(other : AbstractMaterialGroup)
+ /**
+ * Get the group as a set
+ */
+ abstract fun getMaterials(): MutableSet
+
/**
* Get the group name in case something is wrong
*/
@@ -41,10 +47,13 @@ abstract class AbstractMaterialGroup(private val name: String) {
}
/**
- * Get the group as a set
+ * Update the contained groups of this group
*/
- fun getSet(): Set {
- return includedMaterial
- }
+ abstract fun setGroups(groups: MutableSet)
+
+ /**
+ * Get the contained group of this material group
+ */
+ abstract fun getGroups(): MutableSet
}
\ No newline at end of file
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
index d8ee38b..24eec47 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
@@ -8,7 +8,8 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
return EnumSet.allOf(Material::class.java)
}
- private val includedGroup = HashSet()
+ private var includedGroup: MutableSet = HashSet()
+ private val groupItems: MutableSet by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) {
@@ -21,11 +22,33 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(mat: Material) {
includedMaterial.remove(mat)
+ groupItems.remove(mat)
}
override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other)
- includedMaterial.removeAll(other.getSet())
+ groupItems.removeAll(other.getMaterials());
+ }
+
+ override fun setGroups(groups: MutableSet) {
+ groupItems.clear()
+ groupItems.addAll(includedMaterial)
+
+ includedGroup.clear();
+ groups.forEach { group ->
+ if(!group.isReferencing(this)) {
+ includedGroup.add(group);
+ groupItems.removeAll(group.getMaterials())
+ }
+ }
+ }
+
+ override fun getGroups(): MutableSet {
+ return includedGroup;
+ }
+
+ override fun getMaterials(): MutableSet {
+ return groupItems;
}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
index bd962c2..20e9ef3 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
@@ -8,7 +8,8 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
return EnumSet.noneOf(Material::class.java)
}
- private val includedGroup = HashSet()
+ private var includedGroup: MutableSet = HashSet()
+ private val groupItems: MutableSet by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) {
@@ -21,11 +22,34 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(mat: Material) {
includedMaterial.add(mat)
+ groupItems.add(mat)
}
override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other)
- includedMaterial.addAll(other.getSet())
+ groupItems.addAll(other.getMaterials());
}
+ override fun setGroups(groups: MutableSet) {
+ groupItems.clear();
+ groupItems.addAll(includedMaterial)
+
+ includedGroup.clear();
+ groups.forEach { group ->
+ if(!group.isReferencing(this)){
+ includedGroup.add(group);
+ groupItems.addAll(group.getMaterials())
+ }
+ }
+ }
+
+ override fun getGroups(): MutableSet {
+ return includedGroup
+ }
+
+ override fun getMaterials(): MutableSet {
+ return groupItems
+ }
+
+
}
\ No newline at end of file
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
index a7c927a..5d57dfa 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
@@ -18,7 +18,7 @@ class ItemGroupManager {
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
}
- private lateinit var groupMap : HashMap
+ lateinit var groupMap : HashMap
// Read and create material groups
fun prepareGroups(config: ConfigurationSection){
From e99fd4d640f64b497aacd31a3b90f5e47dffce1f Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 21 Mar 2024 01:42:48 +0100
Subject: [PATCH 061/691] Add display item for group and order them in create
order
---
.../settings/GroupSelectSettingGui.java | 21 ++++++----
.../cuanvil/group/AbstractMaterialGroup.kt | 41 ++++++++++++++++---
.../alexcrea/cuanvil/group/ExcludeGroup.kt | 14 +++----
.../alexcrea/cuanvil/group/IncludeGroup.kt | 23 ++++++++---
.../cuanvil/group/ItemGroupManager.kt | 5 ++-
5 files changed, 76 insertions(+), 28 deletions(-)
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
index 5ed1a6d..13c183b 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java
@@ -19,10 +19,7 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
import java.util.function.Consumer;
public class GroupSelectSettingGui extends AbstractSettingGui{
@@ -30,7 +27,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
SelectGroupContainer groupContainer;
int page;
- HashSet selectedGroups;
+ Set selectedGroups;
public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) {
super(6, title, parent);
@@ -63,13 +60,13 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
Set illegalGroup = this.groupContainer.illegalGroups();
- ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().forEach((name, group)->{
+ for (AbstractMaterialGroup group : ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values()) {
if(illegalGroup.contains(group)) {
return;
}
filledEnchant.addItem(getGuiItemFromGroup(group));
+ }
- });
addPane(filledEnchant);
}
@@ -77,7 +74,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){
boolean isIn = this.selectedGroups.contains(group);
- Material usedMaterial = Material.PAPER;
+ Material usedMaterial = group.getRepresentativeMaterial();
ItemStack item = new ItemStack(usedMaterial);
setGroupItemMeta(item, group.getName(), isIn);
@@ -93,6 +90,14 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
public void setGroupItemMeta(ItemStack item, String name, boolean isIn){
ItemMeta meta = item.getItemMeta();
+ if(meta == null){
+ CustomAnvil.instance.getLogger().warning("Could not create item for group: "+name+":\n" +
+ "Item do not gave item meta: "+item+". Using placeholder instead");
+ item.setType(Material.PAPER);
+ meta = item.getItemMeta();
+ assert meta != null;
+ }
+
meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name));
if(isIn){
meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true);
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
index 2030741..233d990 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt
@@ -5,7 +5,6 @@ import java.util.*
abstract class AbstractMaterialGroup(private val name: String) {
protected val includedMaterial by lazy {createDefaultSet()}
- protected var groupChangeNotified = false
/**
* Get the group default set
@@ -15,7 +14,7 @@ abstract class AbstractMaterialGroup(private val name: String) {
/**
* Get if a material is allowed following the group policy
*/
- fun contain(mat : Material): Boolean {
+ open fun contain(mat : Material): Boolean {
return mat in includedMaterial
}
@@ -35,14 +34,28 @@ abstract class AbstractMaterialGroup(private val name: String) {
abstract fun addToPolicy(other : AbstractMaterialGroup)
/**
- * Get the group as a set
+ * Get the group contained material as a set
*/
- abstract fun getMaterials(): MutableSet
+ abstract fun getMaterials(): EnumSet
+
+ /**
+ * Get the group non-inherited material as a set
+ */
+ open fun getNonGroupInheritedMaterials(): EnumSet {
+ return includedMaterial
+ }
+ /**
+ * Get the group non-inherited material as a set
+ */
+ open fun setNonGroupInheritedMaterials(materials: EnumSet) {
+ this.includedMaterial.clear()
+ this.includedMaterial.addAll(materials)
+ }
/**
* Get the group name in case something is wrong
*/
- fun getName(): String {
+ open fun getName(): String {
return name
}
@@ -56,4 +69,22 @@ abstract class AbstractMaterialGroup(private val name: String) {
*/
abstract fun getGroups(): MutableSet
+ open fun getRepresentativeMaterial() : Material {
+ // Test inner material
+ val matIterator = includedMaterial.iterator()
+ while(matIterator.hasNext()){
+ val material = matIterator.next();
+ if(material.isAir) continue
+ return material;
+ }
+ // Test included group representative material
+ val groupIterator = getGroups().iterator()
+ while (groupIterator.hasNext()){
+ val groupMat = groupIterator.next().getRepresentativeMaterial()
+ if(groupMat.isAir) continue
+ return groupMat;
+ }
+ return Material.PAPER;
+ }
+
}
\ No newline at end of file
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
index 24eec47..82a691b 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt
@@ -9,7 +9,7 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
}
private var includedGroup: MutableSet = HashSet()
- private val groupItems: MutableSet by lazy {createDefaultSet()}
+ private val groupItems by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) {
@@ -27,28 +27,28 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other)
- groupItems.removeAll(other.getMaterials());
+ groupItems.removeAll(other.getMaterials())
}
override fun setGroups(groups: MutableSet) {
groupItems.clear()
groupItems.addAll(includedMaterial)
- includedGroup.clear();
+ includedGroup.clear()
groups.forEach { group ->
if(!group.isReferencing(this)) {
- includedGroup.add(group);
+ includedGroup.add(group)
groupItems.removeAll(group.getMaterials())
}
}
}
override fun getGroups(): MutableSet {
- return includedGroup;
+ return includedGroup
}
- override fun getMaterials(): MutableSet {
- return groupItems;
+ override fun getMaterials(): EnumSet {
+ return groupItems
}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
index 20e9ef3..05a85c2 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt
@@ -9,7 +9,7 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
}
private var includedGroup: MutableSet = HashSet()
- private val groupItems: MutableSet by lazy {createDefaultSet()}
+ private val groupItems by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) {
@@ -27,27 +27,38 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other)
- groupItems.addAll(other.getMaterials());
+ groupItems.addAll(other.getMaterials())
}
override fun setGroups(groups: MutableSet) {
- groupItems.clear();
+ groupItems.clear()
groupItems.addAll(includedMaterial)
- includedGroup.clear();
+ includedGroup.clear()
groups.forEach { group ->
if(!group.isReferencing(this)){
- includedGroup.add(group);
+ includedGroup.add(group)
groupItems.addAll(group.getMaterials())
}
}
}
+ override fun setNonGroupInheritedMaterials(materials: EnumSet) {
+ super.setNonGroupInheritedMaterials(materials)
+ // Update group items
+ groupItems.clear()
+ groupItems.addAll(includedMaterial)
+
+ includedGroup.forEach { group ->
+ groupItems.addAll(group.getMaterials())
+ }
+ }
+
override fun getGroups(): MutableSet {
return includedGroup
}
- override fun getMaterials(): MutableSet {
+ override fun getMaterials(): EnumSet {
return groupItems
}
diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
index 5d57dfa..6403e73 100644
--- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
+++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt
@@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import java.util.*
+import kotlin.collections.LinkedHashMap
class ItemGroupManager {
@@ -18,11 +19,11 @@ class ItemGroupManager {
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
}
- lateinit var groupMap : HashMap
+ lateinit var groupMap : LinkedHashMap
// Read and create material groups
fun prepareGroups(config: ConfigurationSection){
- groupMap = HashMap()
+ groupMap = LinkedHashMap()
val keys = config.getKeys(false)
for (key in keys) {
From 7129aed585c27516fcd3e03a5334325b58b35a8a Mon Sep 17 00:00:00 2001
From: alexcrea
Date: Thu, 21 Mar 2024 02:22:07 +0100
Subject: [PATCH 062/691] Add select enchant setting gui & fix a typo.
---
.../gui/config/AbstractEnchantConfigGui.java | 11 +-
.../config/SelectEnchantmentContainer.java | 4 +-
.../settings/EnchantSelectSettingGui.java | 157 ++++++++++++++++++
.../settings/GroupSelectSettingGui.java | 4 +-
4 files changed, 168 insertions(+), 8 deletions(-)
create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
index ae04fb2..4998f8e 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java
@@ -26,6 +26,12 @@ import java.util.List;
*/
public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui {
+ public static final List SORTED_ENCHANTMENT_LIST;
+ static {
+ SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values());
+ SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
+ }
+
private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
private final Gui backGui;
@@ -90,10 +96,7 @@ public abstract class AbstractEnchantConfigGui();
- List enchantments = Arrays.asList(Enchantment.values());
- enchantments.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
-
- for (Enchantment enchant : enchantments) {
+ for (Enchantment enchant : SORTED_ENCHANTMENT_LIST) {
T factory = getFactoryFromEnchant(enchant);
bookItemFactoryList.add(factory);
diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
index 668aace..dcbbb47 100644
--- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
+++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java
@@ -8,9 +8,9 @@ import java.util.Set;
public interface SelectEnchantmentContainer {
- List getSelectedEnchantments();
+ Set getSelectedEnchantments();
- boolean setSelectedEnchantments(List enchantments);
+ boolean setSelectedEnchantments(Set enchantments);
Set