Fix the spacing and clean it up.
This commit is contained in:
@ -32,25 +32,25 @@ import com.graywolf336.jail.beans.Prisoner;
|
||||
* @version 3.0.0
|
||||
*/
|
||||
public class Util {
|
||||
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* Checks if the first {@link Vector} is inside this region.
|
||||
*
|
||||
* @param point The point to check
|
||||
* @param first point of the region
|
||||
* @param second second point of the region
|
||||
* @return True if all the coords of the first vector are in the entire region.
|
||||
*/
|
||||
public static boolean isInsideAB(Vector point, Vector first, Vector second) {
|
||||
boolean x = isInside(point.getBlockX(), first.getBlockX(), second.getBlockX());
|
||||
boolean y = isInside(point.getBlockY(), first.getBlockY(), second.getBlockY());
|
||||
boolean z = isInside(point.getBlockZ(), first.getBlockZ(), second.getBlockZ());
|
||||
|
||||
return x && y && z;
|
||||
}
|
||||
|
||||
/**
|
||||
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* Checks if the first {@link Vector} is inside this region.
|
||||
*
|
||||
* @param point The point to check
|
||||
* @param first point of the region
|
||||
* @param second second point of the region
|
||||
* @return True if all the coords of the first vector are in the entire region.
|
||||
*/
|
||||
public static boolean isInsideAB(Vector point, Vector first, Vector second) {
|
||||
boolean x = isInside(point.getBlockX(), first.getBlockX(), second.getBlockX());
|
||||
boolean y = isInside(point.getBlockY(), first.getBlockY(), second.getBlockY());
|
||||
boolean z = isInside(point.getBlockZ(), first.getBlockZ(), second.getBlockZ());
|
||||
|
||||
return x && y && z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if two numbers are inside a point, or something.
|
||||
*
|
||||
* <p />
|
||||
@ -73,43 +73,43 @@ public class Util {
|
||||
|
||||
return (point1 <= loc) && (loc <= point2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given string is inside the list, ignoring the casing.
|
||||
*
|
||||
* <p />
|
||||
*
|
||||
* @param list of strings to check
|
||||
* @param value to check
|
||||
* @param value to check
|
||||
* @return true if the list contains the provided value, false if it doesn't
|
||||
*/
|
||||
public static boolean isStringInsideList(List<String> list, String value) {
|
||||
for(String s : list)
|
||||
if(s.equalsIgnoreCase(value))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
for(String s : list)
|
||||
if(s.equalsIgnoreCase(value))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/** Returns a colorful message from the color codes. */
|
||||
public static String getColorfulMessage(String message) {
|
||||
return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1");
|
||||
}
|
||||
|
||||
return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1");
|
||||
}
|
||||
|
||||
/** Returns the wand used throughout the different creation steps. */
|
||||
public static ItemStack getWand() {
|
||||
ItemStack wand = new ItemStack(Material.WOOD_SWORD);
|
||||
ItemMeta meta = wand.getItemMeta();
|
||||
meta.setDisplayName(ChatColor.AQUA + "Jail Wand");
|
||||
LinkedList<String> lore = new LinkedList<String>();
|
||||
lore.add(ChatColor.BLUE + "The wand for creating");
|
||||
lore.add(ChatColor.BLUE + "a jail or cell.");
|
||||
meta.setLore(lore);
|
||||
wand.setItemMeta(meta);
|
||||
|
||||
return wand;
|
||||
ItemStack wand = new ItemStack(Material.WOOD_SWORD);
|
||||
ItemMeta meta = wand.getItemMeta();
|
||||
meta.setDisplayName(ChatColor.AQUA + "Jail Wand");
|
||||
LinkedList<String> lore = new LinkedList<String>();
|
||||
lore.add(ChatColor.BLUE + "The wand for creating");
|
||||
lore.add(ChatColor.BLUE + "a jail or cell.");
|
||||
meta.setLore(lore);
|
||||
wand.setItemMeta(meta);
|
||||
|
||||
return wand;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Converts a string like '20minutes' into the appropriate amount of the given unit.
|
||||
@ -120,9 +120,9 @@ public class Util {
|
||||
* @throws Exception if there are no matches
|
||||
*/
|
||||
public static Long getTime(String time, TimeUnit unit) throws Exception {
|
||||
return unit.convert(getTime(time), TimeUnit.MILLISECONDS);
|
||||
return unit.convert(getTime(time), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a string like '20minutes' into the appropriate amount of milliseconds.
|
||||
*
|
||||
@ -131,35 +131,35 @@ public class Util {
|
||||
* @throws Exception if there are no matches
|
||||
*/
|
||||
public static Long getTime(String time) throws Exception {
|
||||
if(time.equalsIgnoreCase("-1")) return -1L;
|
||||
|
||||
Long t = 10L;
|
||||
Matcher match = DURATION_PATTERN.matcher(time);
|
||||
|
||||
if (match.matches()) {
|
||||
String units = match.group(2);
|
||||
if ("seconds".equals(units) || "second".equals(units) || "s".equals(units))
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS);
|
||||
else if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units) || "m".equals(units))
|
||||
if(time.equalsIgnoreCase("-1")) return -1L;
|
||||
|
||||
Long t = 10L;
|
||||
Matcher match = DURATION_PATTERN.matcher(time);
|
||||
|
||||
if (match.matches()) {
|
||||
String units = match.group(2);
|
||||
if ("seconds".equals(units) || "second".equals(units) || "s".equals(units))
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS);
|
||||
else if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units) || "m".equals(units))
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.MINUTES);
|
||||
else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units))
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS);
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS);
|
||||
else if ("days".equals(units) || "day".equals(units) || "d".equals(units))
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS);
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS);
|
||||
else {
|
||||
try {
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.parseLong(time), TimeUnit.MINUTES);
|
||||
}catch(NumberFormatException e) {
|
||||
throw new Exception("Invalid format.");
|
||||
}
|
||||
try {
|
||||
t = TimeUnit.MILLISECONDS.convert(Long.parseLong(time), TimeUnit.MINUTES);
|
||||
}catch(NumberFormatException e) {
|
||||
throw new Exception("Invalid format.");
|
||||
}
|
||||
}
|
||||
}else {
|
||||
throw new Exception("Invalid format.");
|
||||
}
|
||||
|
||||
return t;
|
||||
}else {
|
||||
throw new Exception("Invalid format.");
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the player inventory to a String array of Base64 strings. First string is the content and second string is the armor.
|
||||
*
|
||||
@ -168,13 +168,13 @@ public class Util {
|
||||
* @throws IllegalStateException
|
||||
*/
|
||||
public static String[] playerInventoryToBase64(PlayerInventory playerInventory) throws IllegalStateException {
|
||||
//get the main content part, this doesn't return the armor
|
||||
String content = toBase64(playerInventory);
|
||||
String armor = itemStackArrayToBase64(playerInventory.getArmorContents());
|
||||
|
||||
return new String[] { content, armor };
|
||||
//get the main content part, this doesn't return the armor
|
||||
String content = toBase64(playerInventory);
|
||||
String armor = itemStackArrayToBase64(playerInventory.getArmorContents());
|
||||
|
||||
return new String[] { content, armor };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* A method to serialize an {@link ItemStack} array to Base64 String.
|
||||
@ -188,18 +188,18 @@ public class Util {
|
||||
* @throws IllegalStateException
|
||||
*/
|
||||
public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
|
||||
try {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||
|
||||
|
||||
// Write the size of the inventory
|
||||
dataOutput.writeInt(items.length);
|
||||
|
||||
|
||||
// Save every element in the list
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
dataOutput.writeObject(items[i]);
|
||||
}
|
||||
|
||||
|
||||
// Serialize that array
|
||||
dataOutput.close();
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
@ -207,7 +207,7 @@ public class Util {
|
||||
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A method to serialize an inventory to Base64 string.
|
||||
*
|
||||
@ -226,15 +226,15 @@ public class Util {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||
|
||||
|
||||
// Write the size of the inventory
|
||||
dataOutput.writeInt(inventory.getSize());
|
||||
|
||||
|
||||
// Save every element in the list
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
dataOutput.writeObject(inventory.getItem(i));
|
||||
}
|
||||
|
||||
|
||||
// Serialize that array
|
||||
dataOutput.close();
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
@ -242,7 +242,7 @@ public class Util {
|
||||
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* A method to get an {@link Inventory} from an encoded, Base64, string.
|
||||
@ -259,25 +259,25 @@ public class Util {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Inventory fromBase64(String data) throws IOException {
|
||||
if(data.isEmpty()) return Bukkit.getServer().createInventory(null, 0);
|
||||
|
||||
if(data.isEmpty()) return Bukkit.getServer().createInventory(null, 0);
|
||||
|
||||
try {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
|
||||
|
||||
|
||||
// Read the serialized inventory
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
inventory.setItem(i, (ItemStack) dataInput.readObject());
|
||||
}
|
||||
|
||||
|
||||
dataInput.close();
|
||||
return inventory;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException("Unable to decode class type.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets an array of ItemStacks from Base64 string.
|
||||
*
|
||||
@ -290,57 +290,57 @@ public class Util {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
|
||||
if(data.isEmpty()) return new ItemStack[] {};
|
||||
|
||||
try {
|
||||
if(data.isEmpty()) return new ItemStack[] {};
|
||||
|
||||
try {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||
|
||||
|
||||
// Read the serialized inventory
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
items[i] = (ItemStack) dataInput.readObject();
|
||||
items[i] = (ItemStack) dataInput.readObject();
|
||||
}
|
||||
|
||||
|
||||
dataInput.close();
|
||||
return items;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException("Unable to decode class type.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void restoreInventory(Player player, Prisoner prisoner) {
|
||||
try {
|
||||
Inventory content = Util.fromBase64(prisoner.getInventory());
|
||||
ItemStack[] armor = Util.itemStackArrayFromBase64(prisoner.getArmor());
|
||||
|
||||
for(ItemStack item : armor) {
|
||||
if(item == null)
|
||||
continue;
|
||||
else if(item.getType().toString().toLowerCase().contains("helmet"))
|
||||
player.getInventory().setHelmet(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("chestplate"))
|
||||
player.getInventory().setChestplate(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("leg"))
|
||||
player.getInventory().setLeggings(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("boots"))
|
||||
player.getInventory().setBoots(item);
|
||||
else if (player.getInventory().firstEmpty() == -1)
|
||||
player.getWorld().dropItem(player.getLocation(), item);
|
||||
else
|
||||
player.getInventory().addItem(item);
|
||||
}
|
||||
|
||||
for(ItemStack item : content.getContents()) {
|
||||
if(item == null) continue;
|
||||
else if(player.getInventory().firstEmpty() == -1)
|
||||
player.getWorld().dropItem(player.getLocation(), item);
|
||||
else
|
||||
player.getInventory().addItem(item);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getLogger().severe("Unable to restore " + player.getName() + "'s inventory.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void restoreInventory(Player player, Prisoner prisoner) {
|
||||
try {
|
||||
Inventory content = Util.fromBase64(prisoner.getInventory());
|
||||
ItemStack[] armor = Util.itemStackArrayFromBase64(prisoner.getArmor());
|
||||
|
||||
for(ItemStack item : armor) {
|
||||
if(item == null)
|
||||
continue;
|
||||
else if(item.getType().toString().toLowerCase().contains("helmet"))
|
||||
player.getInventory().setHelmet(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("chestplate"))
|
||||
player.getInventory().setChestplate(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("leg"))
|
||||
player.getInventory().setLeggings(item);
|
||||
else if(item.getType().toString().toLowerCase().contains("boots"))
|
||||
player.getInventory().setBoots(item);
|
||||
else if (player.getInventory().firstEmpty() == -1)
|
||||
player.getWorld().dropItem(player.getLocation(), item);
|
||||
else
|
||||
player.getInventory().addItem(item);
|
||||
}
|
||||
|
||||
for(ItemStack item : content.getContents()) {
|
||||
if(item == null) continue;
|
||||
else if(player.getInventory().firstEmpty() == -1)
|
||||
player.getWorld().dropItem(player.getLocation(), item);
|
||||
else
|
||||
player.getInventory().addItem(item);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getLogger().severe("Unable to restore " + player.getName() + "'s inventory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user