Skip to content

Commit

Permalink
update to 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
myunco committed Dec 1, 2021
1 parent ec6a8a3 commit 0a6a460
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
63 changes: 13 additions & 50 deletions src/ml/mcos/liteitemshow/LiteItemShow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ml.mcos.liteitemshow;

import ml.mcos.liteitemshow.metrics.Metrics;
import ml.mcos.liteitemshow.nbt.NMS;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
Expand All @@ -18,47 +19,27 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Method;
import java.util.List;

public class LiteItemShow extends JavaPlugin implements Listener {
public String keyword;
Class<?> NMS_NBTTagCompound;
Class<?> OBC_CraftItemStack;
Class<?> NMS_ItemStack;
Method OBC_CraftItemStack_asNMSCopy;
Method NMS_ItemStack_save;
Method NMS_ItemStack_a;
boolean fixedServer;
int MCVersion;
int mcVersion;
NMS nms;

@Override
public void onEnable() {
initConfig();
String OBCPackage = getServer().getClass().getPackage().getName();
String version = OBCPackage.substring(23);
try {
MCVersion = Integer.parseInt(version.split("_")[1]);
OBC_CraftItemStack = Class.forName(OBCPackage + ".inventory.CraftItemStack");
if (MCVersion < 17) {
String NMSPackage = "net.minecraft.server." + version;
NMS_NBTTagCompound = Class.forName(NMSPackage + ".NBTTagCompound");
NMS_ItemStack = Class.forName(NMSPackage + ".ItemStack");
if (MCVersion == 12) {
fixedServer = getServer().getName().equals("CatServer") || getServer().getName().equals("Paper");
NMS_ItemStack_a = NMS_ItemStack.getMethod("a");
}
} else {
NMS_NBTTagCompound = Class.forName("net.minecraft.nbt.NBTTagCompound");
NMS_ItemStack = Class.forName("net.minecraft.world.item.ItemStack");
}
OBC_CraftItemStack_asNMSCopy = OBC_CraftItemStack.getMethod("asNMSCopy", ItemStack.class);
NMS_ItemStack_save = NMS_ItemStack.getMethod("save", NMS_NBTTagCompound);
mcVersion = Integer.parseInt(getServer().getBukkitVersion().replace('-', '.').split("\\.")[1]);
getLogger().info("minecraft version: 1." + mcVersion);
fixedServer = getServer().getName().equals("CatServer") || getServer().getName().equals("Paper");
nms = new NMS(this);
getServer().getPluginManager().registerEvents(this, this);
new Metrics(this, 13325);
} catch (Exception e) {
e.printStackTrace();
getLogger().info("[LiteItemShow] 插件加载出错:不受支持的服务端版本");
getLogger().severe("插件加载出错:不受支持的服务端版本");
}
}

Expand Down Expand Up @@ -98,15 +79,6 @@ private void sendMessage(CommandSender sender, String msg) {
sender.sendMessage("§8[§3LiteItemShow§8] " + msg);
}

private String getItemNBT(ItemStack item) {
try {
return NMS_ItemStack_save.invoke(OBC_CraftItemStack_asNMSCopy.invoke(OBC_CraftItemStack, item), NMS_NBTTagCompound.newInstance()).toString();
} catch (Exception e) {
e.printStackTrace();
}
return "{id:\"minecraft:air\"}";
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void asyncPlayerChatEvent(AsyncPlayerChatEvent event) {
String msg = event.getMessage();
Expand All @@ -116,7 +88,7 @@ public void asyncPlayerChatEvent(AsyncPlayerChatEvent event) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
event.setCancelled(true);
String nbt = getItemNBT(item);
String nbt = nms.getItemNBT(item);
ComponentBuilder builder = new ComponentBuilder("");
builder.append(TextComponent.fromLegacyText(getTextLeft(event.getFormat(), "%2$s").replaceAll("%1\\$s", event.getPlayer().getDisplayName())));
TextComponent itemInfo = new TextComponent("[");
Expand All @@ -126,27 +98,27 @@ public void asyncPlayerChatEvent(AsyncPlayerChatEvent event) {
itemInfo.addExtra(component);
}
} else {
String key = MCVersion == 12 ? getTranslateKey(item) : getTranslateKey(item.getType().getKey().toString(), item.getType().isBlock());
String key = mcVersion == 12 ? nms.getTranslateKey(item) : getTranslateKey(item.getType().getKey().toString(), item.getType().isBlock());
itemInfo.addExtra(new TranslatableComponent(key));
}
itemInfo.addExtra("]");
itemInfo.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new BaseComponent[]{new TextComponent(nbt)}));
if (msg.equals(keyword)) {
if (MCVersion == 12) {
if (mcVersion == 12) {
builder.append(new BaseComponent[]{itemInfo});
} else {
builder.append(itemInfo);
}
} else {
String left = getTextLeft(msg, keyword);
builder.append(TextComponent.fromLegacyText(left));
if (MCVersion == 12) {
if (mcVersion == 12) {
builder.append(new BaseComponent[]{itemInfo});
} else {
builder.append(itemInfo);
}
String color = org.bukkit.ChatColor.getLastColors(left);
builder.append(TextComponent.fromLegacyText(color + getTextRight(msg, keyword)), MCVersion != 12 || fixedServer ? ComponentBuilder.FormatRetention.NONE : ComponentBuilder.FormatRetention.ALL);
builder.append(TextComponent.fromLegacyText(color + getTextRight(msg, keyword)), mcVersion != 12 || fixedServer ? ComponentBuilder.FormatRetention.NONE : ComponentBuilder.FormatRetention.ALL);
}
BaseComponent[] messages = builder.create();
getServer().spigot().broadcast(builder.create());
Expand All @@ -156,15 +128,6 @@ public void asyncPlayerChatEvent(AsyncPlayerChatEvent event) {
}
}

private String getTranslateKey(ItemStack item) {
try {
return NMS_ItemStack_a.invoke(OBC_CraftItemStack_asNMSCopy.invoke(OBC_CraftItemStack, item)) + ".name";
} catch (Exception e) {
e.printStackTrace();
}
return "tile.air.name";
}

private static String getTranslateKey(String id, boolean isBlock) {
return (isBlock ? "block." : "item.") + id.replace(':', '.');
}
Expand Down
60 changes: 60 additions & 0 deletions src/ml/mcos/liteitemshow/nbt/NMS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ml.mcos.liteitemshow.nbt;

import ml.mcos.liteitemshow.LiteItemShow;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Method;

public class NMS {
Class<?> OBC_CraftItemStack;
Class<?> NMS_NBTTagCompound;
Class<?> NMS_ItemStack;
Method OBC_CraftItemStack_asNMSCopy;
Method NMS_ItemStack_save;
Method NMS_ItemStack_a;
LiteItemShow plugin;
int mcVersion;

public NMS(LiteItemShow plugin) throws Exception {
this.plugin = plugin;
String OBCPackage = plugin.getServer().getClass().getPackage().getName();
String version = OBCPackage.substring(23);
this.mcVersion = Integer.parseInt(version.split("_")[1]);
OBC_CraftItemStack = Class.forName(OBCPackage + ".inventory.CraftItemStack");
OBC_CraftItemStack_asNMSCopy = OBC_CraftItemStack.getMethod("asNMSCopy", ItemStack.class);
if (mcVersion < 17) {
String NMSPackage = "net.minecraft.server." + version;
NMS_NBTTagCompound = Class.forName(NMSPackage + ".NBTTagCompound");
NMS_ItemStack = Class.forName(NMSPackage + ".ItemStack");
if (mcVersion == 12) {
NMS_ItemStack_a = NMS_ItemStack.getMethod("a");
}} else {
NMS_NBTTagCompound = Class.forName("net.minecraft.nbt.NBTTagCompound");
NMS_ItemStack = Class.forName("net.minecraft.world.item.ItemStack");
}
if (mcVersion == 18) {
NMS_ItemStack_save = NMS_ItemStack.getMethod("b", NMS_NBTTagCompound);
} else {
NMS_ItemStack_save = NMS_ItemStack.getMethod("save", NMS_NBTTagCompound);
}
}

public String getItemNBT(ItemStack item) {
try {
return NMS_ItemStack_save.invoke(OBC_CraftItemStack_asNMSCopy.invoke(OBC_CraftItemStack, item), NMS_NBTTagCompound.newInstance()).toString();
} catch (Exception e) {
e.printStackTrace();
}
return "{id:\"minecraft:air\"}";
}

public String getTranslateKey(ItemStack item) {
try {
return NMS_ItemStack_a.invoke(OBC_CraftItemStack_asNMSCopy.invoke(OBC_CraftItemStack, item)) + ".name";
} catch (Exception e) {
e.printStackTrace();
}
return "tile.air.name";
}

}
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: LiteItemShow
main: ml.mcos.liteitemshow.LiteItemShow
version: 1.0.0
version: 1.0.1
api-version: 1.13
author: unco_
commands:
Expand Down

0 comments on commit 0a6a460

Please sign in to comment.