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:
@@ -7,7 +7,7 @@ yarn_mappings=1.21.11+build.2
|
|||||||
loader_version=0.18.2
|
loader_version=0.18.2
|
||||||
loom_version=1.14-SNAPSHOT
|
loom_version=1.14-SNAPSHOT
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version=1.9
|
mod_version=1.10
|
||||||
maven_group=com.kasetoatz
|
maven_group=com.kasetoatz
|
||||||
archives_base_name=SuperEnchants
|
archives_base_name=SuperEnchants
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ public class Config
|
|||||||
public static boolean NO_PRIOR_WORK_PENALTY = true;
|
public static boolean NO_PRIOR_WORK_PENALTY = true;
|
||||||
public static boolean ENABLE_CUSTOM_ENCHANT_LEVELS = true;
|
public static boolean ENABLE_CUSTOM_ENCHANT_LEVELS = true;
|
||||||
public static boolean CUSTOM_LEVELS_IN_LOOT_TABLE = false;
|
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(
|
public static Map<Identifier, Integer> LEVELS = new HashMap<>(Map.ofEntries(
|
||||||
Map.entry(Identifier.of("minecraft:unbreaking"), 5),
|
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();
|
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"))
|
if (data.has("custom-levels"))
|
||||||
{
|
{
|
||||||
JsonObject levels = data.get("custom-levels").getAsJsonObject();
|
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("no-prior-work-penalty", NO_PRIOR_WORK_PENALTY);
|
||||||
data.addProperty("enable-custom-enchant-levels", ENABLE_CUSTOM_ENCHANT_LEVELS);
|
data.addProperty("enable-custom-enchant-levels", ENABLE_CUSTOM_ENCHANT_LEVELS);
|
||||||
data.addProperty("custom-levels-in-loot-table", CUSTOM_LEVELS_IN_LOOT_TABLE);
|
data.addProperty("custom-levels-in-loot-table", CUSTOM_LEVELS_IN_LOOT_TABLE);
|
||||||
|
data.addProperty("max-anvil-cost", MAX_ANVIL_COST);
|
||||||
JsonObject levels = new JsonObject();
|
JsonObject levels = new JsonObject();
|
||||||
LEVELS.forEach((id, level) -> levels.addProperty(id.toString(), level));
|
LEVELS.forEach((id, level) -> levels.addProperty(id.toString(), level));
|
||||||
data.add("custom-levels", levels);
|
data.add("custom-levels", levels);
|
||||||
|
|||||||
@@ -1,16 +1,59 @@
|
|||||||
package com.kasetoatz.superenchants.mixin;
|
package com.kasetoatz.superenchants.mixin;
|
||||||
|
|
||||||
import net.minecraft.enchantment.Enchantment;
|
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.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Unique;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
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.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;
|
import static com.kasetoatz.superenchants.util.Util.getCustomLevel;
|
||||||
|
|
||||||
@Mixin(AnvilScreenHandler.class)
|
@Mixin(AnvilScreenHandler.class)
|
||||||
public class AnvilScreenHandlerMixin
|
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"))
|
@Redirect(method="updateResult", at=@At(value="INVOKE", target="Lnet/minecraft/enchantment/Enchantment;getMaxLevel()I"))
|
||||||
public int getMaxLevel(Enchantment enchant)
|
public int getMaxLevel(Enchantment enchant)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
"ChangeItemDamageEnchantmentEffectMixin",
|
"ChangeItemDamageEnchantmentEffectMixin",
|
||||||
"ComponentHolderMixin",
|
"ComponentHolderMixin",
|
||||||
"CrossbowItemMixin",
|
"CrossbowItemMixin",
|
||||||
|
"EnchantCommandMixin",
|
||||||
"EnchantmentMixin",
|
"EnchantmentMixin",
|
||||||
"MerchantEntityMixin",
|
"MerchantEntityMixin",
|
||||||
"PersistentProjectileMixin",
|
"PersistentProjectileMixin",
|
||||||
|
|||||||
Reference in New Issue
Block a user