Last active
August 29, 2015 14:15
-
-
Save Techcable/c6a5caad442a220b4dab to your computer and use it in GitHub Desktop.
1.8 Title API Requires my Reflection Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The MIT License | |
* Copyright (c) 2015 Techcable | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
/** | |
* Represents a 1.8 title | |
* | |
* Supports Real 1.8 and Fake 1.8 | |
* | |
* @author Techcable | |
*/ | |
public class Title { | |
public static final int RESET_ACTION = 4; | |
public static final int SET_TITLE_ACTION = 0; | |
public static final int SET_SUBTITLE_ACTION = 1; | |
public static final int DISPLAY_ACTION = 2; | |
public Title() {} | |
public Title(String title, String subtitle, int fadeIn, int stay, int fadeOut) { | |
this.title = title; | |
this.subtitle = subtitle; | |
this.fadeIn = fadeIn; | |
this.stay = stay; | |
this.fadeOut = fadeOut; | |
} | |
public Title(String title, String subtitle) { | |
this(title, subtitle, 20, 100, 20); | |
} | |
private String title; | |
private String subtitle; | |
private int fadeIn, stay, fadeOut = 100; //100 Ticks each default | |
/** | |
* Display the players this title | |
* Only shows players who are on 1.8 | |
* | |
* @param players players to display this title to | |
*/ | |
public void sendTo(Player... players) { | |
for (Player player : players) { | |
if (!is1_8(player)) continue; | |
sendPacket(createTitlePacket(RESET_ACTION), player); | |
if (title != null && !title.isEmpty()) { | |
sendPacket(createTitlePacket(SET_TITLE_ACTION, title), player); | |
} | |
if (subtitle != null && !subtitle.isEmpty()) { | |
sendPacket(createTitlePacket(SET_SUBTITLE_ACTION, subtitle), player); | |
} | |
if (title != null && !title.isEmpty() || subtitle != null && !subtitle.isEmpty()) { | |
sendPacket(createTitlePacket(DISPLAY_ACTION, fadeIn, stay, fadeOut), player); | |
} | |
} | |
} | |
//Packet Code | |
private static boolean is1_8(Player player) { | |
if (getNmsClass("TitlePacket") != null) return true; //Real 1.8 | |
Object handle = getHandle(player); | |
Field playerConnectionField = makeField(handle.getClass(), "playerConnection"); | |
Object connection = getField(playerConnectionField, handle); | |
Field networkManagerField = makeField(connection.getClass(), "networkManager"); | |
Object networkManager = getField(networkManagerField, connection); | |
Method getVersion = makeMethod(networkManager.getClass(), "getVersion"); | |
if (getVersion() == null) return false; //Not Fake 1.8 | |
int version = callMethod(getVersion, networkManager); | |
return version >= 47; | |
} | |
private static Object createTitlePacket(int action) { | |
Class<?> titlePacketClass = getNmsClass("TitlePacket"); | |
if (titlePacketClass == null) { //Not Real 1.8 | |
titlePacketClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle"); | |
Class<?> actionClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle$Action"); | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, actionClass); | |
return callConstructor(chatConstructor, createAction(action)); | |
} else { | |
Class<?> actionClass = getEnumTitleActionClass(); | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, actionClass, getNmsClass("IChatBaseComponent")); | |
return callConstructor(chatConstructor, createAction(action), null); | |
} | |
} | |
private static Object createTitlePacket(int action, int fadeIn, int stay, int fadeOut) { | |
Class<?> titlePacketClass = getNmsClass("TitlePacket"); | |
if (titlePacketClass == null) { //Not Real 1.8 | |
titlePacketClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle"); | |
Class<?> actionClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle$Action"); | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, actionClass, int.class, int.class, int.class); | |
return callConstructor(chatConstructor, createAction(action), fadeIn, stay, fadeOut); | |
} else { | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, int.class, int.class, int.class); | |
return callConstructor(chatConstructor, createAction(action), fadeIn, stay, fadeOut); | |
} | |
} | |
private static Object createTitlePacket(int action, String text) { | |
Class<?> titlePacketClass = getNmsClass("TitlePacket"); | |
if (titlePacketClass == null) { //Not Real 1.8 | |
titlePacketClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle"); | |
Class<?> actionClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle$Action"); | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, actionClass, getNmsClass("IChatBaseComponent")); | |
return callConstructor(chatConstructor, createAction(action), toBaseComponents(text)[0]); | |
} else { | |
Class<?> actionClass = getEnumTitleActionClass(); | |
Constructor<?> chatConstructor = makeConstructor(titlePacketClass, actionClass, getNmsClass("IChatBaseComponent")); | |
return callConstructor(chatConstructor, createAction(action), toBaseComponents(text)[0]); | |
} | |
} | |
private static Object createAction(int id) { | |
Class<?> actionClass = getEnumTitleActionClass(); | |
if (actionClass == null) { | |
actionClass = getClass("org.spigotmc.ProtocolInjector$PacketTitle$Action"); | |
return actionClass.getEnumConstants()[id]; | |
} else { | |
return actionClass.getEnumConstants()[id]; | |
} | |
} | |
private static Object[] toBaseComponents(String chat) { | |
Class<?> craftChatMessage = getCbClass("util.CraftChatMessage"); | |
Method fromString = makeMethod(craftChatMessage, "fromString", String.class); | |
return callMethod(fromString, null, chat); | |
} | |
private static void sendPacket(Object packet, Player player) { | |
Object handle = getHandle(player); | |
Field playerConnectionField = makeField(handle.getClass(), "playerConnection"); | |
Object connection = getField(playerConnectionField, handle); | |
Method sendPacket = makeMethod(connection.getClass(), "sendPacket", getNmsClass("Packet")); | |
callMethod(sendPacket, connection, packet); | |
} | |
private Class<?> getEnumTitleActionClass() { | |
if (getNmsClass("PacketPlayOutTitle$EnumTitleAction") != null) { | |
return getNmsClass("PacketPlayOutTitle$EnumTitleAction"); | |
} else { | |
return getNmsClass("EnumTitleAction"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment