diff --git a/src/main/java/com/astrazoey/indexed/ConfigMain.java b/src/main/java/com/astrazoey/indexed/ConfigMain.java index fce2017..0c22ac5 100644 --- a/src/main/java/com/astrazoey/indexed/ConfigMain.java +++ b/src/main/java/com/astrazoey/indexed/ConfigMain.java @@ -34,6 +34,9 @@ public class ConfigMain { private static final int linearXpAmountDefault = 30; public static int linearXpAmount = linearXpAmountDefault; + private static final String ENABLE_OVERCHARGE = "enable-overenchanting"; + public static boolean enableOvercharge = true; + public static void save() { Properties props = new Properties(); read(props); @@ -101,6 +104,7 @@ public static void read(Properties props) { props.setProperty(ENABLE_VILLAGER_NERFS_KEY, String.valueOf(enableVillagerNerfs)); props.setProperty(ENABLE_LINEAR_XP, String.valueOf(enableLinearXp)); props.setProperty(LINEAR_XP_AMOUNT, String.valueOf(linearXpAmount)); + props.setProperty(ENABLE_OVERCHARGE, String.valueOf(enableOvercharge)); } public static void assign(Properties props) { @@ -110,6 +114,7 @@ public static void assign(Properties props) { enableVillagerNerfs = defaultBoolean(props.getProperty(ENABLE_VILLAGER_NERFS_KEY), true); enableLinearXp = defaultBoolean(props.getProperty(ENABLE_LINEAR_XP), true); linearXpAmount = defaultInt(props.getProperty(LINEAR_XP_AMOUNT), linearXpAmountDefault); + enableOvercharge = defaultBoolean(props.getProperty(ENABLE_OVERCHARGE), true); } private static boolean defaultBoolean(String bool, boolean defaultOption) { diff --git a/src/main/java/com/astrazoey/indexed/mixins/EnchantmentHelperMixin.java b/src/main/java/com/astrazoey/indexed/mixins/EnchantmentHelperMixin.java index f57b5d8..585e964 100644 --- a/src/main/java/com/astrazoey/indexed/mixins/EnchantmentHelperMixin.java +++ b/src/main/java/com/astrazoey/indexed/mixins/EnchantmentHelperMixin.java @@ -1,9 +1,11 @@ package com.astrazoey.indexed.mixins; +import com.astrazoey.indexed.ConfigMain; import com.astrazoey.indexed.Indexed; import com.astrazoey.indexed.MaxEnchantingSlots; import com.astrazoey.indexed.registry.IndexedItems; import com.google.common.collect.Lists; +import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.advancement.criterion.EnchantedItemCriterion; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; @@ -19,6 +21,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.random.Random; import net.minecraft.world.World; +import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.*; @@ -26,6 +29,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.List; +import java.util.function.Consumer; import java.util.stream.Stream; import static java.lang.Math.min; @@ -67,6 +71,25 @@ private static void onStoppedUsing(ServerWorld world, ItemStack rangedWeaponStac } } + @ModifyArg(method = "generateEnchantments", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ifPresent(Ljava/util/function/Consumer;)V", ordinal = 1)) + private static @NotNull Consumer returnEarlyIfOverBudget(Consumer action, @Local(ordinal = 0) List list, @Local(ordinal = 0) ItemStack stack) { + // if overcharge is enabled, return default value. Else, run custom function + return ConfigMain.enableOvercharge ? action : (enchant) -> { + // get the list of enchants in itemstack form, so that I can use MaxEnchantingSlots functions + ItemStack stackCopy = stack.copy(); + for (EnchantmentLevelEntry e : list) { + stackCopy.addEnchantment(e.enchantment(), e.level()); + } + + int maxSlots = MaxEnchantingSlots.getEnchantType(stackCopy).maxEnchantingSlots; + int usedSlots = MaxEnchantingSlots.getCurrent(stackCopy); + + //only add the enchantment if it fits in the remaining slots + if (usedSlots + enchant.level() <= maxSlots) { + list.add(enchant); + } + }; + } @Inject(method = "getPossibleEntries", at = @At(value = "HEAD"), cancellable = true) @@ -161,11 +184,15 @@ public void getPlayerEnchantedLevel(ItemStack itemStack, int i, PlayerEntity pla //Take Enchanted Status Effect Into Account @Redirect(method="method_17410", at = @At(value="INVOKE", target = "Lnet/minecraft/item/ItemStack;addEnchantment(Lnet/minecraft/registry/entry/RegistryEntry;I)V")) public void enchantedStatusEffect(ItemStack instance, RegistryEntry enchantment, int level) { + int newEnchantmentLevel; if(effectLevel != null) { - int newEnchantmentLevel = min(level+effectLevel.get(), enchantment.value().getMaxLevel()); - instance.addEnchantment(enchantment, newEnchantmentLevel); + newEnchantmentLevel = min(level+effectLevel.get(), enchantment.value().getMaxLevel()); } else { - instance.addEnchantment(enchantment, level); + newEnchantmentLevel = level; + } + + if (ConfigMain.enableOvercharge || MaxEnchantingSlots.getCurrent(instance) + newEnchantmentLevel <= MaxEnchantingSlots.getEnchantType(instance).maxEnchantingSlots) { + instance.addEnchantment(enchantment, newEnchantmentLevel); } }