diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 3a143107f..9174cbe8f 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -118,6 +118,7 @@ import net.citizensnpcs.trait.GameModeTrait; import net.citizensnpcs.trait.Gravity; import net.citizensnpcs.trait.HologramTrait; import net.citizensnpcs.trait.HologramTrait.HologramDirection; +import net.citizensnpcs.trait.HomeTrait; import net.citizensnpcs.trait.HorseModifiers; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.MirrorTrait; @@ -1163,6 +1164,47 @@ public class NPCCommands { } } + @Command( + aliases = { "npc" }, + usage = "homeloc --location [loc] --delay [delay] -h(ere) -p(athfind) -t(eleport)", + desc = "Controls home location", + modifiers = { "home" }, + min = 1, + max = 1, + flags = "pth", + permission = "citizens.npc.home") + @Requirements(ownership = true, selected = true) + public void home(CommandContext args, CommandSender sender, NPC npc, @Flag("location") Location loc, + @Flag("delay") Integer delay) throws CommandException { + HomeTrait trait = npc.getOrAddTrait(HomeTrait.class); + String output = ""; + if (args.hasFlag('h')) { + if (!(sender instanceof Player)) + throw new RequirementMissingException(Messaging.tr(CommandMessages.REQUIREMENTS_MUST_BE_LIVING_ENTITY)); + trait.setHomeLocation(((Player) sender).getLocation()); + output += Messaging.tr(Messages.HOME_TRAIT_LOCATION_SET, Util.prettyPrintLocation(trait.getHomeLocation())); + } + if (loc != null) { + trait.setHomeLocation(loc); + output += Messaging.tr(Messages.HOME_TRAIT_LOCATION_SET, Util.prettyPrintLocation(trait.getHomeLocation())); + } + if (args.hasFlag('p')) { + trait.setReturnStrategy(HomeTrait.ReturnStrategy.PATHFIND); + output += Messaging.tr(Messages.HOME_TRAIT_PATHFIND_SET, npc.getName()); + } + if (args.hasFlag('t')) { + trait.setReturnStrategy(HomeTrait.ReturnStrategy.TELEPORT); + output += Messaging.tr(Messages.HOME_TRAIT_TELEPORT_SET, npc.getName()); + } + if (delay != null) { + trait.setDelayTicks(delay); + output += Messaging.tr(Messages.HOME_TRAIT_DELAY_SET, delay); + } + if (!output.isEmpty()) { + Messaging.send(sender, output); + } + } + @Command( aliases = { "npc" }, usage = "horse|donkey|mule (--color color) (--type type) (--style style) (-cb)", diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index f84b0eb93..b12ca664a 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -33,6 +33,7 @@ import net.citizensnpcs.trait.FollowTrait; import net.citizensnpcs.trait.GameModeTrait; import net.citizensnpcs.trait.Gravity; import net.citizensnpcs.trait.HologramTrait; +import net.citizensnpcs.trait.HomeTrait; import net.citizensnpcs.trait.HorseModifiers; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.MirrorTrait; @@ -80,6 +81,7 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(FollowTrait.class)); registerTrait(TraitInfo.create(GameModeTrait.class)); registerTrait(TraitInfo.create(Gravity.class)); + registerTrait(TraitInfo.create(HomeTrait.class)); registerTrait(TraitInfo.create(HorseModifiers.class)); registerTrait(TraitInfo.create(HologramTrait.class)); registerTrait(TraitInfo.create(Inventory.class)); diff --git a/main/src/main/java/net/citizensnpcs/trait/HomeTrait.java b/main/src/main/java/net/citizensnpcs/trait/HomeTrait.java new file mode 100644 index 000000000..2b9a3eef4 --- /dev/null +++ b/main/src/main/java/net/citizensnpcs/trait/HomeTrait.java @@ -0,0 +1,71 @@ +package net.citizensnpcs.trait; + +import org.bukkit.Location; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import net.citizensnpcs.api.persistence.Persist; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.trait.TraitName; + +@TraitName("hometrait") +public class HomeTrait extends Trait { + @Persist + private int delay = -1; + @Persist + private Location location; + @Persist + private ReturnStrategy strategy = ReturnStrategy.TELEPORT; + private int t; + + public HomeTrait() { + super("hometrait"); + } + + public int getDelayTicks() { + return delay; + } + + public Location getHomeLocation() { + return location.clone(); + } + + public ReturnStrategy getReturnStrategy() { + return strategy; + } + + @Override + public void run() { + if (!npc.isSpawned() || location == null || npc.getStoredLocation().distance(location) < 0.1 + || npc.getNavigator().isNavigating()) { + t = 0; + return; + } + t++; + if (t > delay || delay == -1) { + if (strategy == ReturnStrategy.TELEPORT) { + npc.teleport(location, TeleportCause.PLUGIN); + } else if (strategy == ReturnStrategy.PATHFIND) { + npc.getNavigator().setTarget(location); + npc.getNavigator().getLocalParameters().distanceMargin(0.9).pathDistanceMargin(0) + .destinationTeleportMargin(1); + } + } + } + + public void setDelayTicks(int delay) { + this.delay = delay; + } + + public void setHomeLocation(Location location) { + this.location = location.clone(); + } + + public void setReturnStrategy(ReturnStrategy strategy) { + this.strategy = strategy; + } + + public static enum ReturnStrategy { + PATHFIND, + TELEPORT + } +} \ No newline at end of file diff --git a/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java b/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java index f30e151c1..a96bda401 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java @@ -94,6 +94,11 @@ public class ScoreboardTrait extends Trait { Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> { if (npc.isSpawned()) return; + try { + team.getSize(); + } catch (IllegalStateException ex) { + return; + } if (team.getSize() == 1) { for (Player player : Bukkit.getOnlinePlayers()) { metadata.remove(player.getUniqueId(), team.getName()); diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 739ea8892..fed976585 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -154,6 +154,10 @@ public class Messages { public static final String HOLOGRAM_LINE_SET = "citizens.commands.npc.hologram.text-set"; public static final String HOLOGRAM_TEXT_MISSING = "citizens.commands.npc.hologram.text-missing"; public static final String HOLOGRAM_TEXT_REMOVED = "citizens.commands.npc.hologram.text-removed"; + public static final String HOME_TRAIT_DELAY_SET = "citizens.commands.npc.home.delay-set"; + public static final String HOME_TRAIT_LOCATION_SET = "citizens.commands.npc.home.home-set"; + public static final String HOME_TRAIT_PATHFIND_SET = "citizens.commands.npc.home.pathfind-set"; + public static final String HOME_TRAIT_TELEPORT_SET = "citizens.commands.npc.home.teleport-set"; public static final String HORSE_CHEST_SET = "citizens.commands.npc.horse.chest-set"; public static final String HORSE_CHEST_UNSET = "citizens.commands.npc.horse.chest-unset"; public static final String HORSE_COLOR_SET = "citizens.commands.npc.horse.color-set"; diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index 41a1712ff..ad8cb3146 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -125,6 +125,10 @@ citizens.commands.npc.hologram.line-removed=Removed line [[{0}]]. citizens.commands.npc.hologram.direction-set=Direction set to [[{0}]]. citizens.commands.npc.hologram.line-add=Added a new hologram line: [[{0}]]. citizens.commands.npc.hologram.cleared=Hologram lines cleared. +citizens.commands.npc.home.home-set=Home set to [[{0}]]. +citizens.commands.npc.home.teleport-set=[[{0}]] will now teleport home. +citizens.commands.npc.home.pathfind-set=[[{0}]] will now try to pathfind home. +citizens.commands.npc.home.delay-set=Delay before returning home set to [[{0}]] ticks. citizens.commands.npc.horse.chest-set=The horse is now carrying a chest. citizens.commands.npc.horse.chest-unset=The horse is no longer carrying a chest. citizens.commands.npc.horse.color-set=The horse''s color was set to [[{0}]].