Skip to content

Instantly share code, notes, and snippets.

@Techcable
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save Techcable/5676710c854a8e7e70ea to your computer and use it in GitHub Desktop.

Select an option

Save Techcable/5676710c854a8e7e70ea to your computer and use it in GitHub Desktop.
ActionBarUtils (Requires My Reflection Class)
/*
* 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.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.bukkit.entity.Player;
import static Reflection.*; //Make sure to change this to the right package
/**
* A 1.8 ActionBar
*
* Works on protocol hack and real 1.8
*/
public class ActionBar {
/**
* Creates a new action bar with the specificed text
*
* @param text text to keep in the action bar
*/
public ActionBar(String text) {
this.text = text;
}
private final String text;
public void sendTo(Player p) {
ActionBarHandler handler = getActionBarHandler();
if (handler == null) return null;
}
private static ActionBarHandler handler;
private static ActionBarHandler getActionBarHandler() {
if (handler != null) return handler;
if (SpigotActionBarHandler.isSupported()) {
handler = new SpigotActionBarHandler();
} else if (NMSActionBarHandler.isSupported()) {
handler = new NMSActionBarHandler();
} else {
return null;
}
return handler;
}
private static interface ActionBarHandler {
public void sendTo(Player p, ActionBar bar);
}
private static class SpigotActionBarHandler implements ActionBarHandler {
private SpigotActionBarHandler() {
assert isSupported() : "Spigot action bar is unsupported!";
}
private final static Constructor packetConstructor = makeConstructor(getNMSClass("PacketPlayOutChat"), getNMSClass("IChatBaseComponent"), int.class);
public void sendTo(Player p, ActionBar bar) {
if (getProtocolVersion(p) < 16) return;
Object baseComponent = serialize(bar.getText());
Object packet = callConstructor(packetConstructor, baseComponent, 2)
sendPacket(p, packet);
}
private static final Field playerConnectionField = makeField(handle.getClass(), "playerConnection");
private static final Field networkManagerField = makeField(connection.getClass(), "networkManager");
private static final Method getVersion = makeMethod(networkManager.getClass(), "getVersion");
private static int getProtocolVersion(Player player) {
Object handle = getHandle(player);
Object connection = getField(playerConnectionField, handle);
Object networkManager = getField(networkManagerField, connection);
assert getVersion() != null : "Not Protocol Hack";
int version = callMethod(getVersion, networkManager);
return version;
}
public static boolean isSupported() {
return getClass("org.spigotmc.ProtocolData") != null;
}
}
private static class NMSActionBarHandler implements ActionBarHandler {
private NMSActionBarHandler() {
assert !SpigotActionBarHandler.isSupported() : "Spigot action bar is supported";
assert NMSActionBarHandler.isSupported : "NMS Action bar isn't supported";
}
private static final Constructor packetConstructor = makeConstructor(getNMSClass("PacketPlayOutChat"), getNMSClass("IChatBaseComponent"), int.class);
public void sendTo(Player p, ActionBar bar) {
Object baseComponent = serialize(bar.getText());
Object packet = callConstructor(packetConstructor, baseComponent, 2)
sendPacket(p, packet);
}
public static boolean isSupported() {
return packetConstructor != null;
}
}
//Utils
private static final Field playerConnectionField = makeField(handle.getClass(), "playerConnection");
private static final Method sendPacketMethod = makeMethod(getNMSClass("PlayerConnection"), "sendPacket", getNMSClass("Packet"));
private static void sendPacket(Player player, Object packet) {
Object handle = getHandle(player);
Object connection = getField(playerConnectionField, handle);
callMethod(sendPacketMethod, connection, packet);
}
private static final Method addSiblingMethod = makeMethod(getNmsClass("IChatBaseComponent"), "addSibling", getNmsClass("IChatBaseComponent"));
private static final Method fromStringMethod = makeMethod(craftChatMessageClass, "fromString", String.class);
private static Object serialize(String text) { //Serialize to IChatBaseComponent
Class<?> craftChatMessageClass = getCbClass("util.CraftChatMessage");
Object baseComponentArray = callMethod(fromStringMethod, null, text);
assert baseChatComponentArray.isArray();
Object first = null;
for (int i = 0; i < Array.getLength(baseComponentArray); i++) {
Object baseComponent = Array.get(baseComponentArray, i);
if (first == null) {
first = baseComponent;
} else {
first = callMethod(addSiblingMethod, first, baseComponent);
}
}
return first;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment