From 7695ad21719badebec7f1df4e2c3669e1a26367d Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Sat, 4 Mar 2023 23:52:33 +0100 Subject: [PATCH] Fix fake players' uuid changing on reload --- changelog.md | 6 ++++++ gradle.properties | 2 +- .../mixins/player/PlayerEntityMixin.java | 20 +++++++++++++++++++ .../ladysnake/otomaton/OtomatonTestSuite.java | 12 +++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 697a8335..c6b8eb9c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +------------------------------------------------------ +Version 0.9.1 +------------------------------------------------------ +**Fixes** +- Fixed fake players' UUID resetting with each world reload + ------------------------------------------------------ Version 0.9.0 ------------------------------------------------------ diff --git a/gradle.properties b/gradle.properties index 59953c1c..dd61e131 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,7 +20,7 @@ org.gradle.jvmargs = -Xmx3G mod_name = Automatone -mod_version = 0.9.0 +mod_version = 0.9.1 maven_group = io.github.ladysnake minecraft_version=1.19.1 diff --git a/src/launch/java/baritone/launch/mixins/player/PlayerEntityMixin.java b/src/launch/java/baritone/launch/mixins/player/PlayerEntityMixin.java index b775260a..6fc59294 100644 --- a/src/launch/java/baritone/launch/mixins/player/PlayerEntityMixin.java +++ b/src/launch/java/baritone/launch/mixins/player/PlayerEntityMixin.java @@ -36,21 +36,31 @@ import baritone.api.fakeplayer.AutomatoneFakePlayer; import baritone.api.fakeplayer.FakeServerPlayerEntity; +import com.mojang.authlib.GameProfile; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.Slice; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(PlayerEntity.class) public abstract class PlayerEntityMixin extends LivingEntity { + + @Shadow @Final @Mutable + private GameProfile gameProfile; + protected PlayerEntityMixin(EntityType entityType, World world) { super(entityType, world); } @@ -62,6 +72,16 @@ private void allowFakePlayerSave(CallbackInfoReturnable cir) { } } + /** + * @reason minecraft overwrites the uuid when reading data, which breaks anything that tries to find this entity after a reload + */ + @Inject(method = "readCustomDataFromNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;readCustomDataFromNbt(Lnet/minecraft/nbt/NbtCompound;)V")) + private void bringBackUuid(NbtCompound nbt, CallbackInfo ci) { + if (this instanceof AutomatoneFakePlayer) { + this.gameProfile = new GameProfile(this.getUuid(), this.gameProfile.getName()); + } + } + /** * Minecraft cancels knockback for players and instead relies entirely on the client for handling the velocity change. * This injection cancels the cancellation for fake players, as they do not have an associated client. diff --git a/src/testmod/java/io/github/ladysnake/otomaton/OtomatonTestSuite.java b/src/testmod/java/io/github/ladysnake/otomaton/OtomatonTestSuite.java index e32f0b4f..5a14227c 100644 --- a/src/testmod/java/io/github/ladysnake/otomaton/OtomatonTestSuite.java +++ b/src/testmod/java/io/github/ladysnake/otomaton/OtomatonTestSuite.java @@ -28,6 +28,7 @@ import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; import net.minecraft.network.NetworkSide; import net.minecraft.network.packet.s2c.play.SystemMessageS2CPacket; import net.minecraft.server.network.ServerPlayNetworkHandler; @@ -82,6 +83,17 @@ public void shellsDoNotPreventSleeping(TestContext ctx) { ctx.complete(); } + + @GameTest(structureName = EMPTY_STRUCTURE) + public void shellsKeepUuidOnReload(TestContext ctx) { + ServerPlayerEntity fakePlayer = new FakeServerPlayerEntity(Otomaton.FAKE_PLAYER, ctx.getWorld()); + ServerPlayerEntity fakePlayer2 = new FakeServerPlayerEntity(Otomaton.FAKE_PLAYER, ctx.getWorld()); + NbtCompound nbtCompound = fakePlayer.writeNbt(new NbtCompound()); + fakePlayer2.readNbt(nbtCompound); + GameTestUtil.assertTrue("Fake players should keep UUID after reload", fakePlayer2.getUuid().equals(fakePlayer.getUuid())); + ctx.complete(); + } + @GameTest(structureName = EMPTY_STRUCTURE) public void realPlayersDoBroadcastAdvancements(TestContext ctx) { ServerPlayerEntity player = ((ElmendorfTestContext) ctx).spawnServerPlayer(1, 0, 1);