mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2024-11-25 15:35:11 +01:00
Updated to eco 3.1.0
This commit is contained in:
parent
cd33d28b32
commit
423b092a3a
@ -49,7 +49,7 @@ allprojects {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'com.willfp:eco:3.0.0'
|
||||
compileOnly 'com.willfp:eco:3.1.0'
|
||||
|
||||
compileOnly 'org.jetbrains:annotations:19.0.0'
|
||||
|
||||
|
@ -1,11 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_15_R1;
|
||||
|
||||
import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class ChatComponent implements ChatComponentProxy {
|
||||
@Override
|
||||
public Object modifyComponent(@NotNull final Object object) {
|
||||
return object;
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_15_R1;
|
||||
|
||||
import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftMerchantRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public final class VillagerTrade implements VillagerTradeProxy {
|
||||
@Override
|
||||
public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) {
|
||||
try {
|
||||
// Enables removing final modifier
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
|
||||
// Bukkit MerchantRecipe result
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult());
|
||||
EnchantDisplay.addV(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
|
||||
fHandle.setAccessible(true);
|
||||
net.minecraft.server.v1_15_R1.MerchantRecipe handle = (net.minecraft.server.v1_15_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe
|
||||
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final
|
||||
|
||||
Field fSelling = net.minecraft.server.v1_15_R1.MerchantRecipe.class.getDeclaredField("sellingItem");
|
||||
fSelling.setAccessible(true);
|
||||
modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
EnchantDisplay.displayEnchantments(selling);
|
||||
EnchantDisplay.addV(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R1;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy;
|
||||
import net.minecraft.server.v1_16_R1.ChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R1.ChatHoverable;
|
||||
import net.minecraft.server.v1_16_R1.ChatMessage;
|
||||
import net.minecraft.server.v1_16_R1.ChatModifier;
|
||||
import net.minecraft.server.v1_16_R1.IChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R1.MojangsonParser;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class ChatComponent implements ChatComponentProxy {
|
||||
@Override
|
||||
public Object modifyComponent(@NotNull final Object object) {
|
||||
if (!(object instanceof IChatBaseComponent)) {
|
||||
return object;
|
||||
}
|
||||
|
||||
IChatBaseComponent chatComponent = (IChatBaseComponent) object;
|
||||
chatComponent.stream().forEach(this::modifyBaseComponent);
|
||||
|
||||
return chatComponent;
|
||||
}
|
||||
|
||||
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
|
||||
component.getSiblings().forEach(this::modifyBaseComponent);
|
||||
if (component instanceof ChatMessage) {
|
||||
Arrays.stream(((ChatMessage) component).getArgs())
|
||||
.filter(o -> o instanceof IChatBaseComponent)
|
||||
.map(o -> (IChatBaseComponent) o)
|
||||
.forEach(this::modifyBaseComponent);
|
||||
}
|
||||
|
||||
ChatHoverable hoverable = component.getChatModifier().getHoverEvent();
|
||||
|
||||
if (hoverable == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = hoverable.b();
|
||||
JsonElement json = hoverable.b().get("contents");
|
||||
if (json.getAsJsonObject().get("id") == null) {
|
||||
return;
|
||||
}
|
||||
if (json.getAsJsonObject().get("tag") == null) {
|
||||
return;
|
||||
}
|
||||
String id = json.getAsJsonObject().get("id").toString();
|
||||
String tag = json.getAsJsonObject().get("tag").toString();
|
||||
ItemStack itemStack = getFromTag(tag, id);
|
||||
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
assert meta != null;
|
||||
boolean hideEnchants = false;
|
||||
if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
|
||||
hideEnchants = true;
|
||||
}
|
||||
|
||||
itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants);
|
||||
EnchantDisplay.addV(itemStack);
|
||||
|
||||
json.getAsJsonObject().remove("tag");
|
||||
String newTag = toJson(itemStack);
|
||||
json.getAsJsonObject().add("tag", new JsonPrimitive(newTag));
|
||||
|
||||
jsonObject.remove("contents");
|
||||
jsonObject.add("contents", json);
|
||||
ChatHoverable newHoverable = ChatHoverable.a(jsonObject);
|
||||
ChatModifier modifier = component.getChatModifier();
|
||||
modifier = modifier.setChatHoverable(newHoverable);
|
||||
|
||||
((ChatBaseComponent) component).setChatModifier(modifier);
|
||||
}
|
||||
|
||||
private static ItemStack getFromTag(@NotNull final String jsonTag,
|
||||
@NotNull final String id) {
|
||||
String processedId = id;
|
||||
String processedJsonTag = jsonTag;
|
||||
processedId = processedId.replace("minecraft:", "");
|
||||
processedId = processedId.toUpperCase();
|
||||
processedId = processedId.replace("\"", "");
|
||||
processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1);
|
||||
processedJsonTag = processedJsonTag.replace("id:", "\"id\":");
|
||||
processedJsonTag = processedJsonTag.replace("\\", "");
|
||||
Material material = Material.getMaterial(processedId);
|
||||
|
||||
assert material != null;
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
|
||||
try {
|
||||
nmsStack.setTag(MojangsonParser.parse(processedJsonTag));
|
||||
} catch (CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CraftItemStack.asBukkitCopy(nmsStack);
|
||||
}
|
||||
|
||||
private static String toJson(@NotNull final ItemStack itemStack) {
|
||||
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R1;
|
||||
|
||||
import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMerchantRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public final class VillagerTrade implements VillagerTradeProxy {
|
||||
@Override
|
||||
public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) {
|
||||
try {
|
||||
// Enables removing final modifier
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
|
||||
// Bukkit MerchantRecipe result
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult());
|
||||
EnchantDisplay.addV(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
|
||||
fHandle.setAccessible(true);
|
||||
net.minecraft.server.v1_16_R1.MerchantRecipe handle = (net.minecraft.server.v1_16_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe
|
||||
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final
|
||||
|
||||
Field fSelling = net.minecraft.server.v1_16_R1.MerchantRecipe.class.getDeclaredField("sellingItem");
|
||||
fSelling.setAccessible(true);
|
||||
modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
EnchantDisplay.displayEnchantments(selling);
|
||||
EnchantDisplay.addV(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R2;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy;
|
||||
import net.minecraft.server.v1_16_R2.ChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R2.ChatHoverable;
|
||||
import net.minecraft.server.v1_16_R2.ChatMessage;
|
||||
import net.minecraft.server.v1_16_R2.ChatModifier;
|
||||
import net.minecraft.server.v1_16_R2.IChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R2.MojangsonParser;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class ChatComponent implements ChatComponentProxy {
|
||||
@Override
|
||||
public Object modifyComponent(@NotNull final Object object) {
|
||||
if (!(object instanceof IChatBaseComponent)) {
|
||||
return object;
|
||||
}
|
||||
|
||||
IChatBaseComponent chatComponent = (IChatBaseComponent) object;
|
||||
chatComponent.stream().forEach(this::modifyBaseComponent);
|
||||
|
||||
return chatComponent;
|
||||
}
|
||||
|
||||
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
|
||||
component.getSiblings().forEach(this::modifyBaseComponent);
|
||||
if (component instanceof ChatMessage) {
|
||||
Arrays.stream(((ChatMessage) component).getArgs())
|
||||
.filter(o -> o instanceof IChatBaseComponent)
|
||||
.map(o -> (IChatBaseComponent) o)
|
||||
.forEach(this::modifyBaseComponent);
|
||||
}
|
||||
|
||||
ChatHoverable hoverable = component.getChatModifier().getHoverEvent();
|
||||
|
||||
if (hoverable == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = hoverable.b();
|
||||
JsonElement json = hoverable.b().get("contents");
|
||||
if (json.getAsJsonObject().get("id") == null) {
|
||||
return;
|
||||
}
|
||||
if (json.getAsJsonObject().get("tag") == null) {
|
||||
return;
|
||||
}
|
||||
String id = json.getAsJsonObject().get("id").toString();
|
||||
String tag = json.getAsJsonObject().get("tag").toString();
|
||||
ItemStack itemStack = getFromTag(tag, id);
|
||||
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
assert meta != null;
|
||||
boolean hideEnchants = false;
|
||||
if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
|
||||
hideEnchants = true;
|
||||
}
|
||||
|
||||
itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants);
|
||||
EnchantDisplay.addV(itemStack);
|
||||
|
||||
json.getAsJsonObject().remove("tag");
|
||||
String newTag = toJson(itemStack);
|
||||
json.getAsJsonObject().add("tag", new JsonPrimitive(newTag));
|
||||
|
||||
jsonObject.remove("contents");
|
||||
jsonObject.add("contents", json);
|
||||
ChatHoverable newHoverable = ChatHoverable.a(jsonObject);
|
||||
ChatModifier modifier = component.getChatModifier();
|
||||
modifier = modifier.setChatHoverable(newHoverable);
|
||||
|
||||
((ChatBaseComponent) component).setChatModifier(modifier);
|
||||
}
|
||||
|
||||
private static ItemStack getFromTag(@NotNull final String jsonTag,
|
||||
@NotNull final String id) {
|
||||
String processedId = id;
|
||||
String processedJsonTag = jsonTag;
|
||||
processedId = processedId.replace("minecraft:", "");
|
||||
processedId = processedId.toUpperCase();
|
||||
processedId = processedId.replace("\"", "");
|
||||
processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1);
|
||||
processedJsonTag = processedJsonTag.replace("id:", "\"id\":");
|
||||
processedJsonTag = processedJsonTag.replace("\\", "");
|
||||
Material material = Material.getMaterial(processedId);
|
||||
|
||||
assert material != null;
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
|
||||
try {
|
||||
nmsStack.setTag(MojangsonParser.parse(processedJsonTag));
|
||||
} catch (CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CraftItemStack.asBukkitCopy(nmsStack);
|
||||
}
|
||||
|
||||
private static String toJson(@NotNull final ItemStack itemStack) {
|
||||
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R2;
|
||||
|
||||
import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftMerchantRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public final class VillagerTrade implements VillagerTradeProxy {
|
||||
@Override
|
||||
public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) {
|
||||
try {
|
||||
// Enables removing final modifier
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
|
||||
// Bukkit MerchantRecipe result
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult());
|
||||
EnchantDisplay.addV(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
|
||||
fHandle.setAccessible(true);
|
||||
net.minecraft.server.v1_16_R2.MerchantRecipe handle = (net.minecraft.server.v1_16_R2.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe
|
||||
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final
|
||||
|
||||
Field fSelling = net.minecraft.server.v1_16_R2.MerchantRecipe.class.getDeclaredField("sellingItem");
|
||||
fSelling.setAccessible(true);
|
||||
modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
EnchantDisplay.displayEnchantments(selling);
|
||||
EnchantDisplay.addV(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R3;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import net.minecraft.server.v1_16_R3.ChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R3.ChatHoverable;
|
||||
import net.minecraft.server.v1_16_R3.ChatMessage;
|
||||
import net.minecraft.server.v1_16_R3.ChatModifier;
|
||||
import net.minecraft.server.v1_16_R3.IChatBaseComponent;
|
||||
import net.minecraft.server.v1_16_R3.MojangsonParser;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class ChatComponent implements ChatComponentProxy {
|
||||
@Override
|
||||
public Object modifyComponent(@NotNull final Object object) {
|
||||
if (!(object instanceof IChatBaseComponent)) {
|
||||
return object;
|
||||
}
|
||||
|
||||
IChatBaseComponent chatComponent = (IChatBaseComponent) object;
|
||||
chatComponent.stream().forEach(this::modifyBaseComponent);
|
||||
|
||||
return chatComponent;
|
||||
}
|
||||
|
||||
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
|
||||
component.getSiblings().forEach(this::modifyBaseComponent);
|
||||
if (component instanceof ChatMessage) {
|
||||
Arrays.stream(((ChatMessage) component).getArgs())
|
||||
.filter(o -> o instanceof IChatBaseComponent)
|
||||
.map(o -> (IChatBaseComponent) o)
|
||||
.forEach(this::modifyBaseComponent);
|
||||
}
|
||||
|
||||
ChatHoverable hoverable = component.getChatModifier().getHoverEvent();
|
||||
|
||||
if (hoverable == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = hoverable.b();
|
||||
JsonElement json = hoverable.b().get("contents");
|
||||
if (json.getAsJsonObject().get("id") == null) {
|
||||
return;
|
||||
}
|
||||
if (json.getAsJsonObject().get("tag") == null) {
|
||||
return;
|
||||
}
|
||||
String id = json.getAsJsonObject().get("id").toString();
|
||||
String tag = json.getAsJsonObject().get("tag").toString();
|
||||
ItemStack itemStack = getFromTag(tag, id);
|
||||
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
assert meta != null;
|
||||
boolean hideEnchants = false;
|
||||
if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
|
||||
hideEnchants = true;
|
||||
}
|
||||
|
||||
itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants);
|
||||
EnchantDisplay.addV(itemStack);
|
||||
|
||||
json.getAsJsonObject().remove("tag");
|
||||
String newTag = toJson(itemStack);
|
||||
json.getAsJsonObject().add("tag", new JsonPrimitive(newTag));
|
||||
|
||||
jsonObject.remove("contents");
|
||||
jsonObject.add("contents", json);
|
||||
ChatHoverable newHoverable = ChatHoverable.a(jsonObject);
|
||||
ChatModifier modifier = component.getChatModifier();
|
||||
modifier = modifier.setChatHoverable(newHoverable);
|
||||
|
||||
((ChatBaseComponent) component).setChatModifier(modifier);
|
||||
}
|
||||
|
||||
private static ItemStack getFromTag(@NotNull final String jsonTag,
|
||||
@NotNull final String id) {
|
||||
String processedId = id;
|
||||
String processedJsonTag = jsonTag;
|
||||
processedId = processedId.replace("minecraft:", "");
|
||||
processedId = processedId.toUpperCase();
|
||||
processedId = processedId.replace("\"", "");
|
||||
processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1);
|
||||
processedJsonTag = processedJsonTag.replace("id:", "\"id\":");
|
||||
processedJsonTag = processedJsonTag.replace("\\", "");
|
||||
Material material = Material.getMaterial(processedId);
|
||||
|
||||
assert material != null;
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
|
||||
try {
|
||||
nmsStack.setTag(MojangsonParser.parse(processedJsonTag));
|
||||
} catch (CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CraftItemStack.asBukkitCopy(nmsStack);
|
||||
}
|
||||
|
||||
private static String toJson(@NotNull final ItemStack itemStack) {
|
||||
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.v1_16_R3;
|
||||
|
||||
import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMerchantRecipe;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public final class VillagerTrade implements VillagerTradeProxy {
|
||||
@Override
|
||||
public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) {
|
||||
try {
|
||||
// Enables removing final modifier
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
|
||||
// Bukkit MerchantRecipe result
|
||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||
fResult.setAccessible(true);
|
||||
ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult());
|
||||
EnchantDisplay.addV(result);
|
||||
fResult.set(merchantRecipe, result);
|
||||
|
||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||
Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle");
|
||||
fHandle.setAccessible(true);
|
||||
net.minecraft.server.v1_16_R3.MerchantRecipe handle = (net.minecraft.server.v1_16_R3.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe
|
||||
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final
|
||||
|
||||
Field fSelling = net.minecraft.server.v1_16_R3.MerchantRecipe.class.getDeclaredField("sellingItem");
|
||||
fSelling.setAccessible(true);
|
||||
modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL);
|
||||
|
||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||
EnchantDisplay.displayEnchantments(selling);
|
||||
EnchantDisplay.addV(selling);
|
||||
|
||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package com.willfp.ecoenchants;
|
||||
|
||||
import com.willfp.eco.util.command.AbstractCommand;
|
||||
import com.willfp.eco.util.display.Display;
|
||||
import com.willfp.eco.util.display.DisplayModule;
|
||||
import com.willfp.eco.util.drops.telekinesis.TelekinesisUtils;
|
||||
import com.willfp.eco.util.integrations.IntegrationLoader;
|
||||
import com.willfp.eco.util.interfaces.EcoRunnable;
|
||||
@ -13,11 +15,6 @@ import com.willfp.ecoenchants.command.tabcompleters.TabCompleterEnchantinfo;
|
||||
import com.willfp.ecoenchants.config.EcoEnchantsConfigs;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import com.willfp.ecoenchants.display.EnchantmentCache;
|
||||
import com.willfp.ecoenchants.display.packets.PacketChat;
|
||||
import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant;
|
||||
import com.willfp.ecoenchants.display.packets.PacketSetCreativeSlot;
|
||||
import com.willfp.ecoenchants.display.packets.PacketSetSlot;
|
||||
import com.willfp.ecoenchants.display.packets.PacketWindowItems;
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchants;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
|
||||
@ -42,6 +39,8 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -68,6 +67,21 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin {
|
||||
*/
|
||||
@Override
|
||||
public void enable() {
|
||||
Display.registerDisplayModule(new DisplayModule(itemStack -> {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return itemStack;
|
||||
}
|
||||
boolean hideEnchants = false;
|
||||
if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
|
||||
hideEnchants = true;
|
||||
}
|
||||
|
||||
return EnchantDisplay.displayEnchantments(itemStack, hideEnchants);
|
||||
}, 500, this.getPluginName()));
|
||||
Display.registerRevertModule(EnchantDisplay::revertDisplay);
|
||||
Display.registerFinalizeModule(EnchantDisplay::addV);
|
||||
|
||||
this.getExtensionLoader().loadExtensions();
|
||||
|
||||
if (this.getExtensionLoader().getLoadedExtensions().isEmpty()) {
|
||||
@ -96,7 +110,6 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin {
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
this.getExtensionLoader().unloadExtensions();
|
||||
}
|
||||
|
||||
@ -176,13 +189,7 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin {
|
||||
*/
|
||||
@Override
|
||||
public List<AbstractPacketAdapter> getPacketAdapters() {
|
||||
return Arrays.asList(
|
||||
new PacketChat(this),
|
||||
new PacketOpenWindowMerchant(this),
|
||||
new PacketSetCreativeSlot(this),
|
||||
new PacketSetSlot(this),
|
||||
new PacketWindowItems(this)
|
||||
);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,41 +0,0 @@
|
||||
package com.willfp.ecoenchants.display.packets;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.ListenerPriority;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedChatComponent;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy;
|
||||
import com.willfp.ecoenchants.util.ProxyUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PacketChat extends AbstractPacketAdapter {
|
||||
/**
|
||||
* Instantiate a new listener for {@link PacketType.Play.Server#CHAT}.
|
||||
*
|
||||
* @param plugin The plugin to listen through.
|
||||
*/
|
||||
public PacketChat(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin, PacketType.Play.Server.CHAT, ListenerPriority.MONITOR, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSend(@NotNull final PacketContainer packet,
|
||||
@NotNull final Player player) {
|
||||
for (int i = 0; i < packet.getChatComponents().size(); i++) {
|
||||
WrappedChatComponent component = packet.getChatComponents().read(i);
|
||||
if (component == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (component.getHandle() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(ProxyUtils.getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle()));
|
||||
packet.getChatComponents().write(i, newComponent);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package com.willfp.ecoenchants.display.packets;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
|
||||
import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy;
|
||||
import com.willfp.ecoenchants.util.ProxyUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PacketOpenWindowMerchant extends AbstractPacketAdapter {
|
||||
/**
|
||||
* Instantiate a new listener for {@link PacketType.Play.Server#OPEN_WINDOW_MERCHANT}.
|
||||
*
|
||||
* @param plugin The plugin to listen through.
|
||||
*/
|
||||
public PacketOpenWindowMerchant(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin, PacketType.Play.Server.OPEN_WINDOW_MERCHANT, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSend(@NotNull final PacketContainer packet,
|
||||
@NotNull final Player player) {
|
||||
List<MerchantRecipe> recipes = packet.getMerchantRecipeLists().readSafely(0);
|
||||
|
||||
recipes = recipes.stream().peek(merchantRecipe -> {
|
||||
if (!EnchantmentTarget.ALL.getMaterials().contains(merchantRecipe.getResult().getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
ProxyUtils.getProxy(VillagerTradeProxy.class).displayTradeEnchantments(merchantRecipe);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
packet.getMerchantRecipeLists().writeSafely(0, recipes);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.willfp.ecoenchants.display.packets;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PacketSetCreativeSlot extends AbstractPacketAdapter {
|
||||
/**
|
||||
* Instantiate a new listener for {@link PacketType.Play.Client#SET_CREATIVE_SLOT}.
|
||||
*
|
||||
* @param plugin The plugin to listen through.
|
||||
*/
|
||||
public PacketSetCreativeSlot(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin, PacketType.Play.Client.SET_CREATIVE_SLOT, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(@NotNull final PacketContainer packet,
|
||||
@NotNull final Player player) {
|
||||
packet.getItemModifier().modify(0, EnchantDisplay::revertDisplay);
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package com.willfp.ecoenchants.display.packets;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PacketSetSlot extends AbstractPacketAdapter {
|
||||
/**
|
||||
* Instantiate a new listener for {@link PacketType.Play.Server#SET_SLOT}.
|
||||
*
|
||||
* @param plugin The plugin to listen through.
|
||||
*/
|
||||
public PacketSetSlot(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin, PacketType.Play.Server.SET_SLOT, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSend(@NotNull final PacketContainer packet,
|
||||
@NotNull final Player player) {
|
||||
packet.getItemModifier().modify(0, item -> {
|
||||
boolean hideEnchants = false;
|
||||
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (item.getItemMeta() != null) {
|
||||
hideEnchants = item.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS);
|
||||
}
|
||||
|
||||
item = EnchantDisplay.displayEnchantments(item, hideEnchants);
|
||||
return item;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package com.willfp.ecoenchants.display.packets;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.ecoenchants.display.EnchantDisplay;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PacketWindowItems extends AbstractPacketAdapter {
|
||||
/**
|
||||
* Instantiate a new listener for {@link PacketType.Play.Server#WINDOW_ITEMS}.
|
||||
*
|
||||
* @param plugin The plugin to listen through.
|
||||
*/
|
||||
public PacketWindowItems(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin, PacketType.Play.Server.WINDOW_ITEMS, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSend(@NotNull final PacketContainer packet,
|
||||
@NotNull final Player player) {
|
||||
packet.getItemListModifier().modify(0, itemStacks -> {
|
||||
if (itemStacks == null) {
|
||||
return null;
|
||||
}
|
||||
itemStacks.forEach(item -> {
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean hideEnchants = false;
|
||||
|
||||
if (item.getItemMeta() != null) {
|
||||
hideEnchants = item.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS);
|
||||
}
|
||||
|
||||
EnchantDisplay.displayEnchantments(item, hideEnchants);
|
||||
});
|
||||
return itemStacks;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.proxies;
|
||||
|
||||
|
||||
import com.willfp.eco.util.proxy.AbstractProxy;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface ChatComponentProxy extends AbstractProxy {
|
||||
/**
|
||||
* Modify hover {@link org.bukkit.inventory.ItemStack}s using EnchantDisplay#displayEnchantments.
|
||||
* @param object The NMS ChatComponent to modify.
|
||||
* @return The modified ChatComponent.
|
||||
*/
|
||||
Object modifyComponent(@NotNull Object object);
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.willfp.ecoenchants.proxy.proxies;
|
||||
|
||||
import com.willfp.eco.util.proxy.AbstractProxy;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
|
||||
public interface VillagerTradeProxy extends AbstractProxy {
|
||||
/**
|
||||
* Apply enchant display to the result of trades.
|
||||
*
|
||||
* @param merchantRecipe The recipe to modify.
|
||||
*/
|
||||
void displayTradeEnchantments(MerchantRecipe merchantRecipe);
|
||||
}
|
Loading…
Reference in New Issue
Block a user