Skip to content

Commit

Permalink
Fix fake players' uuid changing on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Mar 4, 2023
1 parent 3f971e0 commit 7695ad2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
------------------------------------------------------
Version 0.9.1
------------------------------------------------------
**Fixes**
- Fixed fake players' UUID resetting with each world reload

------------------------------------------------------
Version 0.9.0
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}
Expand All @@ -62,6 +72,16 @@ private void allowFakePlayerSave(CallbackInfoReturnable<Boolean> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7695ad2

Please sign in to comment.