fix nullpointerexception

This commit is contained in:
2025-12-28 20:10:57 +01:00
parent 86dd3c2b22
commit 81ed0e65dd
2 changed files with 12 additions and 18 deletions

View File

@@ -1,14 +1,11 @@
package com.kasetoatz.superenchants.mixin;
import net.minecraft.enchantment.Enchantment;
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;
@@ -22,19 +19,6 @@ 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)
{
@@ -43,13 +27,23 @@ public class AnvilScreenHandlerMixin
levelCost.set(MAX_ANVIL_COST);
try
{
if (properties == null)
{
properties = ScreenHandler.class.getDeclaredField("properties");
properties.setAccessible(true);
}
if (syncHandler == null)
{
syncHandler = ScreenHandler.class.getDeclaredField("syncHandler");
syncHandler.setAccessible(true);
}
@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) {}
catch (NoSuchFieldException | IllegalAccessException ignored) {}
}
return levelCost.get();
}