YumCore/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java

428 lines
17 KiB
Java
Raw Normal View History

package pw.yumc.YumCore.bukkit.compatible;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.json.simple.JSONObject;
import com.google.common.base.Charsets;
import pw.yumc.YumCore.bukkit.Log;
/**
* Bukkit
*
* @since 2016723 1:04:56
* @author
*/
public class C {
private static Class<?> nmsChatSerializer;
private static Class<?> nmsIChatBaseComponent;
private static Class<?> packetType;
private static Class<?> packetActions;
private static Class<?> packetTitle;
private static Method getHandle;
private static String version;
private static Field playerConnection;
private static Method sendPacket;
static {
try {
version = getNMSVersion();
final boolean newversion = Integer.parseInt(version.split("_")[1]) > 7;
nmsChatSerializer = Class.forName(a(newversion ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"));
nmsIChatBaseComponent = Class.forName(a("IChatBaseComponent"));
packetType = Class.forName(a("PacketPlayOutChat"));
packetActions = Class.forName(a(newversion ? "PacketPlayOutTitle$EnumTitleAction" : "EnumTitleAction"));
packetTitle = Class.forName(a("PacketPlayOutTitle"));
final Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer"));
final Class<?> typeNMSPlayer = Class.forName(a("EntityPlayer"));
final Class<?> typePlayerConnection = Class.forName(a("PlayerConnection"));
getHandle = typeCraftPlayer.getMethod("getHandle");
playerConnection = typeNMSPlayer.getField("playerConnection");
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(a("Packet")));
} catch (final Exception e) {
Log.warning(C.class.getSimpleName() + " 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.debug(e);
}
}
private C() {
}
public static String a(final String str) {
return "net.minecraft.server." + version + "." + str;
}
public static String b(final String str) {
return "org.bukkit.craftbukkit." + version + "." + str;
}
/**
* NMS
*
* @return NMS
*/
public static String getNMSVersion() {
return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
}
public static class ActionBar {
private ActionBar() {
}
/**
* ActionBar
*
* @param message
*
*/
public static void broadcast(final String message) {
for (final org.bukkit.entity.Player player : C.Player.getOnlinePlayers()) {
send(player, message);
}
}
/**
* ActionBar
*
* @param message
*
* @param times
*
*/
public static void broadcast(final String message, final int times) {
new Thread(new Runnable() {
@Override
public void run() {
int time = times;
do {
for (final org.bukkit.entity.Player player : C.Player.getOnlinePlayers()) {
send(player, message);
}
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
// Ignore
}
time--;
} while (time > 0);
}
}).start();
}
/**
* ActionBar()
*
* @param world
*
* @param message
*
* @param times
*
*/
public static void broadcast(final World world, final String message, final int times) {
new Thread(new Runnable() {
@Override
public void run() {
int time = times;
do {
for (final org.bukkit.entity.Player player : C.Player.getOnlinePlayers()) {
if (player.getWorld().getName().equalsIgnoreCase(world.getName())) {
send(player, message);
}
}
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
// Ignore
}
time--;
} while (time > 0);
}
}).start();
}
/**
* ActionBar
*
* @param receivingPacket
*
* @param msg
* ActionBar
*/
public static void send(final org.bukkit.entity.Player receivingPacket, final String msg) {
Object packet = null;
try {
final Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', JSONObject.escape(msg)) + "\"}");
if (!version.contains("1_7")) {
packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2);
} else {
packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2);
}
final Object player = getHandle.invoke(receivingPacket);
final Object connection = playerConnection.get(player);
sendPacket.invoke(connection, packet);
} catch (final Exception ex) {
Log.debug("ActionBar发包错误 " + version, ex);
}
}
/**
* ActionBar
*
* @param receivingPacket
*
* @param msg
*
* @param times
*
*/
public static void send(final org.bukkit.entity.Player receivingPacket, final String msg, final int times) {
new Thread(new Runnable() {
@Override
public void run() {
int time = times;
do {
send(receivingPacket, msg);
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
// Ignore
}
time--;
} while (time > 0);
}
}).start();
}
}
/**
* Bukkit Player
*
* @since 2016723 4:33:40
* @author
*/
public static class Player {
private static Class<?> gameProfileClass;
private static Constructor<?> gameProfileConstructor;
private static Constructor<?> craftOfflinePlayerConstructor;
private static Method getOnlinePlayers;
static {
try {
// getOnlinePlayers start
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
if (getOnlinePlayers.getReturnType() != org.bukkit.entity.Player[].class) {
for (final Method method : Bukkit.class.getDeclaredMethods()) {
if (method.getReturnType() == org.bukkit.entity.Player[].class && method.getName().endsWith("getOnlinePlayers")) {
getOnlinePlayers = method;
}
}
}
// getOnlinePlayers end
} catch (final Exception e) {
Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!");
}
try {
// getOfflinePlayer start
try {
gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile");
} catch (final Exception e) {
try {
gameProfileClass = Class.forName("com.mojang.authlib.GameProfile");
} catch (final Exception e1) {
}
}
gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class });
gameProfileConstructor.setAccessible(true);
final Class<? extends Server> craftServer = Bukkit.getServer().getClass();
final Class<?> craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer"));
craftOfflinePlayerConstructor = craftOfflinePlayer.getDeclaredConstructor(new Class[] { craftServer, gameProfileClass });
craftOfflinePlayerConstructor.setAccessible(true);
// getOfflinePlayer end
} catch (final Exception e) {
Log.debug(e);
}
}
private Player() {
}
/**
* 线()
*
* @param playerName
*
* @return {@link OfflinePlayer}
*/
public static OfflinePlayer getOfflinePlayer(final String playerName) {
try {
final Object gameProfile = gameProfileConstructor.newInstance(new Object[] { UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8)), playerName });
final Object offlinePlayer = craftOfflinePlayerConstructor.newInstance(new Object[] { Bukkit.getServer(), gameProfile });
return (OfflinePlayer) offlinePlayer;
} catch (final Exception e) {
return Bukkit.getOfflinePlayer(playerName);
}
}
/**
* 线
*
* @return 线
*/
public static Collection<? extends org.bukkit.entity.Player> getOnlinePlayers() {
try {
return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null));
} catch (final Exception e) {
return Bukkit.getOnlinePlayers();
}
}
}
public static class Title {
private Title() {
}
/**
* Title
*
* @param title
*
* @param subtitle
*
*/
public static void broadcast(final String title, final String subtitle) {
for (final org.bukkit.entity.Player player : Player.getOnlinePlayers()) {
send(player, title, subtitle);
}
}
/**
* Title
*
* @param title
*
* @param subtitle
*
* @param fadeInTime
*
* @param stayTime
*
* @param fadeOutTime
*
*/
public static void broadcast(final String title, final String subtitle, final int fadeInTime, final int stayTime, final int fadeOutTime) {
for (final org.bukkit.entity.Player player : Player.getOnlinePlayers()) {
send(player, title, subtitle, fadeInTime, stayTime, fadeOutTime);
}
}
/**
* Title
*
* @param world
*
* @param title
*
* @param subtitle
*
*/
public static void broadcast(final World world, final String title, final String subtitle) {
for (final org.bukkit.entity.Player player : Player.getOnlinePlayers()) {
if (player.getWorld().getName().equalsIgnoreCase(world.getName())) {
send(player, title, subtitle);
}
}
}
/**
* Title
*
* @param recoverPlayer
*
* @throws Exception
*
*/
public static void reset(final org.bukkit.entity.Player recoverPlayer) throws Exception {
// Send timings first
final Object player = getHandle.invoke(recoverPlayer);
final Object connection = playerConnection.get(player);
final Object[] actions = packetActions.getEnumConstants();
final Object packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent).newInstance(actions[4], null);
sendPacket.invoke(connection, packet);
}
/**
* Titile( 1 2 1)
*
* @param receivingPacket
*
* @param title
*
* @param subtitle
*
*/
public static void send(final org.bukkit.entity.Player receivingPacket, final String title, final String subtitle) {
send(receivingPacket, title, subtitle, 1, 2, 1);
}
/**
* Titile
*
* @param receivingPacket
*
* @param title
*
* @param subtitle
*
* @param fadeInTime
*
* @param stayTime
*
* @param fadeOutTime
*
*/
public static void send(final org.bukkit.entity.Player receivingPacket, final String title, final String subtitle, final int fadeInTime, final int stayTime, final int fadeOutTime) {
if (packetTitle != null) {
try {
// First reset previous settings
reset(receivingPacket);
// Send timings first
final Object player = getHandle.invoke(receivingPacket);
final Object connection = playerConnection.get(player);
final Object[] actions = packetActions.getEnumConstants();
Object packet = null;
// Send if set
if ((fadeInTime != -1) && (fadeOutTime != -1) && (stayTime != -1)) {
packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE).newInstance(actions[2], null, fadeInTime * 20, stayTime * 20, fadeOutTime * 20);
sendPacket.invoke(connection, packet);
}
// Send title
Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', title) + "\"}");
packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent).newInstance(actions[0], serialized);
sendPacket.invoke(connection, packet);
if (!"".equals(subtitle)) {
// Send subtitle if present
serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', subtitle) + "\"}");
packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent).newInstance(actions[1], serialized);
sendPacket.invoke(connection, packet);
}
} catch (final Exception e) {
Log.debug(e);
}
}
}
}
}