Add max anvil cost config and EnchantCommand mixin

Introduces a configurable max anvil cost via the config and enforces it in AnvilScreenHandlerMixin. Adds EnchantCommandMixin to support custom enchantment levels in the /enchant command. Updates mod version to 1.10 and registers the new mixin in the mixins config.
This commit is contained in:
2025-12-28 19:37:26 +01:00
parent a560e64029
commit 86dd3c2b22
5 changed files with 72 additions and 2 deletions

View File

@@ -32,6 +32,7 @@ public class Config
public static boolean NO_PRIOR_WORK_PENALTY = true;
public static boolean ENABLE_CUSTOM_ENCHANT_LEVELS = true;
public static boolean CUSTOM_LEVELS_IN_LOOT_TABLE = false;
public static int MAX_ANVIL_COST = -1;
public static Map<Identifier, Integer> LEVELS = new HashMap<>(Map.ofEntries(
Map.entry(Identifier.of("minecraft:unbreaking"), 5),
@@ -148,6 +149,10 @@ public class Config
{
CUSTOM_LEVELS_IN_LOOT_TABLE = data.get("custom-levels-in-loot-table").getAsBoolean();
}
if (data.has("max-anvil-cost"))
{
MAX_ANVIL_COST = data.get("max-anvil-cost").getAsInt();
}
if (data.has("custom-levels"))
{
JsonObject levels = data.get("custom-levels").getAsJsonObject();
@@ -182,6 +187,7 @@ public class Config
data.addProperty("no-prior-work-penalty", NO_PRIOR_WORK_PENALTY);
data.addProperty("enable-custom-enchant-levels", ENABLE_CUSTOM_ENCHANT_LEVELS);
data.addProperty("custom-levels-in-loot-table", CUSTOM_LEVELS_IN_LOOT_TABLE);
data.addProperty("max-anvil-cost", MAX_ANVIL_COST);
JsonObject levels = new JsonObject();
LEVELS.forEach((id, level) -> levels.addProperty(id.toString(), level));
data.add("custom-levels", levels);

View File

@@ -1,16 +1,59 @@
package com.kasetoatz.superenchants.mixin;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.lang.reflect.Field;
import java.util.List;
import static com.kasetoatz.superenchants.config.Config.MAX_ANVIL_COST;
import static com.kasetoatz.superenchants.util.Util.getCustomLevel;
@Mixin(AnvilScreenHandler.class)
public class AnvilScreenHandlerMixin
{
@Unique private Field properties;
@Unique private Field syncHandler;
@Inject(method="<init>(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/screen/ScreenHandlerContext;)V", at=@At("TAIL"))
public void init(int syncId, PlayerInventory inventory, ScreenHandlerContext context, CallbackInfo ci)
{
try
{
properties = ScreenHandler.class.getDeclaredField("properties");
properties.setAccessible(true);
syncHandler = ScreenHandler.class.getDeclaredField("syncHandler");
syncHandler.setAccessible(true);
}
catch (NoSuchFieldException ignored) {}
}
@Redirect(method="updateResult", at=@At(value="INVOKE", target="Lnet/minecraft/screen/Property;get()I"))
public int getLevel(Property levelCost)
{
if (MAX_ANVIL_COST > -1 && levelCost.get() > MAX_ANVIL_COST)
{
levelCost.set(MAX_ANVIL_COST);
try
{
@SuppressWarnings("unchecked")
int index = ((List<Property>)properties.get(this)).indexOf(levelCost);
@SuppressWarnings("DataFlowIssue")
AnvilScreenHandler handler = (AnvilScreenHandler)(Object)this;
((ScreenHandlerSyncHandler)syncHandler.get(this)).updateProperty(handler, index, levelCost.get());
}
catch (IllegalAccessException ignored) {}
}
return levelCost.get();
}
@Redirect(method="updateResult", at=@At(value="INVOKE", target="Lnet/minecraft/enchantment/Enchantment;getMaxLevel()I"))
public int getMaxLevel(Enchantment enchant)
{

View File

@@ -0,0 +1,20 @@
package com.kasetoatz.superenchants.mixin;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.server.command.EnchantCommand;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import static com.kasetoatz.superenchants.util.Util.getCustomLevel;
@Mixin(EnchantCommand.class)
public class EnchantCommandMixin
{
@Redirect(method="execute", at= @At(value = "INVOKE", target = "Lnet/minecraft/enchantment/Enchantment;getMaxLevel()I"))
private static int execute(Enchantment enchant)
{
int custom = getCustomLevel(enchant);
return (custom > 0) ? custom : enchant.getMaxLevel();
}
}