More FastItemStack
This commit is contained in:
@@ -5,16 +5,11 @@ import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* FastItemStack contains methods to modify and read items faster than in default bukkit.
|
||||
* <p>
|
||||
* If the ItemStack wrapped is a CraftItemStack, then the instance will be modified, allowing for set methods to work.
|
||||
* <p>
|
||||
* Otherwise, apply() must be called in order to apply the changes.
|
||||
* <p>
|
||||
* apply() <b>will</b> call getItemMeta and setItemMeta which will hurt performance, however this will still be faster.
|
||||
*/
|
||||
public interface FastItemStack {
|
||||
/**
|
||||
@@ -36,15 +31,18 @@ public interface FastItemStack {
|
||||
boolean checkStored);
|
||||
|
||||
/**
|
||||
* Apply the changes made in FastItemStack.
|
||||
* <p>
|
||||
* If the ItemStack was a CraftItemStack, then no code will run - the changes are automatically applied.
|
||||
* <p>
|
||||
* If the ItemStack wasn't a CraftItemStack, then the unwrapped ItemStack's ItemMeta will be applied to the original ItemStack.
|
||||
* <p>
|
||||
* You should <b>always</b> call apply() if you have used any set methods.
|
||||
* Set the item lore.
|
||||
*
|
||||
* @param lore The lore.
|
||||
*/
|
||||
void apply();
|
||||
void setLore(@NotNull List<String> lore);
|
||||
|
||||
/**
|
||||
* Get the item lore.
|
||||
*
|
||||
* @return The lore.
|
||||
*/
|
||||
List<String> getLore();
|
||||
|
||||
/**
|
||||
* Wrap an ItemStack to create a FastItemStack.
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
package com.willfp.eco.proxy.v1_16_R3.fast;
|
||||
|
||||
import com.willfp.eco.core.fast.FastItemStack;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.minecraft.server.v1_16_R3.IChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R3.ItemEnchantedBook;
|
||||
import net.minecraft.server.v1_16_R3.ItemStack;
|
||||
import net.minecraft.server.v1_16_R3.Items;
|
||||
import net.minecraft.server.v1_16_R3.NBTBase;
|
||||
import net.minecraft.server.v1_16_R3.NBTTagCompound;
|
||||
import net.minecraft.server.v1_16_R3.NBTTagList;
|
||||
import net.minecraft.server.v1_16_R3.NBTTagString;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.util.CraftChatMessage;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.util.CraftMagicNumbers;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class EcoFastItemStack implements FastItemStack {
|
||||
@@ -67,6 +76,55 @@ public class EcoFastItemStack implements FastItemStack {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLore(@Nullable final List<String> lore) {
|
||||
List<String> jsonLore = new ArrayList<>();
|
||||
if (lore != null) {
|
||||
for (String s : lore) {
|
||||
jsonLore.add(ComponentSerializer.toString(TextComponent.fromLegacyText(s)));
|
||||
}
|
||||
}
|
||||
|
||||
NBTTagCompound displayTag = handle.a("display");
|
||||
if (!displayTag.hasKey("Lore")) {
|
||||
displayTag.set("Lore", new NBTTagList());
|
||||
}
|
||||
|
||||
NBTTagList loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING);
|
||||
loreTag.clear();
|
||||
for (String s : jsonLore) {
|
||||
loreTag.add(NBTTagString.a(s));
|
||||
}
|
||||
|
||||
apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getLore() {
|
||||
List<String> lore = new ArrayList<>();
|
||||
|
||||
for (String s : this.getLoreJSON()) {
|
||||
IChatBaseComponent component = IChatBaseComponent.ChatSerializer.a(s);
|
||||
lore.add(CraftChatMessage.fromComponent(component));
|
||||
}
|
||||
|
||||
return lore;
|
||||
}
|
||||
|
||||
private List<String> getLoreJSON() {
|
||||
NBTTagCompound displayTag = handle.a("display");
|
||||
|
||||
if (displayTag.hasKey("Lore")) {
|
||||
NBTTagList loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING);
|
||||
List<String> lore = new ArrayList<>(loreTag.size());
|
||||
for (int i = 0; i < loreTag.size(); i++) {
|
||||
lore.add(loreTag.getString(i));
|
||||
}
|
||||
return lore;
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public void apply() {
|
||||
if (!this.isCIS) {
|
||||
bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta());
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
package com.willfp.eco.proxy.v1_17_R1.fast;
|
||||
|
||||
import com.willfp.eco.core.fast.FastItemStack;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.item.EnchantedBookItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_17_R1.util.CraftChatMessage;
|
||||
import org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers;
|
||||
import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class EcoFastItemStack implements FastItemStack {
|
||||
@@ -67,6 +76,55 @@ public class EcoFastItemStack implements FastItemStack {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLore(@Nullable final List<String> lore) {
|
||||
List<String> jsonLore = new ArrayList<>();
|
||||
if (lore != null) {
|
||||
for (String s : lore) {
|
||||
jsonLore.add(ComponentSerializer.toString(TextComponent.fromLegacyText(s)));
|
||||
}
|
||||
}
|
||||
|
||||
CompoundTag displayTag = handle.getOrCreateTagElement("display");
|
||||
if (!displayTag.contains("Lore")) {
|
||||
displayTag.put("Lore", new ListTag());
|
||||
}
|
||||
|
||||
ListTag loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING);
|
||||
loreTag.clear();
|
||||
for (String s : jsonLore) {
|
||||
loreTag.add(StringTag.valueOf(s));
|
||||
}
|
||||
|
||||
apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getLore() {
|
||||
List<String> lore = new ArrayList<>();
|
||||
|
||||
for (String s : this.getLoreJSON()) {
|
||||
Component component = Component.Serializer.fromJson(s);
|
||||
lore.add(CraftChatMessage.fromComponent(component));
|
||||
}
|
||||
|
||||
return lore;
|
||||
}
|
||||
|
||||
private List<String> getLoreJSON() {
|
||||
CompoundTag displayTag = handle.getOrCreateTagElement("display");
|
||||
|
||||
if (displayTag.contains("Lore")) {
|
||||
ListTag loreTag = displayTag.getList("Lore", CraftMagicNumbers.NBT.TAG_STRING);
|
||||
List<String> lore = new ArrayList<>(loreTag.size());
|
||||
for (int i = 0; i < loreTag.size(); i++) {
|
||||
lore.add(loreTag.getString(i));
|
||||
}
|
||||
return lore;
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public void apply() {
|
||||
if (!this.isCIS) {
|
||||
bukkit.setItemMeta(CraftItemStack.asCraftMirror(handle).getItemMeta());
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.spigot;
|
||||
import com.willfp.eco.core.AbstractPacketAdapter;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.display.Display;
|
||||
import com.willfp.eco.core.fast.FastItemStack;
|
||||
import com.willfp.eco.core.integrations.IntegrationLoader;
|
||||
import com.willfp.eco.core.integrations.anticheat.AnticheatManager;
|
||||
import com.willfp.eco.core.integrations.antigrief.AntigriefManager;
|
||||
@@ -43,14 +44,19 @@ import com.willfp.eco.spigot.integrations.mcmmo.McmmoIntegrationImpl;
|
||||
import com.willfp.eco.spigot.recipes.ShapedRecipeListener;
|
||||
import com.willfp.eco.util.BlockUtils;
|
||||
import com.willfp.eco.util.SkullUtils;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class EcoSpigotPlugin extends EcoPlugin {
|
||||
@@ -67,6 +73,19 @@ public abstract class EcoSpigotPlugin extends EcoPlugin {
|
||||
|
||||
BlockBreakProxy blockBreakProxy = this.getProxy(BlockBreakProxy.class);
|
||||
BlockUtils.initialize(blockBreakProxy::breakBlock);
|
||||
|
||||
|
||||
// Run static init with CIS.
|
||||
Inventory inventory = Bukkit.createInventory(null, 9);
|
||||
inventory.addItem(new ItemStack(Material.ACACIA_DOOR));
|
||||
ItemStack testItem = inventory.getItem(0);
|
||||
assert testItem != null;
|
||||
FastItemStack.wrap(testItem);
|
||||
List<String> testLore = Collections.singletonList(StringUtils.format("&e&lTest&r &a&lTest!&r <gradient:000000>123456789</gradient:ffffff>"));
|
||||
|
||||
FastItemStack.wrap(testItem).setLore(testLore);
|
||||
|
||||
Bukkit.getLogger().info(FastItemStack.wrap(testItem).getLore().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user