Fixed DebugPotato not WordWrapping

This commit is contained in:
alisolarflare 2016-11-25 19:59:13 -05:00
parent 85d2506da7
commit 7ca344e4f9

View file

@ -1,133 +1,134 @@
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); return setMessage(Arrays.asList(message));
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;
if (message.length() <= maxLineLength){ if (message.length() <= maxLineLength){
newMessage.add(message); newMessage.add(message);
return newMessage; return newMessage;
} }
for (String word : splitString) { for (String word : splitString) {
wordlength = word.length(); wordlength = word.length();
if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) { if (currentLineLength == 0 || (currentLineLength + wordlength) < maxLineLength) {
currentLine += word + " "; currentLine += word + " ";
currentLineLength += wordlength + 1; currentLineLength += wordlength + 1;
} else { } else {
newMessage.add(currentLine); newMessage.add(currentLine);
currentLine = word + " "; currentLine = word + " ";
currentLineLength = word.length(); currentLineLength = word.length();
} }
} }
return newMessage; newMessage.add(currentLine);
} return newMessage;
public ItemStack toItemStack() { }
ItemStack potato = new ItemStack(Material.BAKED_POTATO); public ItemStack toItemStack() {
ItemMeta meta = potato.getItemMeta(); ItemStack potato = new ItemStack(Material.BAKED_POTATO);
meta.setDisplayName(this.getType() == null ? "Null Flavoured Debug Potato" : this.getType()); ItemMeta meta = potato.getItemMeta();
if (this.getMessage() == null){ meta.setDisplayName(this.getType() == null ? "Null Flavoured Debug Potato" : this.getType());
List<String> message = new ArrayList<String>(); if (this.getMessage() == null){
message.add("nullMessage"); List<String> message = new ArrayList<String>();
meta.setLore(message); message.add("nullMessage");
}else{ meta.setLore(message);
meta.setLore(this.getMessage()); }else{
} meta.setLore(this.getMessage());
potato.setItemMeta(meta); }
potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10); potato.setItemMeta(meta);
return potato; potato.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 10);
} return potato;
} }
}