mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2024-11-25 15:35:11 +01:00
Added ChatComponent nms API
This commit is contained in:
parent
c2948525d4
commit
cd9ff42711
@ -0,0 +1,98 @@
|
||||
package com.willfp.ecoenchants.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.nms.API.ChatComponentWrapper;
|
||||
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.ItemStack;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ChatComponent implements ChatComponentWrapper {
|
||||
@Override
|
||||
public Object modifyComponent(Object object) {
|
||||
if(!(object instanceof IChatBaseComponent)) {
|
||||
return object;
|
||||
}
|
||||
|
||||
IChatBaseComponent chatComponent = (IChatBaseComponent) object;
|
||||
chatComponent.stream().forEach(this::modifyBaseComponent);
|
||||
|
||||
return chatComponent;
|
||||
}
|
||||
|
||||
public void modifyBaseComponent(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);
|
||||
try {
|
||||
itemStack = (ItemStack) Class.forName("com.willfp.ecoenchants.display.EnchantDisplay").getMethod("displayEnchantments", ItemStack.class).invoke(null, itemStack);
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
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(String jsonTag, String id) {
|
||||
id = id.replaceAll("minecraft:", "");
|
||||
id = id.toUpperCase();
|
||||
id = id.replaceAll("\"", "");
|
||||
jsonTag = jsonTag.substring( 1, jsonTag.length() - 1 );
|
||||
jsonTag = jsonTag.replaceAll("id:", "\"id\":");
|
||||
jsonTag = jsonTag.replace("\\", "");
|
||||
Material material = Material.getMaterial(id);
|
||||
|
||||
assert material != null;
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
|
||||
try {
|
||||
nmsStack.setTag(MojangsonParser.parse(jsonTag));
|
||||
} catch (CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CraftItemStack.asBukkitCopy(nmsStack);
|
||||
}
|
||||
|
||||
private static String toJson(ItemStack itemStack) {
|
||||
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.willfp.ecoenchants.v1_16_R3;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.willfp.ecoenchants.nms.API.JsonStackWrapper;
|
||||
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.ItemStack;
|
||||
|
||||
public class JsonStack implements JsonStackWrapper {
|
||||
@Override
|
||||
public ItemStack getFromTag(String jsonTag, String id) {
|
||||
id = id.replaceAll("minecraft:", "").toUpperCase();
|
||||
Material material = Material.getMaterial(id);
|
||||
|
||||
assert material != null;
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
|
||||
try {
|
||||
nmsStack.setTag(MojangsonParser.parse(jsonTag));
|
||||
} catch (CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CraftItemStack.asBukkitCopy(nmsStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(ItemStack itemStack) {
|
||||
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.willfp.ecoenchants.nms;
|
||||
|
||||
|
||||
import com.willfp.ecoenchants.EcoEnchantsPlugin;
|
||||
import com.willfp.ecoenchants.nms.API.ChatComponentWrapper;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* Utility class to manage chat components
|
||||
*/
|
||||
public class ChatComponent {
|
||||
private static ChatComponentWrapper chatComponentWrapper;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static boolean init() {
|
||||
try {
|
||||
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".ChatComponent");
|
||||
if (ChatComponentWrapper.class.isAssignableFrom(class2)) {
|
||||
chatComponentWrapper = (ChatComponentWrapper) class2.getConstructor().newInstance();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
chatComponentWrapper = null;
|
||||
}
|
||||
return chatComponentWrapper != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify chat component
|
||||
*/
|
||||
public static Object modifyComponent(Object object) {
|
||||
assert chatComponentWrapper != null;
|
||||
return chatComponentWrapper.modifyComponent(object);
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package com.willfp.ecoenchants.nms;
|
||||
|
||||
|
||||
import com.willfp.ecoenchants.EcoEnchantsPlugin;
|
||||
import com.willfp.ecoenchants.nms.API.JsonStackWrapper;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* Utility class to read/write NBTTagCompounds through json
|
||||
*/
|
||||
public class JsonStack {
|
||||
private static JsonStackWrapper jsonStackWrapper;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static boolean init() {
|
||||
try {
|
||||
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".JsonStack");
|
||||
if (JsonStackWrapper.class.isAssignableFrom(class2)) {
|
||||
jsonStackWrapper = (JsonStackWrapper) class2.getConstructor().newInstance();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
jsonStackWrapper = null;
|
||||
}
|
||||
return jsonStackWrapper != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the NBTTagCompound of an item as json
|
||||
*
|
||||
* @param itemStack The item to query
|
||||
* @return The NBTTagCompound of the item as json
|
||||
*/
|
||||
public static String toJson(ItemStack itemStack) {
|
||||
assert jsonStackWrapper != null;
|
||||
return jsonStackWrapper.toJson(itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ItemStack from NBTTagCompound json
|
||||
* @param jsonTag The tag of the item
|
||||
* @param id The fully qualified material ID
|
||||
* @return The reconstructed ItemStack
|
||||
*/
|
||||
public static ItemStack fromJson(String jsonTag, String id) {
|
||||
assert jsonStackWrapper != null;
|
||||
return jsonStackWrapper.getFromTag(jsonTag, id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user