From 35d326d74fdee4bc1abe18d16e8aebe6184e4109 Mon Sep 17 00:00:00 2001 From: PikaMug Date: Tue, 15 Oct 2019 01:53:08 -0400 Subject: [PATCH] Supply external conversation hooks, part 14 --- .../main/java/me/blackvein/quests/Quests.java | 4 +- .../QuestsCommandPreQuestsListEvent.java | 43 +++++++++++++++++++ .../quests/listeners/CmdExecutor.java | 25 +++++++++-- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 main/src/main/java/me/blackvein/quests/events/command/QuestsCommandPreQuestsListEvent.java diff --git a/main/src/main/java/me/blackvein/quests/Quests.java b/main/src/main/java/me/blackvein/quests/Quests.java index c1bc55e97..2ddcb9cdf 100644 --- a/main/src/main/java/me/blackvein/quests/Quests.java +++ b/main/src/main/java/me/blackvein/quests/Quests.java @@ -1337,7 +1337,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } } } - if ((available.size() + rows) <= ((page * rows)) || available.size() == 0) { + if ((available.size() + rows) <= (page * rows) || available.size() == 0) { player.sendMessage(ChatColor.YELLOW + Lang.get(player, "pageNotExist")); } else { player.sendMessage(ChatColor.GOLD + Lang.get(player, "questListTitle")); @@ -1360,7 +1360,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener player.sendMessage(ChatColor.GOLD + msg); } } else { - if ((quests.size() + rows) <= ((page * rows)) || quests.size() == 0) { + if ((quests.size() + rows) <= (page * rows) || quests.size() == 0) { player.sendMessage(ChatColor.YELLOW + Lang.get(player, "pageNotExist")); } else { player.sendMessage(ChatColor.GOLD + Lang.get(player, "questListTitle")); diff --git a/main/src/main/java/me/blackvein/quests/events/command/QuestsCommandPreQuestsListEvent.java b/main/src/main/java/me/blackvein/quests/events/command/QuestsCommandPreQuestsListEvent.java new file mode 100644 index 000000000..a92a12b1f --- /dev/null +++ b/main/src/main/java/me/blackvein/quests/events/command/QuestsCommandPreQuestsListEvent.java @@ -0,0 +1,43 @@ +package me.blackvein.quests.events.command; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +import me.blackvein.quests.Quester; + +/** + * Called when the /quests list command is run by a player + */ +public class QuestsCommandPreQuestsListEvent extends QuestsCommandEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private final int page; + private boolean cancel = false; + + public QuestsCommandPreQuestsListEvent(Quester quester, int page) { + super(quester); + this.page = page; + } + + @Override + public boolean isCancelled() { + return cancel; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancel = cancel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public int getPage() { + return page; + } +} diff --git a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java index 066efc258..23d7ad137 100644 --- a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java +++ b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java @@ -32,6 +32,7 @@ import me.blackvein.quests.Requirements; import me.blackvein.quests.Stage; import me.blackvein.quests.events.command.QuestsCommandPreQuestsEditorEvent; import me.blackvein.quests.events.command.QuestsCommandPreQuestsJournalEvent; +import me.blackvein.quests.events.command.QuestsCommandPreQuestsListEvent; import me.blackvein.quests.events.quest.QuestQuitEvent; import me.blackvein.quests.exceptions.InvalidStageException; import me.blackvein.quests.util.ItemUtil; @@ -724,22 +725,38 @@ public class CmdExecutor implements CommandExecutor { @SuppressWarnings("deprecation") private void questsList(final CommandSender cs, String[] args) { - if (((Player) cs).hasPermission("quests.list")) { + Player player = (Player)cs; + if (player.hasPermission("quests.list")) { if (args.length == 1) { - plugin.listQuests((Player) cs, 1); + Quester quester = plugin.getQuester(player.getUniqueId()); + QuestsCommandPreQuestsListEvent preEvent = new QuestsCommandPreQuestsListEvent(quester, 1); + plugin.getServer().getPluginManager().callEvent(preEvent); + if (preEvent.isCancelled()) { + return; + } + + plugin.listQuests(player, 1); } else if (args.length == 2) { - int page; + int page = 1; try { page = Integer.parseInt(args[1]); if (page < 1) { cs.sendMessage(ChatColor.YELLOW + Lang.get("pageSelectionPosNum")); return; + } else { + Quester quester = plugin.getQuester(player.getUniqueId()); + QuestsCommandPreQuestsListEvent preEvent = new QuestsCommandPreQuestsListEvent(quester, page); + plugin.getServer().getPluginManager().callEvent(preEvent); + if (preEvent.isCancelled()) { + return; + } + + plugin.listQuests(player, page); } } catch (NumberFormatException e) { cs.sendMessage(ChatColor.YELLOW + Lang.get("pageSelectionNum")); return; } - plugin.listQuests((Player) cs, page); } } else { cs.sendMessage(ChatColor.RED + Lang.get("noPermission"));