2018-11-06 04:33:10 +01:00
|
|
|
package com.songoda.ultimatestacker.utils;
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
import com.songoda.core.compatibility.LegacyMaterials;
|
|
|
|
import com.songoda.core.compatibility.ServerVersion;
|
|
|
|
import com.songoda.core.utils.TextUtils;
|
2018-11-06 04:33:10 +01:00
|
|
|
import com.songoda.ultimatestacker.UltimateStacker;
|
2019-09-03 23:22:15 +02:00
|
|
|
import com.songoda.ultimatestacker.settings.Setting;
|
2019-09-03 22:38:00 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.GameMode;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
2019-01-10 21:27:01 +01:00
|
|
|
import org.bukkit.block.Block;
|
2018-11-06 04:33:10 +01:00
|
|
|
import org.bukkit.block.CreatureSpawner;
|
2019-09-03 22:38:00 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.Item;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
2019-07-13 21:49:40 +02:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2018-11-06 04:33:10 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.inventory.meta.BlockStateMeta;
|
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
2019-06-06 06:43:38 +02:00
|
|
|
import org.bukkit.util.Vector;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
public class Methods {
|
|
|
|
|
2019-07-13 21:49:40 +02:00
|
|
|
public static void updateInventory(Item item, Inventory inventory) {
|
|
|
|
int amount = Methods.getActualItemAmount(item);
|
2019-08-13 02:59:32 +02:00
|
|
|
ItemStack itemStack = item.getItemStack();
|
2019-08-19 00:46:35 +02:00
|
|
|
final int maxStack = itemStack.getMaxStackSize();
|
2019-07-13 21:49:40 +02:00
|
|
|
|
|
|
|
while (amount > 0) {
|
2019-08-19 00:46:35 +02:00
|
|
|
int subtract = Math.min(amount, maxStack);
|
2019-07-13 21:49:40 +02:00
|
|
|
amount -= subtract;
|
2019-08-13 02:59:32 +02:00
|
|
|
ItemStack newItem = itemStack.clone();
|
2019-07-13 21:49:40 +02:00
|
|
|
newItem.setAmount(subtract);
|
|
|
|
Map<Integer, ItemStack> result = inventory.addItem(newItem);
|
|
|
|
if (result.get(0) != null) {
|
|
|
|
amount += result.get(0).getAmount();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amount <= 0)
|
|
|
|
item.remove();
|
|
|
|
else
|
2019-08-13 02:59:32 +02:00
|
|
|
Methods.updateItemAmount(item, itemStack, amount);
|
2019-07-13 21:49:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-08-13 04:55:31 +02:00
|
|
|
public static boolean isMaterialBlacklisted(Material type) {
|
2019-09-03 22:38:00 +02:00
|
|
|
return UltimateStacker.isMaterialBlacklisted(type);
|
2019-08-13 04:55:31 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-08-19 20:35:15 +02:00
|
|
|
public static boolean isMaterialBlacklisted(Material type, byte data) {
|
2019-09-03 22:38:00 +02:00
|
|
|
return UltimateStacker.isMaterialBlacklisted(type, data);
|
2019-08-19 20:35:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-08-27 16:35:48 +02:00
|
|
|
public static void updateItemAmount(Item item, int newAmount) {
|
2019-09-03 22:38:00 +02:00
|
|
|
UltimateStacker.updateItemAmount(item, newAmount);
|
2019-08-27 16:35:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-08-13 02:59:32 +02:00
|
|
|
public static void updateItemAmount(Item item, ItemStack itemStack, int newAmount) {
|
2019-09-03 22:38:00 +02:00
|
|
|
UltimateStacker.updateItemAmount(item, itemStack, newAmount);
|
2019-07-13 21:41:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-07-13 21:41:35 +02:00
|
|
|
public static int getActualItemAmount(Item item) {
|
2019-09-03 22:38:00 +02:00
|
|
|
return UltimateStacker.getActualItemAmount(item);
|
2019-07-13 21:41:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
// Do not touch! API for older plugins
|
|
|
|
@Deprecated
|
2019-08-19 00:46:35 +02:00
|
|
|
public static boolean hasCustomAmount(Item item) {
|
2019-09-03 22:38:00 +02:00
|
|
|
return UltimateStacker.hasCustomAmount(item);
|
2019-08-19 00:46:35 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 21:41:35 +02:00
|
|
|
public static String compileItemName(ItemStack item, int amount) {
|
|
|
|
String nameFormat = Setting.NAME_FORMAT_ITEM.getString();
|
2019-09-03 22:38:00 +02:00
|
|
|
String displayName = Methods.formatText(UltimateStacker.getInstance().getItemFile()
|
2019-07-13 21:41:35 +02:00
|
|
|
.getString("Items." + item.getType().name() + ".Display Name"));
|
|
|
|
|
|
|
|
if (item.hasItemMeta() && item.getItemMeta().hasDisplayName())
|
2019-07-14 20:37:54 +02:00
|
|
|
displayName = Setting.NAME_FORMAT_RESET.getBoolean() ?
|
|
|
|
ChatColor.stripColor(item.getItemMeta().getDisplayName()) : item.getItemMeta().getDisplayName();
|
2019-07-13 21:41:35 +02:00
|
|
|
|
|
|
|
nameFormat = nameFormat.replace("{TYPE}", displayName);
|
|
|
|
nameFormat = nameFormat.replace("{AMT}", Integer.toString(amount));
|
|
|
|
|
|
|
|
if (amount == 1 && !Setting.SHOW_STACK_SIZE_SINGLE.getBoolean()) {
|
|
|
|
nameFormat = nameFormat.replaceAll("\\[.*?]", "");
|
|
|
|
} else {
|
|
|
|
nameFormat = nameFormat.replace("[", "").replace("]", "");
|
|
|
|
}
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
String info = TextUtils.convertToInvisibleString(Methods.insertSemicolon(String.valueOf(amount)) + ":");
|
2019-07-13 21:41:35 +02:00
|
|
|
|
|
|
|
return info + Methods.formatText(nameFormat).trim();
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
public static boolean canFly(LivingEntity entity) {
|
|
|
|
switch (entity.getType()) {
|
|
|
|
case GHAST:
|
|
|
|
case BLAZE:
|
|
|
|
case PHANTOM:
|
2019-06-30 01:32:16 +02:00
|
|
|
case BAT:
|
2019-06-23 03:10:11 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:29:10 +02:00
|
|
|
static Vector getRandomVector() {
|
2019-06-06 06:43:38 +02:00
|
|
|
return new Vector(ThreadLocalRandom.current().nextDouble(-1, 1.01), 0, ThreadLocalRandom.current().nextDouble(-1, 1.01)).normalize().multiply(0.5);
|
|
|
|
}
|
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
public static String compileSpawnerName(EntityType entityType, int amount) {
|
|
|
|
String nameFormat = UltimateStacker.getInstance().getConfig().getString("Spawners.Name Format");
|
2019-09-03 22:38:00 +02:00
|
|
|
String displayName = Methods.formatText(UltimateStacker.getInstance().getSpawnerFile().getString("Spawners." + entityType.name() + ".Display Name"));
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
nameFormat = nameFormat.replace("{TYPE}", displayName);
|
|
|
|
nameFormat = nameFormat.replace("{AMT}", Integer.toString(amount));
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
String info = TextUtils.convertToInvisibleString(insertSemicolon(String.valueOf(amount)) + ":");
|
2019-01-10 21:27:01 +01:00
|
|
|
return info + Methods.formatText(nameFormat).trim();
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String compileEntityName(Entity entity, int amount) {
|
2019-06-06 23:07:04 +02:00
|
|
|
String nameFormat = Setting.NAME_FORMAT_ENTITY.getString();
|
2019-09-03 22:38:00 +02:00
|
|
|
String displayName = Methods.formatText(UltimateStacker.getInstance().getMobFile().getString("Mobs." + entity.getType().name() + ".Display Name"));
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
nameFormat = nameFormat.replace("{TYPE}", displayName);
|
|
|
|
nameFormat = nameFormat.replace("{AMT}", Integer.toString(amount));
|
|
|
|
|
2019-09-03 22:38:00 +02:00
|
|
|
String info = TextUtils.convertToInvisibleString(insertSemicolon(String.valueOf(amount)) + ":");
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-01-10 21:27:01 +01:00
|
|
|
return info + Methods.formatText(nameFormat).trim();
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void takeItem(Player player, int amount) {
|
|
|
|
if (player.getGameMode() == GameMode.CREATIVE) return;
|
|
|
|
|
|
|
|
ItemStack item = player.getInventory().getItemInHand();
|
|
|
|
|
|
|
|
int result = item.getAmount() - amount;
|
|
|
|
item.setAmount(result);
|
|
|
|
|
|
|
|
player.setItemInHand(result > 0 ? item : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getSpawnerItem(EntityType entityType, int amount) {
|
2019-09-03 22:38:00 +02:00
|
|
|
ItemStack item = LegacyMaterials.SPAWNER.getItem();
|
2018-11-06 04:33:10 +01:00
|
|
|
ItemMeta meta = item.getItemMeta();
|
|
|
|
meta.setDisplayName(Methods.compileSpawnerName(entityType, amount));
|
2018-11-06 06:09:40 +01:00
|
|
|
CreatureSpawner cs = (CreatureSpawner) ((BlockStateMeta) meta).getBlockState();
|
2018-11-06 04:33:10 +01:00
|
|
|
cs.setSpawnedType(entityType);
|
|
|
|
((BlockStateMeta) meta).setBlockState(cs);
|
|
|
|
item.setItemMeta(meta);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:47:29 +02:00
|
|
|
public static String formatTitle(String text) {
|
|
|
|
if (text == null || text.equals(""))
|
|
|
|
return "";
|
2019-09-03 22:38:00 +02:00
|
|
|
if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
|
2019-06-10 02:47:29 +02:00
|
|
|
if (text.length() > 31)
|
|
|
|
text = text.substring(0, 29) + "...";
|
|
|
|
}
|
|
|
|
text = formatText(text);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:48:34 +02:00
|
|
|
public static boolean isInt(String number) {
|
|
|
|
if (number == null || number.equals(""))
|
|
|
|
return false;
|
|
|
|
try {
|
|
|
|
Integer.parseInt(number);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-10 21:27:01 +01:00
|
|
|
/**
|
|
|
|
* Serializes the location of the block specified.
|
|
|
|
*
|
|
|
|
* @param b The block whose location is to be saved.
|
|
|
|
* @return The serialized data.
|
|
|
|
*/
|
|
|
|
public static String serializeLocation(Block b) {
|
|
|
|
if (b == null)
|
|
|
|
return "";
|
|
|
|
return serializeLocation(b.getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes the location specified.
|
|
|
|
*
|
|
|
|
* @param location The location that is to be saved.
|
|
|
|
* @return The serialized data.
|
|
|
|
*/
|
|
|
|
public static String serializeLocation(Location location) {
|
2019-07-24 15:51:51 +02:00
|
|
|
if (location == null || location.getWorld() == null)
|
2019-01-10 21:27:01 +01:00
|
|
|
return "";
|
|
|
|
String w = location.getWorld().getName();
|
|
|
|
double x = location.getX();
|
|
|
|
double y = location.getY();
|
|
|
|
double z = location.getZ();
|
|
|
|
String str = w + ":" + x + ":" + y + ":" + z;
|
2019-04-18 05:57:03 +02:00
|
|
|
str = str.replace(".0", "").replace(".", "/");
|
2019-01-10 21:27:01 +01:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<String, Location> serializeCache = new HashMap<>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes a location from the string.
|
|
|
|
*
|
|
|
|
* @param str The string to parse.
|
|
|
|
* @return The location that was serialized in the string.
|
|
|
|
*/
|
|
|
|
public static Location unserializeLocation(String str) {
|
|
|
|
if (str == null || str.equals(""))
|
|
|
|
return null;
|
|
|
|
if (serializeCache.containsKey(str)) {
|
|
|
|
return serializeCache.get(str).clone();
|
|
|
|
}
|
|
|
|
String cacheKey = str;
|
|
|
|
str = str.replace("y:", ":").replace("z:", ":").replace("w:", "").replace("x:", ":").replace("/", ".");
|
|
|
|
List<String> args = Arrays.asList(str.split("\\s*:\\s*"));
|
|
|
|
|
|
|
|
World world = Bukkit.getWorld(args.get(0));
|
|
|
|
double x = Double.parseDouble(args.get(1)), y = Double.parseDouble(args.get(2)), z = Double.parseDouble(args.get(3));
|
|
|
|
Location location = new Location(world, x, y, z, 0, 0);
|
|
|
|
serializeCache.put(cacheKey, location.clone());
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2019-06-20 07:51:38 +02:00
|
|
|
public static String insertSemicolon(String s) {
|
|
|
|
if (s == null || s.equals(""))
|
|
|
|
return "";
|
|
|
|
StringBuilder hidden = new StringBuilder();
|
|
|
|
for (char c : s.toCharArray()) hidden.append(";").append(c);
|
|
|
|
return hidden.toString();
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:27:01 +01:00
|
|
|
|
|
|
|
public static String formatText(String text) {
|
|
|
|
if (text == null || text.equals(""))
|
|
|
|
return "";
|
|
|
|
return formatText(text, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatText(String text, boolean cap) {
|
|
|
|
if (text == null || text.equals(""))
|
|
|
|
return "";
|
|
|
|
if (cap)
|
|
|
|
text = text.substring(0, 1).toUpperCase() + text.substring(1);
|
|
|
|
return ChatColor.translateAlternateColorCodes('&', text);
|
|
|
|
}
|
|
|
|
|
2019-07-14 20:05:15 +02:00
|
|
|
public static class Tuple<key, value> {
|
|
|
|
public final key x;
|
|
|
|
public final value y;
|
2019-07-30 03:44:09 +02:00
|
|
|
|
2019-07-14 20:05:15 +02:00
|
|
|
public Tuple(key x, value y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
public key getKey() {
|
|
|
|
return this.x;
|
|
|
|
}
|
2019-01-10 21:27:01 +01:00
|
|
|
|
2019-07-14 20:05:15 +02:00
|
|
|
public value getValue() {
|
|
|
|
return this.y;
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|