From 2fc88703a4897beec7f7b352c522fcd313599352 Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:10:41 +0100 Subject: [PATCH 1/7] convert config bonus ingredients to an array --- .../config/ToolLevelingConfig.java | 28 +++++++--- .../config/values/BonusIngredient.java | 32 +++++++++++ .../config/values/BonusIngredientsValue.java | 56 +++++++++++++++++++ .../config/values/IngredientValue.java | 50 ----------------- 4 files changed, 108 insertions(+), 58 deletions(-) create mode 100644 Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java create mode 100644 Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java delete mode 100644 Common/src/main/java/com/tristankechlo/toolleveling/config/values/IngredientValue.java diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index da579a6..d02567b 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -3,7 +3,8 @@ import com.tristankechlo.toolleveling.ToolLeveling; import com.tristankechlo.toolleveling.config.util.AbstractConfig; import com.tristankechlo.toolleveling.config.values.AbstractConfigValue; -import com.tristankechlo.toolleveling.config.values.IngredientValue; +import com.tristankechlo.toolleveling.config.values.BonusIngredient; +import com.tristankechlo.toolleveling.config.values.BonusIngredientsValue; import com.tristankechlo.toolleveling.config.values.NumberValue; import net.minecraft.util.GsonHelper; import net.minecraft.world.item.ItemStack; @@ -19,8 +20,7 @@ public final class ToolLevelingConfig extends AbstractConfig { private final NumberValue maxSuccessChance; private final NumberValue requiredBookshelves; private final NumberValue requiredBooks; - private final IngredientValue bonusItemMoreEnchantments; - private final IngredientValue bonusItemMoreLevels; + private final BonusIngredientsValue bonusIngredients; private final List> values; public static final ToolLevelingConfig INSTANCE = new ToolLevelingConfig(); @@ -31,10 +31,12 @@ private ToolLevelingConfig() { maxSuccessChance = new NumberValue<>("max_success_chance", 100.0F, 0.0F, 100.0F, GsonHelper::getAsFloat); requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); - bonusItemMoreEnchantments = new IngredientValue("bonus_item_more_enchantments", Ingredient.of(Items.NETHER_STAR)); - bonusItemMoreLevels = new IngredientValue("bonus_item_more_levels", Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE)); + bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ + new BonusIngredient(Ingredient.of(Items.NETHER_STAR), false, true), + new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), true, false), + }); - values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, bonusItemMoreEnchantments, bonusItemMoreLevels); + values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, bonusIngredients); } @Override @@ -64,11 +66,21 @@ public int requiredBooks() { } public boolean isBonusItemStrength(ItemStack stack) { - return bonusItemMoreLevels.get().test(stack); + for (BonusIngredient bonus : bonusIngredients.get()) { + if (bonus.ingredient().test(stack) && bonus.maxLevelBonus()) { + return true; + } + } + return false; } public boolean isBonusItemIterations(ItemStack stack) { - return bonusItemMoreEnchantments.get().test(stack); + for (BonusIngredient bonus : bonusIngredients.get()) { + if (bonus.ingredient().test(stack) && bonus.iterationsBonus()) { + return true; + } + } + return false; } } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java new file mode 100644 index 0000000..bb203a9 --- /dev/null +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java @@ -0,0 +1,32 @@ +package com.tristankechlo.toolleveling.config.values; + +import com.google.gson.JsonObject; +import net.minecraft.util.GsonHelper; +import net.minecraft.world.item.crafting.Ingredient; + +public record BonusIngredient(Ingredient ingredient, boolean maxLevelBonus, boolean iterationsBonus) { + + public BonusIngredient { + if (ingredient == null) { + throw new NullPointerException("ingredient of the bonus ingredient can't be null"); + } + if (!maxLevelBonus && !iterationsBonus) { + throw new NullPointerException("bonus ingredient must provide at least one bonus"); + } + } + + public static void serialize(BonusIngredient value, JsonObject json) { + json.add("ingredient", value.ingredient().toJson()); + json.addProperty("max_level_bonus", value.maxLevelBonus()); + json.addProperty("iterations_bonus", value.iterationsBonus()); + } + + public static BonusIngredient deserialize(JsonObject json) { + JsonObject obj = GsonHelper.getAsJsonObject(json, "ingredient"); + Ingredient ingredient = Ingredient.fromJson(obj); + boolean maxLevelBonus = GsonHelper.getAsBoolean(json, "max_level_bonus", false); + boolean iterationsBonus = GsonHelper.getAsBoolean(json, "iterations_bonus", false); + return new BonusIngredient(ingredient, maxLevelBonus, iterationsBonus); + } + +} diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java new file mode 100644 index 0000000..9383eb6 --- /dev/null +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java @@ -0,0 +1,56 @@ +package com.tristankechlo.toolleveling.config.values; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.tristankechlo.toolleveling.ToolLeveling; +import net.minecraft.util.GsonHelper; + +public final class BonusIngredientsValue extends AbstractConfigValue { + + private BonusIngredient[] value; + private final BonusIngredient[] defaultValue; + + public BonusIngredientsValue(String name, BonusIngredient[] defaultValue) { + super(name); + this.value = defaultValue; + this.defaultValue = defaultValue; + } + + @Override + public void setToDefault() { + this.value = defaultValue; + } + + @Override + public void serialize(JsonObject json) { + JsonArray arr = new JsonArray(); + for (BonusIngredient bonus : value) { + JsonObject obj = new JsonObject(); + BonusIngredient.serialize(bonus, obj); + arr.add(obj); + } + json.add(getIdentifier(), arr); + } + + @Override + public void deserialize(JsonObject json) { + try { + JsonArray arr = GsonHelper.getAsJsonArray(json, getIdentifier()); + value = new BonusIngredient[arr.size()]; + for (int i = 0; i < arr.size(); i++) { + JsonObject obj = GsonHelper.convertToJsonObject(arr.get(i), "[" + i + "]"); + value[i] = BonusIngredient.deserialize(obj); + } + } catch (Exception e) { + value = defaultValue; + ToolLeveling.LOGGER.warn(e.getMessage()); + ToolLeveling.LOGGER.warn("Error while loading the config value " + getIdentifier() + ", using default value instead"); + } + } + + @Override + public BonusIngredient[] get() { + return value; + } + +} diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/IngredientValue.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/IngredientValue.java deleted file mode 100644 index e724735..0000000 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/IngredientValue.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.tristankechlo.toolleveling.config.values; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.tristankechlo.toolleveling.ToolLeveling; -import net.minecraft.world.item.crafting.Ingredient; - -public final class IngredientValue extends AbstractConfigValue { - - private Ingredient value; - private final Ingredient defaultValue; - - public IngredientValue(String name, Ingredient defaultValue) { - super(name); - this.value = defaultValue; - this.defaultValue = defaultValue; - } - - @Override - public void setToDefault() { - this.value = defaultValue; - } - - @Override - public void serialize(JsonObject json) { - json.add(getIdentifier(), value.toJson()); - } - - @Override - public void deserialize(JsonObject json) { - try { - JsonElement jsonElement = json.get(getIdentifier()); - if (jsonElement != null) { - value = Ingredient.fromJson(jsonElement, true); - } else { - value = defaultValue; - } - } catch (Exception e) { - value = defaultValue; - ToolLeveling.LOGGER.warn(e.getMessage()); - ToolLeveling.LOGGER.warn("Error while loading the config value " + getIdentifier() + ", using default value instead"); - } - } - - @Override - public Ingredient get() { - return value; - } - -} From d652655fb98880db7905aea7944342435516e2e0 Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Fri, 22 Mar 2024 19:48:36 +0100 Subject: [PATCH 2/7] support different strengths of bonuses --- .../config/ToolLevelingConfig.java | 30 ++++++++++++++++--- .../config/values/BonusIngredient.java | 8 ++--- .../network/packets/TableUpgradeProcess.java | 2 +- .../toolleveling/util/Predicates.java | 7 +++-- .../tristankechlo/toolleveling/util/Util.java | 8 ++--- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index d02567b..1a2d13d 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -32,8 +32,8 @@ private ToolLevelingConfig() { requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ - new BonusIngredient(Ingredient.of(Items.NETHER_STAR), false, true), - new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), true, false), + new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 1), + new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 1, 0), }); values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, bonusIngredients); @@ -65,22 +65,44 @@ public int requiredBooks() { return requiredBooks.get(); } + @Deprecated public boolean isBonusItemStrength(ItemStack stack) { for (BonusIngredient bonus : bonusIngredients.get()) { - if (bonus.ingredient().test(stack) && bonus.maxLevelBonus()) { + if (bonus.ingredient().test(stack) && bonus.maxLevelBonus() != 0) { return true; } } return false; } + public int getBonusItemStrength(ItemStack stack) { + int total = 0; + for (BonusIngredient bonus : bonusIngredients.get()) { + if (bonus.ingredient().test(stack)) { + total += bonus.maxLevelBonus(); + } + } + return total; + } + + @Deprecated public boolean isBonusItemIterations(ItemStack stack) { for (BonusIngredient bonus : bonusIngredients.get()) { - if (bonus.ingredient().test(stack) && bonus.iterationsBonus()) { + if (bonus.ingredient().test(stack) && bonus.iterationsBonus() != 0) { return true; } } return false; } + public int getBonusItemIterations(ItemStack stack) { + int total = 0; + for (BonusIngredient bonus : bonusIngredients.get()) { + if (bonus.ingredient().test(stack)) { + total += bonus.iterationsBonus(); + } + } + return total; + } + } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java index bb203a9..3ae0423 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java @@ -4,13 +4,13 @@ import net.minecraft.util.GsonHelper; import net.minecraft.world.item.crafting.Ingredient; -public record BonusIngredient(Ingredient ingredient, boolean maxLevelBonus, boolean iterationsBonus) { +public record BonusIngredient(Ingredient ingredient, int maxLevelBonus, int iterationsBonus) { public BonusIngredient { if (ingredient == null) { throw new NullPointerException("ingredient of the bonus ingredient can't be null"); } - if (!maxLevelBonus && !iterationsBonus) { + if (maxLevelBonus == 0 && iterationsBonus == 0) { throw new NullPointerException("bonus ingredient must provide at least one bonus"); } } @@ -24,8 +24,8 @@ public static void serialize(BonusIngredient value, JsonObject json) { public static BonusIngredient deserialize(JsonObject json) { JsonObject obj = GsonHelper.getAsJsonObject(json, "ingredient"); Ingredient ingredient = Ingredient.fromJson(obj); - boolean maxLevelBonus = GsonHelper.getAsBoolean(json, "max_level_bonus", false); - boolean iterationsBonus = GsonHelper.getAsBoolean(json, "iterations_bonus", false); + int maxLevelBonus = GsonHelper.getAsInt(json, "max_level_bonus", 0); + int iterationsBonus = GsonHelper.getAsInt(json, "iterations_bonus", 0); return new BonusIngredient(ingredient, maxLevelBonus, iterationsBonus); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java index b4440d2..f5c4b16 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java @@ -79,7 +79,7 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { // add the enchantments to the item for (var entry : enchantmentsToAdd.entrySet()) { - oldEnchantments.merge(entry.getKey(), entry.getValue(), Integer::sum); + oldEnchantments.merge(entry.getKey(), entry.getValue(), (l, r) -> Math.min(l + r, Short.MAX_VALUE)); } EnchantmentHelper.setEnchantments(oldEnchantments, tool); table.setChanged(); diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java index a7aedcb..49636bb 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java @@ -5,14 +5,17 @@ import net.minecraft.world.item.Items; import java.util.function.Predicate; +import java.util.function.ToIntFunction; public final class Predicates { public static final Predicate IS_BOOK = (stack) -> stack.is(Items.ENCHANTED_BOOK); // purple slots public static final Predicate IS_UPGRADE_ITEM = (stack) -> !IS_BOOK.test(stack) && (stack.isEnchanted() || stack.isEnchantable()); // red slot - public static final Predicate BONUS_ITEM_STRENGTH = ToolLevelingConfig.INSTANCE::isBonusItemStrength; - public static final Predicate BONUS_ITEM_ITERATIONS = ToolLevelingConfig.INSTANCE::isBonusItemIterations; + public static final ToIntFunction BONUS_ITEM_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemStrength; + public static final Predicate BONUS_ITEM_STRENGTH = (stack) -> BONUS_ITEM_STRENGTH_VAL.applyAsInt(stack) != 0; + public static final ToIntFunction BONUS_ITEM_ITERATIONS_VAL = ToolLevelingConfig.INSTANCE::getBonusItemIterations; + public static final Predicate BONUS_ITEM_ITERATIONS = (stack) -> BONUS_ITEM_ITERATIONS_VAL.applyAsInt(stack) != 0; public static final Predicate IS_BONUS_ITEM = (stack) -> BONUS_ITEM_STRENGTH.test(stack) || BONUS_ITEM_ITERATIONS.test(stack); // green slots } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java index 1490257..c3b34e5 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java @@ -61,9 +61,7 @@ public static float getSuccessChance(Level level, BlockPos tablePos) { public static int getIterations(Container menu) { int count = 1; for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { - if (Predicates.BONUS_ITEM_ITERATIONS.test(menu.getItem(i))) { - count++; - } + count += Predicates.BONUS_ITEM_ITERATIONS_VAL.applyAsInt(menu.getItem(i)); } return count; } @@ -71,9 +69,7 @@ public static int getIterations(Container menu) { public static int getEnchantmentStrength(Container menu) { int count = 1; for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { - if (Predicates.BONUS_ITEM_STRENGTH.test(menu.getItem(i))) { - count++; - } + count += Predicates.BONUS_ITEM_STRENGTH_VAL.applyAsInt(menu.getItem(i)); } return count; } From 1f973465e3d581199fa8a844b4d54ca2cca5bffd Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:21:43 +0100 Subject: [PATCH 3/7] make default upgrade strength also configurable --- .../toolleveling/config/ToolLevelingConfig.java | 15 ++++++++++++++- .../toolleveling/util/Predicates.java | 3 +++ .../com/tristankechlo/toolleveling/util/Util.java | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index 1a2d13d..5096477 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -20,6 +20,8 @@ public final class ToolLevelingConfig extends AbstractConfig { private final NumberValue maxSuccessChance; private final NumberValue requiredBookshelves; private final NumberValue requiredBooks; + private final NumberValue baseIterations; + private final NumberValue baseStrength; private final BonusIngredientsValue bonusIngredients; private final List> values; public static final ToolLevelingConfig INSTANCE = new ToolLevelingConfig(); @@ -31,12 +33,15 @@ private ToolLevelingConfig() { maxSuccessChance = new NumberValue<>("max_success_chance", 100.0F, 0.0F, 100.0F, GsonHelper::getAsFloat); requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); + baseIterations = new NumberValue<>("base_num_enchantments", 1, 1, Integer.MAX_VALUE, GsonHelper::getAsInt); + baseStrength = new NumberValue<>("base_num_levels", 1, 1, Integer.MAX_VALUE, GsonHelper::getAsInt); bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 1), new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 1, 0), }); - values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, bonusIngredients); + values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, + baseIterations, baseStrength, bonusIngredients); } @Override @@ -65,6 +70,14 @@ public int requiredBooks() { return requiredBooks.get(); } + public int getBaseStrength() { + return baseStrength.get(); + } + + public int getBaseIterations() { + return baseIterations.get(); + } + @Deprecated public boolean isBonusItemStrength(ItemStack stack) { for (BonusIngredient bonus : bonusIngredients.get()) { diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java index 49636bb..05267d0 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java @@ -4,6 +4,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import java.util.function.IntSupplier; import java.util.function.Predicate; import java.util.function.ToIntFunction; @@ -12,8 +13,10 @@ public final class Predicates { public static final Predicate IS_BOOK = (stack) -> stack.is(Items.ENCHANTED_BOOK); // purple slots public static final Predicate IS_UPGRADE_ITEM = (stack) -> !IS_BOOK.test(stack) && (stack.isEnchanted() || stack.isEnchantable()); // red slot + public static final IntSupplier BASE_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBaseStrength; public static final ToIntFunction BONUS_ITEM_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemStrength; public static final Predicate BONUS_ITEM_STRENGTH = (stack) -> BONUS_ITEM_STRENGTH_VAL.applyAsInt(stack) != 0; + public static final IntSupplier BASE_ITERATIONS_VAL= ToolLevelingConfig.INSTANCE::getBaseIterations; public static final ToIntFunction BONUS_ITEM_ITERATIONS_VAL = ToolLevelingConfig.INSTANCE::getBonusItemIterations; public static final Predicate BONUS_ITEM_ITERATIONS = (stack) -> BONUS_ITEM_ITERATIONS_VAL.applyAsInt(stack) != 0; public static final Predicate IS_BONUS_ITEM = (stack) -> BONUS_ITEM_STRENGTH.test(stack) || BONUS_ITEM_ITERATIONS.test(stack); // green slots diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java index c3b34e5..2abe459 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java @@ -59,7 +59,7 @@ public static float getSuccessChance(Level level, BlockPos tablePos) { } public static int getIterations(Container menu) { - int count = 1; + int count = Predicates.BASE_ITERATIONS_VAL.getAsInt(); for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { count += Predicates.BONUS_ITEM_ITERATIONS_VAL.applyAsInt(menu.getItem(i)); } @@ -67,7 +67,7 @@ public static int getIterations(Container menu) { } public static int getEnchantmentStrength(Container menu) { - int count = 1; + int count = Predicates.BASE_STRENGTH_VAL.getAsInt(); for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { count += Predicates.BONUS_ITEM_STRENGTH_VAL.applyAsInt(menu.getItem(i)); } From be5d16ceac458d1514b2b3deaee568c2330984ae Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:41:26 +0100 Subject: [PATCH 4/7] implement migration from previous config format --- .../config/values/BonusIngredientsValue.java | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java index 9383eb6..c18e77c 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java @@ -4,6 +4,7 @@ import com.google.gson.JsonObject; import com.tristankechlo.toolleveling.ToolLeveling; import net.minecraft.util.GsonHelper; +import net.minecraft.world.item.crafting.Ingredient; public final class BonusIngredientsValue extends AbstractConfigValue { @@ -34,17 +35,37 @@ public void serialize(JsonObject json) { @Override public void deserialize(JsonObject json) { - try { - JsonArray arr = GsonHelper.getAsJsonArray(json, getIdentifier()); - value = new BonusIngredient[arr.size()]; - for (int i = 0; i < arr.size(); i++) { - JsonObject obj = GsonHelper.convertToJsonObject(arr.get(i), "[" + i + "]"); - value[i] = BonusIngredient.deserialize(obj); + if (json.has("bonus_item_more_enchantments") && json.has("bonus_item_more_levels")) { + // migrate from one ingredient per bonus + ToolLeveling.LOGGER.info("Migrating for the config value " + getIdentifier()); + try { + JsonObject moreEnchantsObj = GsonHelper.getAsJsonObject(json, "bonus_item_more_enchantments"); + Ingredient maxLevelBonusIngredient = Ingredient.fromJson(moreEnchantsObj); + JsonObject moreLevelsObj = GsonHelper.getAsJsonObject(json, "bonus_item_more_levels"); + Ingredient iterationsBonusIngredient = Ingredient.fromJson(moreLevelsObj); + value = new BonusIngredient[]{ + new BonusIngredient(maxLevelBonusIngredient, 0, 1), + new BonusIngredient(iterationsBonusIngredient, 1, 0) + }; + } catch (Exception e) { + value = defaultValue; + ToolLeveling.LOGGER.warn(e.getMessage()); + ToolLeveling.LOGGER.warn("Error while migrating the config value " + getIdentifier() + ", using default value instead"); + } + } else { + // deserialize the array-based format + try { + JsonArray arr = GsonHelper.getAsJsonArray(json, getIdentifier()); + value = new BonusIngredient[arr.size()]; + for (int i = 0; i < arr.size(); i++) { + JsonObject obj = GsonHelper.convertToJsonObject(arr.get(i), "[" + i + "]"); + value[i] = BonusIngredient.deserialize(obj); + } + } catch (Exception e) { + value = defaultValue; + ToolLeveling.LOGGER.warn(e.getMessage()); + ToolLeveling.LOGGER.warn("Error while loading the config value " + getIdentifier() + ", using default value instead"); } - } catch (Exception e) { - value = defaultValue; - ToolLeveling.LOGGER.warn(e.getMessage()); - ToolLeveling.LOGGER.warn("Error while loading the config value " + getIdentifier() + ", using default value instead"); } } From 712b2c18d6b05bc49a956fa97a8a102ac5920e62 Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Sat, 23 Mar 2024 00:16:29 +0100 Subject: [PATCH 5/7] ensure correct behavior of non-positive bonuses --- .../toolleveling/client/ComponentUtil.java | 13 ++++++++++-- .../config/ToolLevelingConfig.java | 4 ++-- .../tristankechlo/toolleveling/util/Util.java | 21 ++++++++++++++----- .../assets/toolleveling/lang/de_de.json | 2 ++ .../assets/toolleveling/lang/en_us.json | 2 ++ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java index c607e8f..47e74b1 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java @@ -55,13 +55,22 @@ public static Component makeHelpField(String str, Integer number) { } public static Component makeSummary(String str, int iterations, int strength) { + if (iterations <= 0) { + MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); + return Component.translatable(START + str + ".iterations_too_low", iterationsText).withStyle(ChatFormatting.GRAY); + } + if (strength <= 0) { + MutableComponent iterationsText = Component.literal("" + strength).withStyle(ChatFormatting.GREEN); + return Component.translatable(START + str + ".strength_too_low", iterationsText).withStyle(ChatFormatting.GRAY); + } MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); MutableComponent strengthText = Component.literal("" + strength).withStyle(ChatFormatting.GREEN); MutableComponent strText = Component.literal("1").withStyle(ChatFormatting.GREEN); if (strength > 1) { - str += ".multi"; + return Component.translatable(START + str + ".multi", iterationsText, strText, strengthText).withStyle(ChatFormatting.GRAY); + } else { + return Component.translatable(START + str, iterationsText, strengthText).withStyle(ChatFormatting.GRAY); } - return Component.translatable(START + str, iterationsText, strText, strengthText).withStyle(ChatFormatting.GRAY); } } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index 5096477..28827bf 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -33,8 +33,8 @@ private ToolLevelingConfig() { maxSuccessChance = new NumberValue<>("max_success_chance", 100.0F, 0.0F, 100.0F, GsonHelper::getAsFloat); requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); - baseIterations = new NumberValue<>("base_num_enchantments", 1, 1, Integer.MAX_VALUE, GsonHelper::getAsInt); - baseStrength = new NumberValue<>("base_num_levels", 1, 1, Integer.MAX_VALUE, GsonHelper::getAsInt); + baseIterations = new NumberValue<>("base_num_enchantments", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); + baseStrength = new NumberValue<>("base_num_levels", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 1), new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 1, 0), diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java index 2abe459..76377d0 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java @@ -12,6 +12,7 @@ import java.util.function.Function; +import static com.tristankechlo.toolleveling.blockentity.ToolLevelingTableBlockEntity.BONUS_SLOTS; import static com.tristankechlo.toolleveling.blockentity.ToolLevelingTableBlockEntity.BOOK_SLOTS; public final class Util { @@ -25,8 +26,10 @@ private static boolean canUpgradeProcessBegin(Function f) { } boolean enoughBooks = bookCount >= ToolLevelingConfig.INSTANCE.requiredBooks(); // the minimum number of books is reached boolean upgradeSlotNotEmpty = !f.apply(0).isEmpty(); // the upgrade slot is not empty + int iterations = getIterations(f); + int strength = getEnchantmentStrength(f); - return enoughBooks && upgradeSlotNotEmpty; + return enoughBooks && upgradeSlotNotEmpty && iterations > 0 && strength > 0; } public static boolean canUpgradeProcessBegin(AbstractContainerMenu menu) { @@ -59,17 +62,25 @@ public static float getSuccessChance(Level level, BlockPos tablePos) { } public static int getIterations(Container menu) { + return getIterations(menu::getItem); + } + + private static int getIterations(Function f) { int count = Predicates.BASE_ITERATIONS_VAL.getAsInt(); - for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { - count += Predicates.BONUS_ITEM_ITERATIONS_VAL.applyAsInt(menu.getItem(i)); + for (int i : BONUS_SLOTS) { + count += Predicates.BONUS_ITEM_ITERATIONS_VAL.applyAsInt(f.apply(i)); } return count; } public static int getEnchantmentStrength(Container menu) { + return getEnchantmentStrength(menu::getItem); + } + + private static int getEnchantmentStrength(Function f) { int count = Predicates.BASE_STRENGTH_VAL.getAsInt(); - for (int i : ToolLevelingTableBlockEntity.BONUS_SLOTS) { - count += Predicates.BONUS_ITEM_STRENGTH_VAL.applyAsInt(menu.getItem(i)); + for (int i : BONUS_SLOTS) { + count += Predicates.BONUS_ITEM_STRENGTH_VAL.applyAsInt(f.apply(i)); } return count; } diff --git a/Common/src/main/resources/assets/toolleveling/lang/de_de.json b/Common/src/main/resources/assets/toolleveling/lang/de_de.json index 175ad99..d43a440 100644 --- a/Common/src/main/resources/assets/toolleveling/lang/de_de.json +++ b/Common/src/main/resources/assets/toolleveling/lang/de_de.json @@ -7,6 +7,8 @@ "screen.toolleveling.tool_leveling_table.summary.field_title": "Zusammenfassung", "screen.toolleveling.tool_leveling_table.summary.field_text": "Bis zu %s Verzauberung(en) werden dem Item hinzugefügt, jede mit einer Stufe von %s", "screen.toolleveling.tool_leveling_table.summary.field_text.multi": "Bis zu %s Verzauberung(en) werden dem Item hinzugefügt, jede mit einer Stufe von %s bis %s", + "screen.toolleveling.tool_leveling_table.summary.field_text.iterations_too_low": "Nicht genug Verzauberungen (%s) f\u00FCr das Upgrade, verwende andere besondere Gegenstände um mindestens 1 Verzauberung zu erreichen", + "screen.toolleveling.tool_leveling_table.summary.field_text.strength_too_low": "Stufe der Verzauberung (%s) zu niedrig f\u00FCr das Upgrade, verwende andere besondere Gegenstände um mindestens Stufe 1 zu erreichen", "screen.toolleveling.tool_leveling_table.percentages.button_text": "Info", "screen.toolleveling.tool_leveling_table.percentages.button_tooltip": "Zeige die Wahrscheinlichkeiten f\u00FCr die Verzauberungen", "screen.toolleveling.tool_leveling_table.percentages.field_title": "Wahrscheinlichkeiten", diff --git a/Common/src/main/resources/assets/toolleveling/lang/en_us.json b/Common/src/main/resources/assets/toolleveling/lang/en_us.json index 674cd0a..ca12386 100644 --- a/Common/src/main/resources/assets/toolleveling/lang/en_us.json +++ b/Common/src/main/resources/assets/toolleveling/lang/en_us.json @@ -11,6 +11,8 @@ "screen.toolleveling.tool_leveling_table.summary.field_title": "Summary", "screen.toolleveling.tool_leveling_table.summary.field_text": "Up to %s enchantment(s) will be added to your item, each with a level of %s", "screen.toolleveling.tool_leveling_table.summary.field_text.multi": "Up to %s enchantment(s) will be added to your item, each with a level between %s and %s", + "screen.toolleveling.tool_leveling_table.summary.field_text.iterations_too_low": "Enchantment count (%s) too low to upgrade, add or remove bonus items for a count of at least 1", + "screen.toolleveling.tool_leveling_table.summary.field_text.strength_too_low": "Enchantment strength (%s) too low to upgrade, add or remove bonus items for a strength of at least 1", "screen.toolleveling.tool_leveling_table.percentages.button_text": "Info", "screen.toolleveling.tool_leveling_table.percentages.button_tooltip": "Show/Hide the percentages", "screen.toolleveling.tool_leveling_table.percentages.field_title": "Chosen Enchantment", From ecb1ea2f3be9f002e8f9621349be91c3a792ee54 Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Sat, 23 Mar 2024 01:10:05 +0100 Subject: [PATCH 6/7] add a mimimum strength bonus --- .../toolleveling/client/ComponentUtil.java | 11 ++++++---- .../client/ToolLevelingTableScreen.java | 3 ++- .../config/ToolLevelingConfig.java | 22 ++++++++++++++++--- .../config/values/BonusIngredient.java | 8 ++++--- .../config/values/BonusIngredientsValue.java | 4 ++-- .../menu/ToolLevelingTableMenu.java | 4 ++++ .../network/packets/TableUpgradeProcess.java | 16 ++++++++++---- .../toolleveling/util/Predicates.java | 5 ++++- .../tristankechlo/toolleveling/util/Util.java | 13 +++++++++++ .../assets/toolleveling/lang/de_de.json | 1 + .../assets/toolleveling/lang/en_us.json | 1 + 11 files changed, 70 insertions(+), 18 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java index 47e74b1..96f94ba 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java @@ -54,7 +54,7 @@ public static Component makeHelpField(String str, Integer number) { return Component.translatable(START + str, number).withStyle(ChatFormatting.GRAY); } - public static Component makeSummary(String str, int iterations, int strength) { + public static Component makeSummary(String str, int iterations, int minStrength, int strength) { if (iterations <= 0) { MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); return Component.translatable(START + str + ".iterations_too_low", iterationsText).withStyle(ChatFormatting.GRAY); @@ -65,9 +65,12 @@ public static Component makeSummary(String str, int iterations, int strength) { } MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); MutableComponent strengthText = Component.literal("" + strength).withStyle(ChatFormatting.GREEN); - MutableComponent strText = Component.literal("1").withStyle(ChatFormatting.GREEN); - if (strength > 1) { - return Component.translatable(START + str + ".multi", iterationsText, strText, strengthText).withStyle(ChatFormatting.GRAY); + if (strength > minStrength) { + MutableComponent minStrText = Component.literal("" + minStrength).withStyle(ChatFormatting.GREEN); + if (minStrength < 0) { + minStrText = Component.translatable(START + ".min_strength_capped", minStrText); + } + return Component.translatable(START + str + ".multi", iterationsText, minStrText, strengthText).withStyle(ChatFormatting.GRAY); } else { return Component.translatable(START + str, iterationsText, strengthText).withStyle(ChatFormatting.GRAY); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java b/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java index ca4ad57..68a14ff 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java @@ -96,8 +96,9 @@ protected void containerTick() { // update bonus items int iterations = this.getMenu().getCycles(); + int minStrength = this.getMenu().getMinLevels(); int strength = this.getMenu().getLevels(); - Component summary = ComponentUtil.makeSummary(".summary.field_text", iterations, strength); + Component summary = ComponentUtil.makeSummary(".summary.field_text", iterations, minStrength, strength); int maxPossibleWidth = this.width - (this.leftPos + this.imageWidth) - 10; this.bonusItemField.setLines(List.of(ComponentUtil.TITLE_BONUSES, summary), font, maxPossibleWidth); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index 28827bf..2c3e0e6 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -21,6 +21,7 @@ public final class ToolLevelingConfig extends AbstractConfig { private final NumberValue requiredBookshelves; private final NumberValue requiredBooks; private final NumberValue baseIterations; + private final NumberValue baseMinStrength; private final NumberValue baseStrength; private final BonusIngredientsValue bonusIngredients; private final List> values; @@ -34,14 +35,15 @@ private ToolLevelingConfig() { requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); baseIterations = new NumberValue<>("base_num_enchantments", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); + baseMinStrength = new NumberValue<>("base_num_min_levels", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); baseStrength = new NumberValue<>("base_num_levels", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ - new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 1), - new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 1, 0), + new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 0, 1), + new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 0, 1, 0), }); values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, - baseIterations, baseStrength, bonusIngredients); + baseIterations, baseMinStrength, baseStrength, bonusIngredients); } @Override @@ -70,6 +72,10 @@ public int requiredBooks() { return requiredBooks.get(); } + public int getBaseMinStrength() { + return baseMinStrength.get(); + } + public int getBaseStrength() { return baseStrength.get(); } @@ -78,6 +84,16 @@ public int getBaseIterations() { return baseIterations.get(); } + public int getBonusItemMinStrength(ItemStack stack) { + int total = 0; + for (BonusIngredient bonus : bonusIngredients.get()) { + if (bonus.ingredient().test(stack)) { + total += bonus.minLevelBonus(); + } + } + return total; + } + @Deprecated public boolean isBonusItemStrength(ItemStack stack) { for (BonusIngredient bonus : bonusIngredients.get()) { diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java index 3ae0423..48af02e 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java @@ -4,19 +4,20 @@ import net.minecraft.util.GsonHelper; import net.minecraft.world.item.crafting.Ingredient; -public record BonusIngredient(Ingredient ingredient, int maxLevelBonus, int iterationsBonus) { +public record BonusIngredient(Ingredient ingredient, int minLevelBonus, int maxLevelBonus, int iterationsBonus) { public BonusIngredient { if (ingredient == null) { throw new NullPointerException("ingredient of the bonus ingredient can't be null"); } - if (maxLevelBonus == 0 && iterationsBonus == 0) { + if (minLevelBonus == 0 && maxLevelBonus == 0 && iterationsBonus == 0) { throw new NullPointerException("bonus ingredient must provide at least one bonus"); } } public static void serialize(BonusIngredient value, JsonObject json) { json.add("ingredient", value.ingredient().toJson()); + json.addProperty("min_level_bonus", value.minLevelBonus()); json.addProperty("max_level_bonus", value.maxLevelBonus()); json.addProperty("iterations_bonus", value.iterationsBonus()); } @@ -24,9 +25,10 @@ public static void serialize(BonusIngredient value, JsonObject json) { public static BonusIngredient deserialize(JsonObject json) { JsonObject obj = GsonHelper.getAsJsonObject(json, "ingredient"); Ingredient ingredient = Ingredient.fromJson(obj); + int minLevelBonus = GsonHelper.getAsInt(json, "min_level_bonus", 0); int maxLevelBonus = GsonHelper.getAsInt(json, "max_level_bonus", 0); int iterationsBonus = GsonHelper.getAsInt(json, "iterations_bonus", 0); - return new BonusIngredient(ingredient, maxLevelBonus, iterationsBonus); + return new BonusIngredient(ingredient, minLevelBonus, maxLevelBonus, iterationsBonus); } } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java index c18e77c..fff8e66 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java @@ -44,8 +44,8 @@ public void deserialize(JsonObject json) { JsonObject moreLevelsObj = GsonHelper.getAsJsonObject(json, "bonus_item_more_levels"); Ingredient iterationsBonusIngredient = Ingredient.fromJson(moreLevelsObj); value = new BonusIngredient[]{ - new BonusIngredient(maxLevelBonusIngredient, 0, 1), - new BonusIngredient(iterationsBonusIngredient, 1, 0) + new BonusIngredient(maxLevelBonusIngredient, 0, 0, 1), + new BonusIngredient(iterationsBonusIngredient, 0, 1, 0) }; } catch (Exception e) { value = defaultValue; diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java b/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java index c30bd04..043ee85 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java @@ -154,6 +154,10 @@ public int getCycles() { return Util.getIterations(this.table); } + public int getMinLevels() { + return Util.getEnchantmentMinStrength(this.table); + } + public int getLevels() { return Util.getEnchantmentStrength(this.table); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java index f5c4b16..bf424fa 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java @@ -45,6 +45,7 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { } int iterations = Util.getIterations(table); // how often the process will be repeated + int minStrength = Util.getEnchantmentMinStrength(table); // the minimum level of the enchantments int strength = Util.getEnchantmentStrength(table); // the maximum level of the enchantments var possibleEnchantments = table.getEnchantments(); // the possible enchantments, with their weight float successChance = Util.getSuccessChance(level, msg.pos); // the chance of a success in each iteration @@ -70,11 +71,18 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { continue; } Enchantment e = o.get().getData(); - int enchantmentLevel = level.getRandom().nextInt(strength) + 1; - if (enchantmentsToAdd.containsKey(e)) { // if the enchantment is already in the map, sum up the levels - enchantmentLevel += enchantmentsToAdd.get(e); + // strength wins if it is lower than minStrengtg + int enchantmentLevel = Math.min(minStrength, strength); + if (strength > minStrength) { + enchantmentLevel += level.getRandom().nextInt(strength - minStrength + 1); + } + // if minimum strength is below 1, the resulting level can be too - don't add or subtract in that case + if (enchantmentLevel > 0) { + if (enchantmentsToAdd.containsKey(e)) { // if the enchantment is already in the map, sum up the levels + enchantmentLevel += enchantmentsToAdd.get(e); + } + enchantmentsToAdd.put(e, enchantmentLevel); } - enchantmentsToAdd.put(e, enchantmentLevel); } // add the enchantments to the item diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java index 05267d0..ccb986d 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java @@ -13,12 +13,15 @@ public final class Predicates { public static final Predicate IS_BOOK = (stack) -> stack.is(Items.ENCHANTED_BOOK); // purple slots public static final Predicate IS_UPGRADE_ITEM = (stack) -> !IS_BOOK.test(stack) && (stack.isEnchanted() || stack.isEnchantable()); // red slot + public static final IntSupplier BASE_MIN_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBaseMinStrength; + public static final ToIntFunction BONUS_ITEM_MIN_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemMinStrength; + public static final Predicate BONUS_ITEM_MIN_STRENGTH = (stack) -> BONUS_ITEM_MIN_STRENGTH_VAL.applyAsInt(stack) != 0; public static final IntSupplier BASE_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBaseStrength; public static final ToIntFunction BONUS_ITEM_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemStrength; public static final Predicate BONUS_ITEM_STRENGTH = (stack) -> BONUS_ITEM_STRENGTH_VAL.applyAsInt(stack) != 0; public static final IntSupplier BASE_ITERATIONS_VAL= ToolLevelingConfig.INSTANCE::getBaseIterations; public static final ToIntFunction BONUS_ITEM_ITERATIONS_VAL = ToolLevelingConfig.INSTANCE::getBonusItemIterations; public static final Predicate BONUS_ITEM_ITERATIONS = (stack) -> BONUS_ITEM_ITERATIONS_VAL.applyAsInt(stack) != 0; - public static final Predicate IS_BONUS_ITEM = (stack) -> BONUS_ITEM_STRENGTH.test(stack) || BONUS_ITEM_ITERATIONS.test(stack); // green slots + public static final Predicate IS_BONUS_ITEM = (stack) -> BONUS_ITEM_MIN_STRENGTH.test(stack) || BONUS_ITEM_STRENGTH.test(stack) || BONUS_ITEM_ITERATIONS.test(stack); // green slots } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java index 76377d0..84fba55 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java @@ -28,6 +28,7 @@ private static boolean canUpgradeProcessBegin(Function f) { boolean upgradeSlotNotEmpty = !f.apply(0).isEmpty(); // the upgrade slot is not empty int iterations = getIterations(f); int strength = getEnchantmentStrength(f); + // no mimumum strength check, it can go below 1 or above strength (which is a waste) return enoughBooks && upgradeSlotNotEmpty && iterations > 0 && strength > 0; } @@ -73,6 +74,18 @@ private static int getIterations(Function f) { return count; } + public static int getEnchantmentMinStrength(Container menu) { + return getEnchantmentMinStrength(menu::getItem); + } + + private static int getEnchantmentMinStrength(Function f) { + int count = Predicates.BASE_MIN_STRENGTH_VAL.getAsInt(); + for (int i : BONUS_SLOTS) { + count += Predicates.BONUS_ITEM_MIN_STRENGTH_VAL.applyAsInt(f.apply(i)); + } + return count; + } + public static int getEnchantmentStrength(Container menu) { return getEnchantmentStrength(menu::getItem); } diff --git a/Common/src/main/resources/assets/toolleveling/lang/de_de.json b/Common/src/main/resources/assets/toolleveling/lang/de_de.json index d43a440..1f0722a 100644 --- a/Common/src/main/resources/assets/toolleveling/lang/de_de.json +++ b/Common/src/main/resources/assets/toolleveling/lang/de_de.json @@ -9,6 +9,7 @@ "screen.toolleveling.tool_leveling_table.summary.field_text.multi": "Bis zu %s Verzauberung(en) werden dem Item hinzugefügt, jede mit einer Stufe von %s bis %s", "screen.toolleveling.tool_leveling_table.summary.field_text.iterations_too_low": "Nicht genug Verzauberungen (%s) f\u00FCr das Upgrade, verwende andere besondere Gegenstände um mindestens 1 Verzauberung zu erreichen", "screen.toolleveling.tool_leveling_table.summary.field_text.strength_too_low": "Stufe der Verzauberung (%s) zu niedrig f\u00FCr das Upgrade, verwende andere besondere Gegenstände um mindestens Stufe 1 zu erreichen", + "screen.toolleveling.tool_leveling_table.min_strength_capped": "%s (resultierende Stufe mindestens 0)", "screen.toolleveling.tool_leveling_table.percentages.button_text": "Info", "screen.toolleveling.tool_leveling_table.percentages.button_tooltip": "Zeige die Wahrscheinlichkeiten f\u00FCr die Verzauberungen", "screen.toolleveling.tool_leveling_table.percentages.field_title": "Wahrscheinlichkeiten", diff --git a/Common/src/main/resources/assets/toolleveling/lang/en_us.json b/Common/src/main/resources/assets/toolleveling/lang/en_us.json index ca12386..94173eb 100644 --- a/Common/src/main/resources/assets/toolleveling/lang/en_us.json +++ b/Common/src/main/resources/assets/toolleveling/lang/en_us.json @@ -13,6 +13,7 @@ "screen.toolleveling.tool_leveling_table.summary.field_text.multi": "Up to %s enchantment(s) will be added to your item, each with a level between %s and %s", "screen.toolleveling.tool_leveling_table.summary.field_text.iterations_too_low": "Enchantment count (%s) too low to upgrade, add or remove bonus items for a count of at least 1", "screen.toolleveling.tool_leveling_table.summary.field_text.strength_too_low": "Enchantment strength (%s) too low to upgrade, add or remove bonus items for a strength of at least 1", + "screen.toolleveling.tool_leveling_table.min_strength_capped": "%s (result lower bounded by 0)", "screen.toolleveling.tool_leveling_table.percentages.button_text": "Info", "screen.toolleveling.tool_leveling_table.percentages.button_tooltip": "Show/Hide the percentages", "screen.toolleveling.tool_leveling_table.percentages.field_title": "Chosen Enchantment", From 0c435981d61aee07d40733ae00aff6f78f6f7ce4 Mon Sep 17 00:00:00 2001 From: KosmosPrime <5663514+KosmosPrime@users.noreply.github.com> Date: Sat, 23 Mar 2024 01:41:41 +0100 Subject: [PATCH 7/7] support floating-point bonus values --- .../toolleveling/client/ComponentUtil.java | 18 +++++-- .../client/ToolLevelingTableScreen.java | 6 +-- .../config/ToolLevelingConfig.java | 54 ++++++------------- .../config/values/BonusIngredient.java | 8 +-- .../config/values/BonusIngredientsValue.java | 4 +- .../menu/ToolLevelingTableMenu.java | 6 +-- .../network/packets/TableUpgradeProcess.java | 26 +++++---- .../toolleveling/util/Predicates.java | 14 ++--- .../tristankechlo/toolleveling/util/Util.java | 30 +++++------ 9 files changed, 78 insertions(+), 88 deletions(-) diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java index 96f94ba..a8b4041 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/client/ComponentUtil.java @@ -54,7 +54,17 @@ public static Component makeHelpField(String str, Integer number) { return Component.translatable(START + str, number).withStyle(ChatFormatting.GRAY); } - public static Component makeSummary(String str, int iterations, int minStrength, int strength) { + private static MutableComponent literalFloat(float value) { + int rounded = Math.round(value); + if (rounded == value) { + return Component.literal("" + rounded); + } + else { + return Component.literal("" + value); + } + } + + public static Component makeSummary(String str, float iterations, float minStrength, float strength) { if (iterations <= 0) { MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); return Component.translatable(START + str + ".iterations_too_low", iterationsText).withStyle(ChatFormatting.GRAY); @@ -63,10 +73,10 @@ public static Component makeSummary(String str, int iterations, int minStrength, MutableComponent iterationsText = Component.literal("" + strength).withStyle(ChatFormatting.GREEN); return Component.translatable(START + str + ".strength_too_low", iterationsText).withStyle(ChatFormatting.GRAY); } - MutableComponent iterationsText = Component.literal("" + iterations).withStyle(ChatFormatting.GREEN); - MutableComponent strengthText = Component.literal("" + strength).withStyle(ChatFormatting.GREEN); + MutableComponent iterationsText = literalFloat(iterations).withStyle(ChatFormatting.GREEN); + MutableComponent strengthText = literalFloat(strength).withStyle(ChatFormatting.GREEN); if (strength > minStrength) { - MutableComponent minStrText = Component.literal("" + minStrength).withStyle(ChatFormatting.GREEN); + MutableComponent minStrText = literalFloat(minStrength).withStyle(ChatFormatting.GREEN); if (minStrength < 0) { minStrText = Component.translatable(START + ".min_strength_capped", minStrText); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java b/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java index 68a14ff..9bf6c63 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/client/ToolLevelingTableScreen.java @@ -95,9 +95,9 @@ protected void containerTick() { this.successChanceField.setLines(List.of(ComponentUtil.TITLE_SUCCESS_CHANCE, chanceText, minChanceText, maxChanceText)); // update bonus items - int iterations = this.getMenu().getCycles(); - int minStrength = this.getMenu().getMinLevels(); - int strength = this.getMenu().getLevels(); + float iterations = this.getMenu().getCycles(); + float minStrength = this.getMenu().getMinLevels(); + float strength = this.getMenu().getLevels(); Component summary = ComponentUtil.makeSummary(".summary.field_text", iterations, minStrength, strength); int maxPossibleWidth = this.width - (this.leftPos + this.imageWidth) - 10; this.bonusItemField.setLines(List.of(ComponentUtil.TITLE_BONUSES, summary), font, maxPossibleWidth); diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java index 2c3e0e6..84d62f1 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/ToolLevelingConfig.java @@ -20,9 +20,9 @@ public final class ToolLevelingConfig extends AbstractConfig { private final NumberValue maxSuccessChance; private final NumberValue requiredBookshelves; private final NumberValue requiredBooks; - private final NumberValue baseIterations; - private final NumberValue baseMinStrength; - private final NumberValue baseStrength; + private final NumberValue baseIterations; + private final NumberValue baseMinStrength; + private final NumberValue baseStrength; private final BonusIngredientsValue bonusIngredients; private final List> values; public static final ToolLevelingConfig INSTANCE = new ToolLevelingConfig(); @@ -34,12 +34,12 @@ private ToolLevelingConfig() { maxSuccessChance = new NumberValue<>("max_success_chance", 100.0F, 0.0F, 100.0F, GsonHelper::getAsFloat); requiredBookshelves = new NumberValue<>("required_bookshelves", 20, 0, 32, GsonHelper::getAsInt); requiredBooks = new NumberValue<>("required_books", 4, 1, 6, GsonHelper::getAsInt); - baseIterations = new NumberValue<>("base_num_enchantments", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); - baseMinStrength = new NumberValue<>("base_num_min_levels", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); - baseStrength = new NumberValue<>("base_num_levels", 1, Integer.MIN_VALUE, Integer.MAX_VALUE, GsonHelper::getAsInt); + baseIterations = new NumberValue<>("base_num_enchantments", 1.0F, -Float.MAX_VALUE, Float.MAX_VALUE, GsonHelper::getAsFloat); + baseMinStrength = new NumberValue<>("base_num_min_levels", 1.0F, -Float.MAX_VALUE, Float.MAX_VALUE, GsonHelper::getAsFloat); + baseStrength = new NumberValue<>("base_num_levels", 1.0F, -Float.MAX_VALUE, Float.MAX_VALUE, GsonHelper::getAsFloat); bonusIngredients = new BonusIngredientsValue("bonus_ingredients", new BonusIngredient[]{ - new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0, 0, 1), - new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 0, 1, 0), + new BonusIngredient(Ingredient.of(Items.NETHER_STAR), 0.0F, 0.0F, 1.0F), + new BonusIngredient(Ingredient.of(Items.ENCHANTED_GOLDEN_APPLE), 0.0F, 1.0F, 0.0F), }); values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, @@ -72,20 +72,20 @@ public int requiredBooks() { return requiredBooks.get(); } - public int getBaseMinStrength() { + public float getBaseMinStrength() { return baseMinStrength.get(); } - public int getBaseStrength() { + public float getBaseStrength() { return baseStrength.get(); } - public int getBaseIterations() { + public float getBaseIterations() { return baseIterations.get(); } - public int getBonusItemMinStrength(ItemStack stack) { - int total = 0; + public float getBonusItemMinStrength(ItemStack stack) { + float total = 0; for (BonusIngredient bonus : bonusIngredients.get()) { if (bonus.ingredient().test(stack)) { total += bonus.minLevelBonus(); @@ -94,18 +94,8 @@ public int getBonusItemMinStrength(ItemStack stack) { return total; } - @Deprecated - public boolean isBonusItemStrength(ItemStack stack) { - for (BonusIngredient bonus : bonusIngredients.get()) { - if (bonus.ingredient().test(stack) && bonus.maxLevelBonus() != 0) { - return true; - } - } - return false; - } - - public int getBonusItemStrength(ItemStack stack) { - int total = 0; + public float getBonusItemStrength(ItemStack stack) { + float total = 0; for (BonusIngredient bonus : bonusIngredients.get()) { if (bonus.ingredient().test(stack)) { total += bonus.maxLevelBonus(); @@ -114,18 +104,8 @@ public int getBonusItemStrength(ItemStack stack) { return total; } - @Deprecated - public boolean isBonusItemIterations(ItemStack stack) { - for (BonusIngredient bonus : bonusIngredients.get()) { - if (bonus.ingredient().test(stack) && bonus.iterationsBonus() != 0) { - return true; - } - } - return false; - } - - public int getBonusItemIterations(ItemStack stack) { - int total = 0; + public float getBonusItemIterations(ItemStack stack) { + float total = 0; for (BonusIngredient bonus : bonusIngredients.get()) { if (bonus.ingredient().test(stack)) { total += bonus.iterationsBonus(); diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java index 48af02e..d5b9163 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredient.java @@ -4,7 +4,7 @@ import net.minecraft.util.GsonHelper; import net.minecraft.world.item.crafting.Ingredient; -public record BonusIngredient(Ingredient ingredient, int minLevelBonus, int maxLevelBonus, int iterationsBonus) { +public record BonusIngredient(Ingredient ingredient, float minLevelBonus, float maxLevelBonus, float iterationsBonus) { public BonusIngredient { if (ingredient == null) { @@ -25,9 +25,9 @@ public static void serialize(BonusIngredient value, JsonObject json) { public static BonusIngredient deserialize(JsonObject json) { JsonObject obj = GsonHelper.getAsJsonObject(json, "ingredient"); Ingredient ingredient = Ingredient.fromJson(obj); - int minLevelBonus = GsonHelper.getAsInt(json, "min_level_bonus", 0); - int maxLevelBonus = GsonHelper.getAsInt(json, "max_level_bonus", 0); - int iterationsBonus = GsonHelper.getAsInt(json, "iterations_bonus", 0); + float minLevelBonus = GsonHelper.getAsFloat(json, "min_level_bonus", 0.0F); + float maxLevelBonus = GsonHelper.getAsFloat(json, "max_level_bonus", 0.0F); + float iterationsBonus = GsonHelper.getAsFloat(json, "iterations_bonus", 0.0F); return new BonusIngredient(ingredient, minLevelBonus, maxLevelBonus, iterationsBonus); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java index fff8e66..afcc7bb 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/config/values/BonusIngredientsValue.java @@ -44,8 +44,8 @@ public void deserialize(JsonObject json) { JsonObject moreLevelsObj = GsonHelper.getAsJsonObject(json, "bonus_item_more_levels"); Ingredient iterationsBonusIngredient = Ingredient.fromJson(moreLevelsObj); value = new BonusIngredient[]{ - new BonusIngredient(maxLevelBonusIngredient, 0, 0, 1), - new BonusIngredient(iterationsBonusIngredient, 0, 1, 0) + new BonusIngredient(maxLevelBonusIngredient, 0.0F, 0.0F, 1.0F), + new BonusIngredient(iterationsBonusIngredient, 0.0F, 1.0F, 0.0F) }; } catch (Exception e) { value = defaultValue; diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java b/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java index 043ee85..f92c9be 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/menu/ToolLevelingTableMenu.java @@ -150,15 +150,15 @@ public boolean hasAnyBooks() { return false; } - public int getCycles() { + public float getCycles() { return Util.getIterations(this.table); } - public int getMinLevels() { + public float getMinLevels() { return Util.getEnchantmentMinStrength(this.table); } - public int getLevels() { + public float getLevels() { return Util.getEnchantmentStrength(this.table); } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java index bf424fa..ee2289b 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/network/packets/TableUpgradeProcess.java @@ -44,9 +44,13 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { return; } - int iterations = Util.getIterations(table); // how often the process will be repeated - int minStrength = Util.getEnchantmentMinStrength(table); // the minimum level of the enchantments - int strength = Util.getEnchantmentStrength(table); // the maximum level of the enchantments + float iterations = Util.getIterations(table); // how often the process will be repeated + int trueIterations = (int) Math.floor(iterations); + if (iterations > trueIterations && level.getRandom().nextFloat() < iterations - trueIterations) { + trueIterations++; + } + float minStrength = Util.getEnchantmentMinStrength(table); // the minimum level of the enchantments + float strength = Util.getEnchantmentStrength(table); // the maximum level of the enchantments var possibleEnchantments = table.getEnchantments(); // the possible enchantments, with their weight float successChance = Util.getSuccessChance(level, msg.pos); // the chance of a success in each iteration ItemStack tool = table.getStackToEnchant(); // the tool to enchant @@ -59,7 +63,7 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { } // calculate the enchantments to add - for (int i = 0; i < iterations; i++) { + for (int i = 0; i < trueIterations; i++) { float nextFloat = level.getRandom().nextFloat(); if (nextFloat > successChance) { ToolLeveling.LOGGER.info("TableUpgradeProcess failed {} {}", successChance, nextFloat); @@ -72,16 +76,20 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) { } Enchantment e = o.get().getData(); // strength wins if it is lower than minStrengtg - int enchantmentLevel = Math.min(minStrength, strength); + float enchantmentLevel = Math.min(minStrength, strength); if (strength > minStrength) { - enchantmentLevel += level.getRandom().nextInt(strength - minStrength + 1); + enchantmentLevel += level.getRandom().nextFloat() * (strength - minStrength); + } + int trueEnchantmentLevel = (int) Math.floor(enchantmentLevel); + if (enchantmentLevel > trueEnchantmentLevel && level.getRandom().nextFloat() < enchantmentLevel - trueEnchantmentLevel) { + trueEnchantmentLevel++; } // if minimum strength is below 1, the resulting level can be too - don't add or subtract in that case - if (enchantmentLevel > 0) { + if (trueEnchantmentLevel > 0) { if (enchantmentsToAdd.containsKey(e)) { // if the enchantment is already in the map, sum up the levels - enchantmentLevel += enchantmentsToAdd.get(e); + trueEnchantmentLevel += enchantmentsToAdd.get(e); } - enchantmentsToAdd.put(e, enchantmentLevel); + enchantmentsToAdd.put(e, trueEnchantmentLevel); } } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java index ccb986d..3fc7b14 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Predicates.java @@ -4,24 +4,16 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -import java.util.function.IntSupplier; import java.util.function.Predicate; -import java.util.function.ToIntFunction; public final class Predicates { public static final Predicate IS_BOOK = (stack) -> stack.is(Items.ENCHANTED_BOOK); // purple slots public static final Predicate IS_UPGRADE_ITEM = (stack) -> !IS_BOOK.test(stack) && (stack.isEnchanted() || stack.isEnchantable()); // red slot - public static final IntSupplier BASE_MIN_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBaseMinStrength; - public static final ToIntFunction BONUS_ITEM_MIN_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemMinStrength; - public static final Predicate BONUS_ITEM_MIN_STRENGTH = (stack) -> BONUS_ITEM_MIN_STRENGTH_VAL.applyAsInt(stack) != 0; - public static final IntSupplier BASE_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBaseStrength; - public static final ToIntFunction BONUS_ITEM_STRENGTH_VAL = ToolLevelingConfig.INSTANCE::getBonusItemStrength; - public static final Predicate BONUS_ITEM_STRENGTH = (stack) -> BONUS_ITEM_STRENGTH_VAL.applyAsInt(stack) != 0; - public static final IntSupplier BASE_ITERATIONS_VAL= ToolLevelingConfig.INSTANCE::getBaseIterations; - public static final ToIntFunction BONUS_ITEM_ITERATIONS_VAL = ToolLevelingConfig.INSTANCE::getBonusItemIterations; - public static final Predicate BONUS_ITEM_ITERATIONS = (stack) -> BONUS_ITEM_ITERATIONS_VAL.applyAsInt(stack) != 0; + public static final Predicate BONUS_ITEM_MIN_STRENGTH = (stack) -> ToolLevelingConfig.INSTANCE.getBonusItemMinStrength(stack) != 0.0F; + public static final Predicate BONUS_ITEM_STRENGTH = (stack) -> ToolLevelingConfig.INSTANCE.getBonusItemStrength(stack) != 0.0F; + public static final Predicate BONUS_ITEM_ITERATIONS = (stack) -> ToolLevelingConfig.INSTANCE.getBonusItemIterations(stack) != 0.0F; public static final Predicate IS_BONUS_ITEM = (stack) -> BONUS_ITEM_MIN_STRENGTH.test(stack) || BONUS_ITEM_STRENGTH.test(stack) || BONUS_ITEM_ITERATIONS.test(stack); // green slots } diff --git a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java index 84fba55..d526dac 100644 --- a/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java +++ b/Common/src/main/java/com/tristankechlo/toolleveling/util/Util.java @@ -26,11 +26,11 @@ private static boolean canUpgradeProcessBegin(Function f) { } boolean enoughBooks = bookCount >= ToolLevelingConfig.INSTANCE.requiredBooks(); // the minimum number of books is reached boolean upgradeSlotNotEmpty = !f.apply(0).isEmpty(); // the upgrade slot is not empty - int iterations = getIterations(f); - int strength = getEnchantmentStrength(f); + float iterations = getIterations(f); + float strength = getEnchantmentStrength(f); // no mimumum strength check, it can go below 1 or above strength (which is a waste) - return enoughBooks && upgradeSlotNotEmpty && iterations > 0 && strength > 0; + return enoughBooks && upgradeSlotNotEmpty && iterations > 0.0F && strength > 0.0F; } public static boolean canUpgradeProcessBegin(AbstractContainerMenu menu) { @@ -62,38 +62,38 @@ public static float getSuccessChance(Level level, BlockPos tablePos) { return minPercentage + ((maxPercentage - minPercentage) * fullPercent); } - public static int getIterations(Container menu) { + public static float getIterations(Container menu) { return getIterations(menu::getItem); } - private static int getIterations(Function f) { - int count = Predicates.BASE_ITERATIONS_VAL.getAsInt(); + private static float getIterations(Function f) { + float count = ToolLevelingConfig.INSTANCE.getBaseIterations(); for (int i : BONUS_SLOTS) { - count += Predicates.BONUS_ITEM_ITERATIONS_VAL.applyAsInt(f.apply(i)); + count += ToolLevelingConfig.INSTANCE.getBonusItemIterations(f.apply(i)); } return count; } - public static int getEnchantmentMinStrength(Container menu) { + public static float getEnchantmentMinStrength(Container menu) { return getEnchantmentMinStrength(menu::getItem); } - private static int getEnchantmentMinStrength(Function f) { - int count = Predicates.BASE_MIN_STRENGTH_VAL.getAsInt(); + private static float getEnchantmentMinStrength(Function f) { + float count = ToolLevelingConfig.INSTANCE.getBaseMinStrength(); for (int i : BONUS_SLOTS) { - count += Predicates.BONUS_ITEM_MIN_STRENGTH_VAL.applyAsInt(f.apply(i)); + count += ToolLevelingConfig.INSTANCE.getBonusItemMinStrength(f.apply(i)); } return count; } - public static int getEnchantmentStrength(Container menu) { + public static float getEnchantmentStrength(Container menu) { return getEnchantmentStrength(menu::getItem); } - private static int getEnchantmentStrength(Function f) { - int count = Predicates.BASE_STRENGTH_VAL.getAsInt(); + private static float getEnchantmentStrength(Function f) { + float count = ToolLevelingConfig.INSTANCE.getBaseStrength(); for (int i : BONUS_SLOTS) { - count += Predicates.BONUS_ITEM_STRENGTH_VAL.applyAsInt(f.apply(i)); + count += ToolLevelingConfig.INSTANCE.getBonusItemStrength(f.apply(i)); } return count; }