From 469aaeecd1612691d2841e1faee718a9606c1b83 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 21 Sep 2024 21:40:20 +0100 Subject: [PATCH] Remove Spell#isDirty and Spell#setDirty --- .../ability/magic/MultiSpellSlot.java | 2 +- .../magic/spell/AbstractDelegatingSpell.java | 12 ---- .../magic/spell/AbstractDisguiseSpell.java | 2 +- .../ability/magic/spell/EmptySpell.java | 8 --- .../magic/spell/PlacementControlSpell.java | 64 +++++++++---------- .../unicopia/ability/magic/spell/Spell.java | 12 ---- .../ability/magic/spell/SpellReference.java | 5 -- .../magic/spell/effect/AbstractSpell.java | 14 ---- .../magic/spell/effect/AttractiveSpell.java | 6 +- .../magic/spell/effect/AwkwardSpell.java | 3 +- .../magic/spell/effect/BubbleSpell.java | 9 +-- .../magic/spell/effect/FeatherFallSpell.java | 3 +- .../magic/spell/effect/FireBoltSpell.java | 2 +- .../magic/spell/effect/LightSpell.java | 3 +- .../magic/spell/effect/MimicSpell.java | 3 +- .../magic/spell/effect/PortalSpell.java | 2 +- .../render/EntityReplacementManager.java | 4 -- .../unicopia/entity/behaviour/Disguise.java | 3 - .../entity/behaviour/EntityAppearance.java | 46 ++++++++++++- .../entity/behaviour/SheepBehaviour.java | 2 +- .../network/track/TrackableDataType.java | 10 +++ .../util/serialization/PacketCodec.java | 17 ++++- 22 files changed, 114 insertions(+), 118 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java index 816047c9..50cb5902 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java @@ -131,7 +131,7 @@ class MultiSpellSlot implements SpellSlots, NbtSerialisable { return hasValue ? Status.NEW : Status.REMOVED; } - return spell.hasDirtySpell() ? Status.UPDATED : Status.DEFAULT; + return Status.DEFAULT; } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java index 78373b5a..9ff86367 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java @@ -80,18 +80,6 @@ public abstract class AbstractDelegatingSpell implements Spell { return getOrEmpty().isDying(); } - @Deprecated - @Override - public boolean isDirty() { - return delegate.hasDirtySpell(); - } - - @Deprecated - @Override - public void setDirty() { - getOrEmpty().setDirty(); - } - @Override public boolean isHidden() { return getOrEmpty().isHidden(); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDisguiseSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDisguiseSpell.java index 41e7c996..b4e6ad08 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDisguiseSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDisguiseSpell.java @@ -20,7 +20,7 @@ import net.minecraft.nbt.NbtCompound; */ public abstract class AbstractDisguiseSpell extends AbstractSpell implements Disguise, ProjectileImpactListener { - private final EntityAppearance disguise = new EntityAppearance(); + private final EntityAppearance disguise = dataTracker.startTracking(new EntityAppearance()); public AbstractDisguiseSpell(CustomisedSpellType type) { super(type); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/EmptySpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/EmptySpell.java index 89b3f986..f4dca074 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/EmptySpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/EmptySpell.java @@ -44,11 +44,6 @@ public final class EmptySpell implements Spell { return false; } - @Override - public boolean isDirty() { - return false; - } - @Override public boolean tick(Caster caster, Situation situation) { return false; @@ -57,9 +52,6 @@ public final class EmptySpell implements Spell { @Override public void tickDying(Caster caster) { } - @Override - public void setDirty() { } - @Override public boolean isHidden() { return true; diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java index 339deac6..e3f31a03 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlacementControlSpell.java @@ -9,6 +9,8 @@ import com.minelittlepony.unicopia.ability.magic.spell.effect.AbstractSpell; import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.entity.mob.CastSpellEntity; +import com.minelittlepony.unicopia.network.track.DataTracker; +import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.server.world.Ether; import com.minelittlepony.unicopia.util.NbtSerialisable; @@ -21,12 +23,10 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; public class PlacementControlSpell extends AbstractSpell implements OrientedSpell { - @Nullable - private UUID placedEntityId; - - private Optional> dimension = Optional.empty(); - private Optional position = Optional.empty(); - private Optional orientation = Optional.empty(); + private final DataTracker.Entry placedEntityId = dataTracker.startTracking(TrackableDataType.UUID, null); + private final DataTracker.Entry>> dimension = dataTracker.startTracking(TrackableDataType.ofRegistryKey(), Optional.empty()); + private final DataTracker.Entry> position = dataTracker.startTracking(TrackableDataType.OPTIONAL_VECTOR, Optional.empty()); + private final DataTracker.Entry> orientation = dataTracker.startTracking(TrackableDataType.OPTIONAL_VECTOR, Optional.empty()); @Nullable private Spell delegate; @@ -46,26 +46,23 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel } public Optional getPosition() { - return position; + return position.get(); } public void setDimension(RegistryKey dimension) { - this.dimension = Optional.of(dimension); - setDirty(); + this.dimension.set(Optional.of(dimension)); } public void setPosition(Vec3d position) { - this.position = Optional.of(position); - setDirty(); + this.position.set(Optional.of(position)); } @Override public void setOrientation(Caster caster, float pitch, float yaw) { - this.orientation = Optional.of(new Vec3d(pitch, yaw, 0)); + this.orientation.set(Optional.of(new Vec3d(pitch, yaw, 0))); if (delegate instanceof OrientedSpell o) { o.setOrientation(caster, pitch, yaw); } - setDirty(); if (!caster.isClient()) { var entry = getConnection(caster); if (entry != null) { @@ -84,24 +81,23 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel public boolean tick(Caster source, Situation situation) { if (!source.isClient()) { - if (placedEntityId == null) { - if (dimension.isEmpty()) { + if (placedEntityId.get() == null) { + if (dimension.get().isEmpty()) { setDimension(source.asWorld().getRegistryKey()); } - if (position.isEmpty()) { + if (getPosition().isEmpty()) { setPosition(source.asEntity().getPos()); } - System.out.println("Creating placed spell"); + CastSpellEntity entity = new CastSpellEntity(source.asWorld(), source, this); - Vec3d pos = position.get(); - Vec3d rot = orientation.orElse(Vec3d.ZERO); + Vec3d pos = getPosition().get(); + Vec3d rot = orientation.get().orElse(Vec3d.ZERO); entity.updatePositionAndAngles(pos.x, pos.y, pos.z, (float)rot.y, (float)rot.x); entity.getWorld().spawnEntity(entity); - placedEntityId = entity.getUuid(); - setDirty(); + placedEntityId.set(entity.getUuid()); } else { if (getConnection(source) == null) { setDead(); @@ -114,24 +110,24 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel @Nullable private Ether.Entry getConnection(Caster source) { - return delegate == null || placedEntityId == null ? null : getWorld(source) - .map(world -> Ether.get(world).get(getDelegate().getTypeAndTraits().type(), placedEntityId, delegate.getUuid())) + return delegate == null || placedEntityId.get() == null ? null : getWorld(source) + .map(world -> Ether.get(world).get(getDelegate().getTypeAndTraits().type(), placedEntityId.get(), delegate.getUuid())) .orElse(null); } private Optional getWorld(Caster source) { - return dimension.map(source.asWorld().getServer()::getWorld); + return dimension.get().map(source.asWorld().getServer()::getWorld); } @Override public void toNBT(NbtCompound compound) { super.toNBT(compound); compound.put("spell", Spell.writeNbt(delegate)); - position.ifPresent(pos -> compound.put("position", NbtSerialisable.writeVector(pos))); - orientation.ifPresent(o -> compound.put("orientation", NbtSerialisable.writeVector(o))); - dimension.ifPresent(d -> compound.putString("dimension", d.getValue().toString())); - if (placedEntityId != null) { - compound.putUuid("placedEntityId", placedEntityId); + position.get().ifPresent(pos -> compound.put("position", NbtSerialisable.writeVector(pos))); + orientation.get().ifPresent(o -> compound.put("orientation", NbtSerialisable.writeVector(o))); + dimension.get().ifPresent(d -> compound.putString("dimension", d.getValue().toString())); + if (placedEntityId.get() != null) { + compound.putUuid("placedEntityId", placedEntityId.get()); } } @@ -139,12 +135,10 @@ public class PlacementControlSpell extends AbstractSpell implements OrientedSpel public void fromNBT(NbtCompound compound) { super.fromNBT(compound); delegate = Spell.readNbt(compound.getCompound("spell")); - placedEntityId = compound.containsUuid("placedEntityId") ? compound.getUuid("placedEntityId") : null; - position = compound.contains("position") ? Optional.of(NbtSerialisable.readVector(compound.getList("position", NbtElement.DOUBLE_TYPE))) : Optional.empty(); - orientation = compound.contains("orientation") ? Optional.of(NbtSerialisable.readVector(compound.getList("orientation", NbtElement.DOUBLE_TYPE))) : Optional.empty(); - if (compound.contains("dimension", NbtElement.STRING_TYPE)) { - dimension = Optional.ofNullable(Identifier.tryParse(compound.getString("dimension"))).map(id -> RegistryKey.of(RegistryKeys.WORLD, id)); - } + placedEntityId.set(compound.containsUuid("placedEntityId") ? compound.getUuid("placedEntityId") : null); + position.set(compound.contains("position") ? Optional.of(NbtSerialisable.readVector(compound.getList("position", NbtElement.DOUBLE_TYPE))) : Optional.empty()); + orientation.set(compound.contains("orientation") ? Optional.of(NbtSerialisable.readVector(compound.getList("orientation", NbtElement.DOUBLE_TYPE))) : Optional.empty()); + dimension.set(compound.contains("dimension", NbtElement.STRING_TYPE) ? Optional.ofNullable(Identifier.tryParse(compound.getString("dimension"))).map(id -> RegistryKey.of(RegistryKeys.WORLD, id)) : Optional.empty()); } public interface PlacementDelegate { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java index aab10703..6add4b0f 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java @@ -74,18 +74,6 @@ public interface Spell extends NbtSerialisable, Affine { boolean isDying(); - /** - * Returns true if this effect has changes that need to be sent to the client. - */ - @Deprecated - boolean isDirty(); - - /** - * Marks this effect as dirty. - */ - @Deprecated - void setDirty(); - /** * Applies this spell to the supplied caster. * @param caster The caster to apply the spell to diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellReference.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellReference.java index 4bab564c..66cd3acc 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellReference.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/SpellReference.java @@ -25,11 +25,6 @@ public final class SpellReference implements NbtSerialisable { set(spell, null); } - @Deprecated - public boolean hasDirtySpell() { - return spell != null && spell.isDirty(); - } - public boolean set(T spell, @Nullable Caster owner) { spell = spell == null || spell.isDead() ? null : spell; if (spell == this.spell) { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AbstractSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AbstractSpell.java index 6d08d6a9..16839123 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AbstractSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AbstractSpell.java @@ -20,7 +20,6 @@ public abstract class AbstractSpell implements Spell { private final DataTracker.Entry dead = dataTracker.startTracking(TrackableDataType.BOOLEAN, false); private final DataTracker.Entry dying = dataTracker.startTracking(TrackableDataType.BOOLEAN, false); - private boolean dirty; private final DataTracker.Entry hidden = dataTracker.startTracking(TrackableDataType.BOOLEAN, false); private boolean destroyed; @@ -66,18 +65,6 @@ public abstract class AbstractSpell implements Spell { return dying.get(); } - @Deprecated - @Override - public final boolean isDirty() { - return dirty; - } - - @Deprecated - @Override - public final void setDirty() { - dirty = true; - } - @Override public final boolean isHidden() { return hidden.get(); @@ -120,7 +107,6 @@ public abstract class AbstractSpell implements Spell { @Override public void fromNBT(NbtCompound compound) { - dirty = false; if (compound.containsUuid("uuid")) { uuid = compound.getUuid("uuid"); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AttractiveSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AttractiveSpell.java index 7bf39425..08b6e1a2 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AttractiveSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AttractiveSpell.java @@ -28,14 +28,12 @@ public class AttractiveSpell extends ShieldSpell implements HomingSpell, TimedSp static final TooltipFactory TARGET = (type, tooltip) -> (TARGET_FOCUSED_ENTITY.get(type.traits()) ? TARGET_FOCUSED_ENTITY : ShieldSpell.TARGET).appendTooltip(type, tooltip); static final TooltipFactory TOOLTIP = TooltipFactory.of(TIME, RANGE, TARGET, STICK_TO_TARGET, CAST_ON); - private final EntityReference target = new EntityReference<>(); + private final EntityReference target = dataTracker.startTracking(new EntityReference<>()); - private final Timer timer; + private final Timer timer = new Timer(TIME.get(getTraits())); protected AttractiveSpell(CustomisedSpellType type) { super(type); - timer = new Timer(TIME.get(getTraits())); - dataTracker.startTracking(target); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AwkwardSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AwkwardSpell.java index dab51b1b..7a9402c5 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AwkwardSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/AwkwardSpell.java @@ -19,11 +19,10 @@ import net.minecraft.registry.Registries; public class AwkwardSpell extends AbstractSpell implements TimedSpell { - private final Timer timer; + private final Timer timer = new Timer(20); protected AwkwardSpell(CustomisedSpellType type) { super(type); - timer = new Timer(20); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/BubbleSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/BubbleSpell.java index df1e9c92..dfd6d601 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/BubbleSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/BubbleSpell.java @@ -56,17 +56,14 @@ public class BubbleSpell extends AbstractSpell implements TimedSpell, static final TooltipFactory TOOLTIP = TooltipFactory.of(TimedSpell.TIME, SOAPINESS); - private final Timer timer; + private final Timer timer = new Timer(TIME.get(getTraits())); private float prevRadius; - private DataTracker.Entry radius; - private DataTracker.Entry struggles; + private DataTracker.Entry radius = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); + private DataTracker.Entry struggles = dataTracker.startTracking(TrackableDataType.INT, SOAPINESS.get(getTraits())); protected BubbleSpell(CustomisedSpellType type) { super(type); - timer = new Timer(TIME.get(getTraits())); - radius = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); - struggles = dataTracker.startTracking(TrackableDataType.INT, SOAPINESS.get(getTraits())); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FeatherFallSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FeatherFallSpell.java index e90558d0..8f7ffabf 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FeatherFallSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FeatherFallSpell.java @@ -59,11 +59,10 @@ public class FeatherFallSpell extends AbstractSpell implements TimedSpell { .with(Trait.ORDER, 15) .build(); - private final Timer timer; + private final Timer timer = new Timer(DURATION.get(getTraits())); protected FeatherFallSpell(CustomisedSpellType type) { super(type); - timer = new Timer(DURATION.get(getTraits())); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java index 9075457a..04cf61fd 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java @@ -47,7 +47,7 @@ public class FireBoltSpell extends AbstractSpell implements HomingSpell, static final TooltipFactory TOOLTIP = TooltipFactory.of(EXPLOSION_STRENGTH, VELOCITY, PROJECTILE_COUNT, FOLLOWS_TARGET, FOLLOW_RANGE.conditionally(FOLLOWS_TARGET::get)); - private final EntityReference target = new EntityReference<>(); + private final EntityReference target = dataTracker.startTracking(new EntityReference<>()); protected FireBoltSpell(CustomisedSpellType type) { super(type); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/LightSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/LightSpell.java index 2c14e4f8..d1bf985f 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/LightSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/LightSpell.java @@ -38,13 +38,12 @@ public class LightSpell extends AbstractSpell implements TimedSpell, ProjectileD static final TooltipFactory TOOLTIP = TooltipFactory.of(TIME, ORB_COUNT); - private final Timer timer; + private final Timer timer = new Timer(TIME.get(getTraits())); private final List> lights = new ArrayList<>(); protected LightSpell(CustomisedSpellType type) { super(type); - timer = new Timer(TIME.get(getTraits())); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/MimicSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/MimicSpell.java index 57b984c1..1108bd43 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/MimicSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/MimicSpell.java @@ -11,11 +11,10 @@ public class MimicSpell extends AbstractDisguiseSpell implements HomingSpell, Ti static final TooltipFactory TOOLTIP = TimedSpell.TIME; - private final Timer timer; + private final Timer timer = new Timer(TIME.get(getTraits())); protected MimicSpell(CustomisedSpellType type) { super(type); - timer = new Timer(TIME.get(getTraits())); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java index 4277ff0c..bb007fe5 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/PortalSpell.java @@ -46,7 +46,7 @@ public class PortalSpell extends AbstractSpell implements PlacementControlSpell. private final DataTracker.Entry targetPortalId = dataTracker.startTracking(TrackableDataType.UUID, Util.NIL_UUID); private final DataTracker.Entry targetPortalPitch = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); private final DataTracker.Entry targetPortalYaw = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); - private final EntityReference teleportationTarget = new EntityReference<>(); + private final EntityReference teleportationTarget = dataTracker.startTracking(new EntityReference<>()); private final DataTracker.Entry pitch = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); private final DataTracker.Entry yaw = dataTracker.startTracking(TrackableDataType.FLOAT, 0F); diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/EntityReplacementManager.java b/src/main/java/com/minelittlepony/unicopia/client/render/EntityReplacementManager.java index 8b759a18..0e1dcd76 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/EntityReplacementManager.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/EntityReplacementManager.java @@ -67,10 +67,6 @@ class EntityReplacementManager implements Disguise { return disguise; } - @Override - public void setDirty() { - } - @Override public boolean isDead() { return false; diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Disguise.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Disguise.java index b34606f7..9604dfc1 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Disguise.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Disguise.java @@ -24,8 +24,6 @@ public interface Disguise extends FlightType.Provider, PlayerDimensions.Provider EntityAppearance getDisguise(); - void setDirty(); - boolean isDead(); default Optional getAppearance() { @@ -57,7 +55,6 @@ public interface Disguise extends FlightType.Provider, PlayerDimensions.Provider } getDisguise().setAppearance(entity); - setDirty(); return this; } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java index acba1d32..f2c0c56f 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java @@ -21,6 +21,7 @@ import com.minelittlepony.unicopia.entity.mob.SombraEntity; import com.minelittlepony.unicopia.entity.mob.UEntityAttributes; import com.minelittlepony.unicopia.entity.player.PlayerDimensions; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.network.track.TrackableObject; import com.minelittlepony.unicopia.projectile.ProjectileUtil; import com.minelittlepony.unicopia.util.NbtSerialisable; import com.mojang.authlib.GameProfile; @@ -51,7 +52,7 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.util.math.Vec3d; import net.minecraft.util.shape.VoxelShape; -public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provider, FlightType.Provider, EntityCollisions.ComplexCollidable { +public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provider, FlightType.Provider, EntityCollisions.ComplexCollidable, TrackableObject { private static final Optional BLOCK_HEIGHT = Optional.of(0.5F); @NotNull @@ -77,6 +78,8 @@ public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provi @Nullable private NbtCompound entityNbt; + private boolean dirty; + @Nullable public Entity getAppearance() { return entity; @@ -109,6 +112,7 @@ public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provi entityNbt = entity == null ? null : encodeEntityToNBT(entity); entityId = entityNbt == null ? "" : entityNbt.getString("id"); + markDirty(); } public boolean isPresent() { @@ -393,4 +397,44 @@ public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provi getAttachments().forEach(e -> EntityCollisions.getCollissionShapes(e.entity(), context, output)); } + public void markDirty() { + dirty = true; + } + + @Override + public Status getStatus() { + if (dirty) { + dirty = false; + return Status.UPDATED; + } + return Status.DEFAULT; + } + + @Override + public void readTrackedNbt(NbtCompound nbt) { + fromNBT(nbt); + } + + @Override + public NbtCompound writeTrackedNbt() { + return toNBT(); + } + + @Override + public void discard(boolean immediate) { + setAppearance(null); + dirty = false; + } + + @Override + public void copyTo(EntityAppearance destination) { + destination.entityId = entityId; + destination.entity = entity; + destination.blockEntity = blockEntity; + destination.attachments.addAll(attachments); + destination.dimensions = dimensions; + destination.tag = tag == null ? null : tag.copy(); + destination.entityNbt = entityNbt == null ? null : entityNbt.copy(); + } + } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/SheepBehaviour.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/SheepBehaviour.java index c4074e27..1a7ac949 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/SheepBehaviour.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/SheepBehaviour.java @@ -65,7 +65,7 @@ public class SheepBehaviour extends EntityBehaviour { } } while (dropAmount-- > 0); } - spell.setDirty(); + spell.getAppearance().ifPresent(EntityAppearance::markDirty); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java b/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java index 9c5c6b74..e918e3e7 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java @@ -11,8 +11,10 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; +import net.minecraft.registry.RegistryKey; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; public record TrackableDataType(int id, PacketCodec codec) { private static final Int2ObjectMap> REGISTRY = new Int2ObjectOpenHashMap<>(); @@ -26,8 +28,16 @@ public record TrackableDataType(int id, PacketCodec codec) { public static final TrackableDataType> RAW_BYTES = of(new Identifier("raw_bytes"), PacketCodec.RAW_BYTES.asOptional()); public static final TrackableDataType> OPTIONAL_POS = of(new Identifier("optional_pos"), PacketCodec.OPTIONAL_POS); + public static final TrackableDataType> OPTIONAL_VECTOR = of(new Identifier("optional_vector"), PacketCodec.OPTIONAL_VECTOR); + private static final TrackableDataType>> OPTIONAL_REGISTRY_KEY = of(new Identifier("optional_registry_key"), PacketCodec.OPTIONAL_REGISTRY_KEY); + public static final TrackableDataType RACE = TrackableDataType.of(Unicopia.id("race"), PacketCodec.ofRegistry(Race.REGISTRY)); + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static TrackableDataType>> ofRegistryKey() { + return (TrackableDataType)OPTIONAL_REGISTRY_KEY; + } + @SuppressWarnings("unchecked") public static TrackableDataType of(PacketByteBuf buffer) { int id = buffer.readInt(); diff --git a/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java index 03336ec2..cd964b76 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java +++ b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java @@ -16,9 +16,11 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtIo; import net.minecraft.network.PacketByteBuf; import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; public record PacketCodec(PacketByteBuf.PacketReader reader, PacketByteBuf.PacketWriter writer) { public static final PacketCodec BOOLEAN = new PacketCodec<>(PacketByteBuf::readBoolean, PacketByteBuf::writeBoolean); @@ -30,7 +32,7 @@ public record PacketCodec(PacketByteBuf.PacketReader reader, PacketByteBuf public static final PacketCodec UUID = new PacketCodec<>(PacketByteBuf::readUuid, PacketByteBuf::writeUuid); public static final PacketCodec> OPTIONAL_UUID = UUID.asOptional(); - public static final PacketCodec IDENTIFIER = STRING.xMap(Identifier::new, Identifier::toString); + public static final PacketCodec IDENTIFIER = new PacketCodec<>(PacketByteBuf::readIdentifier, PacketByteBuf::writeIdentifier); public static final PacketCodec NBT = new PacketCodec<>(PacketByteBuf::readNbt, PacketByteBuf::writeNbt); @@ -58,6 +60,19 @@ public record PacketCodec(PacketByteBuf.PacketReader reader, PacketByteBuf public static final PacketCodec POS = new PacketCodec<>(PacketByteBuf::readBlockPos, PacketByteBuf::writeBlockPos); public static final PacketCodec> OPTIONAL_POS = POS.asOptional(); + public static final PacketCodec VECTOR = new PacketCodec<>(buffer -> new Vec3d(buffer.readDouble(), buffer.readDouble(), buffer.readDouble()), (buffer, vector) -> { + buffer.writeDouble(vector.x); + buffer.writeDouble(vector.y); + buffer.writeDouble(vector.z); + }); + public static final PacketCodec> OPTIONAL_VECTOR = VECTOR.asOptional(); + public static final PacketCodec> REGISTRY_KEY = new PacketCodec<>(buffer -> { + return RegistryKey.of(RegistryKey.ofRegistry(IDENTIFIER.read(buffer)), IDENTIFIER.read(buffer)); + }, (buffer, key) -> { + IDENTIFIER.write(buffer, key.getRegistry()); + IDENTIFIER.write(buffer, key.getValue()); + }); + public static final PacketCodec>> OPTIONAL_REGISTRY_KEY = REGISTRY_KEY.asOptional(); public static final PacketCodec ofRegistry(Registry registry) { return INT.xMap(registry::get, registry::getRawId);