mirror of
https://github.com/EarthMC/QuickShop-Reremake.git
synced 2026-11-24 17:39:52 -05:00
Better nms handling
Former-commit-id: 87395c6f56bfab82b464c3a75c92486b62009a30 [formerly cd489dd86d7042986715269788b7a328c76a5e5d] Former-commit-id: dd7e26f891c222a6d91be386fc41dd5603b1016c
This commit is contained in:
1 parent
fcfe8fc588
commit
96cae3261f
8 files changed
+42
-472
No files matched your search
@@ -1,43 +1,42 @@
|
||||
package org.maxgamer.quickshop.Util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.maxgamer.quickshop.QuickShop;
|
||||
|
||||
public class ItemNMS {
|
||||
public String getItemJSON(ItemStack iStack) {
|
||||
ItemStack itemStack = iStack.clone();
|
||||
String name = Bukkit.getServer().getClass().getPackage().getName();
|
||||
String version = name.substring(name.lastIndexOf('.') + 1);
|
||||
Class<?> nmsClass;
|
||||
try {
|
||||
nmsClass = Class.forName("org.maxgamer.quickshop.Nms." + version);
|
||||
} catch (ClassNotFoundException e2) {
|
||||
return null;
|
||||
}
|
||||
Object obj;
|
||||
try {
|
||||
obj = nmsClass.newInstance();
|
||||
} catch (InstantiationException e1) { //NMS Class not found, Use old preview
|
||||
return null;
|
||||
} catch (IllegalAccessException e1) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Method method = nmsClass.getMethod("getNBTJson", ItemStack.class);
|
||||
String itemJSON = (String) method.invoke(obj, new Object[] { itemStack });
|
||||
return itemJSON;
|
||||
} catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException
|
||||
| IllegalAccessException e) {
|
||||
// do somethings
|
||||
QuickShop.instance.getLogger().info("A error happend:");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public abstract class ItemNMS {
|
||||
private static final String nmsVersion;
|
||||
|
||||
private static Method craftItemStack_asNMSCopyMethod;
|
||||
private static Class<?> nbtTagCompoundClass;
|
||||
private static Method itemStack_saveMethod;
|
||||
|
||||
static {
|
||||
String name = Bukkit.getServer().getClass().getPackage().getName();
|
||||
nmsVersion = name.substring(name.lastIndexOf('.') + 1);
|
||||
|
||||
try {
|
||||
craftItemStack_asNMSCopyMethod = Class.forName("org.bukkit.craftbukkit." + nmsVersion + ".inventory.CraftItemStack")
|
||||
.getDeclaredMethod("asNMSCopy", ItemStack.class);
|
||||
|
||||
nbtTagCompoundClass = Class.forName("net.minecraft.server." + nmsVersion + ".NBTTagCompound");
|
||||
|
||||
itemStack_saveMethod = Class.forName("net.minecraft.server." + nmsVersion + ".ItemStack")
|
||||
.getDeclaredMethod("save", nbtTagCompoundClass);
|
||||
|
||||
} catch (Throwable t) {
|
||||
QuickShop.instance.getLogger().info("A error happend:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String saveJsonfromNMS(ItemStack bStack) throws Throwable {
|
||||
Object mcStack = craftItemStack_asNMSCopyMethod.invoke(null, bStack);
|
||||
Object nbtTagCompound = nbtTagCompoundClass.newInstance();
|
||||
|
||||
itemStack_saveMethod.invoke(mcStack, nbtTagCompound);
|
||||
return nbtTagCompound.toString();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@@ -560,21 +559,16 @@ public class Util {
|
||||
}
|
||||
//Use NMS
|
||||
public static void sendItemholochat(ItemStack itemStack, Player player, String normalText) {
|
||||
ItemNMS nms = new ItemNMS();
|
||||
String json = nms.getItemJSON(itemStack);
|
||||
if(json == null) {
|
||||
sendItemholochatAsNormaly(itemStack, player, normalText);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
TextComponent normalmessage = new TextComponent(normalText+" "+MsgUtil.getMessage("menu.preview"));
|
||||
ComponentBuilder cBuilder = new ComponentBuilder(json);
|
||||
HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_ITEM, cBuilder.create());
|
||||
normalmessage.setHoverEvent(he);
|
||||
player.spigot().sendMessage(normalmessage);
|
||||
}catch (Exception e) {
|
||||
sendItemholochatAsNormaly(itemStack, player, normalText);
|
||||
}
|
||||
try {
|
||||
String json = ItemNMS.saveJsonfromNMS(itemStack);
|
||||
TextComponent normalmessage = new TextComponent(normalText+" "+MsgUtil.getMessage("menu.preview"));
|
||||
ComponentBuilder cBuilder = new ComponentBuilder(json);
|
||||
HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_ITEM, cBuilder.create());
|
||||
normalmessage.setHoverEvent(he);
|
||||
player.spigot().sendMessage(normalmessage);
|
||||
} catch (Throwable t) {
|
||||
sendItemholochatAsNormaly(itemStack, player, normalText);
|
||||
}
|
||||
}
|
||||
|
||||
// Without NMS
|
||||
|
||||
Reference in new issue
Block a user