Skip to content

Commit

Permalink
Reimplement hologram display entities using interaction entities as s…
Browse files Browse the repository at this point in the history
…uggested by Owen1212055
  • Loading branch information
fullwall committed Aug 27, 2023
1 parent c447b0d commit db6058b
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 34 deletions.
3 changes: 3 additions & 0 deletions main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -166,6 +166,9 @@ public void loadFromKey(DataKey root) {
"Minecraft will pick a 'close-enough' location when pathfinding to a block if it can't find a direct path<br>Disabled by default",
"npc.pathfinding.disable-mc-fallback-navigation", true),
DISABLE_TABLIST("Whether to remove NPCs from the tablist", "npc.tablist.disable", true),
DISPLAY_ENTITY_HOLOGRAMS(
"Whether to use display entities for holograms by default. In theory more performant than armor stands. Requires 1.19.4 or above. Defaults to false",
"npc.hologram.use-display-entities", false),
ENTITY_SPAWN_WAIT_DURATION(
"Entities are no longer spawned until the chunks are loaded from disk<br>Wait for chunk loading for one second by default, increase if your disk is slow",
"general.entity-spawn-wait-ticks", "general.wait-for-entity-spawn", "1s"),
Expand Down
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -99,7 +99,7 @@
import net.citizensnpcs.commands.gui.NPCConfigurator;
import net.citizensnpcs.commands.history.CommandHistory;
import net.citizensnpcs.commands.history.CreateNPCHistoryItem;
import net.citizensnpcs.commands.history.RemoveNPCHistoryItem;
import net.citizensnpcs.commands.history.RemoveNPCHistoryItem;
import net.citizensnpcs.npc.EntityControllers;
import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.npc.Template;
Expand Down Expand Up @@ -2797,7 +2797,7 @@ public void sitting(CommandContext args, CommandSender sender, NPC npc, @Flag("e
Messaging.sendTr(sender, Messages.SITTING_SET, npc.getName(), Util.prettyPrintLocation(at));
}

@Command(
@Command(
aliases = { "npc" },
usage = "skin (-e(xport) -c(lear) -l(atest)) [name] (or --url [url] --file [file] (-s(lim)) or -t [uuid/name] [data] [signature])",
desc = "Sets an NPC's skin name. Use -l to set the skin to always update to the latest",
Expand Down
Expand Up @@ -39,9 +39,9 @@ public PassableState isPassable(BlockSource source, PathPoint point) {
if (above == null || below == null)
return PassableState.IGNORE;
float height = (float) (above.minY - below.maxY);
if (height < this.height) {
if (height < this.height)
return PassableState.UNPASSABLE;
}

}
return PassableState.IGNORE;
}
Expand Down
26 changes: 13 additions & 13 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Expand Up @@ -12,11 +12,10 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Display.Billboard;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.Player;
import org.bukkit.entity.TextDisplay;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;

Expand Down Expand Up @@ -55,7 +54,7 @@ public class HologramTrait extends Trait {
private HologramLine nameLine;
private final NPCRegistry registry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
private int t;
private boolean useTextDisplay;
private boolean useDisplayEntities = Setting.DISPLAY_ENTITY_HOLOGRAMS.asBoolean();

public HologramTrait() {
super("hologramtrait");
Expand Down Expand Up @@ -99,9 +98,10 @@ public void clear() {
@SuppressWarnings("deprecation")
private NPC createHologram(String line, double heightOffset) {
NPC hologramNPC = null;
if (useTextDisplay) {
hologramNPC = registry.createNPC(EntityType.TEXT_DISPLAY, line);
if (useDisplayEntities) {
hologramNPC = registry.createNPC(EntityType.INTERACTION, line);
hologramNPC.addTrait(new ClickRedirectTrait(npc));
hologramNPC.data().set(NPC.Metadata.NAMEPLATE_VISIBLE, true);
} else {
hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
hologramNPC.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntityWithName(npc);
Expand All @@ -116,9 +116,9 @@ private NPC createHologram(String line, double heightOffset) {
+ (direction == HologramDirection.BOTTOM_UP ? heightOffset : getMaxHeight() - heightOffset),
0));

if (useTextDisplay) {
((TextDisplay) hologramNPC.getEntity()).setBillboard(Billboard.CENTER);
((TextDisplay) hologramNPC.getEntity()).setInterpolationDelay(0);
if (useDisplayEntities) {
((Interaction) hologramNPC.getEntity()).setInteractionWidth(0);
NMS.updateMountedInteractionHeight(hologramNPC.getEntity(), npc.getEntity(), heightOffset);
}

Matcher itemMatcher = ITEM_MATCHER.matcher(line);
Expand Down Expand Up @@ -159,7 +159,7 @@ public HologramDirection getDirection() {
}

private double getEntityHeight() {
return NMS.getHeight(npc.getEntity()) + (useTextDisplay ? 0.27 : 0);
return NMS.getHeight(npc.getEntity());
}

private double getHeight(int lineNumber) {
Expand Down Expand Up @@ -320,7 +320,7 @@ public void run() {
}

if (nameLine != null && nameLine.hologram.isSpawned()) {
if (updatePosition) {
if (updatePosition && !useDisplayEntities) {
nameLine.hologram.teleport(currentLoc.clone().add(0, getEntityHeight(), 0), TeleportCause.PLUGIN);
}
if (updateName) {
Expand All @@ -334,7 +334,7 @@ public void run() {
if (hologramNPC == null || !hologramNPC.isSpawned())
continue;

if (updatePosition) {
if (updatePosition && !useDisplayEntities) {
Location tp = currentLoc.clone().add(0, lastEntityHeight
+ (direction == HologramDirection.BOTTOM_UP ? getHeight(i) : getMaxHeight() - getHeight(i)), 0);
hologramNPC.teleport(tp, TeleportCause.PLUGIN);
Expand Down Expand Up @@ -443,8 +443,8 @@ public void setPerPlayerTextSupplier(BiFunction<String, Player, String> nameSupp
this.customHologramSupplier = nameSupplier;
}

public void setUseTextDisplay(boolean use) {
this.useTextDisplay = use;
public void setUseDisplayEntities(boolean use) {
this.useDisplayEntities = use;
reloadLineHolograms();
}

Expand Down
3 changes: 3 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/MountTrait.java
Expand Up @@ -71,6 +71,9 @@ public void run() {

public void setMountedOn(UUID uuid) {
this.uuid = uuid;
if (npc.isSpawned()) {
checkMounted();
}
}

public void unmount() {
Expand Down
Expand Up @@ -270,6 +270,22 @@ private LinearWaypointEditor(Player player) {
this.markers = new EntityMarkers<Waypoint>();
}

private void addWaypoint(Location at) {
Waypoint element = new Waypoint(at);
int idx = waypoints.size();
if (waypoints.indexOf(selectedWaypoint) != -1) {
idx = waypoints.indexOf(selectedWaypoint);
waypoints.add(idx, element);
} else {
waypoints.add(element);
}

if (showingMarkers) {
markers.createMarker(element, element.getLocation().clone());
}
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_ADDED_WAYPOINT, formatLoc(at), waypoints.size());
}

@Override
public void begin() {
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_BEGIN);
Expand Down Expand Up @@ -369,6 +385,10 @@ public void run() {
Messaging.sendTr(event.getPlayer(), cycle ? Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_SET
: Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_UNSET);
});
} else if (message.equalsIgnoreCase("here")) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> addWaypoint(player.getLocation()));
}
}

Expand All @@ -394,20 +414,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}
}

Waypoint element = new Waypoint(at);
int idx = waypoints.size();
if (waypoints.indexOf(selectedWaypoint) != -1) {
idx = waypoints.indexOf(selectedWaypoint);
waypoints.add(idx, element);
} else {
waypoints.add(element);
}

if (showingMarkers) {
markers.createMarker(element, element.getLocation().clone());
}
Messaging.sendTr(player, Messages.LINEAR_WAYPOINT_EDITOR_ADDED_WAYPOINT, formatLoc(at),
waypoints.size());
addWaypoint(at);
} else if (waypoints.size() > 0 && !event.getPlayer().isSneaking()) {
event.setCancelled(true);

Expand Down
6 changes: 5 additions & 1 deletion main/src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -868,6 +868,10 @@ public static void updateInventoryTitle(Player player, InventoryView view, Strin
BRIDGE.updateInventoryTitle(player, view, newTitle);
}

public static void updateMountedInteractionHeight(Entity entity, Entity mount, double height) {
BRIDGE.updateMountedInteractionHeight(entity, mount, height);
}

public static void updateNavigationWorld(org.bukkit.entity.Entity entity, org.bukkit.World world) {
BRIDGE.updateNavigationWorld(entity, world);
}
Expand All @@ -879,7 +883,6 @@ public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
private static Method ADD_OPENS;

private static NMSBridge BRIDGE;

private static Method GET_MODULE;
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static Field MODIFIERS_FIELD;
Expand All @@ -894,6 +897,7 @@ public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
private static MethodHandle UNSAFE_PUT_LONG;
private static MethodHandle UNSAFE_PUT_OBJECT;
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;

static {
try {
Class.forName("com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent");
Expand Down
5 changes: 4 additions & 1 deletion main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Expand Up @@ -260,7 +260,10 @@ public default void setTeamNameTagVisible(Team team, boolean visible) {

public void updateInventoryTitle(Player player, InventoryView view, String newTitle);

public default void updateMountedInteractionHeight(Entity entity, Entity mount, double height) {
}

public void updateNavigationWorld(Entity entity, World world);

public void updatePathfindingRange(NPC npc, float pathfindingRange);
public void updatePathfindingRange(NPC npc, float pathfindingRange);;
}
Expand Up @@ -280,10 +280,12 @@
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
import net.minecraft.network.protocol.game.VecDeltaCodec;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerAdvancements;
Expand All @@ -304,6 +306,7 @@
import net.minecraft.world.entity.Entity.RemovalReason;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Interaction;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MoverType;
Expand Down Expand Up @@ -1795,6 +1798,22 @@ public void updateInventoryTitle(Player player, InventoryView view, String newTi
player.updateInventory();
}

@Override
public void updateMountedInteractionHeight(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity mount,
double offset) {
Interaction handle = (Interaction) getHandle(entity);
offset += -0.5 + getHandle(mount).getPassengersRidingOffset();
((org.bukkit.entity.Interaction) entity).setInteractionHeight((float) offset);
mount.addPassenger(entity);
handle.setPose(Pose.SNIFFING);
Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), () -> {
if (!entity.isValid())
return;
sendPacketNearby(null, entity.getLocation(), new ClientboundSetEntityDataPacket(handle.getId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())));
});
}

@Override
public void updateNavigationWorld(org.bukkit.entity.Entity entity, World world) {
if (NAVIGATION_WORLD_FIELD == null)
Expand Down Expand Up @@ -2547,6 +2566,7 @@ public static void updateMinecraftAIState(NPC npc, Mob entity) {
private static final MethodHandle HEAD_HEIGHT = NMS.getSetter(Entity.class, "bf");
private static final MethodHandle HEAD_HEIGHT_METHOD = NMS.getFirstMethodHandle(Entity.class, true, Pose.class,
EntityDimensions.class);
private static EntityDataAccessor<Float> INTERACTION_HEIGHT = null;
private static final MethodHandle JUMP_FIELD = NMS.getGetter(LivingEntity.class, "bi");
private static final MethodHandle LOOK_CONTROL_SETTER = NMS.getFirstSetter(Mob.class, LookControl.class);
private static final MethodHandle MAKE_REQUEST = NMS.getMethodHandle(YggdrasilAuthenticationService.class,
Expand Down Expand Up @@ -2602,5 +2622,10 @@ public static void updateMinecraftAIState(NPC npc, Mob entity) {
} catch (Throwable e) {
e.printStackTrace();
}
try {
INTERACTION_HEIGHT = (EntityDataAccessor<Float>) NMS.getGetter(Interaction.class, "d").invoke();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Expand Up @@ -279,10 +279,12 @@
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
import net.minecraft.network.protocol.game.VecDeltaCodec;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerAdvancements;
Expand All @@ -303,6 +305,7 @@
import net.minecraft.world.entity.Entity.RemovalReason;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Interaction;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MoverType;
Expand Down Expand Up @@ -395,7 +398,7 @@ public boolean addEntityToWorld(org.bukkit.entity.Entity entity, SpawnReason cus
e.printStackTrace();
}
return success;
}
};

@Override
public void addOrRemoveFromPlayerList(org.bukkit.entity.Entity entity, boolean remove) {
Expand Down Expand Up @@ -1795,6 +1798,22 @@ public void updateInventoryTitle(Player player, InventoryView view, String newTi
player.updateInventory();
}

@Override
public void updateMountedInteractionHeight(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity mount,
double offset) {
Interaction handle = (Interaction) getHandle(entity);
offset += -0.5 + getHandle(mount).getPassengersRidingOffset();
((org.bukkit.entity.Interaction) entity).setInteractionHeight((float) offset);
mount.addPassenger(entity);
handle.setPose(Pose.SNIFFING);
Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), () -> {
if (!entity.isValid())
return;
sendPacketNearby(null, entity.getLocation(), new ClientboundSetEntityDataPacket(handle.getId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())));
});
}

@Override
public void updateNavigationWorld(org.bukkit.entity.Entity entity, World world) {
if (NAVIGATION_WORLD_FIELD == null)
Expand Down Expand Up @@ -2509,6 +2528,7 @@ public static void updateMinecraftAIState(NPC npc, Mob entity) {

private static final MethodHandle ADVANCEMENTS_PLAYER_SETTER = NMS.getFirstFinalSetter(ServerPlayer.class,
PlayerAdvancements.class);

private static final MethodHandle ATTRIBUTE_PROVIDER_MAP = NMS.getFirstGetter(AttributeSupplier.class, Map.class);
private static final MethodHandle ATTRIBUTE_PROVIDER_MAP_SETTER = NMS.getFirstFinalSetter(AttributeSupplier.class,
Map.class);
Expand Down Expand Up @@ -2548,6 +2568,7 @@ public static void updateMinecraftAIState(NPC npc, Mob entity) {
private static final MethodHandle HEAD_HEIGHT = NMS.getSetter(Entity.class, "bi");
private static final MethodHandle HEAD_HEIGHT_METHOD = NMS.getFirstMethodHandle(Entity.class, true, Pose.class,
EntityDimensions.class);
private static EntityDataAccessor<Float> INTERACTION_HEIGHT = null;
private static final MethodHandle JUMP_FIELD = NMS.getGetter(LivingEntity.class, "bk");
private static final MethodHandle LOOK_CONTROL_SETTER = NMS.getFirstSetter(Mob.class, LookControl.class);
private static final MethodHandle MAKE_REQUEST = NMS.getMethodHandle(YggdrasilAuthenticationService.class,
Expand Down Expand Up @@ -2603,5 +2624,10 @@ public static void updateMinecraftAIState(NPC npc, Mob entity) {
} catch (Throwable e) {
e.printStackTrace();
}
try {
INTERACTION_HEIGHT = (EntityDataAccessor<Float>) NMS.getGetter(Interaction.class, "d").invoke();
} catch (Throwable e) {
e.printStackTrace();
}
}
}

0 comments on commit db6058b

Please sign in to comment.