Skip to content

Commit

Permalink
Merge pull request #693 from ShaneBeee/dev/patch
Browse files Browse the repository at this point in the history
dev/patch - future update
  • Loading branch information
ShaneBeee authored Aug 10, 2024
2 parents 3312f98 + dd255c9 commit 5dfa9b1
Show file tree
Hide file tree
Showing 34 changed files with 1,103 additions and 385 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
compileOnly("org.apache.commons:commons-text:1.10.0")

// NBT-API
implementation("de.tr7zw:item-nbt-api:2.13.1") {
implementation("de.tr7zw:item-nbt-api:2.13.2") {
transitive = false
}

Expand Down Expand Up @@ -100,7 +100,7 @@ shadowJar {
tasks.register('server', Copy) {
from shadowJar
// Change this to wherever you want your jar to build
into '/Users/ShaneBee/Desktop/Server/Skript/1-21/plugins'
into '/Users/ShaneBee/Desktop/Server/Skript/1-21-1/plugins'
}

publishing {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/shanebeestudios/skbee/AddonLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.test.runner.TestMode;
import ch.njol.skript.util.Version;
Expand Down Expand Up @@ -141,6 +142,16 @@ private void loadSkriptElements() {
for (int i = 0; i < finish.length; i++) {
Util.log(" - %s %s%s", finish[i], elementNames[i], finish[i] == 1 ? "" : "s");
}

if (this.config.SETTINGS_DEBUG) {
// Print names of ClassInfos with missing lang entry
Bukkit.getScheduler().runTaskLater(this.plugin, () -> Classes.getClassInfos().forEach(classInfo -> {
Noun name = classInfo.getName();
if (name.toString().contains("types.")) {
Util.log("ClassInfo missing lang entry for: &c%s", name);
}
}), 1);
}
}

private void loadNBTElements() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/shanebeestudios/skbee/SkBee.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.shanebeestudios.skbee.api.structure.StructureManager;
import com.shanebeestudios.skbee.api.util.UpdateChecker;
import com.shanebeestudios.skbee.api.util.Util;
import com.shanebeestudios.skbee.api.wrapper.LazyLocation;
import com.shanebeestudios.skbee.config.BoundConfig;
import com.shanebeestudios.skbee.config.Config;
import com.shanebeestudios.skbee.elements.other.sections.SecRunTaskLater;
Expand All @@ -25,6 +26,7 @@ public class SkBee extends JavaPlugin {

static {
ConfigurationSerialization.registerClass(Bound.class, "Bound");
ConfigurationSerialization.registerClass(LazyLocation.class, "LazyLocation");
}

// Earliest MC Version that SkBee will support
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/shanebeestudios/skbee/api/bound/Bound.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Preconditions;
import com.shanebeestudios.skbee.api.util.WorldUtils;
import com.shanebeestudios.skbee.api.wrapper.LazyLocation;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand All @@ -12,6 +13,7 @@
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -139,11 +141,11 @@ public boolean overlaps(Location l1, Location l2) {
* @param type Type of entity to get
* @return List of loaded entities in bound
*/
public List<Entity> getEntities(Class<? extends Entity> type) {
public @Nullable List<Entity> getEntities(Class<? extends Entity> type) {
World world = getWorld();
if (world == null) return null;
Collection<Entity> nearbyEntities = world.getNearbyEntities(this.boundingBox, entity ->
type.isAssignableFrom(entity.getClass()));
type.isAssignableFrom(entity.getClass()));
return new ArrayList<>(nearbyEntities);
}

Expand All @@ -152,7 +154,7 @@ public List<Entity> getEntities(Class<? extends Entity> type) {
*
* @return List of blocks within bound
*/
public List<Block> getBlocks() {
public @Nullable List<Block> getBlocks() {
World w = getWorld();
if (w == null) return null;
List<Block> array = new ArrayList<>();
Expand Down Expand Up @@ -457,7 +459,11 @@ public void removeMember(UUID member) {
* @param value Value to set
*/
public void setValue(String key, Object value) {
this.values.put(key, value);
if (value instanceof Location location) {
this.values.put(key, new LazyLocation(location));
} else {
this.values.put(key, value);
}
}

/**
Expand All @@ -483,7 +489,9 @@ public void clearValues() {
* @return Value from bound
*/
public Object getValue(String key) {
return this.values.get(key);
Object o = this.values.get(key);
if (o instanceof LazyLocation lazyLocation) return lazyLocation.getLocation();
return o;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,40 @@
public class NBTCustomItemStack extends NBTContainer {

private final ItemStack originalItemStack;
private final boolean useComponents;
private final boolean isCustomData;

public NBTCustomItemStack(ItemStack itemStack, boolean useComponents) {
super(getContainer(NBTItem.convertItemtoNBT(itemStack), useComponents).toString());
public NBTCustomItemStack(ItemStack itemStack, boolean isCustomData, boolean isVanilla, boolean isFull) {
super(getInitialContainer(itemStack, isCustomData, isVanilla, isFull).toString());
this.originalItemStack = itemStack;
this.useComponents = useComponents;
this.isCustomData = isCustomData;
}

private static NBTCompound getContainer(NBTContainer itemContainer, boolean useComponents) {
NBTCompound componentsContainer = itemContainer.getOrCreateCompound(NBTApi.TAG_NAME);
if (useComponents) {
return componentsContainer;
private static NBTCompound getInitialContainer(ItemStack itemStack, boolean isCustomData, boolean isVanilla, boolean isFull) {
NBTCompound nbtContainer;
if (isVanilla) {
nbtContainer = NBTReflection.getVanillaNBT(itemStack);
} else {
nbtContainer = NBTItem.convertItemtoNBT(itemStack);
}
if (nbtContainer == null) nbtContainer = new NBTContainer();
return getContainer(nbtContainer, isCustomData, isFull);
}

private static NBTCompound getContainer(NBTCompound itemContainer, boolean isCustomData, boolean isFull) {
if (isFull) return itemContainer;
NBTCompound componentsContainer = itemContainer.getOrCreateCompound(NBTApi.TAG_NAME);
if (isCustomData) {
return componentsContainer.getOrCreateCompound("minecraft:custom_data");
} else {
return componentsContainer;
}
}

@Override
protected void saveCompound() {
super.saveCompound();
NBTContainer originalItemContainer = NBTItem.convertItemtoNBT(this.originalItemStack.clone());
NBTCompound components = getContainer(originalItemContainer, this.useComponents);
NBTCompound components = getContainer(originalItemContainer, this.isCustomData, false);
components.clearNBT();
components.mergeCompound(this);
ItemStack itemStack = NBTItem.convertNBTtoItem(originalItemContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class NBTCustomItemType extends NBTCustomItemStack {

private final ItemType itemType;

public NBTCustomItemType(ItemType itemType, boolean useComponents) {
super(itemType.getRandom(), useComponents);
public NBTCustomItemType(ItemType itemType, boolean isCustomData, boolean isVanilla, boolean isFull) {
super(itemType.getRandom(), isCustomData, isVanilla, isFull);
this.itemType = itemType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class NBTCustomSlot extends NBTCustomItemStack {

private final Slot slot;

public NBTCustomSlot(Slot slot, boolean useComponents) {
super(Objects.requireNonNull(slot.getItem()), useComponents);
public NBTCustomSlot(Slot slot, boolean isCustomData, boolean isVanilla, boolean isFull) {
super(Objects.requireNonNull(slot.getItem()), isCustomData, isVanilla, isFull);
this.slot = slot;
}

Expand Down
98 changes: 98 additions & 0 deletions src/main/java/com/shanebeestudios/skbee/api/nbt/NBTReflection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.shanebeestudios.skbee.api.nbt;

import com.shanebeestudios.skbee.SkBee;
import com.shanebeestudios.skbee.api.reflection.ReflectionUtils;
import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTContainer;
import de.tr7zw.changeme.nbtapi.NBTItem;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressWarnings({"SequencedCollectionMethodCanBeUsed", "CallToPrintStackTrace", "DataFlowIssue"})
public class NBTReflection {

private static final boolean DEBUG = SkBee.getPlugin().getPluginConfig().SETTINGS_DEBUG;

// Classes
private static Class<?> CRAFT_ITEM_STACK_CLASS;

// Fields/Objects
private static Object CODEC;
private static Object REGISTERY_ACCESS;
private static Object NBT_OPS_INSTANCE;

// Methods
private static Method GET_COMPONENTS_METHOD;
private static Method ENCODE_METHOD;
private static Method GET_OR_ELSE;
private static Method CREATE_SERIALIZER_METHOD;

// Constructors
private static Constructor<?> NBT_COMPOUND_CONSTRUCTOR;

static {
try {
// Classes
CRAFT_ITEM_STACK_CLASS = ReflectionUtils.getOBCClass("inventory.CraftItemStack");
Class<?> compoundTag = ReflectionUtils.getNMSClass("net.minecraft.nbt.CompoundTag");
Class<?> craftWorld = ReflectionUtils.getOBCClass("CraftWorld");
Class<?> dataComponentMap = ReflectionUtils.getNMSClass("net.minecraft.core.component.DataComponentMap");
Class<?> dataResult = ReflectionUtils.getNMSClass("com.mojang.serialization.DataResult");
Class<?> dynamicOps = ReflectionUtils.getNMSClass("com.mojang.serialization.DynamicOps");
Class<?> encoder = ReflectionUtils.getNMSClass("com.mojang.serialization.Encoder");
Class<?> holderLookup = ReflectionUtils.getNMSClass("net.minecraft.core.HolderLookup$Provider");
Class<?> itemStack = ReflectionUtils.getNMSClass("net.minecraft.world.item.ItemStack");
Class<?> level = ReflectionUtils.getNMSClass("net.minecraft.world.level.Level");
Class<?> nbtOps = ReflectionUtils.getNMSClass("net.minecraft.nbt.NbtOps");

// Fields/Objects
CODEC = ReflectionUtils.getField("CODEC", dataComponentMap, null);
Object nmsWorld = craftWorld.getDeclaredMethod("getHandle").invoke(Bukkit.getWorlds().get(0));
REGISTERY_ACCESS = level.getDeclaredMethod("registryAccess").invoke(nmsWorld);
NBT_OPS_INSTANCE = ReflectionUtils.getField("INSTANCE", nbtOps, null);

// Methods
GET_COMPONENTS_METHOD = itemStack.getDeclaredMethod("getComponents");
ENCODE_METHOD = encoder.getMethod("encode", Object.class, dynamicOps, Object.class);
GET_OR_ELSE = dataResult.getDeclaredMethod("getOrThrow");
CREATE_SERIALIZER_METHOD = holderLookup.getDeclaredMethod("createSerializationContext", dynamicOps);

// Constructors
NBT_COMPOUND_CONSTRUCTOR = compoundTag.getDeclaredConstructor();

} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
if (DEBUG) e.printStackTrace();
}
}

/**
* Get the vanilla version of NBT of an item
* <br>This will show components which don't normally show in NBT
*
* @param itemStack Item to grab NBT from
* @return Vanilla NBT of item
*/
@SuppressWarnings({"deprecation"})
public static NBTCompound getVanillaNBT(ItemStack itemStack) {
try {
Object nmsItem = ReflectionUtils.getField("handle", CRAFT_ITEM_STACK_CLASS, itemStack);
Object components = GET_COMPONENTS_METHOD.invoke(nmsItem);
Object serial = CREATE_SERIALIZER_METHOD.invoke(REGISTERY_ACCESS, NBT_OPS_INSTANCE);
Object newNBTCompound = NBT_COMPOUND_CONSTRUCTOR.newInstance();

Object encoded = ENCODE_METHOD.invoke(CODEC, components, serial, newNBTCompound);
Object nmsNbt = GET_OR_ELSE.invoke(encoded);
NBTCompound itemNbt = NBTItem.convertItemtoNBT(itemStack);
itemNbt.getOrCreateCompound("components").mergeCompound(new NBTContainer(nmsNbt));
return itemNbt;
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
if (DEBUG) e.printStackTrace();
return new NBTContainer();
}
}

}
46 changes: 46 additions & 0 deletions src/main/java/com/shanebeestudios/skbee/api/util/ItemUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.shanebeestudios.skbee.api.util;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.registrations.Classes;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ItemUtils {

Expand Down Expand Up @@ -67,4 +71,46 @@ public static String attributeModifierToString(AttributeModifier attributeModifi
return builder.toString();
}

/**
* Add ItemTypes to a list of ItemStacks
* <br>This will split up oversized stacks as well
*
* @param itemTypes List of ItemTypes to add
* @param itemStacks Option list of ItemStacks to modify
* @return List of ItemStacks split up correctly
*/
public static List<ItemStack> addItemTypesToList(List<ItemType> itemTypes, List<ItemStack> itemStacks) {
List<ItemStack> originalList = itemStacks != null ? new ArrayList<>(itemStacks) : new ArrayList<>();
ItemStack[] buffer = originalList.toArray(new ItemStack[1000]);
for (ItemType itemType : itemTypes) {
// Split up oversized stacks
itemType.addTo(buffer);
}
List<ItemStack> newList = new ArrayList<>();
for (ItemStack itemStack : buffer) {
if (itemStack != null && !itemStack.isEmpty()) newList.add(itemStack);
}
return newList;
}

/**
* Remove list of ItemTypes from a list of ItemStacks
* <br>This will split up oversized stacks as well
*
* @param itemStacks List of ItemStacks to have ItemTypes removed from
* @param itemTypes List of ItemTypes to remove
* @return Updated list of ItemStacks
*/
public static List<ItemStack> removeItemTypesFromList(List<ItemStack> itemStacks, List<ItemType> itemTypes) {
List<ItemStack> copyList = new ArrayList<>(itemStacks);
for (ItemType itemType : itemTypes) {
itemType.removeFrom(copyList);
}
List<ItemStack> newItems = new ArrayList<>();
for (ItemStack itemStack : copyList) {
if (itemStack != null && !itemStack.isEmpty()) newItems.add(itemStack);
}
return newItems;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class ComponentWrapper {

// STATIC
private static final boolean HAS_SIDES = Skript.classExists("org.bukkit.block.sign.SignSide");
/**
* Check if ItemMeta supports 'itemName' ('item_name' component
*/
public static final boolean HAS_ITEM_NAME = Skript.methodExists(ItemMeta.class, "itemName");

/**
* Create an empty component
Expand Down Expand Up @@ -521,17 +525,6 @@ public void setEntityName(Entity entity, boolean alwaysVisible) {
}
}

/**
* Set the name of an item
*
* @param itemType Item to change name
*/
public void setItemName(ItemType itemType) {
ItemMeta itemMeta = itemType.getItemMeta();
itemMeta.displayName(this.component);
itemType.setItemMeta(itemMeta);
}

/**
* Set the name of the inventory
* <p>NOTE: This is not permanent, this will just rename the open inventory view</p>
Expand Down
Loading

0 comments on commit 5dfa9b1

Please sign in to comment.