From bd46c3f5a00bd3ed01904ee7dd5b0b6daa0069e4 Mon Sep 17 00:00:00 2001 From: Paulomart Date: Tue, 1 Mar 2016 22:28:18 +0100 Subject: [PATCH] Reflector Main Class and add API framework. --- .../{Core.java => ViaVersionPlugin.java} | 37 +++++++++++++++++-- .../us/myles/ViaVersion/api/ViaVersion.java | 17 +++++++++ .../myles/ViaVersion/api/ViaVersionAPI.java | 8 ++++ .../transformers/IncomingTransformer.java | 2 +- .../transformers/OutgoingTransformer.java | 8 +++- src/main/resources/plugin.yml | 2 +- 6 files changed, 67 insertions(+), 7 deletions(-) rename src/main/java/us/myles/ViaVersion/{Core.java => ViaVersionPlugin.java} (74%) create mode 100644 src/main/java/us/myles/ViaVersion/api/ViaVersion.java create mode 100644 src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java diff --git a/src/main/java/us/myles/ViaVersion/Core.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java similarity index 74% rename from src/main/java/us/myles/ViaVersion/Core.java rename to src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index cff6755f8..b647a00ef 100644 --- a/src/main/java/us/myles/ViaVersion/Core.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -8,18 +8,31 @@ import io.netty.channel.socket.SocketChannel; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; + +import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.handlers.ViaVersionInitializer; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -public class Core extends JavaPlugin { +public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { + + private final Set portedPlayers = Collections.newSetFromMap(new ConcurrentHashMap()); + @Override public void onEnable() { + ViaVersion.setInstance(this); System.out.println("ViaVersion enabled, injecting. (Allows 1.8 to be accessed via 1.9)"); try { injectPacketHandler(); @@ -30,6 +43,12 @@ public class Core extends JavaPlugin { System.out.println("Unable to inject handlers, are you on 1.8? "); e.printStackTrace(); } + Bukkit.getPluginManager().registerEvents(new Listener() { + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + setPorted(e.getPlayer().getUniqueId(), false); + } + }, this); } public void injectPacketHandler() throws Exception { @@ -51,10 +70,22 @@ public class Core extends JavaPlugin { } } + @Override + public boolean isPorted(Player player) { + return portedPlayers.contains(player.getUniqueId()); + } + + public void setPorted(UUID id, boolean value) { + if (value) { + portedPlayers.add(id); + } else { + portedPlayers.remove(id); + } + } public static Entity getEntity(final UUID player, final int id) { try { - return Bukkit.getScheduler().callSyncMethod(getPlugin(Core.class), new Callable() { + return Bukkit.getScheduler().callSyncMethod(getPlugin(ViaVersionPlugin.class), new Callable() { @Override public Entity call() throws Exception { Player p = Bukkit.getPlayer(player); @@ -76,7 +107,7 @@ public class Core extends JavaPlugin { public static ItemStack getHandItem(final ConnectionInfo info) { try { - return Bukkit.getScheduler().callSyncMethod(getPlugin(Core.class), new Callable() { + return Bukkit.getScheduler().callSyncMethod(getPlugin(ViaVersionPlugin.class), new Callable() { @Override public ItemStack call() throws Exception { if (info.getPlayer() != null) { diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersion.java b/src/main/java/us/myles/ViaVersion/api/ViaVersion.java new file mode 100644 index 000000000..857e2ab86 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersion.java @@ -0,0 +1,17 @@ +package us.myles.ViaVersion.api; + +public class ViaVersion { + + private static ViaVersionAPI INSTANCE; + + public static void setInstance(ViaVersionAPI api) { + if (INSTANCE != null) { + throw new IllegalStateException("Instance already set."); + } + INSTANCE = api; + } + + public static ViaVersionAPI getInstance() { + return INSTANCE; + } +} diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java new file mode 100644 index 000000000..cdccc80ce --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -0,0 +1,8 @@ +package us.myles.ViaVersion.api; + +import org.bukkit.entity.Player; + +public interface ViaVersionAPI { + + boolean isPorted(Player player); +} diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index 5ef68334f..84381a4a5 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -178,7 +178,7 @@ public class IncomingTransformer { output.writeByte(face); int hand = PacketUtil.readVarInt(input); - ItemStack inHand = Core.getHandItem(info); + ItemStack inHand = ViaVersionPlugin.getHandItem(info); Object item = null; try { Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 57a07b75f..55a7581eb 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Entity; import org.spacehq.mc.protocol.data.game.chunk.Column; import org.spacehq.mc.protocol.util.NetUtil; import us.myles.ViaVersion.*; +import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.metadata.MetaIndex; import us.myles.ViaVersion.metadata.NewType; @@ -29,6 +30,7 @@ public class OutgoingTransformer { private final Channel channel; private final ConnectionInfo info; private final ViaVersionInitializer init; + private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); private boolean cancel = false; private Map uuidMap = new HashMap(); @@ -177,7 +179,9 @@ public class OutgoingTransformer { if (packet == PacketType.LOGIN_SUCCESS) { String uu = PacketUtil.readString(input); PacketUtil.writeString(uu, output); - info.setUUID(UUID.fromString(uu)); + UUID uniqueId = UUID.fromString(uu); + info.setUUID(uniqueId); + plugin.setPorted(uniqueId, true); output.writeBytes(input); return; } @@ -205,7 +209,7 @@ public class OutgoingTransformer { try { List dw = ReflectionUtil.get(info.getLastPacket(), "b", List.class); // get entity via entityID, not preferred but we need it. - Entity entity = Core.getEntity(info.getUUID(), id); + Entity entity = ViaVersionPlugin.getEntity(info.getUUID(), id); if (entity != null) { transformMetadata(entity, dw, output); } else { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 66d889a9f..02f08ac2e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: ViaVersion -main: us.myles.ViaVersion.Core +main: us.myles.ViaVersion.ViaVersionPlugin author: _MylesC version: 0.3.4 \ No newline at end of file