From 525e5e58b4da5823b89b238c213a123f82f8f2f5 Mon Sep 17 00:00:00 2001 From: cnaude Date: Wed, 15 May 2013 18:38:58 -0700 Subject: [PATCH] Make ProtocolLib optional. --- src/net/spoothie/chairs/ChairWatcher.java | 30 +++++++++++ src/net/spoothie/chairs/Chairs.java | 66 +++++++++++++++-------- src/plugin.yml | 4 +- 3 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 src/net/spoothie/chairs/ChairWatcher.java diff --git a/src/net/spoothie/chairs/ChairWatcher.java b/src/net/spoothie/chairs/ChairWatcher.java new file mode 100644 index 0000000..7e8a1de --- /dev/null +++ b/src/net/spoothie/chairs/ChairWatcher.java @@ -0,0 +1,30 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package net.spoothie.chairs; + +import java.util.ArrayList; +import net.minecraft.server.v1_5_R3.DataWatcher; +import net.minecraft.server.v1_5_R3.WatchableObject; + +/** + * + * @author cnaude + */ +public class ChairWatcher extends DataWatcher { + + private byte metadata; + + public ChairWatcher(byte i) { + this.metadata = i; + } + + @Override + public ArrayList b() { + ArrayList list = new ArrayList(); + WatchableObject wo = new WatchableObject(0, 0, Byte.valueOf(this.metadata)); + list.add(wo); + return list; + } +} \ No newline at end of file diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index c1f92fe..cef863e 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -10,15 +10,16 @@ import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import net.minecraft.server.v1_5_R3.Packet40EntityMetadata; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -49,11 +50,6 @@ public class Chairs extends JavaPlugin { @Override public void onEnable() { - if (!checkForProtocolLib()) { - logError("This plugin requires ProtocolLib. Please download the latest: http://dev.bukkit.org/server-mods/protocollib/"); - Bukkit.getServer().getPluginManager().disablePlugin(this); - return; - } instance = this; ignoreList = new ChairsIgnoreList(); ignoreList.load(); @@ -70,7 +66,12 @@ public class Chairs extends JavaPlugin { logInfo("Enabling sitting effects."); chairEffects = new ChairEffects(this); } - protocolManager = ProtocolLibrary.getProtocolManager(); + if (isProtocolLibLoaded()) { + logInfo("ProtocolLib detected."); + protocolManager = ProtocolLibrary.getProtocolManager(); + } else { + logInfo("ProtocolLib not detected. Using NMS code methods instead."); + } } @Override @@ -113,6 +114,10 @@ public class Chairs extends JavaPlugin { } } } + + public boolean isProtocolLibLoaded() { + return (getServer().getPluginManager().getPlugin("ProtocolLib") != null); + } public void loadConfig() { autoRotate = getConfig().getBoolean("auto-rotate"); @@ -228,15 +233,34 @@ public class Chairs extends JavaPlugin { // Send sit packet to all online players that are on same world and can see player public void sendSit(Player p) { - sendPacketToPlayers(getSitPacket(p),p); - } + if (protocolManager != null) { + sendPacketToPlayers(getSitPacket(p),p); + } else { + Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); + sendPacketToPlayers(packet,p); + } + } private void sendPacketToPlayers(PacketContainer pc, Player p) { for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { if (onlinePlayer.canSee(p)) { if (onlinePlayer.getWorld().equals(p.getWorld())) { - try { - protocolManager.sendServerPacket(onlinePlayer, pc); + try { + protocolManager.sendServerPacket(onlinePlayer, pc); + } catch (Exception ex) { + // Nothing here + } + } + } + } + } + + private void sendPacketToPlayers(Packet40EntityMetadata packet, Player p) { + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + if (onlinePlayer.canSee(p)) { + if (onlinePlayer.getWorld().equals(p.getWorld())) { + try { + ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet); } catch (Exception ex) { // Nothing here } @@ -261,8 +285,13 @@ public class Chairs extends JavaPlugin { p.sendMessage(msgStanding); } sit.remove(p.getName()); - } - sendPacketToPlayers(getStandPacket(p),p); + } + if (protocolManager != null) { + sendPacketToPlayers(getStandPacket(p),p); + } else { + Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false); + sendPacketToPlayers(packet,p); + } } public void logInfo(String _message) { @@ -276,14 +305,5 @@ public class Chairs extends JavaPlugin { public static Chairs get() { return instance; } - - public boolean checkForProtocolLib() { - Plugin plugin = getServer().getPluginManager().getPlugin("ProtocolLib"); - if (plugin == null) { - return false; - } else { - return true; - } - } - + } diff --git a/src/plugin.yml b/src/plugin.yml index 20c8e7a..2f5d37a 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,10 +1,12 @@ name: Chairs -version: 2.0.3 +version: 2.1.3 description: Let players sit on blocks. authors: - spoothie - cnaude main: net.spoothie.chairs.Chairs +softdepend: +- ProtocolLib commands: chairs: description: Various commands for Chairs.