2525import com .viaversion .viaversion .api .minecraft .entities .EntityTypes1_21_11 ;
2626import com .viaversion .viaversion .api .protocol .packet .PacketWrapper ;
2727import com .viaversion .viaversion .api .type .Types ;
28+ import com .viaversion .viaversion .libs .fastutil .ints .Int2ObjectMap ;
29+ import com .viaversion .viaversion .libs .fastutil .ints .Int2ObjectOpenHashMap ;
30+ import com .viaversion .viaversion .libs .fastutil .longs .Long2ObjectMap ;
31+ import com .viaversion .viaversion .libs .fastutil .longs .Long2ObjectOpenHashMap ;
32+ import com .viaversion .viaversion .libs .fastutil .objects .Object2IntMap ;
33+ import com .viaversion .viaversion .libs .fastutil .objects .Object2IntOpenHashMap ;
2834import com .viaversion .viaversion .protocols .v1_21_11to26_1 .packet .ClientboundPackets26_1 ;
2935import net .raphimc .viabedrock .ViaBedrock ;
3036import net .raphimc .viabedrock .api .model .BlockState ;
3137import net .raphimc .viabedrock .api .model .entity .*;
3238import net .raphimc .viabedrock .protocol .BedrockProtocol ;
3339
34- import java .util .*;
40+ import java .util .ArrayList ;
41+ import java .util .List ;
42+ import java .util .UUID ;
3543import java .util .concurrent .atomic .AtomicInteger ;
3644import java .util .logging .Level ;
3745
@@ -40,10 +48,10 @@ public class EntityTracker extends StoredObject {
4048 private final AtomicInteger ID_COUNTER = new AtomicInteger (1 );
4149
4250 private ClientPlayerEntity clientPlayerEntity = null ;
43- private final Map < Long , Entity > entities = new HashMap <>();
44- private final Map <Long , Long > runtimeIdToUniqueId = new HashMap <>();
45- private final Map < Integer , Long > javaIdToUniqueId = new HashMap <>();
46- private final Map <BlockPosition , Integer > itemFrames = new HashMap <>();
51+ private final Long2ObjectMap < Entity > entities = new Long2ObjectOpenHashMap <>();
52+ private final Long2ObjectMap <Long > runtimeIdToUniqueId = new Long2ObjectOpenHashMap <>();
53+ private final Int2ObjectMap < Long > javaIdToUniqueId = new Int2ObjectOpenHashMap <>();
54+ private final Object2IntMap <BlockPosition > itemFrames = new Object2IntOpenHashMap <>();
4755
4856 public EntityTracker (final UserConnection user ) {
4957 super (user );
@@ -67,8 +75,8 @@ public <T extends Entity> T addEntity(final T entity) {
6775 }
6876
6977 public <T extends Entity > T addEntity (final T entity , final boolean updateTeam ) {
70- if (entity instanceof ClientPlayerEntity ) {
71- this .clientPlayerEntity = ( ClientPlayerEntity ) entity ;
78+ if (entity instanceof ClientPlayerEntity clientPlayerEntity ) {
79+ this .clientPlayerEntity = clientPlayerEntity ;
7280 }
7381
7482 final Entity prevEntity = this .entities .put (entity .uniqueId (), entity );
@@ -79,10 +87,10 @@ public <T extends Entity> T addEntity(final T entity, final boolean updateTeam)
7987 removeEntities .write (Types .VAR_INT_ARRAY_PRIMITIVE , new int []{prevEntity .javaId ()}); // entity ids
8088 removeEntities .send (BedrockProtocol .class );
8189 }
82- if (this .javaIdToUniqueId .put (entity .javaId (), entity .uniqueId ()) != null ) {
90+ if (this .javaIdToUniqueId .put (entity .javaId (), ( Long ) entity .uniqueId ()) != null ) {
8391 ViaBedrock .getPlatform ().getLogger ().log (Level .WARNING , "Duplicate Java entity ID: " + entity .javaId ());
8492 }
85- if (this .runtimeIdToUniqueId .putIfAbsent (entity .runtimeId (), entity .uniqueId ()) != null ) {
93+ if (this .runtimeIdToUniqueId .putIfAbsent (entity .runtimeId (), ( Long ) entity .uniqueId ()) != null ) {
8694 ViaBedrock .getPlatform ().getLogger ().log (Level .WARNING , "Duplicate entity runtime ID: " + entity .runtimeId ());
8795 }
8896
@@ -129,31 +137,22 @@ public void spawnItemFrame(final BlockPosition position, final BlockState blockS
129137 spawnEntity .send (BedrockProtocol .class );
130138 }
131139
132- public int getItemFrameId (final BlockPosition position ) {
133- return this .itemFrames .getOrDefault (position , -1 );
134- }
135-
136140 public void removeItemFrame (final BlockPosition position ) {
137- final Integer javaId = this .itemFrames .remove (position );
138- if (javaId == null ) {
139- return ;
141+ if (this .itemFrames .containsKey (position )) {
142+ final PacketWrapper removeEntities = PacketWrapper .create (ClientboundPackets26_1 .REMOVE_ENTITIES , this .user ());
143+ removeEntities .write (Types .VAR_INT_ARRAY_PRIMITIVE , new int []{this .itemFrames .getInt (position )}); // entity ids
144+ removeEntities .send (BedrockProtocol .class );
140145 }
141-
142- final PacketWrapper removeEntities = PacketWrapper .create (ClientboundPackets26_1 .REMOVE_ENTITIES , this .user ());
143- removeEntities .write (Types .VAR_INT_ARRAY_PRIMITIVE , new int []{javaId }); // entity ids
144- removeEntities .send (BedrockProtocol .class );
145146 }
146147
147148 public void removeItemFrame (final ChunkPosition chunkPos ) {
148149 final List <BlockPosition > toRemove = new ArrayList <>();
149- for (final Map .Entry <BlockPosition , Integer > entry : this .itemFrames .entrySet ()) {
150- final BlockPosition position = entry .getKey ();
150+ for (BlockPosition position : this .itemFrames .keySet ()) {
151151 if (position .x () >> 4 == chunkPos .chunkX () && position .z () >> 4 == chunkPos .chunkZ ()) {
152152 toRemove .add (position );
153153 }
154154 }
155-
156- for (final BlockPosition position : toRemove ) {
155+ for (BlockPosition position : toRemove ) {
157156 this .removeItemFrame (position );
158157 }
159158 }
@@ -173,15 +172,15 @@ public void prepareForRespawn() {
173172 }
174173
175174 public Entity getEntityByRid (final long runtimeId ) {
176- return this .entities .get (this .runtimeIdToUniqueId .get (runtimeId ));
175+ return this .entities .get (this .runtimeIdToUniqueId .get (runtimeId ). longValue () );
177176 }
178177
179178 public Entity getEntityByUid (final long uniqueId ) {
180179 return this .entities .get (uniqueId );
181180 }
182181
183182 public Entity getEntityByJid (final int javaId ) {
184- return this .entities .get (this .javaIdToUniqueId .get (javaId ));
183+ return this .entities .get (this .javaIdToUniqueId .get (javaId ). longValue () );
185184 }
186185
187186 public ClientPlayerEntity getClientPlayer () {
0 commit comments