Fix crash when loading without trinkets. Fixes #60

This commit is contained in:
Sollace 2022-09-22 11:59:37 +02:00
parent 69772d084d
commit 534a9d000a
3 changed files with 17 additions and 9 deletions

View file

@ -5,7 +5,8 @@ import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.Redirect;
import dev.emi.trinkets.TrinketSlot; import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.Slot;
@ -37,7 +38,7 @@ abstract class MixinScreenHandler {
) )
// redirect slot.getMaxItemCount() to stack aware version // redirect slot.getMaxItemCount() to stack aware version
protected int onGetMaxItemCount(Slot sender, ItemStack stack) { protected int onGetMaxItemCount(Slot sender, ItemStack stack) {
return canApply(sender) ? sender.getMaxItemCount(stack) : sender.getMaxItemCount(); return TrinketsDelegate.getInstance().isTrinketSlot(sender) ? sender.getMaxItemCount(stack) : sender.getMaxItemCount();
} }
@Redirect(method = "insertItem", @Redirect(method = "insertItem",
@ -49,7 +50,7 @@ abstract class MixinScreenHandler {
) )
// redirect "if (!itemStack.isEmpty() && ItemStack.canCombine(stack, itemStack))" -> "if (!canNotInsert(itemStack, slot) && ItemStack.canCombine(stack, itemStack))" // redirect "if (!itemStack.isEmpty() && ItemStack.canCombine(stack, itemStack))" -> "if (!canNotInsert(itemStack, slot) && ItemStack.canCombine(stack, itemStack))"
protected boolean canNotInsert(ItemStack sender) { protected boolean canNotInsert(ItemStack sender) {
return sender.isEmpty() || (canApply(currentSlot) && (currentSlot.getStack().getCount() + sender.getCount()) <= currentSlot.getMaxItemCount(sender)); return sender.isEmpty() || (TrinketsDelegate.getInstance().isTrinketSlot(currentSlot) && (currentSlot.getStack().getCount() + sender.getCount()) <= currentSlot.getMaxItemCount(sender));
} }
@Redirect(method = "canInsertItemIntoSlot", @Redirect(method = "canInsertItemIntoSlot",
@ -58,11 +59,7 @@ abstract class MixinScreenHandler {
target = "Lnet/minecraft/item/ItemStack;getMaxCount()I" target = "Lnet/minecraft/item/ItemStack;getMaxCount()I"
) )
) )
private static int onCanInsertItemIntoSlot(ItemStack sender, @Nullable Slot slot) { private static int onGetMaxCount(ItemStack sender, @Nullable Slot slot) {
return canApply(slot) ? slot.getMaxItemCount(sender) : sender.getMaxCount(); return TrinketsDelegate.getInstance().isTrinketSlot(slot) ? slot.getMaxItemCount(sender) : sender.getMaxCount();
}
private static boolean canApply(Slot slot) {
return slot instanceof TrinketSlot;
} }
} }

View file

@ -9,6 +9,7 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
public interface TrinketsDelegate { public interface TrinketsDelegate {
@ -69,4 +70,8 @@ public interface TrinketsDelegate {
default void registerTrinket(Item item) { default void registerTrinket(Item item) {
} }
default boolean isTrinketSlot(Slot slot) {
return false;
}
} }

View file

@ -12,6 +12,7 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.world.event.GameEvent; import net.minecraft.world.event.GameEvent;
@ -80,6 +81,11 @@ public class TrinketsDelegateImpl implements TrinketsDelegate {
.flatMap(group -> group.values().stream()); .flatMap(group -> group.values().stream());
} }
@Override
public boolean isTrinketSlot(Slot slot) {
return slot instanceof TrinketSlot;
}
private static Identifier getSlotId(SlotType slotType) { private static Identifier getSlotId(SlotType slotType) {
return new Identifier(slotType.getGroup(), slotType.getName()); return new Identifier(slotType.getGroup(), slotType.getName());
} }