Fixed compatibility with older versions

This commit is contained in:
Kikisito 2020-06-25 16:30:32 +02:00
parent 1ddbd60b69
commit cd328ea00c
2 changed files with 39 additions and 6 deletions

View File

@ -0,0 +1,35 @@
package com.zeshanaslam.actionhealth.support;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
public class LegacyPreAction {
private final String packageVersion = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
public LegacyPreAction(Player player, String message) throws ClassNotFoundException {
try {
Object chatComponentText = getNMSClass("ChatComponentText").getConstructor(new Class[]{String.class}).newInstance(message);
Object chatMessageType = getNMSClass("ChatMessageType").getField("GAME_INFO").get(null);
Object packetPlayOutChat = getNMSClass("PacketPlayOutChat").getConstructor(new Class[]{getNMSClass("IChatBaseComponent"), getNMSClass("ChatMessageType")}).newInstance(chatComponentText, chatMessageType);
Object getHandle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player);
Object playerConnection = getHandle.getClass().getField("playerConnection").get(getHandle);
playerConnection.getClass().getMethod("sendPacket", new Class[]{getNMSClass("Packet")}).invoke(playerConnection, packetPlayOutChat);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) {
e.printStackTrace();
}
}
private Class<?> getNMSClass(String nmsClassName) {
try {
return Class.forName("net.minecraft.server." + packageVersion + "." + nmsClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -2,15 +2,11 @@ package com.zeshanaslam.actionhealth.utils;
import com.zeshanaslam.actionhealth.Main;
import com.zeshanaslam.actionhealth.api.HealthSendEvent;
import com.zeshanaslam.actionhealth.support.LangUtilsSupport;
import com.zeshanaslam.actionhealth.support.McMMOSupport;
import com.zeshanaslam.actionhealth.support.MythicMobsSupport;
import com.zeshanaslam.actionhealth.support.PreAction;
import com.zeshanaslam.actionhealth.support.*;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -242,8 +238,10 @@ public class HealthUtil {
message = ChatColor.translateAlternateColorCodes('&', message);
try {
if (plugin.configStore.mcVersion.equals("v1_12_R1") || plugin.configStore.mcVersion.startsWith("v1_13") || plugin.configStore.mcVersion.startsWith("v1_14_") || plugin.configStore.mcVersion.startsWith("v1_15_") || plugin.configStore.mcVersion.startsWith("v1_16_")) {
if(plugin.configStore.mcVersion.equals("v1_16_R1")){
new PreAction(player, message);
} else if (plugin.configStore.mcVersion.equals("v1_12_R1") || plugin.configStore.mcVersion.startsWith("v1_13") || plugin.configStore.mcVersion.startsWith("v1_14_") || plugin.configStore.mcVersion.startsWith("v1_15_")) {
new LegacyPreAction(player, message);
} else if (!(plugin.configStore.mcVersion.equalsIgnoreCase("v1_8_R1") || (plugin.configStore.mcVersion.contains("v1_7_")))) {
Class<?> c1 = Class.forName("org.bukkit.craftbukkit." + plugin.configStore.mcVersion + ".entity.CraftPlayer");
Object p = c1.cast(player);