58
58
public class EntitiesDataManager implements IClientTickHandler , IDataSyncer
59
59
{
60
60
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 ; }
66
62
67
63
private final static ServuxEntitiesHandler <ServuxEntitiesPacket .Payload > HANDLER = ServuxEntitiesHandler .getInstance ();
68
64
private final static MinecraftClient mc = MinecraftClient .getInstance ();
69
- //private int uptimeTicks = 0;
70
65
private boolean servuxServer = false ;
71
66
private boolean hasInValidServux = false ;
72
67
private String servuxVersion ;
68
+ private boolean checkOpStatus = true ;
69
+ private boolean hasOpStatus = false ;
70
+ private long lastOpCheck = 0L ;
73
71
74
72
// Data Cache
75
73
private final ConcurrentHashMap <BlockPos , Pair <Long , Pair <BlockEntity , NbtCompound >>> blockEntityCache = new ConcurrentHashMap <>();
@@ -106,24 +104,25 @@ private EntitiesDataManager() { }
106
104
public void onClientTick (MinecraftClient mc )
107
105
{
108
106
long now = System .currentTimeMillis ();
109
- //this.uptimeTicks++;
110
107
111
108
if (now - this .serverTickTime > 50 )
112
109
{
113
110
// 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 ())
115
112
{
116
113
this .serverTickTime = now ;
117
- if (DataStorage .getInstance ().hasIntegratedServer () == false && this .hasServuxServer ())
114
+
115
+ if (!DataStorage .getInstance ().hasIntegratedServer () && this .hasServuxServer ())
118
116
{
119
117
this .servuxServer = false ;
120
118
HANDLER .unregisterPlayReceiver ();
121
119
}
120
+
122
121
return ;
123
122
}
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 &&
127
126
this .getWorld () != null )
128
127
{
129
128
// Make sure we're Play Registered, and request Metadata
@@ -142,25 +141,29 @@ else if (DataStorage.getInstance().hasIntegratedServer() == false &&
142
141
var iter = this .pendingBlockEntitiesQueue .iterator ();
143
142
BlockPos pos = iter .next ();
144
143
iter .remove ();
144
+
145
145
if (this .hasServuxServer ())
146
146
{
147
147
requestServuxBlockEntityData (pos );
148
148
}
149
- else
149
+ else if ( this . shouldUseQuery ())
150
150
{
151
+ // Only check once if we have OP
151
152
requestQueryBlockEntity (pos );
152
153
}
153
154
}
155
+
154
156
if (!this .pendingEntitiesQueue .isEmpty ())
155
157
{
156
158
var iter = this .pendingEntitiesQueue .iterator ();
157
159
int entityId = iter .next ();
158
160
iter .remove ();
161
+
159
162
if (this .hasServuxServer ())
160
163
{
161
164
requestServuxEntityData (entityId );
162
165
}
163
- else
166
+ else if ( this . shouldUseQuery ())
164
167
{
165
168
requestQueryEntityData (entityId );
166
169
}
@@ -201,6 +204,9 @@ public void reset(boolean isLogout)
201
204
HANDLER .resetFailures (this .getNetworkChannel ());
202
205
this .servuxServer = false ;
203
206
this .hasInValidServux = false ;
207
+ this .checkOpStatus = false ;
208
+ this .hasOpStatus = false ;
209
+ this .lastOpCheck = 0L ;
204
210
}
205
211
else
206
212
{
@@ -210,54 +216,70 @@ public void reset(boolean isLogout)
210
216
this .tickCache (now );
211
217
this .serverTickTime = now ;
212
218
this .clientWorld = mc .world ;
219
+ this .checkOpStatus = true ;
220
+ this .lastOpCheck = now ;
213
221
}
222
+
214
223
// Clear data
215
224
this .blockEntityCache .clear ();
216
225
this .entityCache .clear ();
217
226
this .pendingBlockEntitiesQueue .clear ();
218
227
this .pendingEntitiesQueue .clear ();
219
228
}
220
229
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
+
221
250
public long getCacheTimeout ()
222
251
{
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 );
224
253
}
225
254
226
255
private void tickCache (long nowTime )
227
256
{
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 ();
232
258
233
259
synchronized (this .blockEntityCache )
234
260
{
235
261
for (BlockPos pos : this .blockEntityCache .keySet ())
236
262
{
237
263
Pair <Long , Pair <BlockEntity , NbtCompound >> pair = this .blockEntityCache .get (pos );
238
264
239
- if (nowTime - pair .getLeft () > blockTimeout || pair .getLeft () > nowTime )
265
+ if (( nowTime - pair .getLeft ()) > timeout || pair .getLeft () > nowTime )
240
266
{
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 );
242
268
this .blockEntityCache .remove (pos );
243
- //count++;
244
269
}
245
270
}
246
271
}
247
- //total = this.entityCache.size();
248
- //count = 0;
249
272
250
273
synchronized (this .entityCache )
251
274
{
252
275
for (Integer entityId : this .entityCache .keySet ())
253
276
{
254
277
Pair <Long , Pair <Entity , NbtCompound >> pair = this .entityCache .get (entityId );
255
278
256
- if (nowTime - pair .getLeft () > entityTimeout || pair .getLeft () > nowTime )
279
+ if (( nowTime - pair .getLeft ()) > timeout || pair .getLeft () > nowTime )
257
280
{
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 );
259
282
this .entityCache .remove (entityId );
260
- //count++;
261
283
}
262
284
}
263
285
}
@@ -320,7 +342,7 @@ public boolean hasServuxServer()
320
342
321
343
public void setServuxVersion (String ver )
322
344
{
323
- if (ver != null && ver .isEmpty () == false )
345
+ if (ver != null && ! ver .isEmpty ())
324
346
{
325
347
this .servuxVersion = ver ;
326
348
MiniHUD .debugLog ("entityDataChannel: joining Servux version {}" , ver );
@@ -366,7 +388,7 @@ public void onGameInit()
366
388
@ Override
367
389
public void onWorldPre ()
368
390
{
369
- if (DataStorage .getInstance ().hasIntegratedServer () == false )
391
+ if (! DataStorage .getInstance ().hasIntegratedServer ())
370
392
{
371
393
HANDLER .registerPlayReceiver (ServuxEntitiesPacket .Payload .ID , HANDLER ::receivePlayPayload );
372
394
}
@@ -390,7 +412,7 @@ public void onEntityDataSyncToggled(ConfigBoolean config)
390
412
391
413
public void requestMetadata ()
392
414
{
393
- if (DataStorage .getInstance ().hasIntegratedServer () == false &&
415
+ if (! DataStorage .getInstance ().hasIntegratedServer () &&
394
416
Configs .Generic .ENTITY_DATA_SYNC .getBooleanValue ())
395
417
{
396
418
NbtCompound nbt = new NbtCompound ();
@@ -402,9 +424,9 @@ public void requestMetadata()
402
424
403
425
public boolean receiveServuxMetadata (NbtCompound data )
404
426
{
405
- if (DataStorage .getInstance ().hasIntegratedServer () == false )
427
+ if (! DataStorage .getInstance ().hasIntegratedServer ())
406
428
{
407
- MiniHUD .debugLog ("EntitiesDataStorage#receiveServuxMetadata() : received METADATA from Servux" );
429
+ MiniHUD .debugLog ("entityDataChannel : received METADATA from Servux" );
408
430
409
431
if (Configs .Generic .ENTITY_DATA_SYNC .getBooleanValue ())
410
432
{
@@ -434,6 +456,17 @@ public void onPacketFailure()
434
456
{
435
457
if (this .blockEntityCache .containsKey (pos ))
436
458
{
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
+
437
470
return this .blockEntityCache .get (pos ).getRight ();
438
471
}
439
472
else if (world .getBlockState (pos ).getBlock () instanceof BlockEntityProvider )
@@ -455,6 +488,7 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
455
488
{
456
489
this .blockEntityCache .put (pos , Pair .of (System .currentTimeMillis (), pair ));
457
490
}
491
+
458
492
return pair ;
459
493
}
460
494
}
@@ -467,6 +501,17 @@ else if (world.getBlockState(pos).getBlock() instanceof BlockEntityProvider)
467
501
{
468
502
if (this .entityCache .containsKey (entityId ))
469
503
{
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
+
470
515
return this .entityCache .get (entityId ).getRight ();
471
516
}
472
517
@@ -624,7 +669,7 @@ else if (entity instanceof PiglinEntity)
624
669
625
670
private void requestQueryBlockEntity (BlockPos pos )
626
671
{
627
- if (Configs .Generic .ENTITY_DATA_SYNC_BACKUP .getBooleanValue () == false )
672
+ if (! Configs .Generic .ENTITY_DATA_SYNC_BACKUP .getBooleanValue ())
628
673
{
629
674
return ;
630
675
}
@@ -633,17 +678,14 @@ private void requestQueryBlockEntity(BlockPos pos)
633
678
634
679
if (handler != null )
635
680
{
636
- handler .getDataQueryHandler ().queryBlockNbt (pos , nbtCompound ->
637
- {
638
- handleBlockEntityData (pos , nbtCompound , null );
639
- });
681
+ handler .getDataQueryHandler ().queryBlockNbt (pos , nbtCompound -> handleBlockEntityData (pos , nbtCompound , null ));
640
682
this .transactionToBlockPosOrEntityId .put (((IMixinDataQueryHandler ) handler .getDataQueryHandler ()).malilib_currentTransactionId (), Either .left (pos ));
641
683
}
642
684
}
643
685
644
686
private void requestQueryEntityData (int entityId )
645
687
{
646
- if (Configs .Generic .ENTITY_DATA_SYNC_BACKUP .getBooleanValue () == false )
688
+ if (! Configs .Generic .ENTITY_DATA_SYNC_BACKUP .getBooleanValue ())
647
689
{
648
690
return ;
649
691
}
@@ -652,10 +694,7 @@ private void requestQueryEntityData(int entityId)
652
694
653
695
if (handler != null )
654
696
{
655
- handler .getDataQueryHandler ().queryEntityNbt (entityId , nbtCompound ->
656
- {
657
- handleEntityData (entityId , nbtCompound );
658
- });
697
+ handler .getDataQueryHandler ().queryEntityNbt (entityId , nbtCompound -> handleEntityData (entityId , nbtCompound ));
659
698
this .transactionToBlockPosOrEntityId .put (((IMixinDataQueryHandler ) handler .getDataQueryHandler ()).malilib_currentTransactionId (), Either .right (entityId ));
660
699
}
661
700
}
@@ -687,7 +726,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl
687
726
688
727
if (blockEntity != null && (type == null || type .equals (BlockEntityType .getId (blockEntity .getType ()))))
689
728
{
690
- if (nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ) == false )
729
+ if (! nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ))
691
730
{
692
731
Identifier id = BlockEntityType .getId (blockEntity .getType ());
693
732
@@ -717,7 +756,7 @@ public BlockEntity handleBlockEntityData(BlockPos pos, NbtCompound nbt, @Nullabl
717
756
718
757
if (blockEntity2 != null )
719
758
{
720
- if (nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ) == false )
759
+ if (! nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ))
721
760
{
722
761
Identifier id = BlockEntityType .getId (beType );
723
762
@@ -755,7 +794,7 @@ public Entity handleEntityData(int entityId, NbtCompound nbt)
755
794
756
795
if (entity != null )
757
796
{
758
- if (nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ) == false )
797
+ if (! nbt .contains (NbtKeys .ID , Constants .NBT .TAG_STRING ))
759
798
{
760
799
Identifier id = EntityType .getId (entity .getType ());
761
800
@@ -787,11 +826,19 @@ public void handleBulkEntityData(int transactionId, NbtCompound nbt)
787
826
@ Override
788
827
public void handleVanillaQueryNbt (int transactionId , NbtCompound nbt )
789
828
{
829
+ if (this .checkOpStatus )
830
+ {
831
+ this .hasOpStatus = true ;
832
+ this .checkOpStatus = false ;
833
+ this .lastOpCheck = System .currentTimeMillis ();
834
+ }
835
+
790
836
Either <BlockPos , Integer > either = this .transactionToBlockPosOrEntityId .remove (transactionId );
837
+
791
838
if (either != null )
792
839
{
793
840
either .ifLeft (pos -> handleBlockEntityData (pos , nbt , null ))
794
- .ifRight (entityId -> handleEntityData (entityId , nbt ));
841
+ .ifRight (entityId -> handleEntityData (entityId , nbt ));
795
842
}
796
843
}
797
844
}
0 commit comments