From 9f6826ee7d7f9ea10a7ad4eb40aec9958cf56f52 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 13 Oct 2022 18:15:21 +0200 Subject: [PATCH] Fix spells not being saved beyond the first --- .../unicopia/InteractionManager.java | 2 +- .../unicopia/entity/Living.java | 2 ++ .../unicopia/entity/player/Pony.java | 33 ++++++++++--------- .../network/MsgOtherPlayerCapabilities.java | 4 +-- .../network/MsgPlayerCapabilities.java | 20 ++++------- .../network/MsgRequestSpeciesChange.java | 2 +- .../unicopia/network/datasync/EffectSync.java | 13 +++++++- .../datasync/NetworkedReferenceSet.java | 2 +- .../network/datasync/Transmittable.java | 8 ++++- 9 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java index 92e1a6ff..0fa2f9c6 100644 --- a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java @@ -88,7 +88,7 @@ public class InteractionManager { * Returns an implementation of PlayerEntity appropriate to the side being called on. */ @NotNull - public PlayerEntity createPlayer(Entity observer, GameProfile profile) { + public final PlayerEntity createPlayer(Entity observer, GameProfile profile) { return createPlayer(observer.world, profile); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 0b80b87e..4e10b746 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -233,11 +233,13 @@ public abstract class Living implements Equine, Caste @Override public void toNBT(NbtCompound compound) { enchants.toNBT(compound); + effectDelegate.toNBT(compound); } @Override public void fromNBT(NbtCompound compound) { enchants.fromNBT(compound); + effectDelegate.fromNBT(compound); } public void updateVelocity() { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index a6e46aa6..b0d619d2 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -239,16 +239,19 @@ public class Pony extends Living implements Transmittable, Copieab return getSpecies().getAffinity(); } + @Override public void setDirty() { dirty = true; } - @Override - public void sendCapabilities(boolean full) { + private void sendCapabilities() { + if (!dirty) { + return; + } dirty = false; if (entity instanceof ServerPlayerEntity) { - MsgOtherPlayerCapabilities packet = new MsgOtherPlayerCapabilities(full, this); + MsgOtherPlayerCapabilities packet = new MsgOtherPlayerCapabilities(this); Channel.SERVER_PLAYER_CAPABILITIES.send((ServerPlayerEntity)entity, packet); Channel.SERVER_OTHER_PLAYER_CAPABILITIES.send(entity.world, packet); } @@ -420,9 +423,7 @@ public class Pony extends Living implements Transmittable, Copieab super.tick(); - if (dirty) { - sendCapabilities(true); - } + sendCapabilities(); } public Optional modifyDamage(DamageSource cause, float amount) { @@ -541,6 +542,11 @@ public class Pony extends Living implements Transmittable, Copieab @Override public void toNBT(NbtCompound compound) { super.toNBT(compound); + toSyncronisedNbt(compound); + } + + @Override + public void toSyncronisedNbt(NbtCompound compound) { compound.putString("playerSpecies", Race.REGISTRY.getId(getActualSpecies()).toString()); compound.putFloat("magicExhaustion", magicExhaustion); compound.putInt("ticksHanging", ticksHanging); @@ -555,10 +561,6 @@ public class Pony extends Living implements Transmittable, Copieab compound.putInt("levels", levels.get()); compound.putInt("corruption", corruption.get()); - getSpellSlot().get(true).ifPresent(effect ->{ - compound.put("effect", Spell.writeNbt(effect)); - }); - NbtCompound progress = new NbtCompound(); advancementProgress.forEach((key, count) -> { progress.putInt(key, count); @@ -569,6 +571,11 @@ public class Pony extends Living implements Transmittable, Copieab @Override public void fromNBT(NbtCompound compound) { super.fromNBT(compound); + fromSynchronizedNbt(compound); + } + + @Override + public void fromSynchronizedNbt(NbtCompound compound) { speciesPersisted = true; setSpecies(Race.fromName(compound.getString("playerSpecies"), Race.HUMAN)); powers.fromNBT(compound.getCompound("powers")); @@ -585,10 +592,6 @@ public class Pony extends Living implements Transmittable, Copieab ticksInSun = compound.getInt("ticksInSun"); hasShades = compound.getBoolean("hasShades"); - if (compound.contains("effect")) { - getSpellSlot().put(Spell.readNbt(compound.getCompound("effect"))); - } - NbtCompound progress = compound.getCompound("advancementProgress"); advancementProgress.clear(); for (String key : progress.getKeys()) { @@ -624,8 +627,8 @@ public class Pony extends Living implements Transmittable, Copieab getCorruption().add(1); } getCorruption().add((int)spell.getTraits().getCorruption()); + setDirty(); } - setDirty(); } public boolean isClientPlayer() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java index 9e8ff416..4318ad26 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java @@ -15,8 +15,8 @@ public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities { super(buffer); } - public MsgOtherPlayerCapabilities(boolean full, Pony player) { - super(full, player); + public MsgOtherPlayerCapabilities(Pony player) { + super(player); } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java index 2609986b..555c9832 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java @@ -5,7 +5,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; -import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.util.network.Packet; @@ -26,13 +25,10 @@ public class MsgPlayerCapabilities implements Packet { protected final UUID playerId; - private final Race newRace; - private final NbtCompound compoundTag; MsgPlayerCapabilities(PacketByteBuf buffer) { playerId = buffer.readUuid(); - newRace = buffer.readRegistryValue(Race.REGISTRY); try (InputStream in = new ByteBufInputStream(buffer)) { compoundTag = NbtIo.readCompressed(in); } catch (IOException e) { @@ -40,16 +36,15 @@ public class MsgPlayerCapabilities implements Packet { } } - public MsgPlayerCapabilities(boolean full, Pony player) { - playerId = player.getMaster().getUuid(); - newRace = player.getActualSpecies(); - compoundTag = full ? player.toNBT() : new NbtCompound(); + public MsgPlayerCapabilities(Pony player) { + playerId = player.getEntity().getUuid(); + compoundTag = new NbtCompound(); + player.toSyncronisedNbt(compoundTag); } @Override public void toBuffer(PacketByteBuf buffer) { buffer.writeUuid(playerId); - buffer.writeRegistryValue(Race.REGISTRY, newRace); try (OutputStream out = new ByteBufOutputStream(buffer)) { NbtIo.writeCompressed(compoundTag, out); } catch (IOException e) { @@ -63,11 +58,8 @@ public class MsgPlayerCapabilities implements Packet { Unicopia.LOGGER.warn("Skipping capabilities for unknown player " + playerId.toString()); return; } - if (compoundTag.isEmpty()) { - player.setSpecies(newRace); - } else { - player.fromNBT(compoundTag); - } + + player.fromSynchronizedNbt(compoundTag); } protected Pony getRecipient(PlayerEntity sender) { diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java index fd583928..add53d72 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestSpeciesChange.java @@ -53,6 +53,6 @@ public class MsgRequestSpeciesChange implements Packet { } } - Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(true, player)); + Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(player)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java index 6e4dd1ca..6b224cd4 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java @@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.SpellContainer; import com.minelittlepony.unicopia.ability.magic.SpellPredicate; import com.minelittlepony.unicopia.ability.magic.spell.Spell; +import com.minelittlepony.unicopia.util.NbtSerialisable; import net.minecraft.entity.data.TrackedData; import net.minecraft.nbt.NbtCompound; @@ -23,7 +24,7 @@ import net.minecraft.nbt.NbtCompound; * * @param The owning entity */ -public class EffectSync implements SpellContainer { +public class EffectSync implements SpellContainer, NbtSerialisable { private final NetworkedReferenceSet spells; @@ -138,6 +139,16 @@ public class EffectSync implements SpellContainer { } } + @Override + public void toNBT(NbtCompound compound) { + compound.put("spells", spells.toNbt()); + } + + @Override + public void fromNBT(NbtCompound compound) { + spells.fromNbt(compound.getCompound("spells")); + } + public interface UpdateCallback { void onSpellSet(@Nullable Spell spell); } diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java index 5b9e1482..4b9084db 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/NetworkedReferenceSet.java @@ -55,8 +55,8 @@ public class NetworkedReferenceSet { } } finally { reading = false; + values.clear(); } - values.clear(); return dirty; } diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/Transmittable.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/Transmittable.java index a45a23a2..8895bd77 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/Transmittable.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/Transmittable.java @@ -1,5 +1,11 @@ package com.minelittlepony.unicopia.network.datasync; +import net.minecraft.nbt.NbtCompound; + public interface Transmittable { - void sendCapabilities(boolean full); + void setDirty(); + + void toSyncronisedNbt(NbtCompound compound); + + void fromSynchronizedNbt(NbtCompound compound); }