From a713fb987ee3c2c7d95990dc98e5dbad0b5ba22c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 20 Nov 2016 02:16:39 +0100 Subject: [PATCH] Reestructured debug potatoes and added per-exception names --- .../java/buttondevteam/lib/DebugPotato.java | 65 +++++++++++++++++++ .../buttondevteam/lib/DebugPotatoAPI.java | 49 ++------------ .../java/buttondevteam/lib/TBMCCoreAPI.java | 14 ++-- 3 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/DebugPotato.java diff --git a/src/main/java/buttondevteam/lib/DebugPotato.java b/src/main/java/buttondevteam/lib/DebugPotato.java new file mode 100644 index 0000000..2b68089 --- /dev/null +++ b/src/main/java/buttondevteam/lib/DebugPotato.java @@ -0,0 +1,65 @@ +package buttondevteam.lib; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.entity.Player; + +public class DebugPotato { + private List message; + private String type; + + public void Send(Player player) { + DebugPotatoAPI.SendDebugPotato(this, player); + } + + public List getMessage() { + return message; + } + + public DebugPotato setMessage(List message) { + this.message = message; + return this; + } + + public DebugPotato setMessage(String message) { + this.message = WordWrap(message); + return this; + } + + public DebugPotato setMessage(String[] message) { + this.message = Arrays.asList(message); + return this; + } + + public String getType() { + return type; + } + + public DebugPotato setType(String type) { + this.type = type; + return this; + } + + public 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; + } +} diff --git a/src/main/java/buttondevteam/lib/DebugPotatoAPI.java b/src/main/java/buttondevteam/lib/DebugPotatoAPI.java index 867b7fe..fa7d699 100644 --- a/src/main/java/buttondevteam/lib/DebugPotatoAPI.java +++ b/src/main/java/buttondevteam/lib/DebugPotatoAPI.java @@ -1,9 +1,5 @@ package buttondevteam.lib; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.enchantments.Enchantment; @@ -12,52 +8,21 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; public class DebugPotatoAPI { - public static ItemStack CreateDebugPotato(List message) { + private static ItemStack CreateDebugPotato(DebugPotato dp) { ItemStack potato = new ItemStack(Material.BAKED_POTATO); ItemMeta meta = potato.getItemMeta(); - meta.setDisplayName("Spicy Debug Potato"); - meta.setLore(message); + meta.setDisplayName(dp.getType() == null ? "Spicy Debug Potato" : dp.getType()); + if (dp.getMessage() == null) + throw new IllegalArgumentException("Potato message is empty!"); + meta.setLore(dp.getMessage()); potato.setItemMeta(meta); potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10); return potato; } - public static ItemStack CreateDebugPotato(String message) { - return CreateDebugPotato(WordWrap(message)); - } - - public static void SendDebugPotato(Player player, List message) { - player.getInventory().addItem(CreateDebugPotato(message)); + public static void SendDebugPotato(DebugPotato dp, Player player) { + player.getInventory().addItem(CreateDebugPotato(dp)); player.getWorld().playSound(player.getLocation(), Sound.ENTITY_SLIME_SQUISH, 0, 0); return; } - - public static void SendDebugPotato(Player player, String[] message) { - SendDebugPotato(player, Arrays.asList(message)); - } - - public static void SendDebugPotato(Player player, String message) { - SendDebugPotato(player, WordWrap(message)); - } - - public 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; - } } diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 01cdf23..f4dab65 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -153,11 +153,15 @@ public final class TBMCCoreAPI { e.printStackTrace(); Optional randomPlayer = Bukkit.getOnlinePlayers().stream().findAny(); if (randomPlayer.isPresent()) - DebugPotatoAPI.SendDebugPotato(randomPlayer.get(), - new String[] { // - "§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], // - "§c§o" + sourcemsg, // - "§a§oFind a dev to fix this issue" }); + DebugPotatoAPI.SendDebugPotato( + new DebugPotato() + .setMessage(new String[] { // + "§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], // + "§c§o" + sourcemsg, // + "§a§oFind a dev to fix this issue" }) + .setType(e instanceof IOException ? "Potato on a Stick" + : e instanceof ClassCastException ? "Square Potato" : "Plain Potato"), + randomPlayer.get()); } /**