Skip to content

Instantly share code, notes, and snippets.

@book000

book000/NMSManager.java

Last active May 5, 2021
Embed
What would you like to do?
/*
MIT License
Copyright (c) 2021 Tomachi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.jaoafa.mymaid4.lib;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// From https://github.com/jaoafa/MyMaid4/blob/master/src/main/java/com/jaoafa/mymaid4/lib/NMSManager.java
public class NMSManager {
/**
* ItemStackの文字列NBTタグを返します
*
* @param is ItemStack
*
* @return 文字列NBTタグ
*/
public static String getNBT(ItemStack is) {
try {
Class<?> CraftItemStack = getNMSClass("org.bukkit.craftbukkit.%s.inventory.CraftItemStack");
assert CraftItemStack != null;
Method asNMSCopy = CraftItemStack.getDeclaredMethod("asNMSCopy", ItemStack.class);
Class<?> ItemStack = getNMSClass("net.minecraft.server.%s.ItemStack");
assert ItemStack != null;
Method getTag = ItemStack.getDeclaredMethod("getTag");
Class<?> NBTTagCompound = getNMSClass("net.minecraft.server.%s.NBTTagCompound");
assert NBTTagCompound != null;
Method toString = NBTTagCompound.getDeclaredMethod("toString");
Object ItemStackObj = asNMSCopy.invoke(null, is);
Object NBTTagCompoundObj = getTag.invoke(ItemStackObj);
if (NBTTagCompoundObj == null) return "{}";
return (String) toString.invoke(NBTTagCompoundObj);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | NullPointerException e) {
MyMaidLibrary.reportError(NMSManager.class, e);
}
return null;
}
private static Class<?> getNMSClass(String nmsClass) {
String version = null;
Pattern pattern = Pattern.compile("net\\.minecraft\\.(?:server)?\\.(v(?:\\d+_)+R\\d+)");
for (Package p : Package.getPackages()) {
String name = p.getName();
Matcher m = pattern.matcher(name);
if (m.matches()) version = m.group(1);
}
if (version == null) return null;
try {
return Class.forName(String.format(nmsClass, version));
} catch (ClassNotFoundException e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment