Attributes | Modify and add attributes to any item easily.
2.0Attributes is a useful liberary to modify the NBT-data for items.
Attributes
Attributes is a useful library to modify the NBT-data for items like adding attack damage, speed, and much more.
Attributes does now use Reflection to make it compitable with any server version (and NMS has been removed).
You can find a list of useful attribute names to use which are currently displayed on the GitHub repository.
How does it work?
1. Attributes receives input from your static call and saves the information to use it to apply on the item.
2. Attributes clones the items and adds the attributes with the data you provided and returns the item.
3. You do now have an item with a custom attribute with the amount you have entered in the static call.
Developers
Attributes has built-in methods for developers to let them easily create and edit attributes with NMS in items.
Get information about Attributes' source-code and how to use these built-in methods on the GitHub repository.
Library
Here is the amazing library class that you can use in your own resources to edit attributes of items.
Click here.
package nl.marido.attributes;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
public class Reflection {
private static String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
private static Class<?> craftitemstackclass, itemstackclass, nbttagcompoundclass, nbttaglistclass;
private static Class<?> nbttagstringclass, nbttagintclass, nbtbaseclass;
private static Constructor<?> nbttagstringconstructor, nbttagintconstructor;
private static Method nbttagcompoundsetmethod, nbttaglistaddmethod, itemstacksettagmethod;
static {
try {
craftitemstackclass = getObcClass("inventory.CraftItemStack");
itemstackclass = getNmsClass("ItemStack");
nbttagcompoundclass = getNmsClass("NBTTagCompound");
nbttaglistclass = getNmsClass("NBTTagList");
nbttagstringclass = getNmsClass("NBTTagString");
nbttagintclass = getNmsClass("NBTTagInt");
nbtbaseclass = getNmsClass("NBTBase");
nbttagintconstructor = nbttagintclass.getConstructor(int.class);
nbttagstringconstructor = nbttagstringclass.getConstructor(String.class);
nbttagcompoundsetmethod = nbttagcompoundclass.getMethod("set", String.class, nbtbaseclass);
nbttaglistaddmethod = nbttaglistclass.getMethod("add", nbtbaseclass);
itemstacksettagmethod = itemstackclass.getMethod("setTag", nbttagcompoundclass);
} catch (Exception error) {
error.printStackTrace();
}
}
public static ItemStack addAttribute(ItemStack item, String attribute, int amount) {
try {
Object nmsitem = craftitemstackclass.getMethod("asNMSCopy", ItemStack.class).invoke(craftitemstackclass, item);
Object compound = itemstackclass.getMethod("getTag").invoke(itemstackclass.cast(nmsitem));
if (compound == null) {
compound = nbttagcompoundclass.newInstance();
itemstacksettagmethod.invoke(nmsitem, nbttagcompoundclass.cast(compound));
}
Object modifiers = nbttaglistclass.newInstance();
Object details = nbttagcompoundclass.newInstance();
nbttagcompoundsetmethod.invoke(details, "AttributeName", nbttagstringconstructor.newInstance(attribute));
nbttagcompoundsetmethod.invoke(details, "Name", nbttagstringconstructor.newInstance(attribute));
nbttagcompoundsetmethod.invoke(details, "Amount", nbttagintconstructor.newInstance(amount));
nbttagcompoundsetmethod.invoke(details, "Operation", nbttagintconstructor.newInstance(0));
nbttagcompoundsetmethod.invoke(details, "UUIDLeast", nbttagintconstructor.newInstance(894654));
nbttagcompoundsetmethod.invoke(details, "UUIDMost", nbttagintconstructor.newInstance(2872));
nbttaglistaddmethod.invoke(modifiers, details);
nbttagcompoundsetmethod.invoke(compound, "AttributeModifiers", nbttaglistclass.cast(modifiers));
itemstacksettagmethod.invoke(nmsitem, compound);
item = (ItemStack) craftitemstackclass.getMethod("asBukkitCopy", itemstackclass).invoke(craftitemstackclass, itemstackclass.cast(nmsitem));
} catch (Exception error) {
error.printStackTrace();
}
return item;
}
public static String getAttributes(ItemStack item) {
try {
Object nmsitem = craftitemstackclass.getMethod("asNMSCopy", ItemStack.class).invoke(craftitemstackclass, item);
Object compound = itemstackclass.getMethod("getTag").invoke(itemstackclass.cast(nmsitem));
if (compound == null) {
compound = nbttagcompoundclass.newInstance();
itemstacksettagmethod.invoke(nmsitem, nbttagcompoundclass.cast(compound));
}
Object details = nbttagcompoundclass.newInstance();
return details.toString();
} catch (Exception error) {
error.printStackTrace();
}
return null;
}
public static Class<?> getNmsClass(String classname) {
String fullname = "net.minecraft.server." + version + classname;
Class<?> realclass = null;
try {
realclass = Class.forName(fullname);
} catch (Exception error) {
error.printStackTrace();
}
return realclass;
}
public static Class<?> getObcClass(String classname) {
String fullname = "org.bukkit.craftbukkit." + version + classname;
Class<?> realclass = null;
try {
realclass = Class.forName(fullname);
} catch (Exception error) {
error.printStackTrace();
}
return realclass;
}
}
Support
Need some help with implementing it or simply encountering issues with the library of me? Do not hesitate to contact me on my brand new epic website or Discord (Marido#0001). I will try to help you as soon as possible when I have the time for it.
Similar projects
Plugin →Let Me Despawn
Improves performance by tweaking mob despawn rules. Say bye to pesky unintentional persistent mobs.
25MSueta
JourneyMap
Real-time map used for mapping in-game or your browser as you explore.
14.7MSueta
Emotecraft
Create your own emotes in Minecraft.
7.5MSueta
Simple Tpa
A simple tpa plugin which sends requests
5.4MSueta
Common Network
Forge and Fabric Unified Networking Solution
3.9MSueta
Set Home
A lightweight plugin for players to set their homes.
3.8MSueta