Skip to content

Commit 94fe9f6

Browse files
committed
Fixed fidelity data persistence
1 parent 46687e2 commit 94fe9f6

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/main/java/org/scbrm/fidelity/mixin/HorseBaseEntityMixin.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.scbrm.fidelity.mixin;
22

3+
import net.minecraft.nbt.CompoundTag;
4+
import net.minecraft.server.ServerConfigHandler;
35
import org.scbrm.fidelity.bridge.IHorseBaseEntity;
46
import org.scbrm.fidelity.entity.ai.goal.ObeyMasterGoal;
57
import net.minecraft.entity.EntityType;
@@ -23,7 +25,7 @@
2325
@Mixin(HorseBaseEntity.class)
2426
public abstract class HorseBaseEntityMixin extends AnimalEntity implements IHorseBaseEntity {
2527

26-
private static final TrackedData<Optional<UUID>> MASTER_UUID;
28+
private static final TrackedData<Optional<UUID>> MASTER_UUID = DataTracker.registerData(HorseBaseEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);
2729
private State state = State.ROAMING_FREE;
2830

2931
/*@Shadow protected abstract void spawnPlayerReactionParticles(boolean positive);*/
@@ -70,7 +72,29 @@ public void setState(@NotNull State state) {
7072
this.state = state;
7173
}
7274

73-
static {
74-
MASTER_UUID = DataTracker.registerData(HorseBaseEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);
75+
@Inject(at = @At("TAIL"), method = "writeCustomDataToTag(Lnet/minecraft/nbt/CompoundTag;)V")
76+
public void _writeCustomDataToTag(CompoundTag tag, CallbackInfo info) {
77+
if (this.getMasterUuid() != null) {
78+
tag.putUuid("Master", this.getMasterUuid());
79+
}
80+
81+
tag.putByte("FidelityState", (byte)this.state.ordinal());
7582
}
83+
84+
@Inject(at = @At("TAIL"), method = "readCustomDataFromTag(Lnet/minecraft/nbt/CompoundTag;)V")
85+
public void _readCustomDataFromTag(CompoundTag tag, CallbackInfo info) {
86+
final UUID uuid = tag.containsUuid("Master") ?
87+
tag.getUuid("Master") :
88+
ServerConfigHandler.getPlayerUuidByName(this.getServer(), tag.getString("Master"));
89+
90+
if (uuid != null) {
91+
try {
92+
this.setMasterUuid(uuid);
93+
this.state = State.values()[tag.getByte("FidelityState")];
94+
} catch (Throwable e) {
95+
this.state = State.ROAMING_FREE;
96+
}
97+
}
98+
}
99+
76100
}

0 commit comments

Comments
 (0)