commit
85d2506da7
4 changed files with 203 additions and 142 deletions
|
@ -1,129 +1,133 @@
|
||||||
package buttondevteam.lib;
|
package buttondevteam.lib;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
public class DebugPotato {
|
public class DebugPotato {
|
||||||
private List<String> message;
|
private List<String> message;
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the debug potato to a player
|
* Send the debug potato to a player
|
||||||
*
|
*
|
||||||
* @param player
|
* @param player
|
||||||
* The player
|
* The player
|
||||||
*/
|
*/
|
||||||
public void Send(Player player){
|
public void Send(Player player){
|
||||||
player.getInventory().addItem(this.toItemStack());
|
player.getInventory().addItem(this.toItemStack());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message (lore of the potato).
|
* Get the message (lore of the potato).
|
||||||
*
|
*
|
||||||
* @return The message
|
* @return The message
|
||||||
*/
|
*/
|
||||||
public List<String> getMessage() {
|
public List<String> getMessage() {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the message (lore of the potato).
|
* Sets the message (lore of the potato).
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
* The message
|
* The message
|
||||||
* @return This potato
|
* @return This potato
|
||||||
*/
|
*/
|
||||||
public DebugPotato setMessage(List<String> message) {
|
public DebugPotato setMessage(List<String> message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the message (lore of the potato). It will be word wrapped automatically.
|
* Sets the message (lore of the potato). It will be word wrapped automatically.
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
* The message
|
* The message
|
||||||
* @return This potato
|
* @return This potato
|
||||||
*/
|
*/
|
||||||
public DebugPotato setMessage(String message) {
|
public DebugPotato setMessage(String message) {
|
||||||
this.message = WordWrap(message);
|
this.message = WordWrap(message);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the message (lore of the potato).
|
* Sets the message (lore of the potato).
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
* The message
|
* The message
|
||||||
* @return This potato
|
* @return This potato
|
||||||
*/
|
*/
|
||||||
public DebugPotato setMessage(String[] message) {
|
public DebugPotato setMessage(String[] message) {
|
||||||
this.message = Arrays.asList(message);
|
this.message = Arrays.asList(message);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the type (potato name).
|
* Gets the type (potato name).
|
||||||
*
|
*
|
||||||
* @return The type
|
* @return The type
|
||||||
*/
|
*/
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the type (potato name).
|
* Sets the type (potato name).
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type
|
||||||
* The type
|
* The type
|
||||||
* @return This potato
|
* @return This potato
|
||||||
*/
|
*/
|
||||||
public DebugPotato setType(String type) {
|
public DebugPotato setType(String type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> WordWrap(String message) {
|
private static List<String> WordWrap(String message) {
|
||||||
String[] splitString = message.split("\\s+");
|
String[] splitString = message.split("\\s+");
|
||||||
List<String> newMessage = new ArrayList<String>();
|
List<String> newMessage = new ArrayList<String>();
|
||||||
String currentLine = "";
|
String currentLine = "";
|
||||||
int currentLineLength = 0;
|
int currentLineLength = 0;
|
||||||
int wordlength;
|
int wordlength;
|
||||||
int maxLineLength = 40;
|
int maxLineLength = 40;
|
||||||
for (String word : splitString) {
|
if (message.length() <= maxLineLength){
|
||||||
wordlength = word.length();
|
newMessage.add(message);
|
||||||
if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) {
|
return newMessage;
|
||||||
currentLine += word + " ";
|
}
|
||||||
currentLineLength += wordlength + 1;
|
for (String word : splitString) {
|
||||||
} else {
|
wordlength = word.length();
|
||||||
newMessage.add(currentLine);
|
if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) {
|
||||||
currentLine = word + " ";
|
currentLine += word + " ";
|
||||||
currentLineLength = word.length();
|
currentLineLength += wordlength + 1;
|
||||||
}
|
} else {
|
||||||
}
|
newMessage.add(currentLine);
|
||||||
return newMessage;
|
currentLine = word + " ";
|
||||||
}
|
currentLineLength = word.length();
|
||||||
public ItemStack toItemStack() {
|
}
|
||||||
ItemStack potato = new ItemStack(Material.BAKED_POTATO);
|
}
|
||||||
ItemMeta meta = potato.getItemMeta();
|
return newMessage;
|
||||||
meta.setDisplayName(this.getType() == null ? "Spicy Debug Potato" : this.getType());
|
}
|
||||||
if (this.getMessage() == null){
|
public ItemStack toItemStack() {
|
||||||
List<String> message = new ArrayList<String>();
|
ItemStack potato = new ItemStack(Material.BAKED_POTATO);
|
||||||
message.add("nullMessage");
|
ItemMeta meta = potato.getItemMeta();
|
||||||
meta.setLore(message);
|
meta.setDisplayName(this.getType() == null ? "Null Flavoured Debug Potato" : this.getType());
|
||||||
}else{
|
if (this.getMessage() == null){
|
||||||
meta.setLore(this.getMessage());
|
List<String> message = new ArrayList<String>();
|
||||||
}
|
message.add("nullMessage");
|
||||||
potato.setItemMeta(meta);
|
meta.setLore(message);
|
||||||
potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10);
|
}else{
|
||||||
return potato;
|
meta.setLore(this.getMessage());
|
||||||
}
|
}
|
||||||
}
|
potato.setItemMeta(meta);
|
||||||
|
potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10);
|
||||||
|
return potato;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -127,6 +127,7 @@ public final class TBMCCoreAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>();
|
private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>();
|
||||||
|
private static List<String> debugMessagesToSend = new ArrayList<>();
|
||||||
|
|
||||||
private static final String[] potatoMessages = new String[] { //
|
private static final String[] potatoMessages = new String[] { //
|
||||||
"Well shit", //
|
"Well shit", //
|
||||||
|
@ -163,6 +164,13 @@ public final class TBMCCoreAPI {
|
||||||
potato.Send(randomPlayer.get());
|
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
|
* Registers Bukkit events, handling the exceptions occuring in those events
|
||||||
|
@ -191,4 +199,17 @@ public final class TBMCCoreAPI {
|
||||||
exceptionsToSend.remove(entry.getKey());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
49
src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java
Normal file
49
src/main/java/buttondevteam/lib/TBMCDebugMessageEvent.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue