From ab0c706da548ee66da5ef266b66276bca7a4bf5c Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 11 Dec 2022 21:28:39 +0800 Subject: [PATCH] Add perplayer lookclose, rework templates, add message to send on click to shop trait, remove profiler call in player navigation which broke compatibility with certain plugins --- .../main/java/net/citizensnpcs/Citizens.java | 6 + .../citizensnpcs/commands/NPCCommands.java | 9 +- .../commands/TemplateCommands.java | 14 +- .../java/net/citizensnpcs/npc/Template.java | 171 +++++++++++------- .../net/citizensnpcs/trait/LookClose.java | 72 ++++++-- .../net/citizensnpcs/trait/RotationTrait.java | 6 +- .../net/citizensnpcs/trait/ShopTrait.java | 43 +++-- .../java/net/citizensnpcs/util/Messages.java | 2 + .../src/main/resources/messages_en.properties | 4 +- .../nms/v1_19_R2/util/PlayerNavigation.java | 4 +- .../nms/v1_19_R2/util/PlayerPathfinder.java | 8 +- 11 files changed, 217 insertions(+), 122 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/Citizens.java b/main/src/main/java/net/citizensnpcs/Citizens.java index bb6c2ba92..baab21efe 100644 --- a/main/src/main/java/net/citizensnpcs/Citizens.java +++ b/main/src/main/java/net/citizensnpcs/Citizens.java @@ -69,6 +69,7 @@ import net.citizensnpcs.editor.Editor; import net.citizensnpcs.npc.CitizensNPCRegistry; import net.citizensnpcs.npc.CitizensTraitFactory; import net.citizensnpcs.npc.NPCSelector; +import net.citizensnpcs.npc.Template; import net.citizensnpcs.npc.ai.speech.CitizensSpeechFactory; import net.citizensnpcs.npc.profile.ProfileFetcher; import net.citizensnpcs.npc.skin.Skin; @@ -363,6 +364,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { locationLookup = null; enabled = false; saveOnDisable = true; + Template.shutdown(); NMS.shutdown(); CitizensAPI.shutdown(); } @@ -387,6 +389,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { Bukkit.getPluginManager().disablePlugin(this); return; } + registerScriptHelpers(); saves = createStorage(getDataFolder()); @@ -421,6 +424,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { registerCommands(); enableSubPlugins(); NMS.load(commands); + Template.migrate(); Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); commands.registerTabCompletion(this); @@ -478,6 +482,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { shops.loadFromDisk(); shops.load(); + Template.shutdown(); + getServer().getPluginManager().callEvent(new CitizensReloadEvent()); } diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 8a7539ef4..167d87975 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -1318,7 +1318,8 @@ public class NPCCommands { @Flag({ "randomlook", "rlook" }) Boolean randomlook, @Flag("range") Double range, @Flag("randomlookdelay") Integer randomLookDelay, @Flag("randomyawrange") String randomYaw, @Flag("randompitchrange") String randomPitch, @Flag("randomswitchtargets") Boolean randomSwitchTargets, - @Flag("disablewhennavigating") Boolean disableWhenNavigating) throws CommandException { + @Flag("disablewhennavigating") Boolean disableWhenNavigating, @Flag("perplayer") Boolean perPlayer) + throws CommandException { boolean toggle = true; LookClose trait = npc.getOrAddTrait(LookClose.class); if (randomlook != null) { @@ -1327,6 +1328,12 @@ public class NPCCommands { npc.getName()); toggle = false; } + if (perPlayer != null) { + trait.setPerPlayer(perPlayer); + Messaging.sendTr(sender, perPlayer ? Messages.LOOKCLOSE_PERPLAYER_SET : Messages.LOOKCLOSE_PERPLAYER_UNSET, + npc.getName()); + toggle = false; + } if (randomSwitchTargets != null) { trait.setRandomlySwitchTargets(randomSwitchTargets); Messaging.sendTr(sender, randomSwitchTargets ? Messages.LOOKCLOSE_RANDOM_TARGET_SWITCH_ENABLED diff --git a/main/src/main/java/net/citizensnpcs/commands/TemplateCommands.java b/main/src/main/java/net/citizensnpcs/commands/TemplateCommands.java index 530b3b048..5c60d7667 100644 --- a/main/src/main/java/net/citizensnpcs/commands/TemplateCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/TemplateCommands.java @@ -1,5 +1,6 @@ package net.citizensnpcs.commands; +import java.io.IOException; import java.util.List; import javax.annotation.Nullable; @@ -21,7 +22,6 @@ import net.citizensnpcs.api.command.exception.CommandException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.npc.Template; -import net.citizensnpcs.npc.Template.TemplateBuilder; import net.citizensnpcs.util.Messages; @Requirements(selected = true, ownership = true) @@ -72,7 +72,7 @@ public class TemplateCommands { @Command( aliases = { "template", "tpl" }, - usage = "create [template name] (-o)", + usage = "create [template name] (-k)", desc = "Creates a template from the selected NPC", modifiers = { "create" }, min = 2, @@ -84,8 +84,12 @@ public class TemplateCommands { if (Template.byName(name) != null) throw new CommandException(Messages.TEMPLATE_CONFLICT); - TemplateBuilder.create(name).from(npc).override(args.hasFlag('o')).buildAndSave(); - Messaging.sendTr(sender, Messages.TEMPLATE_CREATED); + try { + Template.Builder.create(name).from(npc).override(!args.hasFlag('k')).buildAndSave(); + Messaging.sendTr(sender, Messages.TEMPLATE_CREATED); + } catch (IOException e) { + Messaging.sendError(sender, "Invalid template filename"); + } } @Command( @@ -114,7 +118,7 @@ public class TemplateCommands { permission = "citizens.templates.list") public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException { Messaging.sendTr(sender, Messages.TEMPLATE_LIST_HEADER); - for (Template template : Template.allTemplates()) { + for (Template template : Template.getTemplates()) { Messaging.send(sender, "[[-]] " + template.getName()); } } diff --git a/main/src/main/java/net/citizensnpcs/npc/Template.java b/main/src/main/java/net/citizensnpcs/npc/Template.java index 892cb6431..ecc696609 100644 --- a/main/src/main/java/net/citizensnpcs/npc/Template.java +++ b/main/src/main/java/net/citizensnpcs/npc/Template.java @@ -1,14 +1,16 @@ package net.citizensnpcs.npc; import java.io.File; +import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.stream.Collectors; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.io.Files; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.event.DespawnReason; @@ -16,6 +18,7 @@ import net.citizensnpcs.api.event.SpawnReason; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.MemoryDataKey; +import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.YamlStorage; import net.citizensnpcs.api.util.YamlStorage.YamlKey; @@ -60,15 +63,62 @@ public class Template { } public void delete() { - templates.load(); - templates.getKey("").removeKey(name); - templates.save(); + new File(getDirectory(), name + ".yml").delete(); } public String getName() { return name; } + public void save() throws IOException { + File file = new File(getDirectory(), name + ".yml"); + if (!file.getParentFile().equals(getDirectory())) + throw new IOException(); + try { + file.createNewFile(); + } catch (IOException e) { + throw e; + } + YamlStorage storage = new YamlStorage(file); + DataKey root = storage.getKey(""); + root.setBoolean("override", override); + root.setRaw("replacements", replacements); + storage.save(); + } + + public static class Builder { + private final String name; + private boolean override = true; + private final Map replacements = Maps.newHashMap(); + + private Builder(String name) { + this.name = name; + } + + public Template buildAndSave() throws IOException { + Template tpl = new Template(name, replacements, override); + tpl.save(); + return tpl; + } + + public Builder from(NPC npc) { + replacements.clear(); + MemoryDataKey key = new MemoryDataKey(); + ((CitizensNPC) npc).save(key); + replacements.putAll(key.getValuesDeep()); + return this; + } + + public Builder override(boolean override) { + this.override = override; + return this; + } + + public static Builder create(String name) { + return new Builder(name); + } + } + private static class Node { String headKey; Map map; @@ -79,69 +129,62 @@ public class Template { } } - public static class TemplateBuilder { - private final String name; - private boolean override; - private final Map replacements = Maps.newHashMap(); - - private TemplateBuilder(String name) { - this.name = name; - } - - public Template buildAndSave() { - save(); - return new Template(name, replacements, override); - } - - public TemplateBuilder from(NPC npc) { - replacements.clear(); - MemoryDataKey key = new MemoryDataKey(); - ((CitizensNPC) npc).save(key); - replacements.putAll(key.getValuesDeep()); - return this; - } - - public TemplateBuilder override(boolean override) { - this.override = override; - return this; - } - - public void save() { - templates.load(); - DataKey root = templates.getKey(name); - root.setBoolean("override", override); - root.setRaw("replacements", replacements); - templates.save(); - } - - public static TemplateBuilder create(String name) { - return new TemplateBuilder(name); - } - } - - public static Iterable