Rework /npc follow toggling

This commit is contained in:
fullwall 2023-05-06 00:45:57 +08:00
parent 3bd406a742
commit 232f5247f1
3 changed files with 24 additions and 24 deletions

View File

@ -139,6 +139,9 @@ public class Settings {
DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE(
"The distance in blocks where the NPC will switch to walking straight towards the target instead of pathfinding<br>Currently only for dynamic targets like entities",
"npc.pathfinding.straight-line-targeting-distance", 5),
DEFAULT_STUCK_ACTION(
"The default action to perform when NPCs are unable to find a path or are stuck in the same block for too long. Supported options are: 'teleport to destination' or 'none'",
"npc.pathfinding.default-stuck-action", "teleport to destination"),
DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false),
DEFAULT_TALK_CLOSE_RANGE("Default talk close range in blocks", "npc.default.talk-close.range", 5),
DEFAULT_TEXT("npc.default.talk-close.text.0", "Hi, I'm <npc>!") {
@ -168,7 +171,7 @@ public class Settings {
"general.entity-spawn-wait-ticks", "general.wait-for-entity-spawn", "1s"),
ERROR_COLOUR("general.color-scheme.message-error", "<red>"),
FOLLOW_ACROSS_WORLDS("Whether /npc follow will teleport across worlds to follow its target",
"npc.follow.teleport-across-worlds", true),
"npc.follow.teleport-across-worlds", false),
HIGHLIGHT_COLOUR("general.color-scheme.message-highlight", "yellow"),
HOLOGRAM_UPDATE_RATE("How often to update hologram names (including placeholders)",
"npc.hologram.update-rate-ticks", "npc.hologram.update-rate", "1s"),
@ -241,9 +244,6 @@ public class Settings {
STORAGE_FILE("storage.file", "saves.yml"),
STORAGE_TYPE("Although technically Citizens can use NBT storage, it is not well tested and YAML is recommended",
"storage.type", "yaml"),
DEFAULT_STUCK_ACTION(
"The default action to perform when NPCs are unable to find a path or are stuck in the same block for too long. Supported options are: 'teleport to destination' or 'none'",
"npc.pathfinding.default-stuck-action", "teleport to destination"),
TABLIST_REMOVE_PACKET_DELAY("How long to wait before sending the tablist remove packet",
"npc.tablist.remove-packet-delay", "1t"),
TALK_CLOSE_TO_NPCS("Whether to talk to NPCs (and therefore bystanders) as well as players",

View File

@ -959,6 +959,8 @@ public class NPCCommands {
permission = "citizens.npc.follow")
public void follow(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
boolean protect = args.hasFlag('p');
FollowTrait trait = npc.getOrAddTrait(FollowTrait.class);
trait.setProtect(protect);
String name = sender.getName();
if (args.argsLength() > 1) {
name = args.getString(1);
@ -972,7 +974,8 @@ public class NPCCommands {
if (!(sender instanceof ConsoleCommandSender)
&& !followingNPC.getOrAddTrait(Owner.class).isOwnedBy(sender))
throw new CommandException(CommandMessages.MUST_BE_OWNER);
boolean following = npc.getOrAddTrait(FollowTrait.class).toggle(followingNPC.getEntity(), protect);
boolean following = !trait.isEnabled();
trait.follow(following ? followingNPC.getEntity() : null);
Messaging.sendTr(sender, following ? Messages.FOLLOW_SET : Messages.FOLLOW_UNSET, npc.getName(),
followingNPC.getName());
};
@ -980,7 +983,8 @@ public class NPCCommands {
args.getString(1));
return;
}
boolean following = npc.getOrAddTrait(FollowTrait.class).toggle(player.getPlayer(), protect);
boolean following = !trait.isEnabled();
trait.follow(following ? player.getPlayer() : null);
Messaging.sendTr(sender, following ? Messages.FOLLOW_SET : Messages.FOLLOW_UNSET, npc.getName(),
player.getName());
}

View File

@ -34,6 +34,18 @@ public class FollowTrait extends Trait {
super("followtrait");
}
/**
* Sets the {@link Entity} to follow
*/
public void follow(Entity entity) {
this.followingUUID = entity == null ? null : entity.getUniqueId();
if (npc.getNavigator().isNavigating() && this.entity != null && npc.getNavigator().getEntityTarget() != null
&& this.entity == npc.getNavigator().getEntityTarget().getTarget()) {
npc.getNavigator().cancelNavigation();
}
this.entity = null;
}
public Entity getFollowing() {
return entity;
}
@ -103,25 +115,9 @@ public class FollowTrait extends Trait {
}
/**
* Toggles and/or sets the {@link Entity} to follow and whether to protect them (similar to wolves in Minecraft,
* attack whoever attacks the entity).
*
* Will toggle if the {@link Entity} is the entity currently being followed.
*
* @param entity
* the player to follow
* @param protect
* whether to protect the player
* @return whether the trait is enabled
* Sets whether to protect the followed Entity (similar to wolves in Minecraft, attack whoever attacks the entity).
*/
public boolean toggle(Entity entity, boolean protect) {
public void setProtect(boolean protect) {
this.protect = protect;
this.followingUUID = entity.getUniqueId().equals(followingUUID) ? null : entity.getUniqueId();
if (npc.getNavigator().isNavigating() && this.entity != null && npc.getNavigator().getEntityTarget() != null
&& this.entity == npc.getNavigator().getEntityTarget().getTarget()) {
npc.getNavigator().cancelNavigation();
}
this.entity = null;
return followingUUID != null;
}
}