Skip to content

Commit e63a8ec

Browse files
committed
Sync behavior to be identical with EpearlLookupZenith (TODO: abstract out shared logic to EpearlLookup somehow?)
1 parent b4e3221 commit e63a8ec

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

src/main/java/net/evmodder/evmod/apis/EpearlLookupFabric.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public final class EpearlLookupFabric extends EpearlLookup{
2626

2727
private final HashMap<ChunkPos, Long> recentlyLoadedChunks = new HashMap<>();
2828
private final HashSet<ChunkPos> loadedChunks = new HashSet<>();
29-
private World currWorld;
29+
private World world;
3030
private List<EnderPearlEntity> loadedEpearls;
31-
private int lastEpearlCount;
31+
private int epearlCount;
3232

3333
@Override protected boolean enableRemoteDbUUID(){return Configs.Database.SHARE_EPEARL_OWNERS.getBooleanValue();}
3434
@Override protected boolean enableRemoteDbXZ(){return Configs.Database.SHARE_EPEARL_OWNERS.getBooleanValue();}
@@ -43,10 +43,10 @@ private boolean isWithinDist(PlayerEntity player, PearlDataClient pdc){
4343
return dx*dx + dz*dz < DIST_XZ_SQ && dy*dy < DIST_Y_SQ;
4444
}
4545

46-
private UUID toKeyXZ(Entity epearl){
46+
private final UUID toKeyXZ(Entity epearl){
4747
return new UUID(Double.doubleToRawLongBits(epearl.getX()), Double.doubleToRawLongBits(epearl.getZ()));
4848
}
49-
private ChunkPos toChunkPos(PearlDataClient pdc){
49+
private final ChunkPos toChunkPos(PearlDataClient pdc){
5050
return new ChunkPos(pdc.x()<<4, pdc.z()<<4);
5151
}
5252

@@ -75,39 +75,42 @@ public EpearlLookupFabric(RemoteServerSender rms){
7575
@Override public void onTickStart(MinecraftClient client){
7676
if(isDisabled()) return;
7777
synchronized(recentlyLoadedChunks){
78-
if(client == null || client.player == null || currWorld != client.world || client.world == null){
79-
currWorld = client.world;
78+
if(client == null || client.player == null || world != client.world || client.world == null){
79+
world = client.world;
8080
recentlyLoadedChunks.clear();
8181
loadedChunks.clear();
8282
return;
8383
}
8484
final long now = System.currentTimeMillis();
85+
// Update recentlyLoadedChunks
8586
final boolean fullyLoadedChunk = recentlyLoadedChunks.entrySet().removeIf(entry -> now > entry.getValue());
8687

8788
final Box box = client.player.getBoundingBox().expand(DIST_XZ, DIST_Y, DIST_XZ);
88-
loadedEpearls = client.world.getEntitiesByType(EntityType.ENDER_PEARL, box, _0->true);
89+
loadedEpearls = world.getEntitiesByType(EntityType.ENDER_PEARL, box, _0->true);
8990

90-
final long epearlLoadShortcut = now+CHUNK_LOAD_WAIT_AFTER_EPEARL;
91-
loadedEpearls.stream().map(ep -> client.world.getChunk(ep.getBlockPos()).getPos()).distinct().forEach(chunkPos -> {
92-
final Long wait = recentlyLoadedChunks.get(chunkPos);
93-
if(wait != null && epearlLoadShortcut < wait) recentlyLoadedChunks.put(chunkPos, epearlLoadShortcut);
91+
// Schedule faster recentlyLoadedChunks updates if loaded epearls are detected
92+
final long epearlLoadShortcut = now + CHUNK_LOAD_WAIT_AFTER_EPEARL;
93+
loadedEpearls.stream().map(Entity::getChunkPos).distinct().forEach(chunk -> {
94+
final Long wait = recentlyLoadedChunks.get(chunk);
95+
if(wait != null && epearlLoadShortcut < wait) recentlyLoadedChunks.put(chunk, epearlLoadShortcut);
9496
});
9597

96-
if(lastEpearlCount != loadedEpearls.size() || fullyLoadedChunk){
97-
lastEpearlCount = loadedEpearls.size();
98-
Main.LOGGER.info("Change to chunks/epearls loaded, calling runRemovalCheck()");
98+
// If any epearl changes (or chunk is fully loaded), update owners for all loaded epearls
99+
if(epearlCount != loadedEpearls.size() || fullyLoadedChunk){
100+
epearlCount = loadedEpearls.size();
101+
Main.LOGGER.info("Change to chunks/epearls loaded, calling getOwner() on all epearls and running removal check");
99102
if(enableKeyUUID()){
100-
final HashSet<UUID> seenKeyUUIDs = new HashSet<>(lastEpearlCount);
101-
loadedEpearls.stream().map(EnderPearlEntity::getUuid).forEach(seenKeyUUIDs::add);
103+
final HashSet<UUID> seenKeyUUIDs = new HashSet<>(epearlCount);
104+
loadedEpearls.stream().map(Entity::getUuid).forEach(seenKeyUUIDs::add);
102105
runRemovalCheckUUID(e -> isWithinDist(client.player, e.getValue()) && !seenKeyUUIDs.contains(e.getKey())
103-
&& loadedChunks.contains(toChunkPos(e.getValue())) // TODO: comment this line out
106+
// && loadedChunks.contains(toChunkPos(e.getValue()))
104107
&& !recentlyLoadedChunks.containsKey(toChunkPos(e.getValue())));
105108
}
106109
if(enableKeyXZ()){
107-
final HashSet<UUID> seenKeyXZs = new HashSet<>(lastEpearlCount);
110+
final HashSet<UUID> seenKeyXZs = new HashSet<>(epearlCount);
108111
loadedEpearls.stream().map(EpearlLookupFabric.this::toKeyXZ).forEach(seenKeyXZs::add);
109112
runRemovalCheckXZ(e -> isWithinDist(client.player, e.getValue()) && !seenKeyXZs.contains(e.getKey())
110-
&& loadedChunks.contains(toChunkPos(e.getValue())) // TODO: comment this line out
113+
// && loadedChunks.contains(toChunkPos(e.getValue()))
111114
&& !recentlyLoadedChunks.containsKey(toChunkPos(e.getValue())));
112115
}
113116
}

0 commit comments

Comments
 (0)