-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68c7c08
commit ddb906d
Showing
4 changed files
with
98 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 87 additions & 82 deletions
169
src/main/java/com/github/mori01231/lifecore/util/ItemUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,117 @@ | ||
package com.github.mori01231.lifecore.util; | ||
package com.github.mori01231.lifecore.util | ||
|
||
import net.azisaba.itemstash.ItemStash; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import org.bukkit.Material; | ||
import org.bukkit.attribute.Attribute; | ||
import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.EquipmentSlot; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.PlayerInventory; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import net.azisaba.itemstash.ItemStash | ||
import net.minecraft.core.component.DataComponents | ||
import net.minecraft.world.item.component.CustomData | ||
import org.bukkit.Material | ||
import org.bukkit.attribute.Attribute | ||
import org.bukkit.attribute.AttributeModifier | ||
import org.bukkit.craftbukkit.inventory.CraftItemStack | ||
import org.bukkit.enchantments.Enchantment | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.EquipmentSlot | ||
import org.bukkit.inventory.ItemStack | ||
import org.jetbrains.annotations.Contract | ||
import java.util.* | ||
import java.util.function.Consumer | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
|
||
public class ItemUtil { | ||
object ItemUtil { | ||
@JvmStatic | ||
@Contract("null -> false") | ||
public static boolean isProbablyAdminSword(@Nullable ItemStack stack) { | ||
fun isProbablyAdminSword(stack: ItemStack?): Boolean { | ||
if (stack == null || | ||
stack.getType().isAir() || | ||
!stack.hasItemMeta() || | ||
!stack.getItemMeta().hasAttributeModifiers()) return false; | ||
return Objects.requireNonNull(stack.getItemMeta().getAttributeModifiers()) | ||
.get(Attribute.GENERIC_ATTACK_DAMAGE) | ||
.stream() | ||
.anyMatch(mod -> mod.getAmount() >= 9999); | ||
stack.type.isAir || | ||
!stack.hasItemMeta() || | ||
!stack.itemMeta.hasAttributeModifiers() | ||
) return false | ||
return stack.itemMeta.attributeModifiers!![Attribute.GENERIC_ATTACK_DAMAGE] | ||
.stream() | ||
.anyMatch { mod: AttributeModifier -> mod.amount >= 9999 } | ||
} | ||
|
||
@JvmStatic | ||
@Contract("null -> null") | ||
public static @Nullable String getMythicType(@Nullable ItemStack stack) { | ||
if (stack == null || stack.getType().isAir()) return null; | ||
NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).u(); // getTag | ||
if (tag == null) return null; | ||
String type = tag.l("MYTHIC_TYPE"); // getString | ||
if (type == null || type.isEmpty()) return null; | ||
return type; | ||
fun getCustomData(stack: ItemStack?): CustomData? { | ||
if (stack == null || stack.type.isAir) return null | ||
return CraftItemStack.asNMSCopy(stack).components[DataComponents.CUSTOM_DATA] | ||
} | ||
|
||
@Contract("null, _ -> null") | ||
public static @Nullable String getStringTag(@Nullable ItemStack stack, @NotNull String key) { | ||
if (stack == null || stack.getType().isAir()) return null; | ||
NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).u(); // getTag | ||
if (tag == null) return null; | ||
return tag.l(key); // getString | ||
@JvmStatic | ||
@Contract("null -> null") | ||
fun getMythicType(stack: ItemStack?): String? { | ||
val data = getCustomData(stack) ?: return null | ||
if (!data.contains("MYTHIC_TYPE")) return null | ||
val type = data.copyTag().getString("MYTHIC_TYPE") | ||
if (type.isEmpty()) return null | ||
return type | ||
} | ||
|
||
public static @NotNull String toString(@NotNull ItemStack stack) { | ||
List<String> props = new ArrayList<>(); | ||
props.add("[Type: " + stack.getType().name() + "]"); | ||
props.add("[Amount: " + stack.getAmount() + "]"); | ||
ItemMeta meta = stack.getItemMeta(); | ||
@JvmStatic | ||
@Contract("null, _ -> null") | ||
fun getStringTag(stack: ItemStack?, key: String): String? = | ||
getCustomData(stack)?.copyTag()?.getString(key) | ||
|
||
@JvmStatic | ||
fun toString(stack: ItemStack): String { | ||
val props: MutableList<String> = ArrayList() | ||
props.add("[Type: " + stack.type.name + "]") | ||
props.add("[Amount: " + stack.amount + "]") | ||
val meta = stack.itemMeta | ||
if (meta != null) { | ||
if (meta.hasDisplayName()) props.add("[Name: " + meta.getDisplayName() + "]"); | ||
if (meta.hasLore()) props.add("[Lore: " + Objects.requireNonNull(meta.getLore()).size() + " entries]"); | ||
if (meta.hasCustomModelData()) props.add("[CustomModelData: " + meta.getCustomModelData() + "]"); | ||
if (getMythicType(stack) != null) props.add("[MMID: " + getMythicType(stack) + "]"); | ||
if (meta.hasEnchants()) meta.getEnchants().forEach((enchant, level) -> props.add("[Enchant: " + enchant.getKey() + " " + level + "]")); | ||
if (meta.hasDisplayName()) props.add("[Name: " + meta.displayName + "]") | ||
if (meta.hasLore()) props.add("[Lore: " + Objects.requireNonNull(meta.lore).size + " entries]") | ||
if (meta.hasCustomModelData()) props.add("[CustomModelData: " + meta.customModelData + "]") | ||
if (getMythicType(stack) != null) props.add("[MMID: " + getMythicType(stack) + "]") | ||
if (meta.hasEnchants()) meta.enchants.forEach { (enchant: Enchantment, level: Int) -> props.add("[Enchant: " + enchant.key + " " + level + "]") } | ||
} | ||
return String.join("", props); | ||
return java.lang.String.join("", props) | ||
} | ||
|
||
public static @NotNull ItemStack createItemStack(@NotNull Material material, int amount, @NotNull Consumer<ItemStack> action) { | ||
ItemStack stack = new ItemStack(material, amount); | ||
action.accept(stack); | ||
return stack; | ||
@JvmStatic | ||
fun createItemStack(material: Material, amount: Int, action: Consumer<ItemStack>): ItemStack { | ||
val stack = ItemStack(material, amount) | ||
action.accept(stack) | ||
return stack | ||
} | ||
|
||
public static @NotNull ItemStack createItemStack(@NotNull Material material, @NotNull String displayName, @NotNull List<String> lore) { | ||
return createItemStack(material, 1, item -> { | ||
ItemMeta meta = item.getItemMeta(); | ||
meta.setDisplayName(displayName); | ||
if (!lore.isEmpty()) { | ||
meta.setLore(lore); | ||
@JvmStatic | ||
fun createItemStack(material: Material, displayName: String, lore: List<String?>): ItemStack { | ||
return createItemStack(material, 1) { item: ItemStack -> | ||
val meta = item.itemMeta | ||
meta.setDisplayName(displayName) | ||
if (lore.isNotEmpty()) { | ||
meta.lore = lore | ||
} | ||
item.setItemMeta(meta); | ||
}); | ||
item.setItemMeta(meta) | ||
} | ||
} | ||
|
||
public static boolean addToStashIfEnabled(@NotNull UUID uuid, @NotNull ItemStack item) { | ||
@JvmStatic | ||
fun addToStashIfEnabled(uuid: UUID, item: ItemStack): Boolean { | ||
try { | ||
Class.forName("net.azisaba.itemstash.ItemStash"); | ||
ItemStash.getInstance().addItemToStash(uuid, item); | ||
return true; | ||
} catch (ClassNotFoundException ignored) { | ||
return false; | ||
Class.forName("net.azisaba.itemstash.ItemStash") | ||
ItemStash.getInstance().addItemToStash(uuid, item) | ||
return true | ||
} catch (ignored: ClassNotFoundException) { | ||
return false | ||
} | ||
} | ||
|
||
@SuppressWarnings("ConstantValue") | ||
public static boolean isEquippedInAnySlot(@NotNull Player player, @NotNull ItemStack stack) { | ||
PlayerInventory inventory = player.getInventory(); | ||
for (EquipmentSlot slot : EquipmentSlot.values()) { | ||
if (inventory.getItem(slot) != null && inventory.getItem(slot).equals(stack)) return true; | ||
@JvmStatic | ||
fun isEquippedInAnySlot(player: Player, stack: ItemStack): Boolean { | ||
val inventory = player.inventory | ||
for (slot in EquipmentSlot.entries) { | ||
if (inventory.getItem(slot) != null && inventory.getItem(slot) == stack) return true | ||
} | ||
return false; | ||
return false | ||
} | ||
|
||
public static boolean isEquippedInAnySlot(@NotNull Player player, @NotNull String mythicType) { | ||
PlayerInventory inventory = player.getInventory(); | ||
for (EquipmentSlot slot : EquipmentSlot.values()) { | ||
if (mythicType.equals(getMythicType(inventory.getItem(slot)))) return true; | ||
@JvmStatic | ||
fun isEquippedInAnySlot(player: Player, mythicType: String): Boolean { | ||
val inventory = player.inventory | ||
for (slot in EquipmentSlot.entries) { | ||
if (mythicType == getMythicType(inventory.getItem(slot))) return true | ||
} | ||
return false; | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters