Skip to content

Commit

Permalink
Add way to set custom name directly
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 29, 2022
1 parent d8a4c73 commit 8a65d4a
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 52 deletions.
6 changes: 3 additions & 3 deletions main/src/main/java/net/citizensnpcs/ProtocolLibListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.SmoothRotationTrait.LocalRotationSession;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.RotationTrait.LocalRotationSession;

public class ProtocolLibListener {
private final Class<?> flagsClass;
Expand All @@ -42,7 +42,7 @@ public void onPacketSending(PacketEvent event) {
return;

NPC npc = ((NPCHolder) entity).getNPC();
SmoothRotationTrait trait = npc.getTraitNullable(SmoothRotationTrait.class);
RotationTrait trait = npc.getTraitNullable(RotationTrait.class);
if (trait == null)
return;

Expand Down
10 changes: 6 additions & 4 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
import net.citizensnpcs.trait.SkinLayers.Layer;
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.trait.SlimeSize;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.WitherTrait;
import net.citizensnpcs.trait.WolfModifiers;
Expand Down Expand Up @@ -2135,7 +2135,7 @@ public void rotate(CommandContext args, CommandSender sender, NPC npc, @Flag("bo
yaw = NMS.getHeadYaw(npc.getEntity());
}
}
npc.getOrAddTrait(SmoothRotationTrait.class).rotateToHave(yaw, pitch);
npc.getOrAddTrait(RotationTrait.class).rotateToHave(yaw, pitch);
return;
}
if (yaw != null) {
Expand Down Expand Up @@ -2294,14 +2294,14 @@ public void sheep(CommandContext args, CommandSender sender, NPC npc, @Flag("col

@Command(
aliases = { "npc" },
usage = "shop (edit|show) (name)",
usage = "shop (edit|show|delete) (name)",
desc = "NPC shop edit/show",
modifiers = { "shop" },
min = 1,
max = 3,
permission = "citizens.npc.shop")
public void shop(CommandContext args, Player sender, NPC npc,
@Arg(value = 1, completions = { "edit", "show" }) String action) throws CommandException {
@Arg(value = 1, completions = { "edit", "show", "delete" }) String action) throws CommandException {
ShopTrait trait = npc.getOrAddTrait(ShopTrait.class);
NPCShop shop = trait.getDefaultShop();
if (args.argsLength() > 1) {
Expand All @@ -2317,6 +2317,8 @@ public void shop(CommandContext args, Player sender, NPC npc,
shop.displayEditor(trait, sender);
} else if (action.equalsIgnoreCase("show")) {
shop.display(sender);
} else if (action.equalsIgnoreCase("delete")) {
trait.deleteShop(args.getString(2));
} else {
throw new CommandUsageException();
}
Expand Down
26 changes: 20 additions & 6 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Nameable;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -59,6 +60,7 @@
public class CitizensNPC extends AbstractNPC {
private ChunkCoord cachedCoord;
private EntityController entityController;
private Object minecraftComponentCache;
private final CitizensNavigator navigator = new CitizensNavigator(this);
private int updateCounter = 0;

Expand Down Expand Up @@ -241,6 +243,7 @@ public void setMoveDestination(Location destination) {

@Override
public void setName(String name) {
minecraftComponentCache = Messaging.minecraftComponentFromRawMessage(name);
super.setName(name);

if (requiresNameHologram() && !hasTrait(HologramTrait.class)) {
Expand Down Expand Up @@ -384,7 +387,7 @@ public void accept(Runnable cancel) {

updateFlyableState();
updateCustomNameVisibility();
updateCustomName();
updateScoreboard();

Messaging.debug("Spawned", CitizensNPC.this, "SpawnReason." + reason);
cancel.run();
Expand Down Expand Up @@ -465,7 +468,7 @@ public void update() {
}
}
if (isLiving) {
updateCustomName();
updateScoreboard();
}
updateCounter = 0;
}
Expand Down Expand Up @@ -505,9 +508,14 @@ public void update() {
}
}

private void updateCustomName() {
if (data().has(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME)) {
getOrAddTrait(ScoreboardTrait.class).update();
@Override
public void updateCustomName() {
if (!(getEntity() instanceof Nameable))
return;
if (minecraftComponentCache != null) {
NMS.setCustomName(getEntity(), minecraftComponentCache);
} else {
super.updateCustomName();
}
}

Expand All @@ -517,7 +525,7 @@ private void updateCustomNameVisibility() {
nameplateVisible = "false";
}
if (nameplateVisible.equals("true") || nameplateVisible.equals("hover")) {
getEntity().setCustomName(getFullName());
updateCustomName();
}
getEntity().setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));
}
Expand All @@ -536,6 +544,12 @@ private void updateFlyableState() {
}
}

private void updateScoreboard() {
if (data().has(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME)) {
getOrAddTrait(ScoreboardTrait.class).update();
}
}

private void updateUsingItemState(Player player) {
boolean useItem = data().get(NPC.Metadata.USING_HELD_ITEM, false),
offhand = data().get(NPC.Metadata.USING_OFFHAND_ITEM, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import net.citizensnpcs.trait.SkinTrait;
import net.citizensnpcs.trait.SleepTrait;
import net.citizensnpcs.trait.SlimeSize;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.WitherTrait;
Expand Down Expand Up @@ -91,7 +91,7 @@ public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Poses.class));
registerTrait(TraitInfo.create(Powered.class));
registerTrait(TraitInfo.create(RabbitType.class));
registerTrait(TraitInfo.create(SmoothRotationTrait.class));
registerTrait(TraitInfo.create(RotationTrait.class));
registerTrait(TraitInfo.create(Saddle.class));
registerTrait(TraitInfo.create(ScoreboardTrait.class));
registerTrait(TraitInfo.create(ScriptTrait.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

@TraitName("smoothrotationtrait")
public class SmoothRotationTrait extends Trait {
@TraitName("rotationtrait")
public class RotationTrait extends Trait {
@Persist(reify = true)
private final RotationParams globalParameters = new RotationParams();
private final RotationSession globalSession = new RotationSession(globalParameters);
private final List<LocalRotationSession> localSessions = Lists.newArrayList();

public SmoothRotationTrait() {
super("smoothrotationtrait");
public RotationTrait() {
super("rotationtrait");
}

public void clearLocalSessions() {
Expand Down
8 changes: 8 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/ShopTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public ShopTrait() {
super("shop");
}

public void deleteShop(String name) {
if (StoredShops.GLOBAL_SHOPS.containsKey(name)) {
StoredShops.GLOBAL_SHOPS.remove(name);
} else {
StoredShops.NPC_SHOPS.remove(name);
}
}

public NPCShop getDefaultShop() {
return StoredShops.NPC_SHOPS.computeIfAbsent(npc.getUniqueId().toString(), NPCShop::new);
}
Expand Down
4 changes: 4 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ public static void setBodyYaw(Entity entity, float yaw) {
BRIDGE.setBodyYaw(entity, yaw);
}

public static void setCustomName(Entity entity, Object component) {
BRIDGE.setCustomName(entity, component);
}

public static void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
BRIDGE.setDestination(entity, x, y, z, speed);
}
Expand Down
2 changes: 2 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public interface NMSBridge {

public void setBodyYaw(Entity entity, float yaw);

public void setCustomName(Entity entity, Object component);

public void setDestination(Entity entity, double x, double y, double z, float speed);

public void setEndermanAngry(Enderman enderman, boolean angry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.BossBarTrait;
import net.citizensnpcs.trait.versioned.PolarBearTrait;
import net.citizensnpcs.trait.versioned.ShulkerTrait;
Expand Down Expand Up @@ -197,6 +197,7 @@
import net.minecraft.server.v1_10_R1.EntityTypes;
import net.minecraft.server.v1_10_R1.EntityWither;
import net.minecraft.server.v1_10_R1.GenericAttributes;
import net.minecraft.server.v1_10_R1.IChatBaseComponent;
import net.minecraft.server.v1_10_R1.IInventory;
import net.minecraft.server.v1_10_R1.MathHelper;
import net.minecraft.server.v1_10_R1.MinecraftKey;
Expand Down Expand Up @@ -764,7 +765,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityInsentient) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand All @@ -788,7 +789,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand Down Expand Up @@ -1024,6 +1025,11 @@ public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}

@Override
public void setCustomName(org.bukkit.entity.Entity entity, Object component) {
getHandle(entity).setCustomName(((IChatBaseComponent) component).getText());
}

@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.BossBarTrait;
import net.citizensnpcs.trait.versioned.LlamaTrait;
import net.citizensnpcs.trait.versioned.PolarBearTrait;
Expand Down Expand Up @@ -215,6 +215,7 @@
import net.minecraft.server.v1_11_R1.EntityWither;
import net.minecraft.server.v1_11_R1.EnumMoveType;
import net.minecraft.server.v1_11_R1.GenericAttributes;
import net.minecraft.server.v1_11_R1.IChatBaseComponent;
import net.minecraft.server.v1_11_R1.IInventory;
import net.minecraft.server.v1_11_R1.MathHelper;
import net.minecraft.server.v1_11_R1.MinecraftKey;
Expand Down Expand Up @@ -819,7 +820,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand All @@ -843,7 +844,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand Down Expand Up @@ -1080,6 +1081,11 @@ public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}

@Override
public void setCustomName(org.bukkit.entity.Entity entity, Object component) {
getHandle(entity).setCustomName(((IChatBaseComponent) component).getText());
}

@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.SmoothRotationTrait;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.BossBarTrait;
import net.citizensnpcs.trait.versioned.LlamaTrait;
import net.citizensnpcs.trait.versioned.ParrotTrait;
Expand Down Expand Up @@ -219,6 +219,7 @@
import net.minecraft.server.v1_12_R1.EntityWither;
import net.minecraft.server.v1_12_R1.EnumMoveType;
import net.minecraft.server.v1_12_R1.GenericAttributes;
import net.minecraft.server.v1_12_R1.IChatBaseComponent;
import net.minecraft.server.v1_12_R1.IInventory;
import net.minecraft.server.v1_12_R1.MathHelper;
import net.minecraft.server.v1_12_R1.MinecraftKey;
Expand Down Expand Up @@ -826,7 +827,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand All @@ -851,7 +852,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(SmoothRotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand Down Expand Up @@ -1090,6 +1091,11 @@ public void setBodyYaw(org.bukkit.entity.Entity entity, float yaw) {
getHandle(entity).yaw = yaw;
}

@Override
public void setCustomName(org.bukkit.entity.Entity entity, Object component) {
getHandle(entity).setCustomName(((IChatBaseComponent) component).getText());
}

@Override
public void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = NMSImpl.getHandle(entity);
Expand Down
Loading

0 comments on commit 8a65d4a

Please sign in to comment.