Fix spells not being saved beyond the first

This commit is contained in:
Sollace 2022-10-13 18:15:21 +02:00
parent c84d938a8b
commit 9f6826ee7d
9 changed files with 50 additions and 36 deletions

View file

@ -88,7 +88,7 @@ public class InteractionManager {
* Returns an implementation of PlayerEntity appropriate to the side being called on. * Returns an implementation of PlayerEntity appropriate to the side being called on.
*/ */
@NotNull @NotNull
public PlayerEntity createPlayer(Entity observer, GameProfile profile) { public final PlayerEntity createPlayer(Entity observer, GameProfile profile) {
return createPlayer(observer.world, profile); return createPlayer(observer.world, profile);
} }

View file

@ -233,11 +233,13 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
@Override @Override
public void toNBT(NbtCompound compound) { public void toNBT(NbtCompound compound) {
enchants.toNBT(compound); enchants.toNBT(compound);
effectDelegate.toNBT(compound);
} }
@Override @Override
public void fromNBT(NbtCompound compound) { public void fromNBT(NbtCompound compound) {
enchants.fromNBT(compound); enchants.fromNBT(compound);
effectDelegate.fromNBT(compound);
} }
public void updateVelocity() { public void updateVelocity() {

View file

@ -239,16 +239,19 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
return getSpecies().getAffinity(); return getSpecies().getAffinity();
} }
@Override
public void setDirty() { public void setDirty() {
dirty = true; dirty = true;
} }
@Override private void sendCapabilities() {
public void sendCapabilities(boolean full) { if (!dirty) {
return;
}
dirty = false; dirty = false;
if (entity instanceof ServerPlayerEntity) { 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_PLAYER_CAPABILITIES.send((ServerPlayerEntity)entity, packet);
Channel.SERVER_OTHER_PLAYER_CAPABILITIES.send(entity.world, packet); Channel.SERVER_OTHER_PLAYER_CAPABILITIES.send(entity.world, packet);
} }
@ -420,9 +423,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
super.tick(); super.tick();
if (dirty) { sendCapabilities();
sendCapabilities(true);
}
} }
public Optional<Float> modifyDamage(DamageSource cause, float amount) { public Optional<Float> modifyDamage(DamageSource cause, float amount) {
@ -541,6 +542,11 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
@Override @Override
public void toNBT(NbtCompound compound) { public void toNBT(NbtCompound compound) {
super.toNBT(compound); super.toNBT(compound);
toSyncronisedNbt(compound);
}
@Override
public void toSyncronisedNbt(NbtCompound compound) {
compound.putString("playerSpecies", Race.REGISTRY.getId(getActualSpecies()).toString()); compound.putString("playerSpecies", Race.REGISTRY.getId(getActualSpecies()).toString());
compound.putFloat("magicExhaustion", magicExhaustion); compound.putFloat("magicExhaustion", magicExhaustion);
compound.putInt("ticksHanging", ticksHanging); compound.putInt("ticksHanging", ticksHanging);
@ -555,10 +561,6 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
compound.putInt("levels", levels.get()); compound.putInt("levels", levels.get());
compound.putInt("corruption", corruption.get()); compound.putInt("corruption", corruption.get());
getSpellSlot().get(true).ifPresent(effect ->{
compound.put("effect", Spell.writeNbt(effect));
});
NbtCompound progress = new NbtCompound(); NbtCompound progress = new NbtCompound();
advancementProgress.forEach((key, count) -> { advancementProgress.forEach((key, count) -> {
progress.putInt(key, count); progress.putInt(key, count);
@ -569,6 +571,11 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
@Override @Override
public void fromNBT(NbtCompound compound) { public void fromNBT(NbtCompound compound) {
super.fromNBT(compound); super.fromNBT(compound);
fromSynchronizedNbt(compound);
}
@Override
public void fromSynchronizedNbt(NbtCompound compound) {
speciesPersisted = true; speciesPersisted = true;
setSpecies(Race.fromName(compound.getString("playerSpecies"), Race.HUMAN)); setSpecies(Race.fromName(compound.getString("playerSpecies"), Race.HUMAN));
powers.fromNBT(compound.getCompound("powers")); powers.fromNBT(compound.getCompound("powers"));
@ -585,10 +592,6 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
ticksInSun = compound.getInt("ticksInSun"); ticksInSun = compound.getInt("ticksInSun");
hasShades = compound.getBoolean("hasShades"); hasShades = compound.getBoolean("hasShades");
if (compound.contains("effect")) {
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
}
NbtCompound progress = compound.getCompound("advancementProgress"); NbtCompound progress = compound.getCompound("advancementProgress");
advancementProgress.clear(); advancementProgress.clear();
for (String key : progress.getKeys()) { for (String key : progress.getKeys()) {
@ -624,9 +627,9 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
getCorruption().add(1); getCorruption().add(1);
} }
getCorruption().add((int)spell.getTraits().getCorruption()); getCorruption().add((int)spell.getTraits().getCorruption());
}
setDirty(); setDirty();
} }
}
public boolean isClientPlayer() { public boolean isClientPlayer() {
return InteractionManager.instance().isClientPlayer(getMaster()); return InteractionManager.instance().isClientPlayer(getMaster());

View file

@ -15,8 +15,8 @@ public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities {
super(buffer); super(buffer);
} }
public MsgOtherPlayerCapabilities(boolean full, Pony player) { public MsgOtherPlayerCapabilities(Pony player) {
super(full, player); super(player);
} }

View file

@ -5,7 +5,6 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.UUID; import java.util.UUID;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.network.Packet; import com.minelittlepony.unicopia.util.network.Packet;
@ -26,13 +25,10 @@ public class MsgPlayerCapabilities implements Packet<PlayerEntity> {
protected final UUID playerId; protected final UUID playerId;
private final Race newRace;
private final NbtCompound compoundTag; private final NbtCompound compoundTag;
MsgPlayerCapabilities(PacketByteBuf buffer) { MsgPlayerCapabilities(PacketByteBuf buffer) {
playerId = buffer.readUuid(); playerId = buffer.readUuid();
newRace = buffer.readRegistryValue(Race.REGISTRY);
try (InputStream in = new ByteBufInputStream(buffer)) { try (InputStream in = new ByteBufInputStream(buffer)) {
compoundTag = NbtIo.readCompressed(in); compoundTag = NbtIo.readCompressed(in);
} catch (IOException e) { } catch (IOException e) {
@ -40,16 +36,15 @@ public class MsgPlayerCapabilities implements Packet<PlayerEntity> {
} }
} }
public MsgPlayerCapabilities(boolean full, Pony player) { public MsgPlayerCapabilities(Pony player) {
playerId = player.getMaster().getUuid(); playerId = player.getEntity().getUuid();
newRace = player.getActualSpecies(); compoundTag = new NbtCompound();
compoundTag = full ? player.toNBT() : new NbtCompound(); player.toSyncronisedNbt(compoundTag);
} }
@Override @Override
public void toBuffer(PacketByteBuf buffer) { public void toBuffer(PacketByteBuf buffer) {
buffer.writeUuid(playerId); buffer.writeUuid(playerId);
buffer.writeRegistryValue(Race.REGISTRY, newRace);
try (OutputStream out = new ByteBufOutputStream(buffer)) { try (OutputStream out = new ByteBufOutputStream(buffer)) {
NbtIo.writeCompressed(compoundTag, out); NbtIo.writeCompressed(compoundTag, out);
} catch (IOException e) { } catch (IOException e) {
@ -63,11 +58,8 @@ public class MsgPlayerCapabilities implements Packet<PlayerEntity> {
Unicopia.LOGGER.warn("Skipping capabilities for unknown player " + playerId.toString()); Unicopia.LOGGER.warn("Skipping capabilities for unknown player " + playerId.toString());
return; return;
} }
if (compoundTag.isEmpty()) {
player.setSpecies(newRace); player.fromSynchronizedNbt(compoundTag);
} else {
player.fromNBT(compoundTag);
}
} }
protected Pony getRecipient(PlayerEntity sender) { protected Pony getRecipient(PlayerEntity sender) {

View file

@ -53,6 +53,6 @@ public class MsgRequestSpeciesChange implements Packet<ServerPlayerEntity> {
} }
} }
Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(true, player)); Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(player));
} }
} }

View file

@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.SpellContainer; import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate; import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.Spell; import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedData;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
@ -23,7 +24,7 @@ import net.minecraft.nbt.NbtCompound;
* *
* @param <T> The owning entity * @param <T> The owning entity
*/ */
public class EffectSync implements SpellContainer { public class EffectSync implements SpellContainer, NbtSerialisable {
private final NetworkedReferenceSet<Spell> spells; private final NetworkedReferenceSet<Spell> 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 { public interface UpdateCallback {
void onSpellSet(@Nullable Spell spell); void onSpellSet(@Nullable Spell spell);
} }

View file

@ -55,8 +55,8 @@ public class NetworkedReferenceSet<T> {
} }
} finally { } finally {
reading = false; reading = false;
}
values.clear(); values.clear();
}
return dirty; return dirty;
} }

View file

@ -1,5 +1,11 @@
package com.minelittlepony.unicopia.network.datasync; package com.minelittlepony.unicopia.network.datasync;
import net.minecraft.nbt.NbtCompound;
public interface Transmittable { public interface Transmittable {
void sendCapabilities(boolean full); void setDirty();
void toSyncronisedNbt(NbtCompound compound);
void fromSynchronizedNbt(NbtCompound compound);
} }