From 1adc2f1b4c3c1f8b65fecad716f7a47135b97a49 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Mon, 21 Nov 2016 14:21:14 -0500 Subject: [PATCH 1/2] Created DebugMessages for Discord Similar, and copypasted from TMBCException Event, TBMCDebugMessageEvent, should ideally send a plain string message to TBMC Devs II - debug-channel --- .../java/buttondevteam/lib/TBMCCoreAPI.java | 21 ++++++++ .../lib/TBMCDebugMessageEvent.java | 49 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 56ba8ce..6f2844b 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -127,6 +127,7 @@ public final class TBMCCoreAPI { } private static HashMap exceptionsToSend = new HashMap<>(); + private static List debugMessagesToSend = new ArrayList<>(); private static final String[] potatoMessages = new String[] { // "Well shit", // @@ -163,6 +164,13 @@ public final class TBMCCoreAPI { potato.Send(randomPlayer.get()); } } + public static void sendDebugMessage(String debugMessage){ + SendUnsentDebugMessages(); + TBMCDebugMessageEvent event = new TBMCDebugMessageEvent(debugMessage); + Bukkit.getPluginManager().callEvent(event); + if (!event.isSent()) + debugMessagesToSend.add(debugMessage); + } /** * Registers Bukkit events, handling the exceptions occuring in those events @@ -191,4 +199,17 @@ public final class TBMCCoreAPI { exceptionsToSend.remove(entry.getKey()); } } + + public static void SendUnsentDebugMessages() { + if (debugMessagesToSend.size() > 20) { + debugMessagesToSend.clear(); // Don't call more and more DebugMessages if all the handler plugins are unloaded + Bukkit.getLogger().warning("Unhandled Debug Message list is over 20! Clearing!"); + } + for (String message : debugMessagesToSend) { + TBMCDebugMessageEvent event = new TBMCDebugMessageEvent(message); + Bukkit.getPluginManager().callEvent(event); + if (event.isSent()) + debugMessagesToSend.remove(message); + } + } } \ No newline at end of file diff --git a/src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java b/src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java new file mode 100644 index 0000000..67debcf --- /dev/null +++ b/src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java @@ -0,0 +1,49 @@ +package buttondevteam.lib; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TBMCDebugMessageEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + private String message; + private boolean sent; + + public TBMCDebugMessageEvent(String message) { + this.message = message; + } + + /** + * Gets the message (where did this exception occur, etc.) + * + * @return The message + */ + public String getDebugMessage() { + return message; + } + + /** + * Gets if this event was handled + * + * @return True if it was handled + */ + public boolean isSent() { + return sent; + } + + /** + * Flags the event as handled + */ + public void setSent() { + this.sent = true; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} -- 2.30.2 From e5bb0e43dd81da11650825ed2edf5f0c1911f500 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Mon, 21 Nov 2016 21:12:58 -0500 Subject: [PATCH 2/2] Fixed bug where short messages aren't sent --- .../java/buttondevteam/lib/DebugPotato.java | 262 +++++++++--------- .../buttondevteam/lib/DebugPotatoAPI.java | 13 - 2 files changed, 133 insertions(+), 142 deletions(-) delete mode 100644 src/main/java/buttondevteam/lib/DebugPotatoAPI.java diff --git a/src/main/java/buttondevteam/lib/DebugPotato.java b/src/main/java/buttondevteam/lib/DebugPotato.java index 48f195a..4b6ce53 100644 --- a/src/main/java/buttondevteam/lib/DebugPotato.java +++ b/src/main/java/buttondevteam/lib/DebugPotato.java @@ -1,129 +1,133 @@ -package buttondevteam.lib; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -public class DebugPotato { - private List message; - private String type; - - /** - * Send the debug potato to a player - * - * @param player - * The player - */ - public void Send(Player player){ - player.getInventory().addItem(this.toItemStack()); - return; - } - - /** - * Get the message (lore of the potato). - * - * @return The message - */ - public List getMessage() { - return message; - } - - /** - * Sets the message (lore of the potato). - * - * @param message - * The message - * @return This potato - */ - public DebugPotato setMessage(List message) { - this.message = message; - return this; - } - - /** - * Sets the message (lore of the potato). It will be word wrapped automatically. - * - * @param message - * The message - * @return This potato - */ - public DebugPotato setMessage(String message) { - this.message = WordWrap(message); - return this; - } - - /** - * Sets the message (lore of the potato). - * - * @param message - * The message - * @return This potato - */ - public DebugPotato setMessage(String[] message) { - this.message = Arrays.asList(message); - return this; - } - - /** - * Gets the type (potato name). - * - * @return The type - */ - public String getType() { - return type; - } - - /** - * Sets the type (potato name). - * - * @param type - * The type - * @return This potato - */ - public DebugPotato setType(String type) { - this.type = type; - return this; - } - - private static List WordWrap(String message) { - String[] splitString = message.split("\\s+"); - List newMessage = new ArrayList(); - String currentLine = ""; - int currentLineLength = 0; - int wordlength; - int maxLineLength = 40; - for (String word : splitString) { - wordlength = word.length(); - if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) { - currentLine += word + " "; - currentLineLength += wordlength + 1; - } else { - newMessage.add(currentLine); - currentLine = word + " "; - currentLineLength = word.length(); - } - } - return newMessage; - } - public ItemStack toItemStack() { - ItemStack potato = new ItemStack(Material.BAKED_POTATO); - ItemMeta meta = potato.getItemMeta(); - meta.setDisplayName(this.getType() == null ? "Spicy Debug Potato" : this.getType()); - if (this.getMessage() == null){ - List message = new ArrayList(); - message.add("nullMessage"); - meta.setLore(message); - }else{ - meta.setLore(this.getMessage()); - } - potato.setItemMeta(meta); - potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10); - return potato; - } -} +package buttondevteam.lib; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class DebugPotato { + private List message; + private String type; + + /** + * Send the debug potato to a player + * + * @param player + * The player + */ + public void Send(Player player){ + player.getInventory().addItem(this.toItemStack()); + return; + } + + /** + * Get the message (lore of the potato). + * + * @return The message + */ + public List getMessage() { + return message; + } + + /** + * Sets the message (lore of the potato). + * + * @param message + * The message + * @return This potato + */ + public DebugPotato setMessage(List message) { + this.message = message; + return this; + } + + /** + * Sets the message (lore of the potato). It will be word wrapped automatically. + * + * @param message + * The message + * @return This potato + */ + public DebugPotato setMessage(String message) { + this.message = WordWrap(message); + return this; + } + + /** + * Sets the message (lore of the potato). + * + * @param message + * The message + * @return This potato + */ + public DebugPotato setMessage(String[] message) { + this.message = Arrays.asList(message); + return this; + } + + /** + * Gets the type (potato name). + * + * @return The type + */ + public String getType() { + return type; + } + + /** + * Sets the type (potato name). + * + * @param type + * The type + * @return This potato + */ + public DebugPotato setType(String type) { + this.type = type; + return this; + } + + private static List WordWrap(String message) { + String[] splitString = message.split("\\s+"); + List newMessage = new ArrayList(); + String currentLine = ""; + int currentLineLength = 0; + int wordlength; + int maxLineLength = 40; + if (message.length() <= maxLineLength){ + newMessage.add(message); + return newMessage; + } + for (String word : splitString) { + wordlength = word.length(); + if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) { + currentLine += word + " "; + currentLineLength += wordlength + 1; + } else { + newMessage.add(currentLine); + currentLine = word + " "; + currentLineLength = word.length(); + } + } + return newMessage; + } + public ItemStack toItemStack() { + ItemStack potato = new ItemStack(Material.BAKED_POTATO); + ItemMeta meta = potato.getItemMeta(); + meta.setDisplayName(this.getType() == null ? "Null Flavoured Debug Potato" : this.getType()); + if (this.getMessage() == null){ + List message = new ArrayList(); + message.add("nullMessage"); + meta.setLore(message); + }else{ + meta.setLore(this.getMessage()); + } + potato.setItemMeta(meta); + potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10); + return potato; + } +} diff --git a/src/main/java/buttondevteam/lib/DebugPotatoAPI.java b/src/main/java/buttondevteam/lib/DebugPotatoAPI.java deleted file mode 100644 index 5b78edd..0000000 --- a/src/main/java/buttondevteam/lib/DebugPotatoAPI.java +++ /dev/null @@ -1,13 +0,0 @@ -package buttondevteam.lib; - -import org.bukkit.entity.Player; - -/**@deprecated - * Fully Replaced by DebugPotato Class - Construct a DebugPotato*/ -public class DebugPotatoAPI { - /**@deprecated Replaced by DebugPotato.send*/ - public static void SendDebugPotato(DebugPotato dp, Player player) { - player.getInventory().addItem(dp.toItemStack()); - return; - } -} -- 2.30.2