Skip to content

Commit 46928ae

Browse files
committed
Mount item labels and align furnace text
1 parent 2688d9f commit 46928ae

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ public Map<String, DisplayEntity> spawnDisplayEntitys(Block block) {
500500
Vector direction = target.toVector().subtract(origin.toVector()).multiply(0.7);
501501

502502
Location loc = block.getLocation().clone().add(direction).add(0.5, 0.2, 0.5);
503+
loc.setDirection(facing.getDirection());
503504
DisplayEntity slot1 = new DisplayEntity(loc.clone());
504505
setStand(slot1);
505506

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ public Map<String, DisplayEntity> spawnDisplayEntitys(Block block) {
501501
Vector direction = target.toVector().subtract(origin.toVector()).multiply(0.7);
502502

503503
Location loc = block.getLocation().clone().add(direction).add(0.5, 0.2, 0.5);
504+
loc.setDirection(facing.getDirection());
504505
DisplayEntity slot1 = new DisplayEntity(loc.clone());
505506
setStand(slot1);
506507

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ public Map<String, DisplayEntity> spawnDisplayEntitys(Block block) {
500500
Vector direction = target.toVector().subtract(origin.toVector()).multiply(0.7);
501501

502502
Location loc = block.getLocation().clone().add(direction).add(0.5, 0.2, 0.5);
503+
loc.setDirection(facing.getDirection());
503504
DisplayEntity slot1 = new DisplayEntity(loc.clone());
504505
setStand(slot1);
505506

common/src/main/java/com/loohp/interactionvisualizer/entities/DroppedItemDisplay.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.loohp.interactionvisualizer.utils.ItemNameUtils;
2525
import com.loohp.interactionvisualizer.scheduler.ScheduledRunnable;
2626
import com.loohp.interactionvisualizer.scheduler.ScheduledTask;
27+
import com.loohp.interactionvisualizer.scheduler.Scheduler;
2728
import net.kyori.adventure.text.Component;
2829
import net.kyori.adventure.text.TextReplacementConfig;
2930
import net.kyori.adventure.text.format.NamedTextColor;
@@ -40,7 +41,9 @@
4041
import org.bukkit.entity.Player;
4142
import org.bukkit.entity.TextDisplay;
4243
import org.bukkit.event.EventHandler;
44+
import org.bukkit.event.EventPriority;
4345
import org.bukkit.event.Listener;
46+
import org.bukkit.event.entity.EntityRemoveEvent;
4447
import org.bukkit.event.entity.ItemSpawnEvent;
4548
import org.bukkit.event.world.EntitiesLoadEvent;
4649
import org.bukkit.event.world.EntitiesUnloadEvent;
@@ -170,6 +173,20 @@ public void onEntitiesUnload(EntitiesUnloadEvent event) {
170173
}
171174
}
172175

176+
@EventHandler(priority = EventPriority.MONITOR)
177+
public void onEntityRemove(EntityRemoveEvent event) {
178+
if (event.getEntity() instanceof Item item) {
179+
UUID itemId = item.getUniqueId();
180+
trackedItems.remove(itemId);
181+
TextDisplay label = labels.remove(itemId);
182+
if (label != null) {
183+
// EntityRemoveEvent is monitoring-only. Defer entity mutation
184+
// until Paper has finished removing the item's passengers.
185+
Scheduler.runTask(InteractionVisualizer.plugin, () -> removeLabel(label));
186+
}
187+
}
188+
}
189+
173190
private void track(Item item) {
174191
trackedItems.put(item.getUniqueId(), item);
175192
}
@@ -211,17 +228,30 @@ private void update(Item item) {
211228
if (!text.equals(label.text())) {
212229
label.text(text);
213230
}
214-
int transitionTicks = Math.min(59, updateRate);
215-
if (label.getInterpolationDuration() != transitionTicks) {
216-
label.setInterpolationDuration(transitionTicks);
217-
}
218-
if (label.getTeleportDuration() != transitionTicks) {
219-
label.setTeleportDuration(transitionTicks);
220-
}
221-
label.teleport(item.getLocation().add(0.0, item.getHeight() * 1.7, 0.0));
231+
boolean mounted = item.equals(label.getVehicle()) || item.addPassenger(label);
222232
if (created) {
233+
// Mount before revealing the label so Paper can pair both entities
234+
// with their passenger relationship in the initial tracking bundle.
223235
showToEligibleViewers(label);
224236
}
237+
if (mounted) {
238+
// A mounted display follows the item on every client render frame.
239+
// Text refreshes stay low-frequency without sampling item positions.
240+
if (label.getInterpolationDuration() != 0) {
241+
label.setInterpolationDuration(0);
242+
}
243+
if (label.getTeleportDuration() != 0) {
244+
label.setTeleportDuration(0);
245+
}
246+
} else {
247+
// Preserve a safe fallback if another plugin cancels the mount or
248+
// changes either entity during the update.
249+
int transitionTicks = Math.min(59, updateRate);
250+
if (label.getTeleportDuration() != transitionTicks) {
251+
label.setTeleportDuration(transitionTicks);
252+
}
253+
label.teleport(item.getLocation().add(0.0, item.getHeight() * 1.7, 0.0));
254+
}
225255
}
226256

227257
private TextDisplay spawnLabel(Item item) {
@@ -235,8 +265,8 @@ private TextDisplay spawnLabel(Item item) {
235265
display.setNoPhysics(true);
236266
display.setBillboard(Display.Billboard.CENTER);
237267
display.setViewRange(1.0F);
238-
display.setInterpolationDuration(Math.min(59, updateRate));
239-
display.setTeleportDuration(Math.min(59, updateRate));
268+
display.setInterpolationDuration(0);
269+
display.setTeleportDuration(0);
240270
display.setShadowed(true);
241271
display.setSeeThrough(false);
242272
display.setDefaultBackground(false);
@@ -344,7 +374,10 @@ private void remove(UUID itemId) {
344374
}
345375

346376
private void removeLabel(UUID itemId) {
347-
TextDisplay label = labels.remove(itemId);
377+
removeLabel(labels.remove(itemId));
378+
}
379+
380+
private void removeLabel(TextDisplay label) {
348381
if (label != null && label.isValid()) {
349382
for (UUID uuid : eligibleViewers) {
350383
Player player = Bukkit.getPlayer(uuid);

0 commit comments

Comments
 (0)