Removed dependency on craftbukkit by using reflection. (Took a while to learn and sort out :P but should be useful for the future anyway so its time well spent :D)

This commit is contained in:
Alastair 2016-08-03 03:30:59 +00:00 committed by Max Qian
parent e4d187be52
commit 62037634bc
3 changed files with 68 additions and 24 deletions

View File

@ -29,12 +29,13 @@
<version>1.10.2-R0.1-SNAPSHOT</version> <version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <!-- spigot's buildtools automatically places it in maven's repository after building it -->
<!-- spigot's buildtools automatically places it in maven's repository after building it --> <!-- used for net.minecraft.server code -->
<!--<dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId> <artifactId>craftbukkit</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version> <version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>-->
</dependencies> </dependencies>
</project> </project>

View File

@ -30,7 +30,7 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
try { try {
this.compat = new CraftBukkit(version); this.compat = new CraftBukkit(this, version);
ConfigAccessor portalConfig = new ConfigAccessor(this, "portals.yml"); ConfigAccessor portalConfig = new ConfigAccessor(this, "portals.yml");
portalConfig.saveDefaultConfig(); portalConfig.saveDefaultConfig();
@ -79,7 +79,7 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
this.getLogger().warning("This version of craftbukkit is not yet supported, please notify sekwah and tell him about this version v:" + version); this.getLogger().warning("This version of craftbukkit is not yet supported, please notify sekwah and tell him about this version v:" + version);
this.setEnabled(false); this.setEnabled(false);
} catch (IllegalArgumentException | } catch (IllegalArgumentException |
NoSuchFieldException | SecurityException e) { NoSuchFieldException | SecurityException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
this.getLogger().warning("Something went wrong, please notify sekwah and tell him about this version v:" + version); this.getLogger().warning("Something went wrong, please notify sekwah and tell him about this version v:" + version);
this.getLogger().warning("Along with the above stacktrace"); this.getLogger().warning("Along with the above stacktrace");

View File

@ -1,8 +1,12 @@
package com.sekwah.advancedportals.compat; package com.sekwah.advancedportals.compat;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/** /**
* Created by on 02/08/2016. * Created by on 02/08/2016.
@ -14,41 +18,77 @@ import java.lang.reflect.Field;
*/ */
public class CraftBukkit { public class CraftBukkit {
private final String craftBukkitVer;
private final String craftBukkitPackage; private final String craftBukkitPackage;
private final String minecraftPackage; private final String minecraftPackage;
private final AdvancedPortalsPlugin plugin;
private Method chatMessageMethod;
private Class<?> craftPlayer;
private Class<?> chatPacket;
private Class<?> chatBaseComponent;
private Class<?> packet;
// Classes so it doesnt keep fetching them. // Classes so it doesnt keep fetching them.
private Class<?> chatBaseComponent;
private Class<?> chatSerializer;
public CraftBukkit(String craftBukkitVer) throws ClassNotFoundException, NoSuchFieldException { public CraftBukkit(AdvancedPortalsPlugin plugin, String craftBukkitVer) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
this.craftBukkitVer = craftBukkitVer;
this.craftBukkitPackage = "org.bukkit.craftbukkit." + craftBukkitVer;
this.minecraftPackage = "net.minecraft.server." + craftBukkitVer;
this.craftBukkitPackage = "org.bukkit.craftbukkit." + craftBukkitVer + ".";
this.minecraftPackage = "net.minecraft.server." + craftBukkitVer + ".";
this.plugin = plugin;
this.setupCompat(); this.setupCompat();
} }
private void setupCompat() throws ClassNotFoundException, NoSuchFieldException { private void setupCompat() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
this.craftPlayer = Class.forName(craftBukkitPackage + "entity.CraftPlayer");
this.chatBaseComponent = Class.forName(minecraftPackage + "IChatBaseComponent"); this.chatBaseComponent = Class.forName(minecraftPackage + "IChatBaseComponent");
Field modfield = chatBaseComponent.getDeclaredField("modifiers");
chatBaseComponent.getDeclaredClasses(); this.chatMessageMethod = this.findClass(chatBaseComponent, "ChatSerializer").getMethod("a", String.class);
this.chatPacket = Class.forName(minecraftPackage + "PacketPlayOutChat");
this.packet = Class.forName(minecraftPackage + "Packet");
} }
// Convert to reflection
public void sendRawMessage(String rawMessage, Player player) { public void sendRawMessage(String rawMessage, Player player) {
this.sendMessage(rawMessage,player, (byte) 1);
}
public void sendActionBarMessage(String rawMessage, Player player) {
this.sendMessage(rawMessage,player, (byte) 2);
}
public void sendMessage(String rawMessage, Player player, byte msgType) {
try { try {
Object comp = this.chatMessageMethod.invoke(null,rawMessage);
Class<?> nmsClass = Class.forName(minecraftPackage + "IChatBaseComponent"); Object handle = this.craftPlayer.getMethod("getHandle").invoke(player);
} catch (ClassNotFoundException e) { Field playerConnectionObj = handle.getClass().getDeclaredField("playerConnection");
Constructor<?> packetConstructor = this.chatPacket.getConstructor(this.chatBaseComponent, byte.class);
Object packet = packetConstructor.newInstance(comp, msgType);
Object playerConnection = playerConnectionObj.get(handle);
playerConnection.getClass().getMethod("sendPacket", this.packet).invoke(playerConnection, packet);
} catch (IllegalAccessException |InvocationTargetException | NoSuchMethodException | NoSuchFieldException
| InstantiationException e) {
this.plugin.getLogger().warning("Error creating raw message, something must be wrong with reflection");
e.printStackTrace(); e.printStackTrace();
} }
@ -58,11 +98,14 @@ public class CraftBukkit {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);*/ ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);*/
} }
public void sendActionBarMessage(String rawMessage, Player player) {
/*IChatBaseComponent comp = IChatBaseComponent.ChatSerializer.a(rawMessage); public Class<?> findClass(Class<?> classObj, String className){
// "json message", position(0: chat (chat box), 1: system message (chat box), 2: above action bar) for(Class<?> classes : classObj.getDeclaredClasses()){
PacketPlayOutChat packet = new PacketPlayOutChat(comp, (byte) 2); if(classes.getSimpleName().equals(className)){
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);*/ return classes;
}
}
return null;
} }