+ TellrawJson NBT 优化

This commit is contained in:
坏黑 2019-01-06 15:36:40 +08:00
parent 98dc366a03
commit 6fea75100f
4 changed files with 101 additions and 6 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.skymc</groupId> <groupId>me.skymc</groupId>
<artifactId>TabooLib</artifactId> <artifactId>TabooLib</artifactId>
<version>4.68</version> <version>4.69</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -25,10 +25,28 @@ import java.util.stream.Collectors;
*/ */
public class TellrawJson { public class TellrawJson {
private int bukkitVersion = TabooLib.getVersionNumber();
private List<BaseComponent> components = new ArrayList<>(); private List<BaseComponent> components = new ArrayList<>();
private List<BaseComponent> componentsLatest = new ArrayList<>(); private List<BaseComponent> componentsLatest = new ArrayList<>();
private Map<String, String[]> itemTag = new HashMap<>(); private Map<String, String[]> itemTag = new HashMap<>();
private int bukkitVersion = TabooLib.getVersionNumber(); private List<String> nbtWhitelist = ArrayUtils.asList(
// 附魔
"ench",
// 附魔书
"StoredEnchantments",
// 展示
"display",
// 属性
"AttributeModifiers",
// 药水
"Potion",
// 特殊药水
"CustomPotionEffects",
// 隐藏标签
"HideFlags",
// 方块标签
"BlockEntityTag"
);
TellrawJson() { TellrawJson() {
} }
@ -101,6 +119,7 @@ public class TellrawJson {
} }
public TellrawJson hoverItem(ItemStack itemStack, boolean supportVersion) { public TellrawJson hoverItem(ItemStack itemStack, boolean supportVersion) {
itemStack = TellrawCreator.getAbstractTellraw().optimizeNBT(itemStack, nbtWhitelist);
BaseComponent[] itemComponentCurrentVersion = new ComponentBuilder(TellrawCreator.getAbstractTellraw().getItemComponent(itemStack, TellrawVersion.CURRENT_VERSION)).create(); BaseComponent[] itemComponentCurrentVersion = new ComponentBuilder(TellrawCreator.getAbstractTellraw().getItemComponent(itemStack, TellrawVersion.CURRENT_VERSION)).create();
getLatestComponent().forEach(component -> component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, itemComponentCurrentVersion))); getLatestComponent().forEach(component -> component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, itemComponentCurrentVersion)));
if (supportVersion) { if (supportVersion) {
@ -175,4 +194,20 @@ public class TellrawJson {
public BaseComponent[] getComponents() { public BaseComponent[] getComponents() {
return components.toArray(new BaseComponent[0]); return components.toArray(new BaseComponent[0]);
} }
public int getBukkitVersion() {
return bukkitVersion;
}
public List<BaseComponent> getComponentsLatest() {
return componentsLatest;
}
public Map<String, String[]> getItemTag() {
return itemTag;
}
public List<String> getNBTWhitelist() {
return nbtWhitelist;
}
} }

View File

@ -4,6 +4,8 @@ import me.skymc.taboolib.json.tellraw.TellrawVersion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.List;
/** /**
* @Author 坏黑 * @Author 坏黑
* @Since 2018-11-07 22:52 * @Since 2018-11-07 22:52
@ -16,4 +18,8 @@ public interface AbstractTellraw {
String getItemComponent(ItemStack itemStack, TellrawVersion version); String getItemComponent(ItemStack itemStack, TellrawVersion version);
ItemStack optimizeNBT(ItemStack itemStack, List<String> nbtWhitelist);
ItemStack optimizeShulkerBox(ItemStack itemStack);
} }

View File

@ -3,14 +3,16 @@ package me.skymc.taboolib.json.tellraw.internal;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.common.packet.TPacketHandler; import me.skymc.taboolib.common.packet.TPacketHandler;
import me.skymc.taboolib.common.util.SimpleReflection; import me.skymc.taboolib.common.util.SimpleReflection;
import me.skymc.taboolib.inventory.ItemUtils;
import me.skymc.taboolib.json.tellraw.TellrawVersion; import me.skymc.taboolib.json.tellraw.TellrawVersion;
import net.minecraft.server.v1_8_R3.IChatBaseComponent; import net.minecraft.server.v1_8_R3.*;
import net.minecraft.server.v1_8_R3.NBTTagCompound; import org.bukkit.Material;
import net.minecraft.server.v1_8_R3.NBTTagList; import org.bukkit.block.ShulkerBox;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -43,6 +45,58 @@ public class InternalTellraw implements AbstractTellraw {
return nbtToString(CraftItemStack.asNMSCopy(itemStack).save(new NBTTagCompound()), version); return nbtToString(CraftItemStack.asNMSCopy(itemStack).save(new NBTTagCompound()), version);
} }
@Override
public ItemStack optimizeNBT(ItemStack itemStack, List<String> nbtWhitelist) {
Object nmsItem = CraftItemStack.asNMSCopy(itemStack);
if (((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).hasTag()) {
Object nbtTag = new NBTTagCompound();
Map<String, NBTBase> mapNew = (Map) SimpleReflection.getFieldValue(NBTTagCompound.class, nbtTag, "map");
Map<String, NBTBase> mapOrigin = (Map) SimpleReflection.getFieldValue(NBTTagCompound.class, ((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).getTag(), "map");
for (Map.Entry<String, NBTBase> entry : mapOrigin.entrySet()) {
if (nbtWhitelist.contains(entry.getKey())) {
mapNew.put(entry.getKey(), entry.getValue());
}
}
((net.minecraft.server.v1_8_R3.ItemStack) nmsItem).setTag((NBTTagCompound) nbtTag);
return CraftItemStack.asBukkitCopy(((net.minecraft.server.v1_8_R3.ItemStack) nmsItem));
}
return itemStack;
}
@Override
public ItemStack optimizeShulkerBox(ItemStack item) {
if (item.getType().name().endsWith("SHULKER_BOX")) {
ItemStack itemClone = item.clone();
BlockStateMeta blockStateMeta = (BlockStateMeta) itemClone.getItemMeta();
ShulkerBox shulkerBox = (ShulkerBox) blockStateMeta.getBlockState();
ItemStack[] contents = shulkerBox.getInventory().getContents();
ItemStack[] contentsClone = new ItemStack[contents.length];
for (int i = 0; i < contents.length; i++) {
ItemStack content = contents[i];
if (!ItemUtils.isNull(content)) {
ItemStack contentClone = new ItemStack(Material.STONE, content.getAmount(), content.getDurability());
if (content.getItemMeta().hasDisplayName()) {
ItemUtils.setName(contentClone, content.getItemMeta().getDisplayName());
}
contentsClone[i] = contentClone;
}
}
shulkerBox.getInventory().setContents(contentsClone);
blockStateMeta.setBlockState(shulkerBox);
itemClone.setItemMeta(blockStateMeta);
return itemClone;
} else if (item.getItemMeta() instanceof BlockStateMeta && ((BlockStateMeta) item.getItemMeta()).getBlockState() instanceof InventoryHolder) {
ItemStack itemClone = item.clone();
BlockStateMeta blockStateMeta = (BlockStateMeta) itemClone.getItemMeta();
InventoryHolder inventoryHolder = (InventoryHolder) blockStateMeta.getBlockState();
inventoryHolder.getInventory().clear();
blockStateMeta.setBlockState((org.bukkit.block.BlockState) inventoryHolder);
itemClone.setItemMeta(blockStateMeta);
return itemClone;
}
return item;
}
private String nbtToString(Object nbtTagCompound, TellrawVersion version) { private String nbtToString(Object nbtTagCompound, TellrawVersion version) {
StringBuilder builder = new StringBuilder("{"); StringBuilder builder = new StringBuilder("{");
Map map = (Map) SimpleReflection.getFieldValue(NBTTagCompound.class, nbtTagCompound, "map"); Map map = (Map) SimpleReflection.getFieldValue(NBTTagCompound.class, nbtTagCompound, "map");