Use HandItemProvider for 1.8 hand items

This commit is contained in:
Myles 2016-09-25 18:06:04 +01:00
parent 84ee4410a6
commit 52610f7c66
8 changed files with 99 additions and 43 deletions

View File

@ -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() {

View File

@ -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<Item>() {
@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;
}
}
}
});
}
}

View File

@ -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<UUID> 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);
}
}

View File

@ -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<UUID, UserConnection> 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

View File

@ -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
*/

View File

@ -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();
}

View File

@ -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<Item>() {
@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

View File

@ -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);
}
}