From c33ac8a1b11d60c4214d32ba32931dc9caad26a9 Mon Sep 17 00:00:00 2001 From: Om Choksi Date: Sun, 23 Apr 2023 19:38:23 +0530 Subject: [PATCH] Spam fix for older versions --- .../commands/list/DefaultCommand.java | 12 ++++++++- .../configuration/ConfigurationConstants.java | 1 + .../configuration/ConfigurationValue.java | 1 + .../github/znetworkw/znpcservers/npc/NPC.java | 27 ++++++++++++++----- .../znetworkw/znpcservers/npc/NPCModel.java | 15 +++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/znetworkw/znpcservers/commands/list/DefaultCommand.java b/src/main/java/io/github/znetworkw/znpcservers/commands/list/DefaultCommand.java index 55f834f..600cede 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/commands/list/DefaultCommand.java +++ b/src/main/java/io/github/znetworkw/znpcservers/commands/list/DefaultCommand.java @@ -84,6 +84,10 @@ public class DefaultCommand extends Command { return; } NPCType npcType = NPCType.valueOf(args.get("type").toUpperCase()); + if (npcType.getConstructor() == null && !npcType.equals(NPCType.PLAYER)) { + Configuration.MESSAGES.sendMessage(sender.getCommandSender(), ConfigurationValue.NOT_SUPPORTED_NPC_TYPE); + return; + } ZNPCsPlus.createNPC(id, npcType, sender.getPlayer().getLocation(), name); Configuration.MESSAGES.sendMessage(sender.getCommandSender(), ConfigurationValue.SUCCESS); } @@ -116,7 +120,13 @@ public class DefaultCommand extends Command { sender.sendMessage(ChatColor.DARK_GREEN + "NPC list:"); for (NPCModel npcModel : ConfigurationConstants.NPC_LIST) { List parts = new ArrayList<>(); - String message = "- " + npcModel.getId() + " " + npcModel.getHologramLines().toString() + " (" + npcModel.getLocation().getWorldName() + " " + (int) npcModel.getLocation().getX() + " " + (int) npcModel.getLocation().getY() + " " + (int) npcModel.getLocation().getZ() + ") "; + TextComponent component1 = new TextComponent("-"); + component1.setColor(ChatColor.GREEN); + parts.add(component1); + TextComponent idComponent = new TextComponent(" " + npcModel.getId()); + idComponent.setColor(npcModel.getShouldSpawn() ? ChatColor.GREEN : ChatColor.RED); + parts.add(idComponent); + String message = " " + npcModel.getHologramLines().toString() + " (" + npcModel.getLocation().getWorldName() + " " + (int) npcModel.getLocation().getX() + " " + (int) npcModel.getLocation().getY() + " " + (int) npcModel.getLocation().getZ() + ") "; TextComponent textComponent = new TextComponent(message); textComponent.setColor(ChatColor.GREEN); parts.add(textComponent); diff --git a/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationConstants.java b/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationConstants.java index 7472672..c0d7811 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationConstants.java +++ b/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationConstants.java @@ -7,6 +7,7 @@ import java.util.List; public final class ConfigurationConstants { public static final String SPACE_SYMBOL = Configuration.CONFIGURATION.getValue(ConfigurationValue.REPLACE_SYMBOL); + public static final boolean DEBUG_ENABLED = Configuration.CONFIGURATION.getValue(ConfigurationValue.DEBUG_ENABLED); public static final int VIEW_DISTANCE = Configuration.CONFIGURATION.getValue(ConfigurationValue.VIEW_DISTANCE); public static final int SAVE_DELAY = Configuration.CONFIGURATION.getValue(ConfigurationValue.SAVE_NPCS_DELAY_SECONDS); public static final boolean RGB_ANIMATION = Configuration.CONFIGURATION.getValue(ConfigurationValue.ANIMATION_RGB); diff --git a/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationValue.java b/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationValue.java index ce96a3b..f9b67bf 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationValue.java +++ b/src/main/java/io/github/znetworkw/znpcservers/configuration/ConfigurationValue.java @@ -51,6 +51,7 @@ public enum ConfigurationValue { FETCHING_SKIN("messages", "&aFetching skin for name: &f%s&a. Please wait...", String.class), CANT_GET_SKIN("messages", "&cCould not fetch skin for name: %s.", String.class), GET_SKIN("messages", "&aSkin successfully fetched!", String.class), + NOT_SUPPORTED_NPC_TYPE("messages", "&cThis NPC type doesn't exists or is not supported in your current server version.", String.class), CONVERSATION_LIST("conversations" /* Leave this lowercase or it will break */, new ArrayList<>(), Conversation.class); public static final Map> VALUES_BY_NAME; diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java index 64bc8f7..235754a 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java @@ -12,6 +12,8 @@ import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; import io.github.retrooper.packetevents.util.SpigotConversionUtil; import io.github.znetworkw.znpcservers.UnexpectedCallException; +import io.github.znetworkw.znpcservers.configuration.ConfigurationConstants; +import io.github.znetworkw.znpcservers.configuration.ConfigurationValue; import io.github.znetworkw.znpcservers.hologram.Hologram; import io.github.znetworkw.znpcservers.nms.PacketCache; import io.github.znetworkw.znpcservers.npc.conversation.ConversationModel; @@ -81,13 +83,21 @@ public class NPC { if (NPC_MAP.containsKey(getNpcPojo().getId())) throw new IllegalStateException("npc with id " + getNpcPojo().getId() + " already exists."); this.gameProfile = new GameProfile(this.uuid, "[ZNPC] " + this.npcName); this.gameProfile.getProperties().put("textures", new Property("textures", this.npcPojo.getSkin(), this.npcPojo.getSignature())); - changeType(this.npcPojo.getNpcType()); - updateProfile(this.gameProfile.getProperties()); - setLocation(getNpcPojo().getLocation().toBukkitLocation(), false); - this.hologram.createHologram(); - if (this.npcPojo.getPathName() != null) - setPath(NPCPath.AbstractTypeWriter.find(this.npcPojo.getPathName())); - this.npcPojo.getCustomizationMap().forEach((key, value) -> this.npcPojo.getNpcType().updateCustomization(this, key, value)); + if (this.npcPojo.getNpcType().getConstructor() == null && !this.npcPojo.getNpcType().equals(NPCType.PLAYER)) { + this.npcPojo.setShouldSpawn(false); + if (ConfigurationConstants.DEBUG_ENABLED) { + ZNPCsPlus.LOGGER.warning("The NPC Type " + npcPojo.getNpcType().name() + " does not exist or is not supported in this version."); + } + } else { + this.npcPojo.setShouldSpawn(true); + changeType(this.npcPojo.getNpcType()); + updateProfile(this.gameProfile.getProperties()); + setLocation(getNpcPojo().getLocation().toBukkitLocation(), false); + this.hologram.createHologram(); + if (this.npcPojo.getPathName() != null) + setPath(NPCPath.AbstractTypeWriter.find(this.npcPojo.getPathName())); + this.npcPojo.getCustomizationMap().forEach((key, value) -> this.npcPojo.getNpcType().updateCustomization(this, key, value)); + } NPC_MAP.put(getNpcPojo().getId(), this); } @@ -212,6 +222,9 @@ public class NPC { if (this.viewers.contains(user)) { return; } + if (!getNpcPojo().getShouldSpawn()) { + return; + } try { this.viewers.add(user); boolean npcIsPlayer = (this.npcPojo.getNpcType() == NPCType.PLAYER); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/NPCModel.java b/src/main/java/io/github/znetworkw/znpcservers/npc/NPCModel.java index 932e335..3131803 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPCModel.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPCModel.java @@ -19,6 +19,7 @@ public class NPCModel { private ConversationModel conversation; private ZLocation location; private NPCType npcType; + private boolean shouldSpawn; private List hologramLines; private List clickActions; private Map npcEquip; @@ -31,6 +32,7 @@ public class NPCModel { this.skin = ""; this.signature = ""; this.npcType = NPCType.PLAYER; + this.shouldSpawn = true; this.hologramLines = Collections.singletonList("/znpcs lines"); this.clickActions = new ArrayList<>(); this.npcEquip = new HashMap<>(); @@ -178,6 +180,19 @@ public class NPCModel { return this; } + public boolean getShouldSpawn() { + return this.shouldSpawn; + } + + public void setShouldSpawn(boolean shouldSpawn) { + this.shouldSpawn = shouldSpawn; + } + + public NPCModel withShouldSpawn(boolean shouldSpawn) { + setShouldSpawn(shouldSpawn); + return this; + } + public List getClickActions() { return this.clickActions; }