1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-21 12:15:50 +00:00
Slimefun4/src/main/java/me/mrCookieSlime/Slimefun/Setup/SlimefunManager.java

217 lines
8.4 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.Setup;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
2016-04-14 16:24:03 +00:00
import org.bukkit.ChatColor;
import org.bukkit.Material;
2019-09-21 11:04:36 +00:00
import org.bukkit.inventory.Inventory;
2017-04-09 07:59:45 +00:00
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
2017-04-09 07:59:45 +00:00
import org.bukkit.potion.PotionEffect;
import io.github.thebusybiscuit.cscorelib2.item.ImmutableItemMeta;
import me.mrCookieSlime.EmeraldEnchants.EmeraldEnchants;
import me.mrCookieSlime.EmeraldEnchants.ItemEnchantment;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.Lists.Categories;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunArmorPiece;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
2017-04-09 07:59:45 +00:00
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.VanillaItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.Soulbound;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
2016-04-14 16:24:03 +00:00
2019-08-27 21:08:13 +00:00
public final class SlimefunManager {
2019-10-03 22:37:13 +00:00
2019-08-27 21:08:13 +00:00
private SlimefunManager() {}
2019-10-03 22:37:13 +00:00
2016-04-14 16:24:03 +00:00
public static void registerArmorSet(ItemStack baseComponent, ItemStack[] items, String idSyntax, PotionEffect[][] effects, boolean special, boolean slimefun) {
String[] components = new String[] {"_HELMET", "_CHESTPLATE", "_LEGGINGS", "_BOOTS"};
Category cat = special ? Categories.MAGIC_ARMOR: Categories.ARMOR;
2019-08-27 21:08:13 +00:00
List<ItemStack[]> recipes = new ArrayList<>();
2016-04-14 16:24:03 +00:00
recipes.add(new ItemStack[] {baseComponent, baseComponent, baseComponent, baseComponent, null, baseComponent, null, null, null});
recipes.add(new ItemStack[] {baseComponent, null, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent});
recipes.add(new ItemStack[] {baseComponent, baseComponent, baseComponent, baseComponent, null, baseComponent, baseComponent, null, baseComponent});
recipes.add(new ItemStack[] {null, null, null, baseComponent, null, baseComponent, baseComponent, null, baseComponent});
2019-10-03 22:37:13 +00:00
2016-04-14 16:24:03 +00:00
for (int i = 0; i < 4; i++) {
2019-08-29 23:11:33 +00:00
if (i < effects.length && effects[i].length > 0) {
2019-08-29 21:18:04 +00:00
new SlimefunArmorPiece(cat, items[i], idSyntax + components[i], RecipeType.ARMOR_FORGE, recipes.get(i), effects[i]).register(slimefun);
2019-10-03 22:37:13 +00:00
}
2019-08-29 23:11:33 +00:00
else {
2019-08-29 21:18:04 +00:00
new SlimefunItem(cat, items[i], idSyntax + components[i], RecipeType.ARMOR_FORGE, recipes.get(i)).register(slimefun);
}
2016-04-14 16:24:03 +00:00
}
}
2019-10-03 22:37:13 +00:00
2017-04-09 07:59:45 +00:00
public static void registerArmorSet(ItemStack baseComponent, ItemStack[] items, String idSyntax, boolean slimefun, boolean vanilla) {
2016-04-14 16:24:03 +00:00
String[] components = new String[] {"_HELMET", "_CHESTPLATE", "_LEGGINGS", "_BOOTS"};
Category cat = Categories.ARMOR;
2019-08-27 21:08:13 +00:00
List<ItemStack[]> recipes = new ArrayList<>();
2016-04-14 16:24:03 +00:00
recipes.add(new ItemStack[] {baseComponent, baseComponent, baseComponent, baseComponent, null, baseComponent, null, null, null});
recipes.add(new ItemStack[] {baseComponent, null, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent, baseComponent});
recipes.add(new ItemStack[] {baseComponent, baseComponent, baseComponent, baseComponent, null, baseComponent, baseComponent, null, baseComponent});
recipes.add(new ItemStack[] {null, null, null, baseComponent, null, baseComponent, baseComponent, null, baseComponent});
2019-10-03 22:37:13 +00:00
2016-04-14 16:24:03 +00:00
for (int i = 0; i < 4; i++) {
2017-04-09 07:59:45 +00:00
if (vanilla) {
new VanillaItem(cat, items[i], idSyntax + components[i], RecipeType.ARMOR_FORGE, recipes.get(i)).register(slimefun);
2019-10-03 22:37:13 +00:00
}
2019-08-29 23:11:33 +00:00
else {
2017-04-09 07:59:45 +00:00
new SlimefunItem(cat, items[i], idSyntax + components[i], RecipeType.ARMOR_FORGE, recipes.get(i)).register(slimefun);
}
2016-04-14 16:24:03 +00:00
}
}
2019-10-03 22:37:13 +00:00
2019-09-21 11:04:36 +00:00
@Deprecated
public static enum DataType {
2019-10-03 22:37:13 +00:00
2019-09-21 11:04:36 +00:00
ALWAYS,
NEVER,
IF_COLORED;
2019-10-03 22:37:13 +00:00
2019-09-21 11:04:36 +00:00
}
2019-10-03 22:37:13 +00:00
@Deprecated
public static boolean isItemSimiliar(ItemStack item, ItemStack sfitem, boolean lore) {
return isItemSimilar(item, sfitem, lore);
}
public static boolean isItemSimilar(ItemStack item, ItemStack sfitem, boolean checkLore) {
if (item == null) return sfitem == null;
if (sfitem == null) return false;
2019-10-03 22:37:13 +00:00
if (item.getType() == sfitem.getType() && item.getAmount() >= sfitem.getAmount()) {
if (item.hasItemMeta() && sfitem.hasItemMeta()) {
ItemMeta itemMeta = item.getItemMeta();
ItemMeta sfitemMeta = sfitem.getItemMeta();
if (sfitem instanceof SlimefunItemStack) {
Optional<String> id = SlimefunPlugin.getItemDataService().getItemData(itemMeta);
if (id.isPresent()) {
2019-12-08 11:59:55 +00:00
return id.get().equals(((SlimefunItemStack) sfitem).getItemID());
}
ImmutableItemMeta meta = ((SlimefunItemStack) sfitem).getImmutableMeta();
Optional<String> displayName = meta.getDisplayName();
if (itemMeta.hasDisplayName() && displayName.isPresent()) {
if (itemMeta.getDisplayName().equals(displayName.get())) {
Optional<List<String>> itemLore = meta.getLore();
if (checkLore) {
if (itemMeta.hasLore() && itemLore.isPresent()) {
return equalsLore(itemMeta.getLore(), itemLore.get());
}
else return !itemMeta.hasLore() && !itemLore.isPresent();
}
else return true;
}
else return false;
}
else if (!itemMeta.hasDisplayName() && !displayName.isPresent()) {
Optional<List<String>> itemLore = meta.getLore();
if (checkLore) {
if (itemMeta.hasLore() && itemLore.isPresent()) {
return equalsLore(itemMeta.getLore(), itemLore.get());
}
else return !itemMeta.hasLore() && !itemLore.isPresent();
}
else return true;
}
else return false;
}
else if (itemMeta.hasDisplayName() && sfitemMeta.hasDisplayName()) {
if (itemMeta.getDisplayName().equals(sfitemMeta.getDisplayName())) {
if (checkLore) {
if (itemMeta.hasLore() && sfitemMeta.hasLore()) {
return equalsLore(itemMeta.getLore(), sfitemMeta.getLore());
2016-04-14 16:24:03 +00:00
}
else return !itemMeta.hasLore() && !sfitemMeta.hasLore();
2016-04-14 16:24:03 +00:00
}
else return true;
}
else return false;
}
else if (!itemMeta.hasDisplayName() && !sfitemMeta.hasDisplayName()) {
if (checkLore) {
if (itemMeta.hasLore() && sfitemMeta.hasLore()) {
return equalsLore(itemMeta.getLore(), sfitemMeta.getLore());
2016-04-14 16:24:03 +00:00
}
else return !itemMeta.hasLore() && !sfitemMeta.hasLore();
2016-04-14 16:24:03 +00:00
}
else return true;
}
else return false;
2019-10-03 22:37:13 +00:00
}
else return !item.hasItemMeta() && !sfitem.hasItemMeta();
2016-04-14 16:24:03 +00:00
}
else return false;
}
2019-08-29 14:19:23 +00:00
@Deprecated
public static boolean isItemSimiliar(ItemStack item, ItemStack sfitem, boolean lore, DataType data) {
return isItemSimiliar(item, sfitem, lore);
2019-08-29 14:19:23 +00:00
}
2019-10-03 22:37:13 +00:00
2019-09-21 11:04:36 +00:00
public static boolean containsSimilarItem(Inventory inventory, ItemStack itemStack, boolean checkLore) {
if (inventory == null || itemStack == null) return false;
for (ItemStack is : inventory.getStorageContents()) {
if (is == null || is.getType() == Material.AIR) continue;
if (isItemSimiliar(is, itemStack, checkLore)) return true;
}
return false;
}
2019-10-03 22:37:13 +00:00
2016-04-14 16:24:03 +00:00
private static boolean equalsLore(List<String> lore, List<String> lore2) {
StringBuilder string1 = new StringBuilder();
StringBuilder string2 = new StringBuilder();
2019-09-05 12:08:19 +00:00
String colors = ChatColor.YELLOW.toString() + ChatColor.YELLOW.toString() + ChatColor.GRAY.toString();
for (String string : lore) {
2019-09-05 12:02:26 +00:00
if (!string.equals(ChatColor.GRAY + "Soulbound") && !string.startsWith(colors)) string1.append("-NEW LINE-").append(string);
2016-04-14 16:24:03 +00:00
}
2019-10-03 22:37:13 +00:00
for (String string : lore2) {
2019-09-05 12:02:26 +00:00
if (!string.equals(ChatColor.GRAY + "Soulbound") && !string.startsWith(colors)) string2.append("-NEW LINE-").append(string);
}
return string1.toString().equals(string2.toString());
}
public static boolean isItemSoulbound(ItemStack item) {
if (item == null || item.getType() == Material.AIR) return false;
else if (SlimefunManager.isItemSimiliar(item, SlimefunItems.BOUND_BACKPACK, false)) {
return true;
}
else {
ItemStack strippedItem = item.clone();
if (SlimefunPlugin.getHooks().isEmeraldEnchantsInstalled()) {
for (ItemEnchantment enchantment : EmeraldEnchants.getInstance().getRegistry().getEnchantments(item)){
EmeraldEnchants.getInstance().getRegistry().applyEnchantment(strippedItem, enchantment.getEnchantment(), 0);
}
}
if (SlimefunItem.getByItem(strippedItem) instanceof Soulbound) {
return true;
}
else if (item.hasItemMeta()) {
ItemMeta im = item.getItemMeta();
return (im.hasLore() && im.getLore().contains(ChatColor.GRAY + "Soulbound"));
}
return false;
2016-04-14 16:24:03 +00:00
}
}
}