From 2910b8d29c13295f3fb407e0e27559b4404aaced Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 22 May 2024 15:25:35 +0100 Subject: [PATCH] Cleanup and sync placed spell owners --- .../com/minelittlepony/unicopia/Race.java | 4 +- .../unicopia/entity/Creature.java | 29 +++++------- .../unicopia/entity/EntityPhysics.java | 7 ++- .../unicopia/entity/EntityReference.java | 40 +++++++++++++++-- .../unicopia/entity/ItemImpl.java | 7 ++- .../unicopia/entity/Living.java | 7 ++- .../unicopia/entity/mob/CastSpellEntity.java | 3 ++ .../unicopia/entity/player/Acrobatics.java | 14 +++--- .../unicopia/entity/player/ManaContainer.java | 10 ++--- .../entity/player/PlayerLevelStore.java | 10 ++--- .../unicopia/entity/player/Pony.java | 13 +++--- .../unicopia/network/track/DataTracker.java | 22 ++++++---- .../network/track/TrackableDataType.java | 44 ++++++++++++------- 13 files changed, 119 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index 2380c241..84994481 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -10,8 +10,8 @@ import com.google.common.base.Strings; import com.minelittlepony.unicopia.ability.Abilities; import com.minelittlepony.unicopia.ability.Ability; import com.minelittlepony.unicopia.ability.magic.Affine; +import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.util.RegistryUtils; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; @@ -45,7 +45,7 @@ public record Race ( public static final String DEFAULT_ID = "unicopia:unset"; public static final Registry REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID); public static final Registry COMMAND_REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race/grantable"), DEFAULT_ID); - public static final PacketCodec PACKET_CODEC = PacketCodec.ofRegistry(REGISTRY); + public static final TrackableDataType TRACKABLE_TYPE = TrackableDataType.RACE; public static final RegistryKey> REGISTRY_KEY = REGISTRY.getKey(); private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("commands.race.fail", id)); private static final Function COMPOSITES = Util.memoize(race -> new Composite(race, null, null)); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java index eb7e27b9..c319f104 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java @@ -23,7 +23,6 @@ import com.minelittlepony.unicopia.entity.ai.WantItTakeItGoal; import com.minelittlepony.unicopia.entity.mob.UEntityAttributes; import com.minelittlepony.unicopia.network.track.DataTracker; import com.minelittlepony.unicopia.network.track.TrackableDataType; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; @@ -62,7 +61,6 @@ public class Creature extends Living implements WeaklyOwned.Mutabl .isEmpty(); }); - protected final DataTracker.Entry master; protected final DataTracker.Entry eating; protected final DataTracker.Entry discorded; protected final DataTracker.Entry smitten; @@ -73,16 +71,15 @@ public class Creature extends Living implements WeaklyOwned.Mutabl addTicker(physics); addTicker(this::updateConsumption); - master = tracker.startTracking(TrackableDataType.of(PacketCodec.NBT), owner.toNBT()); - eating = tracker.startTracking(TrackableDataType.of(PacketCodec.INT), 0); - discorded = tracker.startTracking(TrackableDataType.of(PacketCodec.BOOLEAN), false); - smitten = tracker.startTracking(TrackableDataType.of(PacketCodec.BOOLEAN), false); + tracker.startTracking(owner); + eating = tracker.startTracking(TrackableDataType.INT, 0); + discorded = tracker.startTracking(TrackableDataType.BOOLEAN, false); + smitten = tracker.startTracking(TrackableDataType.BOOLEAN, false); } @Override public void setMaster(LivingEntity owner) { this.owner.set(owner); - tracker.set(master, this.owner.toNBT()); if (owner != null) { targets.ifPresent(this::initMinionAi); } @@ -93,20 +90,20 @@ public class Creature extends Living implements WeaklyOwned.Mutabl } public boolean isDiscorded() { - return tracker.get(this.discorded); + return discorded.get(); } public boolean isSmitten() { - return tracker.get(this.smitten); + return smitten.get(); } public void setSmitten(boolean smitten) { smittenTicks = smitten ? 20 : 0; - tracker.set(this.smitten, smitten); + this.smitten.set(smitten); } public void setDiscorded(boolean discorded) { - tracker.set(this.discorded, discorded); + this.discorded.set(discorded); discordedChanged = true; } @@ -118,9 +115,6 @@ public class Creature extends Living implements WeaklyOwned.Mutabl @Override public EntityReference getMasterReference() { - if (master != null) { - owner.fromNBT(tracker.get(master)); - } return owner; } @@ -232,10 +226,10 @@ public class Creature extends Living implements WeaklyOwned.Mutabl private void updateConsumption() { if (isClient()) { - eatTimer = tracker.get(eating); + eatTimer = eating.get(); } else if (eatMuffinGoal != null) { eatTimer = eatMuffinGoal.getTimer(); - tracker.set(eating, eatTimer); + eating.set(eatTimer); } } @@ -324,9 +318,6 @@ public class Creature extends Living implements WeaklyOwned.Mutabl } if (compound.contains("master", NbtElement.COMPOUND_TYPE)) { owner.fromNBT(compound.getCompound("master")); - if (master != null) { - tracker.set(master, owner.toNBT()); - } if (owner.isSet()) { targets.ifPresent(this::initMinionAi); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java index b08ce4cd..c50d740f 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityPhysics.java @@ -6,7 +6,6 @@ import com.minelittlepony.unicopia.network.track.Trackable; import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.util.Copyable; import com.minelittlepony.unicopia.util.Tickable; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; import net.minecraft.block.BlockState; import net.minecraft.block.FenceGateBlock; @@ -31,7 +30,7 @@ public class EntityPhysics implements Physics, Copyable implements Physics, Copyable The type of the entity this reference points to. */ -public class EntityReference implements NbtSerialisable { +public class EntityReference implements NbtSerialisable, TrackableObject { private static final Serializer SERIALIZER = Serializer.of(EntityReference::new); @SuppressWarnings("unchecked") @@ -42,6 +43,8 @@ public class EntityReference implements NbtSerialisable { private WeakReference directReference = new WeakReference<>(null); + private boolean dirty = true; + public EntityReference() {} public EntityReference(T entity) { @@ -61,13 +64,14 @@ public class EntityReference implements NbtSerialisable { public boolean set(@Nullable T entity) { this.directReference = new WeakReference<>(entity); this.reference = entity == null ? null : new EntityValues<>(entity); + this.dirty = true; return entity != null; } public Optional> getTarget() { T value = directReference.get(); if (value != null) { - set(value); + this.reference = new EntityValues<>(value); } return Optional.ofNullable(reference); } @@ -113,11 +117,41 @@ public class EntityReference implements NbtSerialisable { @Override public void fromNBT(NbtCompound tag) { this.reference = tag.contains("uuid") ? new EntityValues<>(tag) : null; + this.dirty = true; } @Override public int hashCode() { - return getTarget().map(EntityValues::uuid).orElse(Util.NIL_UUID).hashCode(); + return getUuid().hashCode(); + } + + @Override + public UUID getUuid() { + return getTarget().map(EntityValues::uuid).orElse(Util.NIL_UUID); + } + + @Override + public Status getStatus() { + if (dirty) { + dirty = false; + return Status.UPDATED; + } + return Status.DEFAULT; + } + + @Override + public NbtCompound toTrackedNbt() { + return toNBT(); + } + + @Override + public void readTrackedNbt(NbtCompound compound) { + fromNBT(compound); + } + + @Override + public void discard(boolean immediate) { + set(null); } public record EntityValues( diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java index a4c30f52..ce04e0d9 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java @@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.item.enchantment.WantItNeedItEnchantment; import com.minelittlepony.unicopia.network.track.DataTracker; import com.minelittlepony.unicopia.network.track.DataTrackerManager; import com.minelittlepony.unicopia.network.track.Trackable; -import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.particle.FollowingParticleEffect; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; @@ -44,7 +43,7 @@ public class ItemImpl implements Equine { this.tracker = trackers.getPrimaryTracker(); this.physics = new ItemPhysics(owner); - race = tracker.startTracking(TrackableDataType.of(Race.PACKET_CODEC), Race.HUMAN); + race = tracker.startTracking(Race.TRACKABLE_TYPE, Race.HUMAN); } @Override @@ -141,12 +140,12 @@ public class ItemImpl implements Equine { @Override public Race getSpecies() { - return tracker.get(race); + return race.get(); } @Override public void setSpecies(Race race) { - tracker.set(this.race, race); + this.race.set(race); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 45d15e84..0ea9bc46 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -40,7 +40,6 @@ import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.projectile.ProjectileImpactListener; import com.minelittlepony.unicopia.server.world.DragonBreathStore; import com.minelittlepony.unicopia.util.*; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; import it.unimi.dsi.fastutil.floats.Float2ObjectFunction; import net.fabricmc.fabric.api.util.TriState; @@ -116,7 +115,7 @@ public abstract class Living implements Equine, Caste this.landedHeuristic = addTicker(new Interactable(entity::isOnGround)); this.jumpingHeuristic = addTicker(new Interactable(((LivingEntityDuck)entity)::isJumping)); - carrierId = tracker.startTracking(TrackableDataType.of(PacketCodec.UUID), Util.NIL_UUID); + carrierId = tracker.startTracking(TrackableDataType.UUID, Util.NIL_UUID); } public Q addTicker(Q tickable) { @@ -171,12 +170,12 @@ public abstract class Living implements Equine, Caste } public Optional getCarrierId() { - UUID carrierId = tracker.get(this.carrierId); + UUID carrierId = this.carrierId.get(); return carrierId == Util.NIL_UUID ? Optional.empty() : Optional.of(carrierId); } public void setCarrier(UUID carrier) { - tracker.set(this.carrierId, carrier == null ? Util.NIL_UUID : carrier); + carrierId.set(carrier == null ? Util.NIL_UUID : carrier); } public void setCarrier(Entity carrier) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java index 9c7312df..01cbf06b 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java @@ -12,6 +12,8 @@ import com.minelittlepony.unicopia.entity.EntityPhysics; import com.minelittlepony.unicopia.entity.EntityReference; import com.minelittlepony.unicopia.entity.MagicImmune; import com.minelittlepony.unicopia.entity.Physics; +import com.minelittlepony.unicopia.network.track.Trackable; + import net.minecraft.entity.Entity; import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityPose; @@ -50,6 +52,7 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster type, World world) { super(type, world); ignoreCameraFrustum = true; + Trackable.of(this).getDataTrackers().getPrimaryTracker().startTracking(owner); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Acrobatics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Acrobatics.java index 75fadc1c..2afbc3ca 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Acrobatics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Acrobatics.java @@ -12,8 +12,6 @@ import com.minelittlepony.unicopia.network.track.DataTracker; import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.Tickable; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; - import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.SideShapeType; @@ -36,14 +34,12 @@ public class Acrobatics implements Tickable, NbtSerialisable { private final Pony pony; private final PlayerEntity entity; - private final DataTracker tracker; private final DataTracker.Entry> hangingPos; public Acrobatics(Pony pony, DataTracker tracker) { this.pony = pony; - this.tracker = tracker; this.entity = pony.asEntity(); - this.hangingPos = tracker.startTracking(TrackableDataType.of(PacketCodec.OPTIONAL_POS), Optional.empty()); + this.hangingPos = tracker.startTracking(TrackableDataType.OPTIONAL_POS, Optional.empty()); pony.addTicker(this::checkDislodge); } @@ -146,7 +142,7 @@ public class Acrobatics implements Tickable, NbtSerialisable { } public Optional getHangingPosition() { - return tracker.get(hangingPos); + return hangingPos.get(); } public boolean isHanging() { @@ -154,13 +150,13 @@ public class Acrobatics implements Tickable, NbtSerialisable { } public void stopHanging() { - tracker.set(hangingPos, Optional.empty()); + hangingPos.set(Optional.empty()); entity.calculateDimensions(); ticksHanging = 0; } public void startHanging(BlockPos pos) { - tracker.set(hangingPos, Optional.of(pos)); + hangingPos.set(Optional.of(pos)); entity.teleport(pos.getX() + 0.5, pos.getY() - 1, pos.getZ() + 0.5); entity.setVelocity(Vec3d.ZERO); entity.setSneaking(false); @@ -201,6 +197,6 @@ public class Acrobatics implements Tickable, NbtSerialisable { @Override public void fromNBT(NbtCompound compound) { ticksHanging = compound.getInt("ticksHanging"); - tracker.set(hangingPos, NbtSerialisable.BLOCK_POS.readOptional("hangingPosition", compound)); + hangingPos.set(NbtSerialisable.BLOCK_POS.readOptional("hangingPosition", compound)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java index ccb7a65c..15bcf34f 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java @@ -8,8 +8,6 @@ import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.util.Copyable; import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.Tickable; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; - import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.MathHelper; @@ -153,7 +151,6 @@ class ManaContainer implements MagicReserves, Tickable, NbtSerialisable, Copyabl } class BarInst implements Bar, NbtSerialisable { - private final DataTracker tracker; private final DataTracker.Entry marker; private final float max; @@ -162,17 +159,16 @@ class ManaContainer implements MagicReserves, Tickable, NbtSerialisable, Copyabl private float prevValue; BarInst(DataTracker tracker, float max, float initial) { - this.tracker = tracker; this.max = max; this.trailingValue = initial; this.prevTrailingValue = initial; this.prevValue = initial; - this.marker = tracker.startTracking(TrackableDataType.of(PacketCodec.FLOAT), max * trailingValue); + this.marker = tracker.startTracking(TrackableDataType.FLOAT, max * trailingValue); } @Override public float get() { - return applyLimits(tracker.get(marker)); + return applyLimits(marker.get()); } @Override @@ -191,7 +187,7 @@ class ManaContainer implements MagicReserves, Tickable, NbtSerialisable, Copyabl } private void load(float value) { - tracker.set(marker, value); + marker.set(value); } protected float getInitial(float initial) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java index edd6f65f..decc9b12 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java @@ -3,8 +3,6 @@ package com.minelittlepony.unicopia.entity.player; import com.minelittlepony.unicopia.ability.magic.Levelled; import com.minelittlepony.unicopia.network.track.DataTracker; import com.minelittlepony.unicopia.network.track.TrackableDataType; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; - import net.minecraft.sound.*; import net.minecraft.util.math.MathHelper; @@ -12,7 +10,6 @@ class PlayerLevelStore implements Levelled.LevelStore { private final Pony pony; - private final DataTracker tracker; private final DataTracker.Entry dataEntry; private final boolean upgradeMana; @@ -21,8 +18,7 @@ class PlayerLevelStore implements Levelled.LevelStore { PlayerLevelStore(Pony pony, DataTracker tracker, boolean upgradeMana, SoundEvent levelUpSound) { this.pony = pony; - this.tracker = tracker; - this.dataEntry = tracker.startTracking(TrackableDataType.of(PacketCodec.INT), 0); + this.dataEntry = tracker.startTracking(TrackableDataType.INT, 0); this.upgradeMana = upgradeMana; this.levelUpSound = levelUpSound; } @@ -45,11 +41,11 @@ class PlayerLevelStore implements Levelled.LevelStore { @Override public int get() { - return tracker.get(dataEntry); + return dataEntry.get(); } @Override public void set(int level) { - tracker.set(dataEntry, MathHelper.clamp(level, 0, getMax())); + dataEntry.set(MathHelper.clamp(level, 0, getMax())); } } 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 3546231d..d9e270ba 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -35,7 +35,6 @@ import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.util.*; import com.minelittlepony.unicopia.network.*; import com.minelittlepony.unicopia.network.track.DataTracker; -import com.minelittlepony.unicopia.network.track.TrackableDataType; import com.minelittlepony.unicopia.server.world.UGameRules; import com.minelittlepony.common.util.animation.LinearInterpolator; import com.google.common.collect.Streams; @@ -117,8 +116,8 @@ public class Pony extends Living implements Copyable, Update sender.accept(Channel.SERVER_PLAYER_CAPABILITIES.toPacket(new MsgPlayerCapabilities(this))); } }); - race = this.tracker.startTracking(TrackableDataType.of(Race.PACKET_CODEC), Race.HUMAN); - suppressedRace = this.tracker.startTracking(TrackableDataType.of(Race.PACKET_CODEC), Race.HUMAN); + race = this.tracker.startTracking(Race.TRACKABLE_TYPE, Race.HUMAN); + suppressedRace = this.tracker.startTracking(Race.TRACKABLE_TYPE, Race.HUMAN); this.levels = new PlayerLevelStore(this, tracker, true, USounds.Vanilla.ENTITY_PLAYER_LEVELUP); this.corruption = new PlayerLevelStore(this, tracker, false, USounds.ENTITY_PLAYER_CORRUPTION); this.mana = addTicker(new ManaContainer(this, tracker)); @@ -203,7 +202,7 @@ public class Pony extends Living implements Copyable, Update */ @Override public Race getSpecies() { - return tracker.get(race); + return race.get(); } /** @@ -227,7 +226,7 @@ public class Pony extends Living implements Copyable, Update public void setSpecies(Race race) { race = race.validate(entity); Race current = getSpecies(); - tracker.set(this.race, race); + this.race.set(race); if (race != current) { clearSuppressedRace(); } @@ -240,7 +239,7 @@ public class Pony extends Living implements Copyable, Update } public void setSuppressedRace(Race race) { - tracker.set(suppressedRace, race.validate(entity)); + suppressedRace.set(race.validate(entity)); } public void clearSuppressedRace() { @@ -248,7 +247,7 @@ public class Pony extends Living implements Copyable, Update } public Race getSuppressedRace() { - return tracker.get(suppressedRace); + return suppressedRace.get(); } public TraitDiscovery getDiscoveries() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java index b730d475..517dccd9 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.Objects; import org.jetbrains.annotations.Nullable; -import com.minelittlepony.unicopia.util.serialization.PacketCodec; - import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; @@ -31,13 +29,13 @@ public class DataTracker { } public Entry startTracking(T value) { - Entry entry = startTracking(TrackableDataType.of(PacketCodec.NBT), value.toTrackedNbt()); + Entry entry = startTracking(TrackableDataType.NBT, value.toTrackedNbt()); persistentObjects.put(entry.id(), value); return entry; } public Entry startTracking(TrackableDataType type, T initialValue) { - Entry entry = new Entry<>(codecs.size()); + Entry entry = new Entry<>(this, codecs.size()); codecs.add(new Pair<>(entry.id(), type, initialValue)); return entry; } @@ -47,11 +45,11 @@ public class DataTracker { return (Pair)codecs.get(entry.id()); } - public T get(Entry entry) { + private T get(Entry entry) { return getPair(entry).value; } - public void set(Entry entry, T value) { + private void set(Entry entry, T value) { if (manager.isClient) { return; } @@ -128,7 +126,15 @@ public class DataTracker { } } - public record Entry(int id) {} + public record Entry(DataTracker tracker, int id) { + public T get() { + return tracker.get(this); + } + + public void set(T t) { + tracker.set(this, t); + } + } static class Pair { private final TrackableDataType type; public final int id; @@ -142,7 +148,7 @@ public class DataTracker { public Pair(PacketByteBuf buffer) { this.id = buffer.readInt(); - this.type = TrackableDataType.of(buffer.readInt()); + this.type = TrackableDataType.of(buffer); this.value = type.read(buffer); } 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 6b4500a4..620edcca 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/TrackableDataType.java @@ -1,30 +1,40 @@ package com.minelittlepony.unicopia.network.track; -import java.util.ArrayList; -import java.util.List; - -import com.google.common.collect.Interner; -import com.google.common.collect.Interners; +import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.util.serialization.PacketCodec; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +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.util.Identifier; +import net.minecraft.util.math.BlockPos; public record TrackableDataType(int id, PacketCodec codec) { - private static final List> REGISTRY = new ArrayList<>(); - private static final Interner> INTERNER = Interners.newStrongInterner(); + private static final Int2ObjectMap> REGISTRY = new Int2ObjectOpenHashMap<>(); + + public static final TrackableDataType INT = of(new Identifier("integer"), PacketCodec.INT); + public static final TrackableDataType FLOAT = of(new Identifier("float"), PacketCodec.FLOAT); + public static final TrackableDataType BOOLEAN = of(new Identifier("boolean"), PacketCodec.BOOLEAN); + public static final TrackableDataType UUID = of(new Identifier("uuid"), PacketCodec.UUID); + public static final TrackableDataType NBT = of(new Identifier("nbt"), PacketCodec.NBT); + + public static final TrackableDataType> OPTIONAL_POS = of(new Identifier("optional_pos"), PacketCodec.OPTIONAL_POS); + public static final TrackableDataType RACE = TrackableDataType.of(Unicopia.id("race"), PacketCodec.ofRegistry(Race.REGISTRY)); @SuppressWarnings("unchecked") - public static TrackableDataType of(int id) { - return (TrackableDataType)REGISTRY.get(id); + public static TrackableDataType of(PacketByteBuf buffer) { + int id = buffer.readInt(); + return Objects.requireNonNull((TrackableDataType)REGISTRY.get(id), "Unknown trackable data type id: " + id); } - public static TrackableDataType of(PacketCodec codec) { - @SuppressWarnings("unchecked") - TrackableDataType type = (TrackableDataType) INTERNER.intern(new TrackableDataType<>(REGISTRY.size(), codec)); - if (type.id() == REGISTRY.size()) { - REGISTRY.add(type); - } - return type; + @SuppressWarnings("unchecked") + public static TrackableDataType of(Identifier typeName, PacketCodec codec) { + return (TrackableDataType)REGISTRY.computeIfAbsent(typeName.hashCode(), t -> new TrackableDataType<>(t, codec)); } public T read(PacketByteBuf buffer) { @@ -32,7 +42,7 @@ public record TrackableDataType(int id, PacketCodec codec) { } public void write(PacketByteBuf buffer, T value) { - buffer.writeInt(id()); + buffer.writeInt(id); codec().write(buffer, value); } }