From c86a41ddda78f5d579a2c5ef0beb91fb436469e7 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Tue, 25 Apr 2023 18:14:15 +0100 Subject: [PATCH] actions base --- .../exception/CommandException.java | 22 ---------------- .../exception/CommandExecuteException.java | 7 ------ .../exception/CommandPermissionException.java | 7 ------ .../inventory => gui}/ConversationGUI.java | 6 ++--- .../InteractionPacketListener.java | 18 +++---------- .../pyr/znpcsplus/interaction/NPCAction.java | 25 +++++++++++++++++++ src/main/java/lol/pyr/znpcsplus/npc/NPC.java | 15 ++++++++--- .../java/lol/pyr/znpcsplus/user/User.java | 13 +++++++++- 8 files changed, 54 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/io/github/znetworkw/znpcservers/exception/CommandException.java delete mode 100644 src/main/java/io/github/znetworkw/znpcservers/exception/CommandExecuteException.java delete mode 100644 src/main/java/io/github/znetworkw/znpcservers/exception/CommandPermissionException.java rename src/main/java/io/github/znetworkw/znpcservers/{commands/list/inventory => gui}/ConversationGUI.java (94%) create mode 100644 src/main/java/lol/pyr/znpcsplus/interaction/NPCAction.java diff --git a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandException.java b/src/main/java/io/github/znetworkw/znpcservers/exception/CommandException.java deleted file mode 100644 index ffc7fb8..0000000 --- a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandException.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.znetworkw.znpcservers.exception; - -import java.io.Serial; - -/** - * @author xCodiq - 20/04/2023 - */ -public class CommandException extends Exception { - @Serial private static final long serialVersionUID = 1L; - - public CommandException(String message) { - super(message); - } - - public CommandException(String message, Throwable cause) { - super(message, cause); - } - - public CommandException(Throwable cause) { - super(cause); - } -} diff --git a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandExecuteException.java b/src/main/java/io/github/znetworkw/znpcservers/exception/CommandExecuteException.java deleted file mode 100644 index 126db39..0000000 --- a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandExecuteException.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.znetworkw.znpcservers.exception; - -public class CommandExecuteException extends CommandException { - public CommandExecuteException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandPermissionException.java b/src/main/java/io/github/znetworkw/znpcservers/exception/CommandPermissionException.java deleted file mode 100644 index 6fb767d..0000000 --- a/src/main/java/io/github/znetworkw/znpcservers/exception/CommandPermissionException.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.znetworkw.znpcservers.exception; - -public class CommandPermissionException extends CommandException { - public CommandPermissionException(String message) { - super(message); - } -} diff --git a/src/main/java/io/github/znetworkw/znpcservers/commands/list/inventory/ConversationGUI.java b/src/main/java/io/github/znetworkw/znpcservers/gui/ConversationGUI.java similarity index 94% rename from src/main/java/io/github/znetworkw/znpcservers/commands/list/inventory/ConversationGUI.java rename to src/main/java/io/github/znetworkw/znpcservers/gui/ConversationGUI.java index d1ead88..8934342 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/commands/list/inventory/ConversationGUI.java +++ b/src/main/java/io/github/znetworkw/znpcservers/gui/ConversationGUI.java @@ -1,4 +1,4 @@ -package io.github.znetworkw.znpcservers.commands.list.inventory; +package io.github.znetworkw.znpcservers.gui; import com.google.common.base.Joiner; import com.google.common.base.Splitter; @@ -65,7 +65,7 @@ public class ConversationGUI extends ZInventory { for (int i = slots - (getRows() - 9); i < min; ++i) { Conversation conversation = ConfigurationConstants.NPC_CONVERSATIONS.get(i); - this.addItem(ItemStackBuilder.forMaterial(Material.PAPER).setName(ChatColor.GREEN + conversation.getName()).setLore("&7this conversation has &b" + conversation.getTexts().size() + " &7texts,", "&7it will activate when a player is on a &b" + conversation.getRadius() + "x" + conversation.getRadius() + " &7radius,", "&7or when a player interacts with an npc.", "&7when the conversation is finish, there is a &b" + conversation.getDelay() + "s &7delay to start again.", "&f&lUSES", " &bLeft-click &7to manage texts.", " &bRight-click &7to add a new text.", " &bQ &7to change the radius.", " &bMiddle-click &7to change the cooldown.").build(), i - ((getRows() - 9) * (pageID - 1)), clickEvent -> { + this.addItem(ItemStackBuilder.forMaterial(Material.PAPER).setName(ChatColor.GREEN + conversation.getName()).setLore("&7this conversation has &b" + conversation.getTexts().size() + " &7texts,", "&7it will activate when a player is on a &b" + conversation.getRadius() + "x" + conversation.getRadius() + " &7radius,", "&7or when a player interacts with an npc.", "&7when the conversation is finish, there is a &b" + conversation.getCooldown() + "s &7delay to start again.", "&f&lUSES", " &bLeft-click &7to manage texts.", " &bRight-click &7to add a new text.", " &bQ &7to change the radius.", " &bMiddle-click &7to change the cooldown.").build(), i - ((getRows() - 9) * (pageID - 1)), clickEvent -> { if (clickEvent.getClick() == ClickType.DROP) { Utils.sendTitle(this.getPlayer(), "&b&lCHANGE RADIUS", "&7Type the new radius..."); EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> { @@ -226,7 +226,7 @@ public class ConversationGUI extends ZInventory { for (int i = slots - (getRows() - 9); i < min; i++) { ConversationKey conversationKey = this.conversation.getTexts().get(i); - this.addItem(ItemStackBuilder.forMaterial(Material.NAME_TAG).setName(ChatColor.AQUA + conversationKey.getTextFormatted() + "....").setLore("&7this conversation text has a delay of &b" + conversationKey.getDelay() + "s &7to be executed,", "&7the sound for the text is &b" + (conversationKey.getSoundName() == null ? "NONE" : conversationKey.getSoundName()) + "&7,", "&7before sending the text there is a delay of &b" + conversationKey.getDelay() + "s", "&7the index for the text is &b" + i + "&7,", "&7and the conversation has currently &b" + conversationKey.getActions().size() + " actions&7.", "&f&lUSES", " &bLeft-click &7to change the position.", " &bRight-click &7to remove text.", " &bLeft-Shift-click &7to change the sound.", " &bMiddle-click &7to change the delay.", " &bRight-Shift-click &7to edit the text.", " &bQ &7to manage actions.").build(), i, clickEvent -> { + this.addItem(ItemStackBuilder.forMaterial(Material.NAME_TAG).setName(ChatColor.AQUA + conversationKey.getTextFormatted() + "....").setLore("&7this conversation text has a delay of &b" + conversationKey.getCooldown() + "s &7to be executed,", "&7the sound for the text is &b" + (conversationKey.getSoundName() == null ? "NONE" : conversationKey.getSoundName()) + "&7,", "&7before sending the text there is a delay of &b" + conversationKey.getCooldown() + "s", "&7the index for the text is &b" + i + "&7,", "&7and the conversation has currently &b" + conversationKey.getActions().size() + " actions&7.", "&f&lUSES", " &bLeft-click &7to change the position.", " &bRight-click &7to remove text.", " &bLeft-Shift-click &7to change the sound.", " &bMiddle-click &7to change the delay.", " &bRight-Shift-click &7to edit the text.", " &bQ &7to manage actions.").build(), i, clickEvent -> { if (clickEvent.getClick() == ClickType.SHIFT_LEFT) { Utils.sendTitle(this.getPlayer(), "&c&lCHANGE SOUND", "&7Type the new sound..."); EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> { diff --git a/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java b/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java index 71e58ab..502efe2 100644 --- a/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java +++ b/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java @@ -8,11 +8,7 @@ import lol.pyr.znpcsplus.ZNPCsPlus; import lol.pyr.znpcsplus.npc.NPC; import lol.pyr.znpcsplus.npc.NPCRegistry; import lol.pyr.znpcsplus.user.User; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import org.bukkit.event.inventory.ClickType; - -import java.util.List; public class InteractionPacketListener implements PacketListener { @Override @@ -28,17 +24,9 @@ public class InteractionPacketListener implements PacketListener { if (npc == null) return; ZNPCsPlus.SCHEDULER.runNextTick(() -> { - Bukkit.getServer().getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc)); - List actions = npc.getNpcPojo().getClickActions(); - if (actions == null) return; - for (NPCAction action : actions) { - if (action.getClickType() != ClickType.DEFAULT && action.getClickType() != clickType) continue; - if (action.getDelay() > 0) { - int actionId = npc.getNpcPojo().getClickActions().indexOf(action); - if (user.getLastClicked().containsKey(actionId) && System.nanoTime() - user.getLastClicked().get(actionId) < action.getFixedDelay()) continue; - user.getLastClicked().put(actionId, System.nanoTime()); - } - action.run(user, action.getAction()); + for (NPCAction action : npc.getActions()) { + if (action.getCooldown() > 0 && !user.actionCooldownCheck(action)) continue; + action.run(player); } }); } diff --git a/src/main/java/lol/pyr/znpcsplus/interaction/NPCAction.java b/src/main/java/lol/pyr/znpcsplus/interaction/NPCAction.java new file mode 100644 index 0000000..0b25459 --- /dev/null +++ b/src/main/java/lol/pyr/znpcsplus/interaction/NPCAction.java @@ -0,0 +1,25 @@ +package lol.pyr.znpcsplus.interaction; + +import org.bukkit.entity.Player; + +import java.util.UUID; + +public abstract class NPCAction { + private final UUID id; + private final long delay; + + protected NPCAction(long delay) { + this.id = UUID.randomUUID(); + this.delay = delay; + } + + public UUID getUuid() { + return id; + } + + public long getCooldown() { + return delay; + } + + public abstract void run(Player player); +} diff --git a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java index 0cbfeae..1a8c298 100644 --- a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java +++ b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java @@ -2,14 +2,12 @@ package lol.pyr.znpcsplus.npc; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PacketLocation; +import lol.pyr.znpcsplus.interaction.NPCAction; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Player; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; public class NPC { protected static final Set _ALL_NPCS = new HashSet<>(); @@ -21,6 +19,7 @@ public class NPC { private NPCType type; private final Map, Object> propertyMap = new HashMap<>(); + private final Set actions = new HashSet<>(); public NPC(World world, NPCType type, PacketLocation location) { this.worldName = world.getName(); @@ -124,4 +123,12 @@ public class NPC { public void removeProperty(NPCProperty key) { propertyMap.remove(key); } + + public Collection getActions() { + return Collections.unmodifiableSet(actions); + } + + public void addAction(NPCAction action) { + actions.add(action); + } } diff --git a/src/main/java/lol/pyr/znpcsplus/user/User.java b/src/main/java/lol/pyr/znpcsplus/user/User.java index e6b7c18..eff4b04 100644 --- a/src/main/java/lol/pyr/znpcsplus/user/User.java +++ b/src/main/java/lol/pyr/znpcsplus/user/User.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.user; import io.github.znetworkw.znpcservers.user.EventService; +import lol.pyr.znpcsplus.interaction.NPCAction; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -27,6 +28,7 @@ public class User { private final UUID uuid; private long lastNPCInteraction; + private final Map actionCooldownMap = new HashMap<>(); private final List> eventServices = new ArrayList<>(); public User(UUID uuid) { @@ -38,7 +40,7 @@ public class User { } public boolean canInteract() { - if (System.currentTimeMillis() - lastNPCInteraction > 1000L) { + if (System.currentTimeMillis() - lastNPCInteraction > 100L) { lastNPCInteraction = System.currentTimeMillis(); return true; } @@ -52,4 +54,13 @@ public class User { public UUID getUuid() { return uuid; } + + public boolean actionCooldownCheck(NPCAction action) { + UUID id = action.getUuid(); + if (System.currentTimeMillis() - actionCooldownMap.getOrDefault(id, 0L) >= action.getCooldown()) { + actionCooldownMap.put(id, System.currentTimeMillis()); + return true; + } + return false; + } }