Add basic /npc home command

This commit is contained in:
fullwall 2023-02-06 00:40:01 +08:00
parent ba04e4b05f
commit a61befbdcc
6 changed files with 128 additions and 0 deletions

View File

@ -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)",

View File

@ -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));

View File

@ -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
}
}

View File

@ -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());

View File

@ -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";

View File

@ -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}]].