Reestructured debug potatoes and added per-exception names

This commit is contained in:
Norbi Peti 2016-11-20 02:16:39 +01:00
parent 64faa95b3b
commit a713fb987e
3 changed files with 81 additions and 47 deletions

View file

@ -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<String> message;
private String type;
public void Send(Player player) {
DebugPotatoAPI.SendDebugPotato(this, player);
}
public List<String> getMessage() {
return message;
}
public DebugPotato setMessage(List<String> 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<String> WordWrap(String message) {
String[] splitString = message.split("\\s+");
List<String> newMessage = new ArrayList<String>();
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;
}
}

View file

@ -1,9 +1,5 @@
package buttondevteam.lib; package buttondevteam.lib;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -12,52 +8,21 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
public class DebugPotatoAPI { public class DebugPotatoAPI {
public static ItemStack CreateDebugPotato(List<String> message) { private static ItemStack CreateDebugPotato(DebugPotato dp) {
ItemStack potato = new ItemStack(Material.BAKED_POTATO); ItemStack potato = new ItemStack(Material.BAKED_POTATO);
ItemMeta meta = potato.getItemMeta(); ItemMeta meta = potato.getItemMeta();
meta.setDisplayName("Spicy Debug Potato"); meta.setDisplayName(dp.getType() == null ? "Spicy Debug Potato" : dp.getType());
meta.setLore(message); if (dp.getMessage() == null)
throw new IllegalArgumentException("Potato message is empty!");
meta.setLore(dp.getMessage());
potato.setItemMeta(meta); potato.setItemMeta(meta);
potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10); potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10);
return potato; return potato;
} }
public static ItemStack CreateDebugPotato(String message) { public static void SendDebugPotato(DebugPotato dp, Player player) {
return CreateDebugPotato(WordWrap(message)); player.getInventory().addItem(CreateDebugPotato(dp));
}
public static void SendDebugPotato(Player player, List<String> message) {
player.getInventory().addItem(CreateDebugPotato(message));
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_SLIME_SQUISH, 0, 0); player.getWorld().playSound(player.getLocation(), Sound.ENTITY_SLIME_SQUISH, 0, 0);
return; 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<String> WordWrap(String message) {
String[] splitString = message.split("\\s+");
List<String> newMessage = new ArrayList<String>();
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;
}
} }

View file

@ -153,11 +153,15 @@ public final class TBMCCoreAPI {
e.printStackTrace(); e.printStackTrace();
Optional<? extends Player> randomPlayer = Bukkit.getOnlinePlayers().stream().findAny(); Optional<? extends Player> randomPlayer = Bukkit.getOnlinePlayers().stream().findAny();
if (randomPlayer.isPresent()) if (randomPlayer.isPresent())
DebugPotatoAPI.SendDebugPotato(randomPlayer.get(), DebugPotatoAPI.SendDebugPotato(
new String[] { // new DebugPotato()
"§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], // .setMessage(new String[] { //
"§c§o" + sourcemsg, // "§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], //
"§a§oFind a dev to fix this issue" }); "§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());
} }
/** /**