Unicopia/src/main/java/com/minelittlepony/unicopia/mixin/MixinLootableContainerBlockEntity.java

80 lines
3 KiB
Java
Raw Normal View History

2024-04-18 22:09:15 +02:00
package com.minelittlepony.unicopia.mixin;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.entity.mob.MimicEntity;
import net.minecraft.block.entity.LockableContainerBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.nbt.NbtCompound;
2024-04-18 22:09:15 +02:00
import net.minecraft.screen.ScreenHandler;
import net.minecraft.util.Identifier;
2024-04-18 22:09:15 +02:00
@Mixin(LootableContainerBlockEntity.class)
abstract class MixinLootableContainerBlockEntity extends LockableContainerBlockEntity implements MimicEntity.MimicGeneratable {
private Identifier mimicLootTable;
private boolean allowMimics = true;
private boolean isMimic;
@Shadow
@Nullable
private Identifier lootTableId;
2024-04-18 22:09:15 +02:00
MixinLootableContainerBlockEntity() { super(null, null, null); }
@Inject(method = "deserializeLootTable", at = @At("HEAD"))
private void deserializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) {
isMimic = nbt.getBoolean("mimic");
}
@Inject(method = "serializeLootTable", at = @At("HEAD"))
private void serializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) {
nbt.putBoolean("mimic", isMimic);
}
@Override
public void setAllowMimics(boolean allowMimics) {
this.allowMimics = allowMimics;
this.isMimic &= allowMimics;
markDirty();
}
@Override
public void setMimic(boolean mimic) {
isMimic = mimic;
markDirty();
}
@Inject(method = "checkLootInteraction", at = @At("HEAD"))
2024-04-18 22:09:15 +02:00
private void onCheckLootInteraction(@Nullable PlayerEntity player, CallbackInfo info) {
if (player != null && allowMimics && lootTableId != null) {
mimicLootTable = lootTableId;
2024-04-18 22:09:15 +02:00
}
}
@Inject(
method = "createMenu",
at = @At(
value = "INVOKE",
target = "net/minecraft/block/entity/LootableContainerBlockEntity.checkLootInteraction(Lnet/minecraft/entity/player/PlayerEntity;)V",
shift = Shift.AFTER
), cancellable = true)
private void onCreateMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player, CallbackInfoReturnable<ScreenHandler> info) {
if (player != null && (isMimic || (allowMimics && MimicEntity.shouldConvert(player.getWorld(), getPos(), player, mimicLootTable)))) {
var mimic = MimicEntity.spawnFromChest(player.getWorld(), getPos());
if (mimic != null) {
info.setReturnValue(mimic.createScreenHandler(syncId, playerInventory, player));
2024-04-18 22:09:15 +02:00
}
mimicLootTable = null;
2024-04-18 22:09:15 +02:00
}
}
}