diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 3aee4194f..0e4bde244 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -93,9 +93,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { getCommand("viaversion").setExecutor(commandHandler = new BukkitCommandHandler()); getCommand("viaversion").setTabCompleter(commandHandler); - // Register Protocol Listeners - ProtocolRegistry.registerListeners(); - // Warn them if they have anti-xray on and they aren't using spigot if (conf.isAntiXRay() && !spigot) { getLogger().info("You have anti-xray on in your config, since you're not using spigot it won't fix xray!"); @@ -104,7 +101,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { @Override public void onDisable() { - // TODO: Call ViaManager.destroy() + Via.getManager().destroy(); } public boolean isCompatSpigotBuild() { diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaLoader.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaLoader.java index b3e87fb44..34ad57130 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaLoader.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/BukkitViaLoader.java @@ -7,10 +7,18 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.listeners.UpdateListener; import us.myles.ViaVersion.listeners.protocol1_9to1_8.*; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ViaIdleThread; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; + +import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; @AllArgsConstructor public class BukkitViaLoader implements ViaPlatformLoader { @@ -32,6 +40,7 @@ public class BukkitViaLoader implements ViaPlatformLoader { }, plugin); /* 1.9 client to 1.8 server */ + new ArmorListener(plugin).register(); new CommandBlockListener(plugin).register(); new DeathListener(plugin).register(); @@ -47,5 +56,34 @@ public class BukkitViaLoader implements ViaPlatformLoader { new HandItemCache().runTaskTimerAsynchronously(plugin, 2L, 2L); // Updates player's items :) HandItemCache.CACHE = true; } + + /* Providers */ + Via.getManager().getProviders().use(HandItemProvider.class, new HandItemProvider() { + @Override + public Item getHandItem(final UserConnection info) { + if (HandItemCache.CACHE) { + return HandItemCache.getHandItem(info.get(ProtocolInfo.class).getUuid()); + } else { + try { + return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), new Callable() { + @Override + public Item call() throws Exception { + UUID playerUUID = info.get(ProtocolInfo.class).getUuid(); + if (Bukkit.getPlayer(playerUUID) != null) { + return HandItemCache.convert(Bukkit.getPlayer(playerUUID).getItemInHand()); + } + return null; + } + }).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + System.out.println("Error fetching hand item: " + e.getClass().getName()); + if (Via.getManager().isDebug()) + e.printStackTrace(); + return null; + } + } + } + }); + } } diff --git a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/HandItemCache.java b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/HandItemCache.java index 81abed547..9a05ad7b2 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/HandItemCache.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/listeners/protocol1_9to1_8/HandItemCache.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.listeners.protocol1_9to1_8; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -25,7 +26,7 @@ public class HandItemCache extends BukkitRunnable { List players = new ArrayList<>(handCache.keySet()); for (Player p : Bukkit.getOnlinePlayers()) { - handCache.put(p.getUniqueId(), Item.getItem(p.getItemInHand())); + handCache.put(p.getUniqueId(), convert(p.getItemInHand())); players.remove(p.getUniqueId()); } // Remove offline players @@ -33,4 +34,9 @@ public class HandItemCache extends BukkitRunnable { handCache.remove(uuid); } } + + public static Item convert(ItemStack itemInHand) { + if (itemInHand == null) return new Item((short) 0, (byte) 0, (short) 0, null); + return new Item((short) itemInHand.getTypeId(), (byte) itemInHand.getAmount(), itemInHand.getDurability(), null); + } } diff --git a/common/src/main/java/us/myles/ViaVersion/ViaManager.java b/common/src/main/java/us/myles/ViaVersion/ViaManager.java index 6ea315204..6f9227270 100644 --- a/common/src/main/java/us/myles/ViaVersion/ViaManager.java +++ b/common/src/main/java/us/myles/ViaVersion/ViaManager.java @@ -8,6 +8,7 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; +import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.commands.ViaCommandHandler; @@ -22,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; public class ViaManager { private ViaPlatform platform; private final Map portedPlayers = new ConcurrentHashMap<>(); + private ViaProviders providers = new ViaProviders(); @Setter private boolean debug = false; // Internals @@ -51,20 +53,30 @@ public class ViaManager { platform.runSync(new Runnable() { @Override public void run() { - ProtocolRegistry.SERVER_PROTOCOL = injector.getServerProtocolVersion(); - - // Check if there are any pipes to this version - if (ProtocolRegistry.SERVER_PROTOCOL != -1) { - getPlatform().getLogger().info("ViaVersion detected server version: " + ProtocolVersion.getProtocol(ProtocolRegistry.SERVER_PROTOCOL)); - if (!ProtocolRegistry.isWorkingPipe()) { - getPlatform().getLogger().warning("ViaVersion does not have any compatible versions for this server version, please read our resource page carefully."); - } - } - ProtocolRegistry.refreshVersions(); + onServerLoaded(); } }); } + public void onServerLoaded() { + // Load Server Protocol + ProtocolRegistry.SERVER_PROTOCOL = injector.getServerProtocolVersion(); + // Check if there are any pipes to this version + if (ProtocolRegistry.SERVER_PROTOCOL != -1) { + getPlatform().getLogger().info("ViaVersion detected server version: " + ProtocolVersion.getProtocol(ProtocolRegistry.SERVER_PROTOCOL)); + if (!ProtocolRegistry.isWorkingPipe()) { + getPlatform().getLogger().warning("ViaVersion does not have any compatible versions for this server version, please read our resource page carefully."); + } + } + // Load Listeners / Tasks + ProtocolRegistry.onServerLoaded(); + + // Load Platform + loader.load(); + + // Refresh Versions + ProtocolRegistry.refreshVersions(); + } public void destroy() { // Uninject diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index 075063c2b..ea56d6191 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -5,6 +5,7 @@ import lombok.Getter; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.packets.Direction; @@ -47,11 +48,22 @@ public abstract class Protocol { /** * Register listeners for this protocol + * + * @deprecated No longer used as listeners are registered in {@link us.myles.ViaVersion.api.platform.ViaPlatformLoader#load} */ + @Deprecated protected void registerListeners() { } + /** + * Register providers for this protocol + * + * @param providers The current providers + */ + protected void registerProviders(ViaProviders providers) { + + } /** * Register the packets for this protocol */ diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index 9f57a93fa..2b6aeb7a0 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -63,6 +63,7 @@ public class ProtocolRegistry { if (Via.getPlatform().isPluginEnabled()) { protocol.registerListeners(); + protocol.registerProviders(Via.getManager().getProviders()); refreshVersions(); } else { registerList.add(protocol); @@ -106,9 +107,10 @@ public class ProtocolRegistry { /** * Called when the server is enabled, to register any non registered listeners. */ - public static void registerListeners() { + public static void onServerLoaded() { for (Protocol protocol : registerList) { protocol.registerListeners(); + protocol.registerProviders(Via.getManager().getProviders()); } registerList.clear(); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java index c5c596b92..82a3c1939 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java @@ -8,19 +8,17 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Metadata1_8Type; import us.myles.ViaVersion.api.type.types.version.MetadataList1_8Type; -import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.*; import java.util.List; -import java.util.UUID; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; public class Protocol1_9TO1_8 extends Protocol { @Deprecated @@ -67,27 +65,7 @@ public class Protocol1_9TO1_8 extends Protocol { } public static Item getHandItem(final UserConnection info) { - if (HandItemCache.CACHE) { - return HandItemCache.getHandItem(info.get(ProtocolInfo.class).getUuid()); - } else { - try { - return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), new Callable() { - @Override - public Item call() throws Exception { - UUID playerUUID = info.get(ProtocolInfo.class).getUuid(); - if (Bukkit.getPlayer(playerUUID) != null) { - return Item.getItem(Bukkit.getPlayer(playerUUID).getItemInHand()); - } - return null; - } - }).get(10, TimeUnit.SECONDS); - } catch (Exception e) { - System.out.println("Error fetching hand item: " + e.getClass().getName()); - if (Via.getManager().isDebug()) - e.printStackTrace(); - return null; - } - } + return Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(info); } @Override @@ -100,8 +78,8 @@ public class Protocol1_9TO1_8 extends Protocol { } @Override - protected void registerListeners() { - + protected void registerProviders(ViaProviders providers) { + providers.register(HandItemProvider.class, new HandItemProvider()); } @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/HandItemProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/HandItemProvider.java new file mode 100644 index 000000000..a839dc353 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/HandItemProvider.java @@ -0,0 +1,11 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.providers; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.platform.providers.Provider; + +public class HandItemProvider implements Provider { + public Item getHandItem(final UserConnection info) { + return new Item((short) 0, (byte) 0, (short) 0, null); + } +}