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.
*/
@NotNull
public PlayerEntity createPlayer(Entity observer, GameProfile profile) {
public final PlayerEntity createPlayer(Entity observer, GameProfile profile) {
return createPlayer(observer.world, profile);
}

View file

@ -233,11 +233,13 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, 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() {

View file

@ -239,16 +239,19 @@ public class Pony extends Living<PlayerEntity> 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<PlayerEntity> implements Transmittable, Copieab
super.tick();
if (dirty) {
sendCapabilities(true);
}
sendCapabilities();
}
public Optional<Float> modifyDamage(DamageSource cause, float amount) {
@ -541,6 +542,11 @@ public class Pony extends Living<PlayerEntity> 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<PlayerEntity> 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<PlayerEntity> 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<PlayerEntity> 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<PlayerEntity> implements Transmittable, Copieab
getCorruption().add(1);
}
getCorruption().add((int)spell.getTraits().getCorruption());
setDirty();
}
setDirty();
}
public boolean isClientPlayer() {

View file

@ -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);
}

View file

@ -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<PlayerEntity> {
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<PlayerEntity> {
}
}
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<PlayerEntity> {
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) {

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.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 <T> The owning entity
*/
public class EffectSync implements SpellContainer {
public class EffectSync implements SpellContainer, NbtSerialisable {
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 {
void onSpellSet(@Nullable Spell spell);
}

View file

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

View file

@ -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);
}