Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,36 @@ 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) {
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";
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);
}
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 = literalFloat(iterations).withStyle(ChatFormatting.GREEN);
MutableComponent strengthText = literalFloat(strength).withStyle(ChatFormatting.GREEN);
if (strength > minStrength) {
MutableComponent minStrText = literalFloat(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);
}
return Component.translatable(START + str, iterationsText, strText, strengthText).withStyle(ChatFormatting.GRAY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ protected void containerTick() {
this.successChanceField.setLines(List.of(ComponentUtil.TITLE_SUCCESS_CHANCE, chanceText, minChanceText, maxChanceText));

// update bonus items
int iterations = this.getMenu().getCycles();
int strength = this.getMenu().getLevels();
Component summary = ComponentUtil.makeSummary(".summary.field_text", iterations, strength);
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,8 +20,10 @@ public final class ToolLevelingConfig extends AbstractConfig {
private final NumberValue<Float> maxSuccessChance;
private final NumberValue<Integer> requiredBookshelves;
private final NumberValue<Integer> requiredBooks;
private final IngredientValue bonusItemMoreEnchantments;
private final IngredientValue bonusItemMoreLevels;
private final NumberValue<Float> baseIterations;
private final NumberValue<Float> baseMinStrength;
private final NumberValue<Float> baseStrength;
private final BonusIngredientsValue bonusIngredients;
private final List<AbstractConfigValue<?>> values;
public static final ToolLevelingConfig INSTANCE = new ToolLevelingConfig();

Expand All @@ -31,10 +34,16 @@ 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));

values = List.of(minSuccessChance, maxSuccessChance, requiredBookshelves, requiredBooks, bonusItemMoreEnchantments, bonusItemMoreLevels);
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.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,
baseIterations, baseMinStrength, baseStrength, bonusIngredients);
}

@Override
Expand Down Expand Up @@ -63,12 +72,46 @@ public int requiredBooks() {
return requiredBooks.get();
}

public boolean isBonusItemStrength(ItemStack stack) {
return bonusItemMoreLevels.get().test(stack);
public float getBaseMinStrength() {
return baseMinStrength.get();
}

public float getBaseStrength() {
return baseStrength.get();
}

public float getBaseIterations() {
return baseIterations.get();
}

public float getBonusItemMinStrength(ItemStack stack) {
float total = 0;
for (BonusIngredient bonus : bonusIngredients.get()) {
if (bonus.ingredient().test(stack)) {
total += bonus.minLevelBonus();
}
}
return total;
}

public float getBonusItemStrength(ItemStack stack) {
float total = 0;
for (BonusIngredient bonus : bonusIngredients.get()) {
if (bonus.ingredient().test(stack)) {
total += bonus.maxLevelBonus();
}
}
return total;
}

public boolean isBonusItemIterations(ItemStack stack) {
return bonusItemMoreEnchantments.get().test(stack);
public float getBonusItemIterations(ItemStack stack) {
float total = 0;
for (BonusIngredient bonus : bonusIngredients.get()) {
if (bonus.ingredient().test(stack)) {
total += bonus.iterationsBonus();
}
}
return total;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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, float minLevelBonus, float maxLevelBonus, float iterationsBonus) {

public BonusIngredient {
if (ingredient == null) {
throw new NullPointerException("ingredient of the bonus ingredient can't be null");
}
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());
}

public static BonusIngredient deserialize(JsonObject json) {
JsonObject obj = GsonHelper.getAsJsonObject(json, "ingredient");
Ingredient ingredient = Ingredient.fromJson(obj);
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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;
import net.minecraft.world.item.crafting.Ingredient;

public final class BonusIngredientsValue extends AbstractConfigValue<BonusIngredient[]> {

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) {
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.0F, 0.0F, 1.0F),
new BonusIngredient(iterationsBonusIngredient, 0.0F, 1.0F, 0.0F)
};
} 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");
}
}
}

@Override
public BonusIngredient[] get() {
return value;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@ public boolean hasAnyBooks() {
return false;
}

public int getCycles() {
public float getCycles() {
return Util.getIterations(this.table);
}

public int getLevels() {
public float getMinLevels() {
return Util.getEnchantmentMinStrength(this.table);
}

public float getLevels() {
return Util.getEnchantmentStrength(this.table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ public static void handle(TableUpgradeProcess msg, ServerLevel level) {
return;
}

int iterations = Util.getIterations(table); // how often the process will be repeated
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
Expand All @@ -58,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);
Expand All @@ -70,16 +75,27 @@ 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
float enchantmentLevel = Math.min(minStrength, strength);
if (strength > minStrength) {
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 (trueEnchantmentLevel > 0) {
if (enchantmentsToAdd.containsKey(e)) { // if the enchantment is already in the map, sum up the levels
trueEnchantmentLevel += enchantmentsToAdd.get(e);
}
enchantmentsToAdd.put(e, trueEnchantmentLevel);
}
enchantmentsToAdd.put(e, enchantmentLevel);
}

// 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();
Expand Down
Loading