2023-05-25 19:20:03 +02:00
|
|
|
package com.craftaro.ultimatestacker.utils;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2023-05-30 11:20:31 +02:00
|
|
|
import com.craftaro.ultimatestacker.api.UltimateStackerAPI;
|
|
|
|
import com.craftaro.ultimatestacker.api.stack.item.StackedItem;
|
2019-09-09 18:08:04 +02:00
|
|
|
import com.songoda.core.compatibility.CompatibleMaterial;
|
2022-03-18 21:38:26 +01:00
|
|
|
import com.songoda.core.third_party.de.tr7zw.nbtapi.NBTItem;
|
2019-09-03 22:38:00 +02:00
|
|
|
import com.songoda.core.utils.TextUtils;
|
2023-05-25 19:20:03 +02:00
|
|
|
import com.craftaro.ultimatestacker.UltimateStacker;
|
|
|
|
import com.craftaro.ultimatestacker.settings.Settings;
|
|
|
|
import com.craftaro.ultimatestacker.stackable.entity.custom.CustomEntity;
|
2020-09-01 20:44:39 +02:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
2018-11-06 04:33:10 +01:00
|
|
|
import org.bukkit.block.CreatureSpawner;
|
2020-09-01 20:13:53 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.Item;
|
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;
|
|
|
|
|
2020-04-26 14:42:18 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
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) {
|
2023-05-30 11:20:31 +02:00
|
|
|
StackedItem stackedItem = UltimateStackerAPI.getStackedItemManager().getStackedItem(item);
|
|
|
|
int amount = stackedItem.getAmount();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:20:31 +02:00
|
|
|
if (amount <= 0) {
|
2019-07-13 21:49:40 +02:00
|
|
|
item.remove();
|
2023-05-30 11:20:31 +02:00
|
|
|
} else {
|
|
|
|
stackedItem.setAmount(amount);
|
|
|
|
}
|
2019-08-19 00:46:35 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 21:41:35 +02:00
|
|
|
public static String compileItemName(ItemStack item, int amount) {
|
2019-09-07 23:55:16 +02:00
|
|
|
String nameFormat = Settings.NAME_FORMAT_ITEM.getString();
|
2021-03-05 15:09:00 +01:00
|
|
|
String displayName = TextUtils.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-09-07 23:55:16 +02:00
|
|
|
displayName = Settings.NAME_FORMAT_RESET.getBoolean() ?
|
2019-07-14 20:37:54 +02:00
|
|
|
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));
|
|
|
|
|
2019-09-07 23:55:16 +02:00
|
|
|
if (amount == 1 && !Settings.SHOW_STACK_SIZE_SINGLE.getBoolean()) {
|
2019-07-13 21:41:35 +02:00
|
|
|
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
|
|
|
|
2021-03-05 15:09:00 +01:00
|
|
|
return info + TextUtils.formatText(nameFormat).trim();
|
2019-07-13 21:41:35 +02:00
|
|
|
}
|
|
|
|
|
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");
|
2021-03-05 15:09:00 +01:00
|
|
|
String displayName = TextUtils.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));
|
|
|
|
|
2021-03-05 15:09:00 +01:00
|
|
|
return TextUtils.formatText(nameFormat).trim();
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String compileEntityName(Entity entity, int amount) {
|
2019-09-07 23:55:16 +02:00
|
|
|
String nameFormat = Settings.NAME_FORMAT_ENTITY.getString();
|
2021-03-05 15:09:00 +01:00
|
|
|
String displayName = TextUtils.formatText(UltimateStacker.getInstance().getMobFile().getString("Mobs." + entity.getType().name() + ".Display Name"));
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2021-03-05 15:09:00 +01:00
|
|
|
CustomEntity customEntity = UltimateStacker.getInstance().getCustomEntityManager().getCustomEntity(entity);
|
2023-01-16 21:32:17 +01:00
|
|
|
if (customEntity != null) {
|
2020-10-26 18:41:54 +01:00
|
|
|
displayName = customEntity.getDisplayName(entity);
|
2023-01-16 21:32:17 +01:00
|
|
|
}
|
2020-10-26 18:41:54 +01:00
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
nameFormat = nameFormat.replace("{TYPE}", displayName);
|
|
|
|
nameFormat = nameFormat.replace("{AMT}", Integer.toString(amount));
|
|
|
|
|
2021-03-05 15:09:00 +01:00
|
|
|
return TextUtils.formatText(nameFormat).trim();
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getSpawnerItem(EntityType entityType, int amount) {
|
2019-09-09 18:08:04 +02:00
|
|
|
ItemStack item = CompatibleMaterial.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);
|
|
|
|
|
2022-03-18 21:38:26 +01:00
|
|
|
NBTItem nbtItem = new NBTItem(item);
|
|
|
|
nbtItem.setInteger("spawner_stack_size", amount);
|
|
|
|
return nbtItem.getItem();
|
2019-06-10 02:47:29 +02:00
|
|
|
}
|
|
|
|
|
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-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();
|
|
|
|
}
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|