diff --git a/pom.xml b/pom.xml
index 5cb3fd7..30f463e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,11 +32,11 @@
1.10.2-R0.1-SNAPSHOT
provided
-
+
\ No newline at end of file
diff --git a/src/com/sekwah/advancedportals/AdvancedPortalsPlugin.java b/src/com/sekwah/advancedportals/AdvancedPortalsPlugin.java
index 9b74b2b..ede6fba 100644
--- a/src/com/sekwah/advancedportals/AdvancedPortalsPlugin.java
+++ b/src/com/sekwah/advancedportals/AdvancedPortalsPlugin.java
@@ -36,7 +36,7 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
try {
- this.compat = new CraftBukkit(version);
+ this.compat = new CraftBukkit(this, version);
ConfigAccessor portalConfig = new ConfigAccessor(this, "portals.yml");
portalConfig.saveDefaultConfig();
@@ -84,7 +84,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.setEnabled(false);
} catch (IllegalArgumentException |
- NoSuchFieldException | SecurityException e) {
+ NoSuchFieldException | SecurityException | NoSuchMethodException e) {
e.printStackTrace();
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");
diff --git a/src/com/sekwah/advancedportals/compat/CraftBukkit.java b/src/com/sekwah/advancedportals/compat/CraftBukkit.java
index 481b464..a6cfa20 100644
--- a/src/com/sekwah/advancedportals/compat/CraftBukkit.java
+++ b/src/com/sekwah/advancedportals/compat/CraftBukkit.java
@@ -1,8 +1,12 @@
package com.sekwah.advancedportals.compat;
+import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import org.bukkit.entity.Player;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
/**
* Created by on 02/08/2016.
@@ -14,41 +18,77 @@ import java.lang.reflect.Field;
*/
public class CraftBukkit {
- private final String craftBukkitVer;
-
private final String craftBukkitPackage;
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.
- private Class> chatBaseComponent;
- private Class> chatSerializer;
- public CraftBukkit(String craftBukkitVer) throws ClassNotFoundException, NoSuchFieldException {
- this.craftBukkitVer = craftBukkitVer;
- this.craftBukkitPackage = "org.bukkit.craftbukkit." + craftBukkitVer;
- this.minecraftPackage = "net.minecraft.server." + craftBukkitVer;
+ public CraftBukkit(AdvancedPortalsPlugin plugin, String craftBukkitVer) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
+ this.craftBukkitPackage = "org.bukkit.craftbukkit." + craftBukkitVer + ".";
+
+ this.minecraftPackage = "net.minecraft.server." + craftBukkitVer + ".";
+
+ this.plugin = plugin;
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");
- 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) {
+ 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 {
+ 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();
}
@@ -58,11 +98,14 @@ public class CraftBukkit {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);*/
}
- public void sendActionBarMessage(String rawMessage, Player player) {
- /*IChatBaseComponent comp = IChatBaseComponent.ChatSerializer.a(rawMessage);
- // "json message", position(0: chat (chat box), 1: system message (chat box), 2: above action bar)
- PacketPlayOutChat packet = new PacketPlayOutChat(comp, (byte) 2);
- ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);*/
+
+ public Class> findClass(Class> classObj, String className){
+ for(Class> classes : classObj.getDeclaredClasses()){
+ if(classes.getSimpleName().equals(className)){
+ return classes;
+ }
+ }
+ return null;
}