Skip to content

Commit

Permalink
Add basic /npc home command
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Feb 5, 2023
1 parent ba04e4b commit a61befb
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
42 changes: 42 additions & 0 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -118,6 +118,7 @@
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;
Expand Down Expand Up @@ -1163,6 +1164,47 @@ public void hologram(CommandContext args, CommandSender sender, NPC npc,
}
}

@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)",
Expand Down
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -80,6 +81,7 @@ public CitizensTraitFactory() {
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));
Expand Down
71 changes: 71 additions & 0 deletions 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
}
}
Expand Up @@ -94,6 +94,11 @@ public void onDespawn() {
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());
Expand Down
4 changes: 4 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/Messages.java
Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions main/src/main/resources/messages_en.properties
Expand Up @@ -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}]].
Expand Down

0 comments on commit a61befb

Please sign in to comment.