From ed5f1ccb5efad032c9480428406114ed421b5d1a Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 1 Aug 2021 22:09:18 +0100 Subject: [PATCH] Switched to 1.17 mojang mappings and began FastItemStack --- .../willfp/eco/core/fast/FastItemStack.java | 14 +--- .../proxy/v1_16_R3/FastItemStackFactory.java | 14 ++++ .../proxy/v1_16_R3/fast/NMSFastItemStack.java | 12 +--- eco-core/core-nms/v1_17_R1/build.gradle | 15 ++++- .../willfp/eco/proxy/v1_17_R1/AutoCraft.java | 10 +-- .../eco/proxy/v1_17_R1/ChatComponent.java | 45 ++++++------- .../proxy/v1_17_R1/FastItemStackFactory.java | 14 ++++ .../eco/proxy/v1_17_R1/VillagerTrade.java | 7 +- .../v1_17_R1/fast/FastItemStackUtils.java | 43 +++++++++++++ .../proxy/v1_17_R1/fast/NMSFastItemStack.java | 64 +++++++++++++++++++ settings.gradle | 7 ++ 11 files changed, 194 insertions(+), 51 deletions(-) create mode 100644 eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/FastItemStackFactory.java create mode 100644 eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/FastItemStackFactory.java create mode 100644 eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/FastItemStackUtils.java create mode 100644 eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java diff --git a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java index 6e06281a..9b7dbbdd 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java +++ b/eco-api/src/main/java/com/willfp/eco/core/fast/FastItemStack.java @@ -5,7 +5,6 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import java.util.List; import java.util.Map; /** @@ -31,18 +30,11 @@ public interface FastItemStack { boolean checkStored); /** - * Get the lore of the item. + * Get bukkit ItemStack again. * - * @return The lore. + * @return The ItemStack. */ - List getLore(); - - /** - * Set the lore of the item. - * - * @param lore The lore. - */ - void setLore(@NotNull List lore); + ItemStack unwrap(); /** * Wrap an ItemStack to create a FastItemStack. diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/FastItemStackFactory.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/FastItemStackFactory.java new file mode 100644 index 00000000..3889299b --- /dev/null +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/FastItemStackFactory.java @@ -0,0 +1,14 @@ +package com.willfp.eco.proxy.v1_16_R3; + +import com.willfp.eco.core.fast.FastItemStack; +import com.willfp.eco.proxy.FastItemStackFactoryProxy; +import com.willfp.eco.proxy.v1_16_R3.fast.NMSFastItemStack; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public final class FastItemStackFactory implements FastItemStackFactoryProxy { + @Override + public FastItemStack create(@NotNull final ItemStack itemStack) { + return new NMSFastItemStack(itemStack); + } +} diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.java index b91ba9d2..8dcc768d 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.java @@ -8,7 +8,7 @@ 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 org.bukkit.craftbukkit.v1_16_R3.util.CraftChatMessage; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey; import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; @@ -58,14 +58,8 @@ public class NMSFastItemStack implements FastItemStack { } return 0; } - @Override - public List getLore() { - return null; - } - - @Override - public void setLore(@NotNull final List lore) { - CraftChatMessage.fromStringToJSON(lore.get0) + public org.bukkit.inventory.ItemStack unwrap() { + return CraftItemStack.asCraftMirror(handle); } } diff --git a/eco-core/core-nms/v1_17_R1/build.gradle b/eco-core/core-nms/v1_17_R1/build.gradle index e732641e..42f7114f 100644 --- a/eco-core/core-nms/v1_17_R1/build.gradle +++ b/eco-core/core-nms/v1_17_R1/build.gradle @@ -1,6 +1,19 @@ +plugins { + id 'xyz.jpenilla.special-gradle' version '1.0.0-SNAPSHOT' +} + group 'com.willfp' version rootProject.version dependencies { - compileOnly 'org.spigotmc:spigot:1.17-R0.1-SNAPSHOT' + compileOnly 'org.spigotmc:spigot:1.17.1-R0.1-SNAPSHOT:remapped-mojang' +} + +specialGradle { + minecraftVersion.set("1.17") + specialSourceVersion.set("1.10.0") +} + +tasks { + build.dependsOn productionMappedJar } \ No newline at end of file diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/AutoCraft.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/AutoCraft.java index c342db55..a68c9d0a 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/AutoCraft.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/AutoCraft.java @@ -1,8 +1,8 @@ package com.willfp.eco.proxy.v1_17_R1; import com.willfp.eco.proxy.AutoCraftProxy; -import net.minecraft.network.protocol.game.PacketPlayOutAutoRecipe; -import net.minecraft.resources.MinecraftKey; +import net.minecraft.network.protocol.game.ClientboundPlaceGhostRecipePacket; +import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -10,10 +10,10 @@ import java.lang.reflect.Field; public final class AutoCraft implements AutoCraftProxy { @Override public void modifyPacket(@NotNull final Object packet) throws NoSuchFieldException, IllegalAccessException { - PacketPlayOutAutoRecipe recipePacket = (PacketPlayOutAutoRecipe) packet; + ClientboundPlaceGhostRecipePacket recipePacket = (ClientboundPlaceGhostRecipePacket) packet; Field fKey = recipePacket.getClass().getDeclaredField("b"); fKey.setAccessible(true); - MinecraftKey key = (MinecraftKey) fKey.get(recipePacket); - fKey.set(recipePacket, new MinecraftKey(key.getNamespace(), key.getKey() + "_displayed")); + ResourceLocation key = (ResourceLocation) fKey.get(recipePacket); + fKey.set(recipePacket, new ResourceLocation(key.getNamespace(), key.getPath() + "_displayed")); } } diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/ChatComponent.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/ChatComponent.java index d34df90a..f226c89c 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/ChatComponent.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/ChatComponent.java @@ -6,12 +6,12 @@ import com.google.gson.JsonPrimitive; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.willfp.eco.core.display.Display; import com.willfp.eco.proxy.ChatComponentProxy; -import net.minecraft.nbt.MojangsonParser; -import net.minecraft.network.chat.ChatBaseComponent; -import net.minecraft.network.chat.ChatHoverable; -import net.minecraft.network.chat.ChatMessage; -import net.minecraft.network.chat.ChatModifier; -import net.minecraft.network.chat.IChatBaseComponent; +import net.minecraft.nbt.TagParser; +import net.minecraft.network.chat.BaseComponent; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.HoverEvent; +import net.minecraft.network.chat.Style; +import net.minecraft.network.chat.TranslatableComponent; import org.bukkit.Material; import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; @@ -24,11 +24,11 @@ public final class ChatComponent implements ChatComponentProxy { @Override public Object modifyComponent(@NotNull final Object object, @NotNull final Player player) { - if (!(object instanceof IChatBaseComponent chatComponent)) { + if (!(object instanceof Component chatComponent)) { return object; } - for (IChatBaseComponent iChatBaseComponent : chatComponent) { + for (Component iChatBaseComponent : chatComponent) { if (iChatBaseComponent == null) { continue; } @@ -39,30 +39,31 @@ public final class ChatComponent implements ChatComponentProxy { return chatComponent; } - private void modifyBaseComponent(@NotNull final IChatBaseComponent component, + private void modifyBaseComponent(@NotNull final Component component, @NotNull final Player player) { - for (IChatBaseComponent sibling : component.getSiblings()) { + for (Component sibling : component.getSiblings()) { if (sibling == null) { continue; } modifyBaseComponent(sibling, player); } - if (component instanceof ChatMessage) { - Arrays.stream(((ChatMessage) component).getArgs()) - .filter(o -> o instanceof IChatBaseComponent) - .map(o -> (IChatBaseComponent) o) + + if (component instanceof TranslatableComponent baseComponent) { + Arrays.stream(baseComponent.getArgs()) + .filter(o -> o instanceof Component) + .map(o -> (Component) o) .forEach(o -> modifyBaseComponent(o, player)); } - ChatHoverable hoverable = component.getChatModifier().getHoverEvent(); + HoverEvent hoverable = component.getStyle().getHoverEvent(); if (hoverable == null) { return; } - JsonObject jsonObject = hoverable.b(); - JsonElement json = hoverable.b().get("contents"); + JsonObject jsonObject = hoverable.serialize(); + JsonElement json = jsonObject.get("contents"); if (json.getAsJsonObject().get("id") == null) { return; } @@ -81,11 +82,11 @@ public final class ChatComponent implements ChatComponentProxy { jsonObject.remove("contents"); jsonObject.add("contents", json); - ChatHoverable newHoverable = ChatHoverable.a(jsonObject); - ChatModifier modifier = component.getChatModifier(); - modifier = modifier.setChatHoverable(newHoverable); + HoverEvent newHoverable = HoverEvent.deserialize(jsonObject); + Style modifier = component.getStyle(); + modifier = modifier.withHoverEvent(newHoverable); - ((ChatBaseComponent) component).setChatModifier(modifier); + ((BaseComponent) component).setStyle(modifier); } private static ItemStack getFromTag(@NotNull final String jsonTag, @@ -105,7 +106,7 @@ public final class ChatComponent implements ChatComponentProxy { net.minecraft.world.item.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); try { - nmsStack.setTag(MojangsonParser.parse(processedJsonTag)); + nmsStack.setTag(TagParser.parseTag(processedJsonTag)); } catch (CommandSyntaxException e) { e.printStackTrace(); } diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/FastItemStackFactory.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/FastItemStackFactory.java new file mode 100644 index 00000000..bc9fbd04 --- /dev/null +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/FastItemStackFactory.java @@ -0,0 +1,14 @@ +package com.willfp.eco.proxy.v1_17_R1; + +import com.willfp.eco.core.fast.FastItemStack; +import com.willfp.eco.proxy.FastItemStackFactoryProxy; +import com.willfp.eco.proxy.v1_17_R1.fast.NMSFastItemStack; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public final class FastItemStackFactory implements FastItemStackFactoryProxy { + @Override + public FastItemStack create(@NotNull final ItemStack itemStack) { + return new NMSFastItemStack(itemStack); + } +} diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/VillagerTrade.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/VillagerTrade.java index 522a13b7..9de1c992 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/VillagerTrade.java +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/VillagerTrade.java @@ -2,6 +2,7 @@ package com.willfp.eco.proxy.v1_17_R1; import com.willfp.eco.core.display.Display; import com.willfp.eco.proxy.VillagerTradeProxy; +import net.minecraft.world.item.trading.MerchantOffer; import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftMerchantRecipe; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -43,15 +44,15 @@ public final class VillagerTrade implements VillagerTradeProxy { newRecipe.addIngredient(Display.display(ingredient.clone(), player)); } - getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice()); + getHandle(newRecipe).setSpecialPriceDiff(getHandle(oldRecipe).getSpecialPriceDiff()); return newRecipe; } @NotNull - private net.minecraft.world.item.trading.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) { + private MerchantOffer getHandle(@NotNull final CraftMerchantRecipe recipe) { try { - return (net.minecraft.world.item.trading.MerchantRecipe) handle.get(recipe); + return (MerchantOffer) handle.get(recipe); } catch (IllegalAccessException e) { e.printStackTrace(); } diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/FastItemStackUtils.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/FastItemStackUtils.java new file mode 100644 index 00000000..c237a816 --- /dev/null +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/FastItemStackUtils.java @@ -0,0 +1,43 @@ +package com.willfp.eco.proxy.v1_17_R1.fast; + +import lombok.experimental.UtilityClass; +import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; + +@UtilityClass +public class FastItemStackUtils { + private final Field field; + + public net.minecraft.world.item.ItemStack getNMSStack(@NotNull final ItemStack itemStack) { + if (itemStack instanceof CraftItemStack) { + throw new IllegalArgumentException("Must be CraftItemStack!"); + } + try { + return (net.minecraft.world.item.ItemStack) field.get(itemStack); + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + return null; + } + } + + static { + Field temp = null; + + try { + Field handleField = CraftItemStack.class.getDeclaredField("handle"); + handleField.setAccessible(true); + temp = handleField; + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + } + + assert temp != null; + Validate.notNull(temp, "Error occurred in initialization!"); + + field = temp; + } +} diff --git a/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java new file mode 100644 index 00000000..930db2d2 --- /dev/null +++ b/eco-core/core-nms/v1_17_R1/src/main/java/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.java @@ -0,0 +1,64 @@ +package com.willfp.eco.proxy.v1_17_R1.fast; + +import com.willfp.eco.core.fast.FastItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; +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.CraftNamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +public class NMSFastItemStack implements FastItemStack { + private final ItemStack handle; + + public NMSFastItemStack(@NotNull final org.bukkit.inventory.ItemStack itemStack) { + this.handle = FastItemStackUtils.getNMSStack(itemStack); + } + + @Override + public Map getEnchantmentsOnItem(final boolean checkStored) { + ListTag enchantmentNBT = checkStored && handle.getItem() == Items.ENCHANTED_BOOK ? EnchantedBookItem.getEnchantments(handle) : handle.getEnchantmentTags(); + Map foundEnchantments = new HashMap<>(); + + for (Tag base : enchantmentNBT) { + CompoundTag compound = (CompoundTag) base; + String key = compound.getString("id"); + int level = '\uffff' & compound.getShort("lvl"); + + Enchantment found = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(key)); + if (found != null) { + foundEnchantments.put(found, level); + } + } + return foundEnchantments; + } + + @Override + public int getLevelOnItem(@NotNull final Enchantment enchantment, + final boolean checkStored) { + ListTag enchantmentNBT = checkStored && handle.getItem() == Items.ENCHANTED_BOOK ? EnchantedBookItem.getEnchantments(handle) : handle.getEnchantmentTags(); + + for (Tag base : enchantmentNBT) { + CompoundTag compound = (CompoundTag) base; + String key = compound.getString("id"); + if (!key.equals(enchantment.getKey().toString())) { + continue; + } + + return '\uffff' & compound.getShort("lvl"); + } + return 0; + } + + @Override + public org.bukkit.inventory.ItemStack unwrap() { + return CraftItemStack.asCraftMirror(handle); + } +} diff --git a/settings.gradle b/settings.gradle index a17feef0..f1e39915 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,10 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { url "https://repo.jpenilla.xyz/snapshots/" } + } +} + rootProject.name = 'eco' // Core