Skip to content

Commit

Permalink
Rework hologram rendering (WIP: text displays not yet functional)
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 14, 2024
1 parent 5f13340 commit cd7f336
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 362 deletions.
2 changes: 1 addition & 1 deletion main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -521,7 +521,7 @@ private void setupTranslator() {
}
Translator.setInstance(new File(getDataFolder(), "lang"), locale);
if (!locale.getLanguage().equals("en")) {
Messaging.logTr(Messages.CONTRIBUTE_TO_TRANSLATION_PROMPT);
Messaging.logTr(Messages.CONTRIBUTE_TO_TRANSLATION_PROMPT, locale.getLanguage());
}
}

Expand Down
15 changes: 5 additions & 10 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -112,7 +112,7 @@
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.HologramTrait.HologramRenderer;
import net.citizensnpcs.trait.ShopTrait;
import net.citizensnpcs.trait.versioned.SnowmanTrait;
import net.citizensnpcs.util.ChunkCoord;
Expand Down Expand Up @@ -469,17 +469,12 @@ public void onNPCLinkToPlayer(NPCLinkToPlayerEvent event) {
if (skinnable.getSkinTracker().getSkin() != null) {
skinnable.getSkinTracker().getSkin().apply(skinnable);
}
}
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
onNPCPlayerLinkToPlayer(event);
}
ClickRedirectTrait crt = npc.getTraitNullable(ClickRedirectTrait.class);
if (crt != null) {
HologramTrait ht = crt.getRedirectNPC().getTraitNullable(HologramTrait.class);
if (ht != null) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> ht.onHologramSeenByPlayer(npc, event.getPlayer()), 2);
}
if (npc.data().has(NPC.Metadata.HOLOGRAM_RENDERER)) {
HologramRenderer hr = npc.data().get(NPC.Metadata.HOLOGRAM_RENDERER);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> hr.onSeenByPlayer(event.getPlayer()), 2);
}
}

Expand Down
20 changes: 7 additions & 13 deletions main/src/main/java/net/citizensnpcs/ProtocolLibListener.java
Expand Up @@ -6,11 +6,9 @@
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;

import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
Expand Down Expand Up @@ -44,8 +42,7 @@
import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.ClickRedirectTrait;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.HologramTrait.HologramRenderer;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.RotationTrait.PacketRotationSession;
Expand All @@ -71,16 +68,13 @@ public void onPacketSending(PacketEvent event) {

PacketContainer packet = event.getPacket();
int version = manager.getProtocolVersion(event.getPlayer());
if (npc.data().has(NPC.Metadata.HOLOGRAM_FOR) || npc.data().has(NPC.Metadata.HOLOGRAM_LINE_SUPPLIER)) {
Function<Player, String> hvs = npc.data().get(NPC.Metadata.HOLOGRAM_LINE_SUPPLIER);
if (npc.data().has(NPC.Metadata.HOLOGRAM_RENDERER)) {
HologramRenderer hr = npc.data().get(NPC.Metadata.HOLOGRAM_RENDERER);
Object fakeName = null;
if (hvs != null) {
String suppliedName = hvs.apply(event.getPlayer());
fakeName = version <= 340 ? suppliedName
: Optional.of(Messaging.minecraftComponentFromRawMessage(suppliedName));
}
boolean sneaking = npc.getOrAddTrait(ClickRedirectTrait.class).getRedirectNPC()
.getOrAddTrait(HologramTrait.class).isHologramSneaking(npc, event.getPlayer());
String suppliedName = hr.getPerPlayerText(npc, event.getPlayer());
fakeName = version <= 340 ? suppliedName
: Optional.of(Messaging.minecraftComponentFromRawMessage(suppliedName));
boolean sneaking = hr.isSneaking(npc, event.getPlayer());
boolean delta = false;

if (version < 761) {
Expand Down
14 changes: 7 additions & 7 deletions main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -108,6 +108,9 @@ public enum Setting {
DEFAULT_DISTANCE_MARGIN(
"The default MOVEMENT distance in blocks where the NPC will move to before considering a path finished<br>Note: this is different from the PATHFINDING distance which is specified by path-distance-margin",
"npc.pathfinding.default-distance-margin", 1),
DEFAULT_HOLOGRAM_RENDERER(
"The default renderer for holograms, must be one of the following:<br>interaction - matches inbuilt nametags most closely<br>display - allows for different colored backgrounds<br>armorstand - creates an armorstand and teleports it to the player",
"npc.hologram.default-renderer", "interaction"),
DEFAULT_LOOK_CLOSE("Enable look close by default", "npc.default.look-close.enabled", false),
DEFAULT_LOOK_CLOSE_RANGE("Default look close range in blocks", "npc.default.look-close.range", 10),
DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT("Default distance between hologram lines", "npc.hologram.default-line-height",
Expand Down Expand Up @@ -166,9 +169,6 @@ 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)<br>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 Expand Up @@ -284,11 +284,11 @@ public void loadFromKey(DataKey root) {
this.value = value;
}

Setting(String migrate, String path, Object value) {
if (migrate.contains(".")) {
this.migrate = migrate;
Setting(String migrateOrComments, String path, Object value) {
if (migrateOrComments.contains(".") && !migrateOrComments.contains(" ")) {
migrate = migrateOrComments;
} else {
comments = migrate;
comments = migrateOrComments;
}
this.path = path;
this.value = value;
Expand Down
8 changes: 4 additions & 4 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -735,7 +735,7 @@ else if (!EntityControllers.controllerExistsForType(type))
throw new CommandException(Messaging.tr(Messages.NPC_CREATE_MISSING_MOBTYPE, args.getFlag("type")));
}
int nameLength = SpigotUtil.getMaxNameLength(type);
if (Placeholders.replace(Messaging.parseComponents(name), sender, npc).length() > nameLength) {
if (Placeholders.replace(Messaging.stripColor(name), sender, npc).length() > nameLength) {
Messaging.sendErrorTr(sender, Messages.NPC_NAME_TOO_LONG, nameLength);
name = name.substring(0, nameLength);
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ public void follow(CommandContext args, CommandSender sender, NPC npc, @Flag("ma
if (!(sender instanceof ConsoleCommandSender)
&& !followingNPC.getOrAddTrait(Owner.class).isOwnedBy(sender))
throw new CommandException(CommandMessages.MUST_BE_OWNER);
boolean following = !trait.isEnabled();
boolean following = explicit == null ? !trait.isEnabled() : explicit;
trait.follow(following ? followingNPC.getEntity() : null);
Messaging.sendTr(sender, following ? Messages.FOLLOW_SET : Messages.FOLLOW_UNSET, npc.getName(),
followingNPC.getName());
Expand Down Expand Up @@ -2532,9 +2532,9 @@ public void remove(CommandContext args, CommandSender sender, NPC npc, @Flag("ow
permission = "citizens.npc.rename")
public void rename(CommandContext args, CommandSender sender, NPC npc) {
String oldName = npc.getName();
String newName = Messaging.parseComponents(args.getJoinedStrings(1));
String newName = args.getJoinedStrings(1);
int nameLength = SpigotUtil.getMaxNameLength(npc.getOrAddTrait(MobType.class).getType());
if (newName.length() > nameLength) {
if (Placeholders.replace(Messaging.stripColor(newName), sender, npc).length() > nameLength) {
Messaging.sendErrorTr(sender, Messages.NPC_NAME_TOO_LONG, nameLength);
newName = newName.substring(0, nameLength);
}
Expand Down
2 changes: 1 addition & 1 deletion main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -192,7 +192,7 @@ public void load(DataKey root) {

@Override
public boolean requiresNameHologram() {
return !data().has(NPC.Metadata.HOLOGRAM_FOR)
return !data().has(NPC.Metadata.HOLOGRAM_RENDERER)
&& (super.requiresNameHologram() || Setting.ALWAYS_USE_NAME_HOLOGRAM.asBoolean());
}

Expand Down
10 changes: 5 additions & 5 deletions main/src/main/java/net/citizensnpcs/trait/FollowTrait.java
Expand Up @@ -38,19 +38,19 @@ public FollowTrait() {
}

private void cancelNavigationIfActive() {
if (npc.getNavigator().isNavigating() && this.entity != null && npc.getNavigator().getEntityTarget() != null
&& this.entity == npc.getNavigator().getEntityTarget().getTarget()) {
if (npc.getNavigator().isNavigating() && entity != null && npc.getNavigator().getEntityTarget() != null
&& entity == npc.getNavigator().getEntityTarget().getTarget()) {
npc.getNavigator().cancelNavigation();
}
}

/**
* Sets the {@link Entity} to follow
*/
public void follow(Entity entity) {
followingUUID = entity == null ? null : entity.getUniqueId();
public void follow(Entity follow) {
cancelNavigationIfActive();
this.entity = null;
followingUUID = follow == null ? null : follow.getUniqueId();
entity = null;
}

public Entity getFollowing() {
Expand Down

0 comments on commit cd7f336

Please sign in to comment.