Skip to content

Commit 1841472

Browse files
committed
update entityDataSync cache requeue system & add checks for OP.
1 parent e2ae771 commit 1841472

File tree

4 files changed

+179
-64
lines changed

4 files changed

+179
-64
lines changed

src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java

+93-46
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,16 @@
5858
public class EntitiesDataManager implements IClientTickHandler, IDataSyncer
5959
{
6060
private static final EntitiesDataManager INSTANCE = new EntitiesDataManager();
61-
62-
public static EntitiesDataManager getInstance()
63-
{
64-
return INSTANCE;
65-
}
61+
public static EntitiesDataManager getInstance() { return INSTANCE; }
6662

6763
private final static ServuxEntitiesHandler<ServuxEntitiesPacket.Payload> HANDLER = ServuxEntitiesHandler.getInstance();
6864
private final static MinecraftClient mc = MinecraftClient.getInstance();
69-
//private int uptimeTicks = 0;
7065
private boolean servuxServer = false;
7166
private boolean hasInValidServux = false;
7267
private String servuxVersion;
68+
private boolean checkOpStatus = true;
69+
private boolean hasOpStatus = false;
70+
private long lastOpCheck = 0L;
7371

7472
// Data Cache
7573
private final ConcurrentHashMap<BlockPos, Pair<Long, Pair<BlockEntity, NbtCompound>>> blockEntityCache = new ConcurrentHashMap<>();
@@ -106,24 +104,25 @@ private EntitiesDataManager() { }
106104
public void onClientTick(MinecraftClient mc)
107105
{
108106
long now = System.currentTimeMillis();
109-
//this.uptimeTicks++;
110107

111108
if (now - this.serverTickTime > 50)
112109
{
113110
// In this block, we do something every server tick
114-
if (Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue() == false)
111+
if (!Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue())
115112
{
116113
this.serverTickTime = now;
117-
if (DataStorage.getInstance().hasIntegratedServer() == false && this.hasServuxServer())
114+
115+
if (!DataStorage.getInstance().hasIntegratedServer() && this.hasServuxServer())
118116
{
119117
this.servuxServer = false;
120118
HANDLER.unregisterPlayReceiver();
121119
}
120+
122121
return;
123122
}
124-
else if (DataStorage.getInstance().hasIntegratedServer() == false &&
125-
this.hasServuxServer() == false &&
126-
this.hasInValidServux == false &&
123+
else if (!DataStorage.getInstance().hasIntegratedServer() &&
124+
!this.hasServuxServer() &&
125+
!this.hasInValidServux &&
127126
this.getWorld() != null)
128127
{
129128
// Make sure we're Play Registered, and request Metadata
@@ -142,25 +141,29 @@ else if (DataStorage.getInstance().hasIntegratedServer() == false &&
142141
var iter = this.pendingBlockEntitiesQueue.iterator();
143142
BlockPos pos = iter.next();
144143
iter.remove();
144+
145145
if (this.hasServuxServer())
146146
{
147147
requestServuxBlockEntityData(pos);
148148
}
149-
else
149+
else if (this.shouldUseQuery())
150150
{
151+
// Only check once if we have OP
151152
requestQueryBlockEntity(pos);
152153
}
153154
}
155+
154156
if (!this.pendingEntitiesQueue.isEmpty())
155157
{
156158
var iter = this.pendingEntitiesQueue.iterator();
157159
int entityId = iter.next();
158160
iter.remove();
161+
159162
if (this.hasServuxServer())
160163
{
161164
requestServuxEntityData(entityId);
162165
}
163-
else
166+
else if (this.shouldUseQuery())
164167
{
165168
requestQueryEntityData(entityId);
166169
}
@@ -201,6 +204,9 @@ public void reset(boolean isLogout)
201204
HANDLER.resetFailures(this.getNetworkChannel());
202205
this.servuxServer = false;
203206
this.hasInValidServux = false;
207+
this.checkOpStatus = false;
208+
this.hasOpStatus = false;
209+
this.lastOpCheck = 0L;
204210
}
205211
else
206212
{
@@ -210,54 +216,70 @@ public void reset(boolean isLogout)
210216
this.tickCache(now);
211217
this.serverTickTime = now;
212218
this.clientWorld = mc.world;
219+
this.checkOpStatus = true;
220+
this.lastOpCheck = now;
213221
}
222+
214223
// Clear data
215224
this.blockEntityCache.clear();
216225
this.entityCache.clear();
217226
this.pendingBlockEntitiesQueue.clear();
218227
this.pendingEntitiesQueue.clear();
219228
}
220229

230+
private boolean shouldUseQuery()
231+
{
232+
if (this.hasOpStatus) return true;
233+
if (this.checkOpStatus)
234+
{
235+
// Check for 15 minutes after login, or changing dimensions
236+
if ((System.currentTimeMillis() - this.lastOpCheck) < 900000L) return true;
237+
this.checkOpStatus = false;
238+
}
239+
240+
return false;
241+
}
242+
243+
public void resetOpCheck()
244+
{
245+
this.hasOpStatus = false;
246+
this.checkOpStatus = true;
247+
this.lastOpCheck = System.currentTimeMillis();
248+
}
249+
221250
public long getCacheTimeout()
222251
{
223-
return (long) (MathHelper.clamp(Configs.Generic.ENTITY_DATA_SYNC_CACHE_TIMEOUT.getFloatValue(), 0.25f, 25.0f) * 1000L);
252+
return (long) (MathHelper.clamp(Configs.Generic.ENTITY_DATA_SYNC_CACHE_TIMEOUT.getFloatValue(), 0.15f, 25.0f) * 1000L);
224253
}
225254

226255
private void tickCache(long nowTime)
227256
{
228-
long blockTimeout = this.getCacheTimeout();
229-
long entityTimeout = this.getCacheTimeout();
230-
//int total = this.blockEntityCache.size();
231-
//int count = 0;
257+
long timeout = this.getCacheTimeout();
232258

233259
synchronized (this.blockEntityCache)
234260
{
235261
for (BlockPos pos : this.blockEntityCache.keySet())
236262
{
237263
Pair<Long, Pair<BlockEntity, NbtCompound>> pair = this.blockEntityCache.get(pos);
238264

239-
if (nowTime - pair.getLeft() > blockTimeout || pair.getLeft() > nowTime)
265+
if ((nowTime - pair.getLeft()) > timeout || pair.getLeft() > nowTime)
240266
{
241-
MiniHUD.debugLog("entityCache: be at pos [{}] has timed out by [{}] ms", pos.toShortString(), blockTimeout);
267+
MiniHUD.debugLog("entityCache: be at pos [{}] has timed out by [{}] ms", pos.toShortString(), timeout);
242268
this.blockEntityCache.remove(pos);
243-
//count++;
244269
}
245270
}
246271
}
247-
//total = this.entityCache.size();
248-
//count = 0;
249272

250273
synchronized (this.entityCache)
251274
{
252275
for (Integer entityId : this.entityCache.keySet())
253276
{
254277
Pair<Long, Pair<Entity, NbtCompound>> pair = this.entityCache.get(entityId);
255278

256-
if (nowTime - pair.getLeft() > entityTimeout || pair.getLeft() > nowTime)
279+
if ((nowTime - pair.getLeft()) > timeout || pair.getLeft() > nowTime)
257280
{
258-
MiniHUD.debugLog("entityCache: enity Id [{}] has timed out by [{}] ms", entityId, entityTimeout);
281+
MiniHUD.debugLog("entityCache: entity Id [{}] has timed out by [{}] ms", entityId, timeout);
259282
this.entityCache.remove(entityId);
260-
//count++;
261283
}
262284
}
263285
}
@@ -320,7 +342,7 @@ public boolean hasServuxServer()
320342

321343
public void setServuxVersion(String ver)
322344
{
323-
if (ver != null && ver.isEmpty() == false)
345+
if (ver != null && !ver.isEmpty())
324346
{
325347
this.servuxVersion = ver;
326348
MiniHUD.debugLog("entityDataChannel: joining Servux version {}", ver);
@@ -366,7 +388,7 @@ public void onGameInit()
366388
@Override
367389
public void onWorldPre()
368390
{
369-
if (DataStorage.getInstance().hasIntegratedServer() == false)
391+
if (!DataStorage.getInstance().hasIntegratedServer())
370392
{
371393
HANDLER.registerPlayReceiver(ServuxEntitiesPacket.Payload.ID, HANDLER::receivePlayPayload);
372394
}
@@ -390,7 +412,7 @@ public void onEntityDataSyncToggled(ConfigBoolean config)
390412

391413
public void requestMetadata()
392414
{
393-
if (DataStorage.getInstance().hasIntegratedServer() == false &&
415+
if (!DataStorage.getInstance().hasIntegratedServer() &&
394416
Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue())
395417
{
396418
NbtCompound nbt = new NbtCompound();
@@ -402,9 +424,9 @@ public void requestMetadata()
402424

403425
public boolean receiveServuxMetadata(NbtCompound data)
404426
{
405-
if (DataStorage.getInstance().hasIntegratedServer() == false)
427+
if (!DataStorage.getInstance().hasIntegratedServer())
406428
{
407-
MiniHUD.debugLog("EntitiesDataStorage#receiveServuxMetadata(): received METADATA from Servux");
429+
MiniHUD.debugLog("entityDataChannel: received METADATA from Servux");
408430

409431
if (Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue())
410432
{
@@ -434,6 +456,17 @@ public void onPacketFailure()
434456
{
435457
if (this.blockEntityCache.containsKey(pos))
436458
{
459+
// Refresh at 25%
460+
if (!DataStorage.getInstance().hasIntegratedServer() &&
461+
Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue())
462+
{
463+
if (System.currentTimeMillis() - this.blockEntityCache.get(pos).getLeft() > (this.getCacheTimeout() / 4))
464+
{
465+
MiniHUD.debugLog("requestBlockEntity: be at pos [{}] requeue at [{}] ms", pos.toShortString(), this.getCacheTimeout() / 4);
466+
this.pendingBlockEntitiesQueue.add(pos);
467+
}
468+
}
469+
437470
return this.blockEntityCache.get(pos).getRight();
438471
}
439472
else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
@@ -455,6 +488,7 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
455488
{
456489
this.blockEntityCache.put(pos, Pair.of(System.currentTimeMillis(), pair));
457490
}
491+
458492
return pair;
459493
}
460494
}
@@ -467,6 +501,17 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
467501
{
468502
if (this.entityCache.containsKey(entityId))
469503
{
504+
// Refresh at 25%
505+
if (!DataStorage.getInstance().hasIntegratedServer() &&
506+
Configs.Generic.ENTITY_DATA_SYNC.getBooleanValue())
507+
{
508+
if (System.currentTimeMillis() - this.entityCache.get(entityId).getLeft() > (this.getCacheTimeout() / 4))
509+
{
510+
MiniHUD.debugLog("requestEntity: entity Id [{}] requeue at [{}] ms", entityId, this.getCacheTimeout() / 4);
511+
this.pendingEntitiesQueue.add(entityId);
512+
}
513+
}
514+
470515
return this.entityCache.get(entityId).getRight();
471516
}
472517

@@ -624,7 +669,7 @@ else if (entity instanceof PiglinEntity)
624669

625670
private void requestQueryBlockEntity(BlockPos pos)
626671
{
627-
if (Configs.Generic.ENTITY_DATA_SYNC_BACKUP.getBooleanValue() == false)
672+
if (!Configs.Generic.ENTITY_DATA_SYNC_BACKUP.getBooleanValue())
628673
{
629674
return;
630675
}
@@ -633,17 +678,14 @@ private void requestQueryBlockEntity(BlockPos pos)
633678

634679
if (handler != null)
635680
{
636-
handler.getDataQueryHandler().queryBlockNbt(pos, nbtCompound ->
637-
{
638-
handleBlockEntityData(pos, nbtCompound, null);
639-
});
681+
handler.getDataQueryHandler().queryBlockNbt(pos, nbtCompound -> handleBlockEntityData(pos, nbtCompound, null));
640682
this.transactionToBlockPosOrEntityId.put(((IMixinDataQueryHandler) handler.getDataQueryHandler()).malilib_currentTransactionId(), Either.left(pos));
641683
}
642684
}
643685

644686
private void requestQueryEntityData(int entityId)
645687
{
646-
if (Configs.Generic.ENTITY_DATA_SYNC_BACKUP.getBooleanValue() == false)
688+
if (!Configs.Generic.ENTITY_DATA_SYNC_BACKUP.getBooleanValue())
647689
{
648690
return;
649691
}
@@ -652,10 +694,7 @@ private void requestQueryEntityData(int entityId)
652694

653695
if (handler != null)
654696
{
655-
handler.getDataQueryHandler().queryEntityNbt(entityId, nbtCompound ->
656-
{
657-
handleEntityData(entityId, nbtCompound);
658-
});
697+
handler.getDataQueryHandler().queryEntityNbt(entityId, nbtCompound -> handleEntityData(entityId, nbtCompound));
659698
this.transactionToBlockPosOrEntityId.put(((IMixinDataQueryHandler) handler.getDataQueryHandler()).malilib_currentTransactionId(), Either.right(entityId));
660699
}
661700
}
@@ -687,7 +726,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl
687726

688727
if (blockEntity != null && (type == null || type.equals(BlockEntityType.getId(blockEntity.getType()))))
689728
{
690-
if (nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING) == false)
729+
if (!nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING))
691730
{
692731
Identifier id = BlockEntityType.getId(blockEntity.getType());
693732

@@ -717,7 +756,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl
717756

718757
if (blockEntity2 != null)
719758
{
720-
if (nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING) == false)
759+
if (!nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING))
721760
{
722761
Identifier id = BlockEntityType.getId(beType);
723762

@@ -755,7 +794,7 @@ public Entity handleEntityData(int entityId, NbtCompound nbt)
755794

756795
if (entity != null)
757796
{
758-
if (nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING) == false)
797+
if (!nbt.contains(NbtKeys.ID, Constants.NBT.TAG_STRING))
759798
{
760799
Identifier id = EntityType.getId(entity.getType());
761800

@@ -787,11 +826,19 @@ public void handleBulkEntityData(int transactionId, NbtCompound nbt)
787826
@Override
788827
public void handleVanillaQueryNbt(int transactionId, NbtCompound nbt)
789828
{
829+
if (this.checkOpStatus)
830+
{
831+
this.hasOpStatus = true;
832+
this.checkOpStatus = false;
833+
this.lastOpCheck = System.currentTimeMillis();
834+
}
835+
790836
Either<BlockPos, Integer> either = this.transactionToBlockPosOrEntityId.remove(transactionId);
837+
791838
if (either != null)
792839
{
793840
either.ifLeft(pos -> handleBlockEntityData(pos, nbt, null))
794-
.ifRight(entityId -> handleEntityData(entityId, nbt));
841+
.ifRight(entityId -> handleEntityData(entityId, nbt));
795842
}
796843
}
797844
}

0 commit comments

Comments
 (0)