Skip to content

Commit 09103ff

Browse files
committed
Coordinate block and workstation updates
1 parent edb0423 commit 09103ff

28 files changed

Lines changed: 1536 additions & 403 deletions

common/src/main/java/com/loohp/interactionvisualizer/blocks/BannerDisplay.java

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public EntryKey key() {
9292

9393
@Override
9494
public ScheduledTask gc() {
95+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
96+
return null;
97+
}
9598
return Scheduler.runTaskTimer(InteractionVisualizer.plugin, () -> {
9699
Iterator<Entry<Block, Map<String, Object>>> itr = bannerMap.entrySet().iterator();
97100
int count = 0;
@@ -130,6 +133,16 @@ public ScheduledTask gc() {
130133

131134
@Override
132135
public ScheduledTask run() {
136+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
137+
BlockUpdateCoordinator.register(this,
138+
BlockUpdateCoordinator.materialsMatching(this::isBanner), this::nearbyBanner,
139+
checkingPeriod, gcPeriod, this::updateHybridBlock, this::removeTrackedDisplay);
140+
return null;
141+
}
142+
return legacyRun();
143+
}
144+
145+
private ScheduledTask legacyRun() {
133146
return Scheduler.runTaskTimer(InteractionVisualizer.plugin, () -> {
134147
Set<Block> list = nearbyBanner();
135148
for (Block block : list) {
@@ -200,19 +213,76 @@ public ScheduledTask run() {
200213
}, 0, checkingPeriod);
201214
}
202215

216+
private boolean updateHybridBlock(Block block) {
217+
if (!nearbyBanner().contains(block)
218+
|| !block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4)
219+
|| !isBanner(block.getType())) {
220+
removeTrackedDisplay(block);
221+
return false;
222+
}
223+
if (!isActive(block.getLocation())) {
224+
return false;
225+
}
226+
Map<String, Object> values = bannerMap.get(block);
227+
if (values == null) {
228+
values = new HashMap<>();
229+
values.put("Item", "N/A");
230+
values.putAll(spawnDisplayEntitys(block));
231+
bannerMap.put(block, values);
232+
}
233+
updateTrackedBlock(block, values);
234+
return false;
235+
}
236+
237+
private void updateTrackedBlock(Block block, Map<String, Object> values) {
238+
Component name = ((Banner) block.getState(false)).customName();
239+
DisplayEntity line = (DisplayEntity) values.get("1");
240+
if (name == null || PlainTextComponentSerializer.plainText().serialize(name).isEmpty()) {
241+
if (!PlainTextComponentSerializer.plainText().serialize(line.getCustomName()).isEmpty()
242+
|| line.isCustomNameVisible()) {
243+
line.setCustomName("");
244+
line.setCustomNameVisible(false);
245+
DisplayManager.updateDisplay(line);
246+
}
247+
return;
248+
}
249+
String matchingName = LegacyComponentSerializer.legacySection().serialize(name);
250+
if (stripColorBlacklist) {
251+
matchingName = ChatColorUtils.stripColor(matchingName);
252+
}
253+
if (blacklist.test(matchingName)) {
254+
if (!PlainTextComponentSerializer.plainText().serialize(line.getCustomName()).isEmpty()
255+
|| line.isCustomNameVisible()) {
256+
line.setCustomName("");
257+
line.setCustomNameVisible(false);
258+
DisplayManager.updateDisplay(line);
259+
}
260+
} else if (!line.getCustomName().equals(name) || !line.isCustomNameVisible()) {
261+
line.setCustomName(name);
262+
line.setCustomNameVisible(true);
263+
DisplayManager.updateDisplay(line);
264+
}
265+
}
266+
203267
@EventHandler(priority = EventPriority.MONITOR)
204268
public void onBreakBanner(TileEntityRemovedEvent event) {
205269
Block block = event.getBlock();
206-
if (!bannerMap.containsKey(block)) {
207-
return;
270+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
271+
BlockUpdateCoordinator.remove(this, block);
272+
} else {
273+
removeTrackedDisplay(block);
208274
}
275+
}
209276

210-
Map<String, Object> map = bannerMap.get(block);
277+
private void removeTrackedDisplay(Block block) {
278+
Map<String, Object> map = bannerMap.remove(block);
279+
if (map == null) {
280+
return;
281+
}
211282
if (map.get("1") instanceof DisplayEntity) {
212283
DisplayEntity stand = (DisplayEntity) map.get("1");
213284
DisplayManager.removeDisplay(InteractionVisualizerAPI.getPlayers(), stand);
214285
}
215-
bannerMap.remove(block);
216286
}
217287

218288
public Set<Block> nearbyBanner() {

common/src/main/java/com/loohp/interactionvisualizer/blocks/BeaconDisplay.java

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public EntryKey key() {
8888

8989
@Override
9090
public ScheduledTask gc() {
91+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
92+
return null;
93+
}
9194
return Scheduler.runTaskTimer(InteractionVisualizer.plugin, () -> {
9295
Iterator<Entry<Block, Map<String, Object>>> itr = beaconMap.entrySet().iterator();
9396
int count = 0;
@@ -143,6 +146,15 @@ public ScheduledTask gc() {
143146

144147
@Override
145148
public ScheduledTask run() {
149+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
150+
BlockUpdateCoordinator.register(this, Set.of(Material.BEACON), this::nearbyBeacon,
151+
checkingPeriod, gcPeriod, this::updateHybridBlock, this::removeTrackedDisplay);
152+
return null;
153+
}
154+
return legacyRun();
155+
}
156+
157+
private ScheduledTask legacyRun() {
146158
return Scheduler.runTaskTimer(InteractionVisualizer.plugin, () -> {
147159
Set<Block> list = nearbyBeacon();
148160
for (Block block : list) {
@@ -178,7 +190,7 @@ public ScheduledTask run() {
178190
if (!block.getType().equals(Material.BEACON)) {
179191
return;
180192
}
181-
org.bukkit.block.Beacon beacon = (org.bukkit.block.Beacon) block.getState();
193+
org.bukkit.block.Beacon beacon = (org.bukkit.block.Beacon) block.getState(false);
182194
{
183195
String arrow = "\u27f9";
184196
String up = "\u25b2";
@@ -277,14 +289,102 @@ public ScheduledTask run() {
277289
}, 0, checkingPeriod);
278290
}
279291

292+
private boolean updateHybridBlock(Block block) {
293+
if (!nearbyBeacon().contains(block)
294+
|| !block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4)
295+
|| block.getType() != Material.BEACON) {
296+
removeTrackedDisplay(block);
297+
return false;
298+
}
299+
if (!isActive(block.getLocation())) {
300+
return false;
301+
}
302+
Map<String, Object> values = beaconMap.get(block);
303+
if (values == null) {
304+
values = new HashMap<>();
305+
values.put("Item", "N/A");
306+
values.putAll(spawnDisplayEntitys(block));
307+
beaconMap.put(block, values);
308+
}
309+
updateTrackedBlock(block, values);
310+
return false;
311+
}
312+
313+
private void updateTrackedBlock(Block block, Map<String, Object> values) {
314+
org.bukkit.block.Beacon beacon = (org.bukkit.block.Beacon) block.getState(false);
315+
String arrow = "\u27f9";
316+
String up = "\u25b2";
317+
NamedTextColor color = getBeaconColor(block);
318+
DisplayEntity line1 = (DisplayEntity) values.get("1");
319+
DisplayEntity line2 = (DisplayEntity) values.get("2");
320+
DisplayEntity line3 = (DisplayEntity) values.get("3");
321+
322+
Component summary = Component.text(up + beacon.getTier() + " " + arrow + " "
323+
+ DECIMAL_FORMAT.format(beacon.getEffectRange()) + "m", color);
324+
if (beacon.getTier() == 0) {
325+
updateLine(line1, null);
326+
updateLine(line2, summary);
327+
updateLine(line3, null);
328+
return;
329+
}
330+
331+
Component primary = null;
332+
Component secondary = null;
333+
if (beacon.getPrimaryEffect() != null) {
334+
TranslatableComponent effect = Component.translatable(
335+
TranslationUtils.getEffect(beacon.getPrimaryEffect().getType())).color(color);
336+
Component level = ComponentFont.parseFont(Component.text(" "
337+
+ RomanNumberUtils.toRoman(beacon.getPrimaryEffect().getAmplifier() + 1), color));
338+
primary = effect.append(level);
339+
}
340+
if (beacon.getSecondaryEffect() != null) {
341+
TranslatableComponent effect = Component.translatable(
342+
TranslationUtils.getEffect(beacon.getSecondaryEffect().getType())).color(color);
343+
Component level = ComponentFont.parseFont(Component.text(" "
344+
+ RomanNumberUtils.toRoman(beacon.getSecondaryEffect().getAmplifier() + 1), color));
345+
secondary = effect.append(level);
346+
}
347+
if (secondary == null) {
348+
updateLine(line1, null);
349+
updateLine(line2, summary);
350+
updateLine(line3, primary);
351+
} else {
352+
updateLine(line1, summary);
353+
updateLine(line2, primary);
354+
updateLine(line3, secondary);
355+
}
356+
}
357+
358+
private static void updateLine(DisplayEntity line, Component component) {
359+
if (component == null) {
360+
if (!PlainTextComponentSerializer.plainText().serialize(line.getCustomName()).isEmpty()
361+
|| line.isCustomNameVisible()) {
362+
line.setCustomName("");
363+
line.setCustomNameVisible(false);
364+
DisplayManager.updateDisplay(line);
365+
}
366+
} else if (!line.getCustomName().equals(component) || !line.isCustomNameVisible()) {
367+
line.setCustomName(component);
368+
line.setCustomNameVisible(true);
369+
DisplayManager.updateDisplay(line);
370+
}
371+
}
372+
280373
@EventHandler(priority = EventPriority.MONITOR)
281374
public void onBreakBeacon(TileEntityRemovedEvent event) {
282375
Block block = event.getBlock();
283-
if (!beaconMap.containsKey(block)) {
284-
return;
376+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
377+
BlockUpdateCoordinator.remove(this, block);
378+
} else {
379+
removeTrackedDisplay(block);
285380
}
381+
}
286382

287-
Map<String, Object> map = beaconMap.get(block);
383+
private void removeTrackedDisplay(Block block) {
384+
Map<String, Object> map = beaconMap.remove(block);
385+
if (map == null) {
386+
return;
387+
}
288388
if (map.get("1") instanceof DisplayEntity) {
289389
DisplayEntity stand = (DisplayEntity) map.get("1");
290390
DisplayManager.removeDisplay(InteractionVisualizerAPI.getPlayers(), stand);
@@ -297,7 +397,6 @@ public void onBreakBeacon(TileEntityRemovedEvent event) {
297397
DisplayEntity stand = (DisplayEntity) map.get("3");
298398
DisplayManager.removeDisplay(InteractionVisualizerAPI.getPlayers(), stand);
299399
}
300-
beaconMap.remove(block);
301400
}
302401

303402
public Set<Block> nearbyBeacon() {

common/src/main/java/com/loohp/interactionvisualizer/blocks/BeeHiveDisplay.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ public class BeeHiveDisplay extends VisualizerRunnableDisplay implements Listene
8383
private String filledColor = "&e";
8484
private String noCampfireColor = "&c";
8585
private String beeCountText = "&e{Current}&6/{Max}";
86-
private final BlockUpdateScheduler<Block> blockUpdates;
8786

8887
public BeeHiveDisplay() {
8988
onReload(new InteractionVisualizerReloadEvent());
90-
this.blockUpdates = new BlockUpdateScheduler<>(this::nearbyBeehive,
91-
this.checkingPeriod, this.gcPeriod);
9289
}
9390

9491
@EventHandler
@@ -162,15 +159,9 @@ public ScheduledTask run() {
162159
if (!InteractionVisualizer.eventDrivenBlockUpdates) {
163160
return legacyRun();
164161
}
165-
return Scheduler.runTaskTimer(InteractionVisualizer.plugin, () -> {
166-
boolean collecting = PerformanceMetrics.isCollecting();
167-
long start = collecting ? System.nanoTime() : 0L;
168-
int checks = this.blockUpdates.tick(Bukkit.getCurrentTick(),
169-
InteractionVisualizer.blockUpdateMaxDirtyPerTick, this::updateHybridBlock);
170-
if (collecting) {
171-
PerformanceMetrics.blockUpdateChecks(checks, System.nanoTime() - start);
172-
}
173-
}, 0, 1);
162+
BlockUpdateCoordinator.register(this, Set.of(Material.BEEHIVE), this::nearbyBeehive,
163+
checkingPeriod, gcPeriod, this::updateHybridBlock, this::removeTrackedDisplay);
164+
return null;
174165
}
175166

176167
private ScheduledTask legacyRun() {
@@ -247,7 +238,7 @@ private boolean updateHybridBlock(Block block) {
247238
private void markDirty(Block block) {
248239
if (InteractionVisualizer.eventDrivenBlockUpdates && block != null
249240
&& block.getType() == Material.BEEHIVE && beehiveMap.containsKey(block)) {
250-
blockUpdates.markDirty(block, (long) Bukkit.getCurrentTick() + 1L);
241+
BlockUpdateCoordinator.markDirty(this, block, (long) Bukkit.getCurrentTick() + 1L);
251242
}
252243
}
253244

@@ -342,21 +333,23 @@ public void onDispenserHarvest(BlockDispenseEvent event) {
342333
public void onBeehiveAdded(TileEntityAddedEvent event) {
343334
if (InteractionVisualizer.eventDrivenBlockUpdates
344335
&& event.getTileEntityType() == TileEntityType.BEEHIVE) {
345-
blockUpdates.markDirty(event.getBlock(), (long) Bukkit.getCurrentTick() + 1L);
336+
BlockUpdateCoordinator.markDirty(
337+
this, event.getBlock(), (long) Bukkit.getCurrentTick() + 1L);
346338
}
347339
}
348340

349341
public void onBeehiveActivated(TileEntityActivatedEvent event) {
350342
if (InteractionVisualizer.eventDrivenBlockUpdates
351343
&& event.getTileEntityType() == TileEntityType.BEEHIVE) {
352-
blockUpdates.markDirty(event.getBlock(), (long) Bukkit.getCurrentTick() + 1L);
344+
BlockUpdateCoordinator.markDirty(
345+
this, event.getBlock(), (long) Bukkit.getCurrentTick() + 1L);
353346
}
354347
}
355348

356349
public void onBeehiveDeactivated(TileEntityDeactivatedEvent event) {
357350
if (InteractionVisualizer.eventDrivenBlockUpdates
358351
&& event.getTileEntityType() == TileEntityType.BEEHIVE) {
359-
removeTrackedDisplay(event.getBlock());
352+
BlockUpdateCoordinator.remove(this, event.getBlock());
360353
}
361354
}
362355

@@ -401,11 +394,18 @@ public void onInteractBeehive(PlayerInteractEvent event) {
401394

402395
@EventHandler(priority = EventPriority.MONITOR)
403396
public void onBreakBeehive(TileEntityRemovedEvent event) {
404-
removeTrackedDisplay(event.getBlock());
397+
invalidateTrackedDisplay(event.getBlock());
398+
}
399+
400+
private void invalidateTrackedDisplay(Block block) {
401+
if (InteractionVisualizer.eventDrivenBlockUpdates) {
402+
BlockUpdateCoordinator.remove(this, block);
403+
} else {
404+
removeTrackedDisplay(block);
405+
}
405406
}
406407

407408
private void removeTrackedDisplay(Block block) {
408-
blockUpdates.remove(block);
409409
Map<String, Object> map = beehiveMap.remove(block);
410410
if (map == null) {
411411
return;

0 commit comments

Comments
 (0)