Fixed mimics rolling multiple times

This commit is contained in:
Sollace 2024-05-19 18:03:28 +01:00
parent 715519e1f1
commit 648f1838e7
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB

View file

@ -10,6 +10,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.entity.mob.MimicEntity; import com.minelittlepony.unicopia.entity.mob.MimicEntity;
import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.block.entity.LockableContainerBlockEntity; import net.minecraft.block.entity.LockableContainerBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity; import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -22,7 +23,7 @@ import net.minecraft.util.Identifier;
abstract class MixinLootableContainerBlockEntity extends LockableContainerBlockEntity implements MimicEntity.MimicGeneratable { abstract class MixinLootableContainerBlockEntity extends LockableContainerBlockEntity implements MimicEntity.MimicGeneratable {
private Identifier mimicLootTable; private Identifier mimicLootTable;
private boolean allowMimics = true; private boolean allowMimics = true;
private boolean isMimic; private TriState isMimic = TriState.DEFAULT;
@Shadow @Shadow
@Nullable @Nullable
@ -32,24 +33,25 @@ abstract class MixinLootableContainerBlockEntity extends LockableContainerBlockE
@Inject(method = "deserializeLootTable", at = @At("HEAD")) @Inject(method = "deserializeLootTable", at = @At("HEAD"))
private void deserializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) { private void deserializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) {
isMimic = nbt.getBoolean("mimic"); isMimic = nbt.contains("mimic") ? TriState.of(nbt.getBoolean("mimic")) : TriState.DEFAULT;
} }
@Inject(method = "serializeLootTable", at = @At("HEAD")) @Inject(method = "serializeLootTable", at = @At("HEAD"))
private void serializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) { private void serializeMimic(NbtCompound nbt, CallbackInfoReturnable<Boolean> info) {
nbt.putBoolean("mimic", isMimic); if (isMimic != TriState.DEFAULT) {
nbt.putBoolean("mimic", isMimic.get());
}
} }
@Override @Override
public void setAllowMimics(boolean allowMimics) { public void setAllowMimics(boolean allowMimics) {
this.allowMimics = allowMimics; this.allowMimics = allowMimics;
this.isMimic &= allowMimics;
markDirty(); markDirty();
} }
@Override @Override
public void setMimic(boolean mimic) { public void setMimic(boolean mimic) {
isMimic = mimic; isMimic = TriState.of(mimic);
markDirty(); markDirty();
} }
@ -68,10 +70,16 @@ abstract class MixinLootableContainerBlockEntity extends LockableContainerBlockE
shift = Shift.AFTER shift = Shift.AFTER
), cancellable = true) ), cancellable = true)
private void onCreateMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player, CallbackInfoReturnable<ScreenHandler> info) { private void onCreateMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player, CallbackInfoReturnable<ScreenHandler> info) {
if (player != null && (isMimic || (allowMimics && MimicEntity.shouldConvert(player.getWorld(), getPos(), player, mimicLootTable)))) { if (player != null && allowMimics) {
var mimic = MimicEntity.spawnFromChest(player.getWorld(), getPos()); if (isMimic == TriState.DEFAULT) {
if (mimic != null) { isMimic = TriState.of(MimicEntity.shouldConvert(player.getWorld(), getPos(), player, mimicLootTable));
info.setReturnValue(mimic.createScreenHandler(syncId, playerInventory, player)); }
if (isMimic.get()) {
var mimic = MimicEntity.spawnFromChest(player.getWorld(), getPos());
if (mimic != null) {
info.setReturnValue(mimic.createScreenHandler(syncId, playerInventory, player));
}
} }
mimicLootTable = null; mimicLootTable = null;
} }