From 965c42ccca71ad0d4b96de430902cd66cb05e77c Mon Sep 17 00:00:00 2001 From: confuser Date: Fri, 19 Dec 2014 12:52:38 +0000 Subject: [PATCH 01/15] Added Spigot 1.8 support Fixes issue #42 and fixes issue #45 --- src/me/confuser/barapi/BarAPI.java | 2 +- src/me/confuser/barapi/Util.java | 15 ++- src/me/confuser/barapi/nms/v1_8.java | 180 +++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 src/me/confuser/barapi/nms/v1_8.java diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index 28d8786..cdea414 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -467,7 +467,7 @@ private static FakeDragon addDragon(Player player, Location loc, String message) } private static Location getDragonLocation(Location loc) { - if (!useSpigotHack()) { + if (Util.isBelowGround) { loc.subtract(0, 300, 0); return loc; } diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index 8ec8f87..71ee7fa 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -24,6 +24,7 @@ public class Util { public static boolean newProtocol = false; public static String version; public static Class fakeDragonClass = v1_6.class; + public static boolean isBelowGround = true; static { detectVersion(); @@ -33,14 +34,22 @@ public static void detectVersion() { if (BarAPI.useSpigotHack()) { fakeDragonClass = v1_8Fake.class; version = "v1_7_R4."; + isBelowGround = false; } else { String name = Bukkit.getServer().getClass().getPackage().getName(); String mcVersion = name.substring(name.lastIndexOf('.') + 1); String[] versions = mcVersion.split("_"); - if (versions[0].equals("v1") && Integer.parseInt(versions[1]) > 6) { - newProtocol = true; - fakeDragonClass = v1_7.class; + if (versions[0].equals("v1")) { + int minor = Integer.parseInt(versions[1]); + + if (minor == 7) { + newProtocol = true; + fakeDragonClass = v1_7.class; + } else if (minor == 8) { + fakeDragonClass = v1_8.class; + isBelowGround = false; + } } version = mcVersion + "."; diff --git a/src/me/confuser/barapi/nms/v1_8.java b/src/me/confuser/barapi/nms/v1_8.java new file mode 100644 index 0000000..ff1e3dc --- /dev/null +++ b/src/me/confuser/barapi/nms/v1_8.java @@ -0,0 +1,180 @@ +package me.confuser.barapi.nms; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import me.confuser.barapi.Util; + +import org.bukkit.Location; + +public class v1_8 extends FakeDragon { + private Object dragon; + private int id; + + public v1_8(String name, Location loc) { + super(name, loc); + } + + @Override + public Object getSpawnPacket() { + Class Entity = Util.getCraftClass("Entity"); + Class EntityLiving = Util.getCraftClass("EntityLiving"); + Class EntityEnderDragon = Util.getCraftClass("EntityEnderDragon"); + Object packet = null; + + try { + dragon = EntityEnderDragon.getConstructor(Util.getCraftClass("World")).newInstance(getWorld()); + + Method setLocation = Util.getMethod(EntityEnderDragon, "setLocation", new Class[] { double.class, double.class, double.class, float.class, float.class }); + setLocation.invoke(dragon, getX(), getY(), getZ(), getPitch(), getYaw()); + + Method setInvisible = Util.getMethod(EntityEnderDragon, "setInvisible", new Class[] { boolean.class }); + setInvisible.invoke(dragon, true); + + Method setCustomName = Util.getMethod(EntityEnderDragon, "setCustomName", new Class[] { String.class }); + setCustomName.invoke(dragon, name); + + Method setHealth = Util.getMethod(EntityEnderDragon, "setHealth", new Class[] { float.class }); + setHealth.invoke(dragon, health); + + Field motX = Util.getField(Entity, "motX"); + motX.set(dragon, getXvel()); + + Field motY = Util.getField(Entity, "motY"); + motY.set(dragon, getYvel()); + + Field motZ = Util.getField(Entity, "motZ"); + motZ.set(dragon, getZvel()); + + Method getId = Util.getMethod(EntityEnderDragon, "getId", new Class[] {}); + this.id = (Integer) getId.invoke(dragon); + + Class PacketPlayOutSpawnEntityLiving = Util.getCraftClass("PacketPlayOutSpawnEntityLiving"); + + packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class[] { EntityLiving }).newInstance(dragon); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getDestroyPacket() { + Class PacketPlayOutEntityDestroy = Util.getCraftClass("PacketPlayOutEntityDestroy"); + + Object packet = null; + try { + packet = PacketPlayOutEntityDestroy.newInstance(); + Field a = PacketPlayOutEntityDestroy.getDeclaredField("a"); + a.setAccessible(true); + a.set(packet, new int[] { id }); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getMetaPacket(Object watcher) { + Class DataWatcher = Util.getCraftClass("DataWatcher"); + + Class PacketPlayOutEntityMetadata = Util.getCraftClass("PacketPlayOutEntityMetadata"); + + Object packet = null; + try { + packet = PacketPlayOutEntityMetadata.getConstructor(new Class[] { int.class, DataWatcher, boolean.class }).newInstance(id, watcher, true); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getTeleportPacket(Location loc) { + Class PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport"); + Object packet = null; + + try { + packet = PacketPlayOutEntityTeleport.getConstructor(new Class[] { int.class, int.class, int.class, int.class, byte.class, byte.class, boolean.class }).newInstance(this.id, loc.getBlockX() * 32, loc.getBlockY() * 32, loc.getBlockZ() * 32, (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360), false); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getWatcher() { + Class Entity = Util.getCraftClass("Entity"); + Class DataWatcher = Util.getCraftClass("DataWatcher"); + + Object watcher = null; + try { + watcher = DataWatcher.getConstructor(new Class[] { Entity }).newInstance(dragon); + Method a = Util.getMethod(DataWatcher, "a", new Class[] { int.class, Object.class }); + + a.invoke(watcher, 5, isVisible() ? (byte) 0 : (byte) 0x20); + a.invoke(watcher, 6, (Float) health); + a.invoke(watcher, 7, (Integer) 0); + a.invoke(watcher, 8, (Byte) (byte) 0); + a.invoke(watcher, 10, name); + a.invoke(watcher, 11, (Byte) (byte) 1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return watcher; + } +} From 2cc78bf8f6d26bf8aea9310d5230b6218c978ca1 Mon Sep 17 00:00:00 2001 From: confuser Date: Fri, 19 Dec 2014 12:56:58 +0000 Subject: [PATCH 02/15] Updated to version 3.3 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 41de64d..9e2af70 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ Allows plugins to control the boss health bar message me.confuser BarAPI - 3.3-SNAPSHOT + 3.3 BarAPI From b71451c75b39f1cf68ae44e16652665825347c3d Mon Sep 17 00:00:00 2001 From: confuser Date: Fri, 19 Dec 2014 12:57:28 +0000 Subject: [PATCH 03/15] Get ready for next version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9e2af70..83bd0bf 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ Allows plugins to control the boss health bar message me.confuser BarAPI - 3.3 + 3.4-SNAPSHOT BarAPI From b49590a8356dde1cdb7c32e55a5b33ad8661f270 Mon Sep 17 00:00:00 2001 From: confuser Date: Fri, 19 Dec 2014 13:01:40 +0000 Subject: [PATCH 04/15] Removed project file, shouldn't be here --- .project | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 .project diff --git a/.project b/.project deleted file mode 100644 index 52727aa..0000000 --- a/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - BarAPI - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.m2e.core.maven2Nature - org.eclipse.jdt.core.javanature - - From 8d07e1d9f0855ec4b72b3474bd78f7ac7dd7f36e Mon Sep 17 00:00:00 2001 From: stefvanschie Date: Wed, 16 Dec 2015 17:24:58 +0100 Subject: [PATCH 05/15] BarAPI.java Automatically remove old bars when new ones are created --- src/me/confuser/barapi/BarAPI.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index cdea414..e825daf 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -197,6 +197,8 @@ public static void setMessage(String message) { * It will be cut to that size automatically. */ public static void setMessage(Player player, String message) { + if (hasBar(player)) + removeBar(player); FakeDragon dragon = getDragon(player, message); dragon.name = cleanMessage(message); @@ -249,6 +251,9 @@ public static void setMessage(String message, float percent) { public static void setMessage(Player player, String message, float percent) { Validate.isTrue(0F <= percent && percent <= 100F, "Percent must be between 0F and 100F, but was: ", percent); + if (hasBar(player)) + removeBar(player); + FakeDragon dragon = getDragon(player, message); dragon.name = cleanMessage(message); @@ -305,6 +310,9 @@ public static void setMessage(String message, int seconds) { public static void setMessage(final Player player, String message, int seconds) { Validate.isTrue(seconds > 0, "Seconds must be above 1 but was: ", seconds); + if (hasBar(player)) + removeBar(player); + FakeDragon dragon = getDragon(player, message); dragon.name = cleanMessage(message); From 36056ccc0a110f193544934181166ea74fb3a344 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sun, 31 Jan 2016 16:41:31 +0000 Subject: [PATCH 06/15] Bumped updater --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 83bd0bf..ec2d680 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ net.gravitydevelopment.updater updater - 2.1 + 2.3 org.mcstats.bukkit From ebceefc43d327d1993852606eab476dc5ec74a2d Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 13:08:48 +0000 Subject: [PATCH 07/15] Formatting --- .gitattributes | 1 + src/me/confuser/barapi/BarAPI.java | 939 ++++++++++++++--------------- 2 files changed, 464 insertions(+), 476 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index e825daf..3396421 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -25,484 +25,471 @@ /** * Allows plugins to safely set a health bar message. - * + * * @author James Mortemore */ public class BarAPI extends JavaPlugin implements Listener { - private static HashMap players = new HashMap(); - private static HashMap timers = new HashMap(); - - private static BarAPI plugin; - - private static boolean useSpigotHack = false; - - public void onEnable() { - getConfig().options().copyDefaults(true); - saveConfig(); - - if (getConfig().getBoolean("autoUpdate")) { - new Updater(this, 64876, getFile(), Updater.UpdateType.DEFAULT, false); - } - - try { - MetricsLite metrics = new MetricsLite(this); - metrics.start(); - } catch (IOException e) { - // Failed to submit the stats :-( - } - - plugin = this; - - useSpigotHack = getConfig().getBoolean("useSpigotHack", false); - - if (!useSpigotHack) { - if (v1_8Fake.isUsable()) { - useSpigotHack = true; - Util.detectVersion(); - getLogger().info("Detected spigot hack, enabling fake 1.8"); - } - } - - getServer().getPluginManager().registerEvents(this, this); - - getLogger().info("Loaded"); - - if (useSpigotHack) { - getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { - public void run() { - for (UUID uuid : players.keySet()) { - Player p = Bukkit.getPlayer(uuid); - Util.sendPacket(p, players.get(uuid).getTeleportPacket(getDragonLocation(p.getLocation()))); - } - } - }, 0L, 5L); - } - - // TestMode - if (getConfig().getBoolean("testMode")) { - plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() { - - @Override - public void run() { - for (Player player : plugin.getServer().getOnlinePlayers()) { - BarAPI.setMessage(player, ChatColor.AQUA + "Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI", 10); - } - } - - }, 30L, 300L); - } - } - - public void onDisable() { - for (Player player : plugin.getServer().getOnlinePlayers()) { - quit(player); - } - - players.clear(); - - for (int timerID : timers.values()) { - Bukkit.getScheduler().cancelTask(timerID); - } - - timers.clear(); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void PlayerLoggout(PlayerQuitEvent event) { - quit(event.getPlayer()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerKick(PlayerKickEvent event) { - quit(event.getPlayer()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerTeleport(final PlayerTeleportEvent event) { - handleTeleport(event.getPlayer(), event.getTo().clone()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerTeleport(final PlayerRespawnEvent event) { - handleTeleport(event.getPlayer(), event.getRespawnLocation().clone()); - } - - private void handleTeleport(final Player player, final Location loc) { - - if (!hasBar(player)) - return; - - Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { - - @Override - public void run() { - // Check if the player still has a dragon after the two ticks! ;) - if (!hasBar(player)) - return; - - FakeDragon oldDragon = getDragon(player, ""); - - float health = oldDragon.health; - String message = oldDragon.name; - - Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); - - players.remove(player.getUniqueId()); - - FakeDragon dragon = addDragon(player, loc, message); - dragon.health = health; - - sendDragon(dragon, player); - } - - }, 2L); - } - - private void quit(Player player) { - removeBar(player); - } - - public static boolean useSpigotHack() { - return useSpigotHack; - } - - /** - * Set a message for all players.
- * It will remain there until the player logs off or another plugin overrides it.
- * This method will show a full health bar and will cancel any running timers. - * - * @param message - * The message shown.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - * @see BarAPI#setMessage(player, message) - */ - public static void setMessage(String message) { - for (Player player : Bukkit.getOnlinePlayers()) { - setMessage(player, message); - } - } - - /** - * Set a message for the given player.
- * It will remain there until the player logs off or another plugin overrides it.
- * This method will show a full health bar and will cancel any running timers. - * - * @param player - * The player who should see the given message. - * @param message - * The message shown to the player.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - */ - public static void setMessage(Player player, String message) { - if (hasBar(player)) - removeBar(player); - FakeDragon dragon = getDragon(player, message); - - dragon.name = cleanMessage(message); - dragon.health = dragon.getMaxHealth(); - - cancelTimer(player); - - sendDragon(dragon, player); - } - - /** - * Set a message for all players.
- * It will remain there for each player until the player logs off or another plugin overrides it.
- * This method will show a health bar using the given percentage value and will cancel any running timers. - * - * @param message - * The message shown to the player.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - * @param percent - * The percentage of the health bar filled.
- * This value must be between 0F (inclusive) and 100F (inclusive). - * @throws IllegalArgumentException - * If the percentage is not within valid bounds. - * @see BarAPI#setMessage(player, message, percent) - */ - public static void setMessage(String message, float percent) { - for (Player player : Bukkit.getOnlinePlayers()) { - setMessage(player, message, percent); - } - } - - /** - * Set a message for the given player.
- * It will remain there until the player logs off or another plugin overrides it.
- * This method will show a health bar using the given percentage value and will cancel any running timers. - * - * @param player - * The player who should see the given message. - * @param message - * The message shown to the player.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - * @param percent - * The percentage of the health bar filled.
- * This value must be between 0F (inclusive) and 100F (inclusive). - * @throws IllegalArgumentException - * If the percentage is not within valid bounds. - */ - public static void setMessage(Player player, String message, float percent) { - Validate.isTrue(0F <= percent && percent <= 100F, "Percent must be between 0F and 100F, but was: ", percent); - - if (hasBar(player)) - removeBar(player); - - FakeDragon dragon = getDragon(player, message); - - dragon.name = cleanMessage(message); - dragon.health = (percent / 100f) * dragon.getMaxHealth(); - - cancelTimer(player); - - sendDragon(dragon, player); - } - - /** - * Set a message for all players.
- * It will remain there for each player until the player logs off or another plugin overrides it.
- * This method will use the health bar as a decreasing timer, all previously started timers will be cancelled.
- * The timer starts with a full bar.
- * The health bar will be removed automatically if it hits zero. - * - * @param message - * The message shown.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - * @param seconds - * The amount of seconds displayed by the timer.
- * Supports values above 1 (inclusive). - * @throws IllegalArgumentException - * If seconds is zero or below. - * @see BarAPI#setMessage(player, message, seconds) - */ - public static void setMessage(String message, int seconds) { - for (Player player : Bukkit.getOnlinePlayers()) { - setMessage(player, message, seconds); - } - } - - /** - * Set a message for the given player.
- * It will remain there until the player logs off or another plugin overrides it.
- * This method will use the health bar as a decreasing timer, all previously started timers will be cancelled.
- * The timer starts with a full bar.
- * The health bar will be removed automatically if it hits zero. - * - * @param player - * The player who should see the given timer/message. - * @param message - * The message shown to the player.
- * Due to limitations in Minecraft this message cannot be longer than 64 characters.
- * It will be cut to that size automatically. - * @param seconds - * The amount of seconds displayed by the timer.
- * Supports values above 1 (inclusive). - * @throws IllegalArgumentException - * If seconds is zero or below. - */ - public static void setMessage(final Player player, String message, int seconds) { - Validate.isTrue(seconds > 0, "Seconds must be above 1 but was: ", seconds); - - if (hasBar(player)) - removeBar(player); - - FakeDragon dragon = getDragon(player, message); - - dragon.name = cleanMessage(message); - dragon.health = dragon.getMaxHealth(); - - final float dragonHealthMinus = dragon.getMaxHealth() / seconds; - - cancelTimer(player); - - timers.put(player.getUniqueId(), Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() { - - @Override - public void run() { - FakeDragon drag = getDragon(player, ""); - drag.health -= dragonHealthMinus; - - if (drag.health <= 1) { - removeBar(player); - cancelTimer(player); - } else { - sendDragon(drag, player); - } - } - - }, 20L, 20L).getTaskId()); - - sendDragon(dragon, player); - } - - /** - * Checks whether the given player has a bar. - * - * @param player - * The player who should be checked. - * @return True, if the player has a bar, False otherwise. - */ - public static boolean hasBar(Player player) { - return players.get(player.getUniqueId()) != null; - } - - /** - * Removes the bar from the given player.
- * If the player has no bar, this method does nothing. - * - * @param player - * The player whose bar should be removed. - */ - public static void removeBar(Player player) { - if (!hasBar(player)) - return; - - Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); - - players.remove(player.getUniqueId()); - - cancelTimer(player); - } - - /** - * Modifies the health of an existing bar.
- * If the player has no bar, this method does nothing. - * - * @param player - * The player whose bar should be modified. - * @param percent - * The percentage of the health bar filled.
- * This value must be between 0F and 100F (inclusive). - */ - public static void setHealth(Player player, float percent) { - if (!hasBar(player)) - return; - - FakeDragon dragon = getDragon(player, ""); - dragon.health = (percent / 100f) * dragon.getMaxHealth(); - - cancelTimer(player); - - if (percent == 0) { - removeBar(player); - } else { - sendDragon(dragon, player); - } - } - - /** - * Get the health of an existing bar. - * - * @param player - * The player whose bar's health should be returned. - * @return The current absolute health of the bar.
- * If the player has no bar, this method returns -1. - */ - public static float getHealth(Player player) { - if (!hasBar(player)) - return -1; - - return getDragon(player, "").health; - } - - /** - * Get the message of an existing bar. - * - * @param player - * The player whose bar's message should be returned. - * @return The current message displayed to the player.
- * If the player has no bar, this method returns an empty string. - */ - public static String getMessage(Player player) { - if (!hasBar(player)) - return ""; - - return getDragon(player, "").name; - } - - private static String cleanMessage(String message) { - if (message.length() > 64) - message = message.substring(0, 63); - - return message; - } - - private static void cancelTimer(Player player) { - Integer timerID = timers.remove(player.getUniqueId()); - - if (timerID != null) { - Bukkit.getScheduler().cancelTask(timerID); - } - } - - private static void sendDragon(FakeDragon dragon, Player player) { - Util.sendPacket(player, dragon.getMetaPacket(dragon.getWatcher())); - Util.sendPacket(player, dragon.getTeleportPacket(getDragonLocation(player.getLocation()))); - } - - private static FakeDragon getDragon(Player player, String message) { - if (hasBar(player)) { - return players.get(player.getUniqueId()); - } else - return addDragon(player, cleanMessage(message)); - } - - private static FakeDragon addDragon(Player player, String message) { - FakeDragon dragon = Util.newDragon(message, getDragonLocation(player.getLocation())); - - Util.sendPacket(player, dragon.getSpawnPacket()); - - players.put(player.getUniqueId(), dragon); - - return dragon; - } - - private static FakeDragon addDragon(Player player, Location loc, String message) { - FakeDragon dragon = Util.newDragon(message, getDragonLocation(loc)); - - Util.sendPacket(player, dragon.getSpawnPacket()); - - players.put(player.getUniqueId(), dragon); - - return dragon; - } - - private static Location getDragonLocation(Location loc) { - if (Util.isBelowGround) { - loc.subtract(0, 300, 0); - return loc; - } - - float pitch = loc.getPitch(); - - if (pitch >= 55) { - loc.add(0, -300, 0); - } else if (pitch <= -55) { - loc.add(0, 300, 0); - } else { - loc = loc.getBlock().getRelative(getDirection(loc), plugin.getServer().getViewDistance() * 16).getLocation(); - } - - return loc; - } - - private static BlockFace getDirection(Location loc) { - float dir = Math.round(loc.getYaw() / 90); - if (dir == -4 || dir == 0 || dir == 4) - return BlockFace.SOUTH; - if (dir == -1 || dir == 3) - return BlockFace.EAST; - if (dir == -2 || dir == 2) - return BlockFace.NORTH; - if (dir == -3 || dir == 1) - return BlockFace.WEST; - return null; - } + + private static HashMap players = new HashMap(); + private static HashMap timers = new HashMap(); + + private static BarAPI plugin; + + private static boolean useSpigotHack = false; + + public static boolean useSpigotHack() { + return useSpigotHack; + } + + /** + * Set a message for all players.
+ * It will remain there until the player logs off or another plugin overrides it.
+ * This method will show a full health bar and will cancel any running timers. + * + * @param message The message shown.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + * + * @see BarAPI#setMessage(player, message) + */ + public static void setMessage(String message) { + for (Player player : Bukkit.getOnlinePlayers()) { + setMessage(player, message); + } + } + + /** + * Set a message for the given player.
+ * It will remain there until the player logs off or another plugin overrides it.
+ * This method will show a full health bar and will cancel any running timers. + * + * @param player The player who should see the given message. + * @param message The message shown to the player.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + */ + public static void setMessage(Player player, String message) { + if (hasBar(player)) + removeBar(player); + FakeDragon dragon = getDragon(player, message); + + dragon.name = cleanMessage(message); + dragon.health = dragon.getMaxHealth(); + + cancelTimer(player); + + sendDragon(dragon, player); + } + + /** + * Set a message for all players.
+ * It will remain there for each player until the player logs off or another plugin overrides it.
+ * This method will show a health bar using the given percentage value and will cancel any running timers. + * + * @param message The message shown to the player.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + * @param percent The percentage of the health bar filled.
+ * This value must be between 0F (inclusive) and 100F (inclusive). + * + * @throws IllegalArgumentException If the percentage is not within valid bounds. + * @see BarAPI#setMessage(player, message, percent) + */ + public static void setMessage(String message, float percent) { + for (Player player : Bukkit.getOnlinePlayers()) { + setMessage(player, message, percent); + } + } + + /** + * Set a message for the given player.
+ * It will remain there until the player logs off or another plugin overrides it.
+ * This method will show a health bar using the given percentage value and will cancel any running timers. + * + * @param player The player who should see the given message. + * @param message The message shown to the player.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + * @param percent The percentage of the health bar filled.
+ * This value must be between 0F (inclusive) and 100F (inclusive). + * + * @throws IllegalArgumentException If the percentage is not within valid bounds. + */ + public static void setMessage(Player player, String message, float percent) { + Validate.isTrue(0F <= percent && percent <= 100F, "Percent must be between 0F and 100F, but was: ", percent); + + if (hasBar(player)) + removeBar(player); + + FakeDragon dragon = getDragon(player, message); + + dragon.name = cleanMessage(message); + dragon.health = (percent / 100f) * dragon.getMaxHealth(); + + cancelTimer(player); + + sendDragon(dragon, player); + } + + /** + * Set a message for all players.
+ * It will remain there for each player until the player logs off or another plugin overrides it.
+ * This method will use the health bar as a decreasing timer, all previously started timers will be cancelled.
+ * The timer starts with a full bar.
+ * The health bar will be removed automatically if it hits zero. + * + * @param message The message shown.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + * @param seconds The amount of seconds displayed by the timer.
+ * Supports values above 1 (inclusive). + * + * @throws IllegalArgumentException If seconds is zero or below. + * @see BarAPI#setMessage(player, message, seconds) + */ + public static void setMessage(String message, int seconds) { + for (Player player : Bukkit.getOnlinePlayers()) { + setMessage(player, message, seconds); + } + } + + /** + * Set a message for the given player.
+ * It will remain there until the player logs off or another plugin overrides it.
+ * This method will use the health bar as a decreasing timer, all previously started timers will be cancelled.
+ * The timer starts with a full bar.
+ * The health bar will be removed automatically if it hits zero. + * + * @param player The player who should see the given timer/message. + * @param message The message shown to the player.
+ * Due to limitations in Minecraft this message cannot be longer than 64 characters.
+ * It will be cut to that size automatically. + * @param seconds The amount of seconds displayed by the timer.
+ * Supports values above 1 (inclusive). + * + * @throws IllegalArgumentException If seconds is zero or below. + */ + public static void setMessage(final Player player, String message, int seconds) { + Validate.isTrue(seconds > 0, "Seconds must be above 1 but was: ", seconds); + + if (hasBar(player)) + removeBar(player); + + FakeDragon dragon = getDragon(player, message); + + dragon.name = cleanMessage(message); + dragon.health = dragon.getMaxHealth(); + + final float dragonHealthMinus = dragon.getMaxHealth() / seconds; + + cancelTimer(player); + + timers.put(player.getUniqueId(), Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() { + + @Override + public void run() { + FakeDragon drag = getDragon(player, ""); + drag.health -= dragonHealthMinus; + + if (drag.health <= 1) { + removeBar(player); + cancelTimer(player); + } else { + sendDragon(drag, player); + } + } + + }, 20L, 20L).getTaskId()); + + sendDragon(dragon, player); + } + + /** + * Checks whether the given player has a bar. + * + * @param player The player who should be checked. + * + * @return True, if the player has a bar, False otherwise. + */ + public static boolean hasBar(Player player) { + return players.get(player.getUniqueId()) != null; + } + + /** + * Removes the bar from the given player.
+ * If the player has no bar, this method does nothing. + * + * @param player The player whose bar should be removed. + */ + public static void removeBar(Player player) { + if (!hasBar(player)) + return; + + Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); + + players.remove(player.getUniqueId()); + + cancelTimer(player); + } + + /** + * Modifies the health of an existing bar.
+ * If the player has no bar, this method does nothing. + * + * @param player The player whose bar should be modified. + * @param percent The percentage of the health bar filled.
+ * This value must be between 0F and 100F (inclusive). + */ + public static void setHealth(Player player, float percent) { + if (!hasBar(player)) + return; + + FakeDragon dragon = getDragon(player, ""); + dragon.health = (percent / 100f) * dragon.getMaxHealth(); + + cancelTimer(player); + + if (percent == 0) { + removeBar(player); + } else { + sendDragon(dragon, player); + } + } + + /** + * Get the health of an existing bar. + * + * @param player The player whose bar's health should be returned. + * + * @return The current absolute health of the bar.
+ * If the player has no bar, this method returns -1. + */ + public static float getHealth(Player player) { + if (!hasBar(player)) + return -1; + + return getDragon(player, "").health; + } + + /** + * Get the message of an existing bar. + * + * @param player The player whose bar's message should be returned. + * + * @return The current message displayed to the player.
+ * If the player has no bar, this method returns an empty string. + */ + public static String getMessage(Player player) { + if (!hasBar(player)) + return ""; + + return getDragon(player, "").name; + } + + private static String cleanMessage(String message) { + if (message.length() > 64) + message = message.substring(0, 63); + + return message; + } + + private static void cancelTimer(Player player) { + Integer timerID = timers.remove(player.getUniqueId()); + + if (timerID != null) { + Bukkit.getScheduler().cancelTask(timerID); + } + } + + private static void sendDragon(FakeDragon dragon, Player player) { + Util.sendPacket(player, dragon.getMetaPacket(dragon.getWatcher())); + Util.sendPacket(player, dragon.getTeleportPacket(getDragonLocation(player.getLocation()))); + } + + private static FakeDragon getDragon(Player player, String message) { + if (hasBar(player)) { + return players.get(player.getUniqueId()); + } else + return addDragon(player, cleanMessage(message)); + } + + private static FakeDragon addDragon(Player player, String message) { + FakeDragon dragon = Util.newDragon(message, getDragonLocation(player.getLocation())); + + Util.sendPacket(player, dragon.getSpawnPacket()); + + players.put(player.getUniqueId(), dragon); + + return dragon; + } + + private static FakeDragon addDragon(Player player, Location loc, String message) { + FakeDragon dragon = Util.newDragon(message, getDragonLocation(loc)); + + Util.sendPacket(player, dragon.getSpawnPacket()); + + players.put(player.getUniqueId(), dragon); + + return dragon; + } + + private static Location getDragonLocation(Location loc) { + if (Util.isBelowGround) { + loc.subtract(0, 300, 0); + return loc; + } + + float pitch = loc.getPitch(); + + if (pitch >= 55) { + loc.add(0, -300, 0); + } else if (pitch <= -55) { + loc.add(0, 300, 0); + } else { + loc = loc.getBlock().getRelative(getDirection(loc), plugin.getServer().getViewDistance() * 16).getLocation(); + } + + return loc; + } + + private static BlockFace getDirection(Location loc) { + float dir = Math.round(loc.getYaw() / 90); + if (dir == -4 || dir == 0 || dir == 4) + return BlockFace.SOUTH; + if (dir == -1 || dir == 3) + return BlockFace.EAST; + if (dir == -2 || dir == 2) + return BlockFace.NORTH; + if (dir == -3 || dir == 1) + return BlockFace.WEST; + return null; + } + + public void onEnable() { + getConfig().options().copyDefaults(true); + saveConfig(); + + if (getConfig().getBoolean("autoUpdate")) { + new Updater(this, 64876, getFile(), Updater.UpdateType.DEFAULT, false); + } + + try { + MetricsLite metrics = new MetricsLite(this); + metrics.start(); + } catch (IOException e) { + // Failed to submit the stats :-( + } + + plugin = this; + + useSpigotHack = getConfig().getBoolean("useSpigotHack", false); + + if (!useSpigotHack) { + if (v1_8Fake.isUsable()) { + useSpigotHack = true; + Util.detectVersion(); + getLogger().info("Detected spigot hack, enabling fake 1.8"); + } + } + + getServer().getPluginManager().registerEvents(this, this); + + getLogger().info("Loaded"); + + if (useSpigotHack) { + getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { + + public void run() { + for (UUID uuid : players.keySet()) { + Player p = Bukkit.getPlayer(uuid); + Util.sendPacket(p, players.get(uuid).getTeleportPacket(getDragonLocation(p.getLocation()))); + } + } + }, 0L, 5L); + } + + // TestMode + if (getConfig().getBoolean("testMode")) { + plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() { + + @Override + public void run() { + for (Player player : plugin.getServer().getOnlinePlayers()) { + BarAPI.setMessage(player, ChatColor.AQUA + "Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI Testing BarAPI", 10); + } + } + + }, 30L, 300L); + } + } + + public void onDisable() { + for (Player player : plugin.getServer().getOnlinePlayers()) { + quit(player); + } + + players.clear(); + + for (int timerID : timers.values()) { + Bukkit.getScheduler().cancelTask(timerID); + } + + timers.clear(); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void PlayerLoggout(PlayerQuitEvent event) { + quit(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerKick(PlayerKickEvent event) { + quit(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(final PlayerTeleportEvent event) { + handleTeleport(event.getPlayer(), event.getTo().clone()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(final PlayerRespawnEvent event) { + handleTeleport(event.getPlayer(), event.getRespawnLocation().clone()); + } + + private void handleTeleport(final Player player, final Location loc) { + + if (!hasBar(player)) + return; + + Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { + + @Override + public void run() { + // Check if the player still has a dragon after the two ticks! ;) + if (!hasBar(player)) + return; + + FakeDragon oldDragon = getDragon(player, ""); + + float health = oldDragon.health; + String message = oldDragon.name; + + Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); + + players.remove(player.getUniqueId()); + + FakeDragon dragon = addDragon(player, loc, message); + dragon.health = health; + + sendDragon(dragon, player); + } + + }, 2L); + } + + private void quit(Player player) { + removeBar(player); + } } From 717c34f473846330d662395b5680beca5ff26787 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 15:07:35 +0000 Subject: [PATCH 08/15] Added 1.9 support Fixed #62 --- pom.xml | 192 +++++++++++------------ src/me/confuser/barapi/BarAPI.java | 47 ++++-- src/me/confuser/barapi/Util.java | 6 +- src/me/confuser/barapi/nms/v1_8Fake.java | 4 +- src/me/confuser/barapi/nms/v1_9.java | 58 +++++++ 5 files changed, 198 insertions(+), 109 deletions(-) create mode 100644 src/me/confuser/barapi/nms/v1_9.java diff --git a/pom.xml b/pom.xml index ec2d680..4c0c1be 100644 --- a/pom.xml +++ b/pom.xml @@ -1,98 +1,98 @@ - 4.0.0 - BarAPI - Allows plugins to control the boss health bar message - me.confuser - BarAPI - 3.4-SNAPSHOT - - BarAPI - - - . - true - ${basedir} - - *.yml - - - - src - - - maven-compiler-plugin - 3.1 - - 1.6 - 1.6 - - - - org.apache.maven.plugins - maven-shade-plugin - 1.5 - - - package - - shade - - - true - - - net.gravitydevelopment.updater - me.confuser.barapi.updater - - - org.mcstats - me.confuser.barapi.mcstats - - - - - - - - - - - bukkit-repo - http://repo.bukkit.org/content/groups/public - - - gravity-repo - http://repo.gravitydevelopment.net - - - Plugin Metrics - http://repo.mcstats.org/content/repositories/public - - - - - org.bukkit - bukkit - 1.7.10-R0.1-SNAPSHOT - provided - - - org.bukkit - craftbukkit - 1.7.10-R0.1-SNAPSHOT - provided - - - net.gravitydevelopment.updater - updater - 2.3 - - - org.mcstats.bukkit - metrics-lite - R6 - compile - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + BarAPI + Allows plugins to control the boss health bar message + me.confuser + BarAPI + 3.4-SNAPSHOT + + BarAPI + + + . + true + ${basedir} + + *.yml + + + + src + + + maven-compiler-plugin + 3.1 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-shade-plugin + 1.5 + + + package + + shade + + + true + + + net.gravitydevelopment.updater + me.confuser.barapi.updater + + + org.mcstats + me.confuser.barapi.mcstats + + + + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + gravity-repo + http://repo.gravitydevelopment.net + + + Plugin Metrics + http://repo.mcstats.org/content/repositories/snapshots + + + + + org.spigotmc + spigot-api + 1.9-R0.1-SNAPSHOT + provided + + + org.bukkit + bukkit + 1.9-R0.1-SNAPSHOT + provided + + + net.gravitydevelopment.updater + updater + 2.3 + + + org.mcstats.bukkit + metrics-lite + R8-SNAPSHOT + compile + + diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index 3396421..78bbd5c 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -1,13 +1,15 @@ package me.confuser.barapi; import me.confuser.barapi.nms.FakeDragon; -import me.confuser.barapi.nms.v1_8Fake; +import me.confuser.barapi.nms.v1_8fake; +import me.confuser.barapi.nms.v1_9; import net.gravitydevelopment.updater.Updater; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.block.BlockFace; +import org.bukkit.boss.BossBar; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -226,7 +228,13 @@ public static void removeBar(Player player) { if (!hasBar(player)) return; - Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); + FakeDragon dragon = getDragon(player, ""); + + if (dragon instanceof v1_9) { + ((v1_9) dragon).getBar().removePlayer(player); + } else { + Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); + } players.remove(player.getUniqueId()); @@ -303,8 +311,15 @@ private static void cancelTimer(Player player) { } private static void sendDragon(FakeDragon dragon, Player player) { - Util.sendPacket(player, dragon.getMetaPacket(dragon.getWatcher())); - Util.sendPacket(player, dragon.getTeleportPacket(getDragonLocation(player.getLocation()))); + if (dragon instanceof v1_9) { + BossBar bar = ((v1_9) dragon).getBar(); + + bar.addPlayer(player); + bar.setProgress(dragon.health / dragon.getMaxHealth()); + } else { + Util.sendPacket(player, dragon.getMetaPacket(dragon.getWatcher())); + Util.sendPacket(player, dragon.getTeleportPacket(getDragonLocation(player.getLocation()))); + } } private static FakeDragon getDragon(Player player, String message) { @@ -317,7 +332,13 @@ private static FakeDragon getDragon(Player player, String message) { private static FakeDragon addDragon(Player player, String message) { FakeDragon dragon = Util.newDragon(message, getDragonLocation(player.getLocation())); - Util.sendPacket(player, dragon.getSpawnPacket()); + if (dragon instanceof v1_9) { + BossBar bar = ((v1_9) dragon).getBar(); + + bar.addPlayer(player); + } else { + Util.sendPacket(player, dragon.getSpawnPacket()); + } players.put(player.getUniqueId(), dragon); @@ -327,7 +348,13 @@ private static FakeDragon addDragon(Player player, String message) { private static FakeDragon addDragon(Player player, Location loc, String message) { FakeDragon dragon = Util.newDragon(message, getDragonLocation(loc)); - Util.sendPacket(player, dragon.getSpawnPacket()); + if (dragon instanceof v1_9) { + BossBar bar = ((v1_9) dragon).getBar(); + + bar.addPlayer(player); + } else { + Util.sendPacket(player, dragon.getSpawnPacket()); + } players.put(player.getUniqueId(), dragon); @@ -386,7 +413,7 @@ public void onEnable() { useSpigotHack = getConfig().getBoolean("useSpigotHack", false); if (!useSpigotHack) { - if (v1_8Fake.isUsable()) { + if (v1_8fake.isUsable()) { useSpigotHack = true; Util.detectVersion(); getLogger().info("Detected spigot hack, enabling fake 1.8"); @@ -463,6 +490,10 @@ private void handleTeleport(final Player player, final Location loc) { if (!hasBar(player)) return; + final FakeDragon oldDragon = getDragon(player, ""); + + if (oldDragon instanceof v1_9) return; + Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { @Override @@ -471,8 +502,6 @@ public void run() { if (!hasBar(player)) return; - FakeDragon oldDragon = getDragon(player, ""); - float health = oldDragon.health; String message = oldDragon.name; diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index 71ee7fa..e2508a6 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -32,7 +32,7 @@ public class Util { public static void detectVersion() { if (BarAPI.useSpigotHack()) { - fakeDragonClass = v1_8Fake.class; + fakeDragonClass = v1_8fake.class; version = "v1_7_R4."; isBelowGround = false; } else { @@ -49,7 +49,9 @@ public static void detectVersion() { } else if (minor == 8) { fakeDragonClass = v1_8.class; isBelowGround = false; - } + } else if (minor == 9) { + fakeDragonClass = v1_9.class; + } } version = mcVersion + "."; diff --git a/src/me/confuser/barapi/nms/v1_8Fake.java b/src/me/confuser/barapi/nms/v1_8Fake.java index a5da361..fd638dc 100644 --- a/src/me/confuser/barapi/nms/v1_8Fake.java +++ b/src/me/confuser/barapi/nms/v1_8Fake.java @@ -17,11 +17,11 @@ * @author James Mortemore */ -public class v1_8Fake extends FakeDragon { +public class v1_8fake extends FakeDragon { private Object dragon; private int id; - public v1_8Fake(String name, Location loc) { + public v1_8fake(String name, Location loc) { super(name, loc); } diff --git a/src/me/confuser/barapi/nms/v1_9.java b/src/me/confuser/barapi/nms/v1_9.java new file mode 100644 index 0000000..ef5a811 --- /dev/null +++ b/src/me/confuser/barapi/nms/v1_9.java @@ -0,0 +1,58 @@ +package me.confuser.barapi.nms; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; + +/** + * This is the FakeDragon class for BarAPI. + * It is based on the code by SoThatsIt. + *

+ * http://forums.bukkit.org/threads/tutorial-utilizing-the-boss-health-bar.158018/page-5#post-2053705 + * + * @author James Mortemore + */ + +public class v1_9 extends FakeDragon { + + private Object dragon; + private int id; + private BossBar bar; + + public v1_9(String name, Location loc) { + super(name, loc); + + bar = Bukkit.createBossBar(name, BarColor.PINK, BarStyle.SOLID); + } + + public BossBar getBar() { + return bar; + } + + @Override + public Object getSpawnPacket() { + return null; + } + + @Override + public Object getDestroyPacket() { + return null; + } + + @Override + public Object getMetaPacket(Object watcher) { + return null; + } + + @Override + public Object getTeleportPacket(Location loc) { + return null; + } + + @Override + public Object getWatcher() { + return null; + } +} From b4b770bf501484bde63c00b6ad231b9437959cd2 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 15:09:14 +0000 Subject: [PATCH 09/15] Deprecated all methods --- src/me/confuser/barapi/BarAPI.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index 78bbd5c..9f2f2eb 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -55,6 +55,7 @@ public static boolean useSpigotHack() { * * @see BarAPI#setMessage(player, message) */ + @Deprecated public static void setMessage(String message) { for (Player player : Bukkit.getOnlinePlayers()) { setMessage(player, message); @@ -71,6 +72,7 @@ public static void setMessage(String message) { * Due to limitations in Minecraft this message cannot be longer than 64 characters.
* It will be cut to that size automatically. */ + @Deprecated public static void setMessage(Player player, String message) { if (hasBar(player)) removeBar(player); @@ -98,6 +100,7 @@ public static void setMessage(Player player, String message) { * @throws IllegalArgumentException If the percentage is not within valid bounds. * @see BarAPI#setMessage(player, message, percent) */ + @Deprecated public static void setMessage(String message, float percent) { for (Player player : Bukkit.getOnlinePlayers()) { setMessage(player, message, percent); @@ -118,6 +121,7 @@ public static void setMessage(String message, float percent) { * * @throws IllegalArgumentException If the percentage is not within valid bounds. */ + @Deprecated public static void setMessage(Player player, String message, float percent) { Validate.isTrue(0F <= percent && percent <= 100F, "Percent must be between 0F and 100F, but was: ", percent); @@ -150,6 +154,7 @@ public static void setMessage(Player player, String message, float percent) { * @throws IllegalArgumentException If seconds is zero or below. * @see BarAPI#setMessage(player, message, seconds) */ + @Deprecated public static void setMessage(String message, int seconds) { for (Player player : Bukkit.getOnlinePlayers()) { setMessage(player, message, seconds); @@ -172,6 +177,7 @@ public static void setMessage(String message, int seconds) { * * @throws IllegalArgumentException If seconds is zero or below. */ + @Deprecated public static void setMessage(final Player player, String message, int seconds) { Validate.isTrue(seconds > 0, "Seconds must be above 1 but was: ", seconds); @@ -214,6 +220,7 @@ public void run() { * * @return True, if the player has a bar, False otherwise. */ + @Deprecated public static boolean hasBar(Player player) { return players.get(player.getUniqueId()) != null; } @@ -224,6 +231,7 @@ public static boolean hasBar(Player player) { * * @param player The player whose bar should be removed. */ + @Deprecated public static void removeBar(Player player) { if (!hasBar(player)) return; @@ -249,6 +257,7 @@ public static void removeBar(Player player) { * @param percent The percentage of the health bar filled.
* This value must be between 0F and 100F (inclusive). */ + @Deprecated public static void setHealth(Player player, float percent) { if (!hasBar(player)) return; @@ -273,6 +282,7 @@ public static void setHealth(Player player, float percent) { * @return The current absolute health of the bar.
* If the player has no bar, this method returns -1. */ + @Deprecated public static float getHealth(Player player) { if (!hasBar(player)) return -1; @@ -288,6 +298,7 @@ public static float getHealth(Player player) { * @return The current message displayed to the player.
* If the player has no bar, this method returns an empty string. */ + @Deprecated public static String getMessage(Player player) { if (!hasBar(player)) return ""; From fe48d27fda615a5505538a5965a4247bc4d65b07 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 15:12:14 +0000 Subject: [PATCH 10/15] Fixed case issue --- src/me/confuser/barapi/nms/v1_8Fake.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/me/confuser/barapi/nms/v1_8Fake.java b/src/me/confuser/barapi/nms/v1_8Fake.java index fd638dc..a5da361 100644 --- a/src/me/confuser/barapi/nms/v1_8Fake.java +++ b/src/me/confuser/barapi/nms/v1_8Fake.java @@ -17,11 +17,11 @@ * @author James Mortemore */ -public class v1_8fake extends FakeDragon { +public class v1_8Fake extends FakeDragon { private Object dragon; private int id; - public v1_8fake(String name, Location loc) { + public v1_8Fake(String name, Location loc) { super(name, loc); } From a3a3aa46fe8d5911c8f01cadccbc4bd874a1638b Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 15:13:46 +0000 Subject: [PATCH 11/15] Formatting --- src/me/confuser/barapi/BarAPI.java | 4 +- src/me/confuser/barapi/Util.java | 358 ++++++++++---------- src/me/confuser/barapi/nms/v1_8Fake.java | 394 ++++++++++++----------- 3 files changed, 381 insertions(+), 375 deletions(-) diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index 9f2f2eb..53bdafa 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -1,7 +1,7 @@ package me.confuser.barapi; import me.confuser.barapi.nms.FakeDragon; -import me.confuser.barapi.nms.v1_8fake; +import me.confuser.barapi.nms.v1_8Fake; import me.confuser.barapi.nms.v1_9; import net.gravitydevelopment.updater.Updater; import org.apache.commons.lang.Validate; @@ -424,7 +424,7 @@ public void onEnable() { useSpigotHack = getConfig().getBoolean("useSpigotHack", false); if (!useSpigotHack) { - if (v1_8fake.isUsable()) { + if (v1_8Fake.isUsable()) { useSpigotHack = true; Util.detectVersion(); getLogger().info("Detected spigot hack, enabling fake 1.8"); diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index e2508a6..d391b45 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -1,199 +1,199 @@ package me.confuser.barapi; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - import me.confuser.barapi.nms.*; - import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** * This is a utility class for BarAPI. It is based on the code by SoThatsIt. - * + *

* http://forums.bukkit.org/threads/tutorial-utilizing-the-boss-health-bar * .158018/page-2#post-1760928 - * + * * @author James Mortemore */ public class Util { - public static boolean newProtocol = false; - public static String version; - public static Class fakeDragonClass = v1_6.class; - public static boolean isBelowGround = true; - - static { - detectVersion(); - } - - public static void detectVersion() { - if (BarAPI.useSpigotHack()) { - fakeDragonClass = v1_8fake.class; - version = "v1_7_R4."; - isBelowGround = false; - } else { - String name = Bukkit.getServer().getClass().getPackage().getName(); - String mcVersion = name.substring(name.lastIndexOf('.') + 1); - String[] versions = mcVersion.split("_"); - - if (versions[0].equals("v1")) { - int minor = Integer.parseInt(versions[1]); - - if (minor == 7) { - newProtocol = true; - fakeDragonClass = v1_7.class; - } else if (minor == 8) { - fakeDragonClass = v1_8.class; - isBelowGround = false; - } else if (minor == 9) { + + public static boolean newProtocol = false; + public static String version; + public static Class fakeDragonClass = v1_6.class; + public static boolean isBelowGround = true; + + static { + detectVersion(); + } + + public static void detectVersion() { + if (BarAPI.useSpigotHack()) { + fakeDragonClass = v1_8Fake.class; + version = "v1_7_R4."; + isBelowGround = false; + } else { + String name = Bukkit.getServer().getClass().getPackage().getName(); + String mcVersion = name.substring(name.lastIndexOf('.') + 1); + String[] versions = mcVersion.split("_"); + + if (versions[0].equals("v1")) { + int minor = Integer.parseInt(versions[1]); + + if (minor == 7) { + newProtocol = true; + fakeDragonClass = v1_7.class; + } else if (minor == 8) { + fakeDragonClass = v1_8.class; + isBelowGround = false; + } else if (minor == 9) { fakeDragonClass = v1_9.class; } - } - - version = mcVersion + "."; - } - } - - public static FakeDragon newDragon(String message, Location loc) { - FakeDragon fakeDragon = null; - - try { - fakeDragon = (FakeDragon) fakeDragonClass.getConstructor(String.class, Location.class).newInstance(message, loc); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - - return fakeDragon; - } - - // Reflection Util - public static void sendPacket(Player p, Object packet) { - try { - Object nmsPlayer = getHandle(p); - Field con_field = nmsPlayer.getClass().getField("playerConnection"); - Object con = con_field.get(nmsPlayer); - Method packet_method = getMethod(con.getClass(), "sendPacket"); - packet_method.invoke(con, packet); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - } - - public static Class getCraftClass(String ClassName) { - String className = "net.minecraft.server." + version + ClassName; - Class c = null; - try { - c = Class.forName(className); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - return c; - } - - public static Object getHandle(World world) { - Object nms_entity = null; - Method entity_getHandle = getMethod(world.getClass(), "getHandle"); - try { - nms_entity = entity_getHandle.invoke(world); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return nms_entity; - } - - public static Object getHandle(Entity entity) { - Object nms_entity = null; - Method entity_getHandle = getMethod(entity.getClass(), "getHandle"); - try { - nms_entity = entity_getHandle.invoke(entity); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return nms_entity; - } - - public static Field getField(Class cl, String field_name) { - try { - Field field = cl.getDeclaredField(field_name); - return field; - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - return null; - } - - public static Method getMethod(Class cl, String method, Class[] args) { - for (Method m : cl.getMethods()) { - if (m.getName().equals(method) && ClassListEqual(args, m.getParameterTypes())) { - return m; - } - } - return null; - } - - public static Method getMethod(Class cl, String method, Integer args) { - for (Method m : cl.getMethods()) { - if (m.getName().equals(method) && args.equals(new Integer(m.getParameterTypes().length))) { - return m; - } - } - return null; - } - - public static Method getMethod(Class cl, String method) { - for (Method m : cl.getMethods()) { - if (m.getName().equals(method)) { - return m; - } - } - return null; - } - - public static boolean ClassListEqual(Class[] l1, Class[] l2) { - boolean equal = true; - - if (l1.length != l2.length) - return false; - for (int i = 0; i < l1.length; i++) { - if (l1[i] != l2[i]) { - equal = false; - break; - } - } - - return equal; - } + } + + version = mcVersion + "."; + } + } + + public static FakeDragon newDragon(String message, Location loc) { + FakeDragon fakeDragon = null; + + try { + fakeDragon = (FakeDragon) fakeDragonClass.getConstructor(String.class, Location.class).newInstance(message, loc); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return fakeDragon; + } + + // Reflection Util + public static void sendPacket(Player p, Object packet) { + try { + Object nmsPlayer = getHandle(p); + Field con_field = nmsPlayer.getClass().getField("playerConnection"); + Object con = con_field.get(nmsPlayer); + Method packet_method = getMethod(con.getClass(), "sendPacket"); + packet_method.invoke(con, packet); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + + public static Class getCraftClass(String ClassName) { + String className = "net.minecraft.server." + version + ClassName; + Class c = null; + try { + c = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return c; + } + + public static Object getHandle(World world) { + Object nms_entity = null; + Method entity_getHandle = getMethod(world.getClass(), "getHandle"); + try { + nms_entity = entity_getHandle.invoke(world); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return nms_entity; + } + + public static Object getHandle(Entity entity) { + Object nms_entity = null; + Method entity_getHandle = getMethod(entity.getClass(), "getHandle"); + try { + nms_entity = entity_getHandle.invoke(entity); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return nms_entity; + } + + public static Field getField(Class cl, String field_name) { + try { + Field field = cl.getDeclaredField(field_name); + return field; + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + return null; + } + + public static Method getMethod(Class cl, String method, Class[] args) { + for (Method m : cl.getMethods()) { + if (m.getName().equals(method) && ClassListEqual(args, m.getParameterTypes())) { + return m; + } + } + return null; + } + + public static Method getMethod(Class cl, String method, Integer args) { + for (Method m : cl.getMethods()) { + if (m.getName().equals(method) && args.equals(new Integer(m.getParameterTypes().length))) { + return m; + } + } + return null; + } + + public static Method getMethod(Class cl, String method) { + for (Method m : cl.getMethods()) { + if (m.getName().equals(method)) { + return m; + } + } + return null; + } + + public static boolean ClassListEqual(Class[] l1, Class[] l2) { + boolean equal = true; + + if (l1.length != l2.length) + return false; + for (int i = 0; i < l1.length; i++) { + if (l1[i] != l2[i]) { + equal = false; + break; + } + } + + return equal; + } } \ No newline at end of file diff --git a/src/me/confuser/barapi/nms/v1_8Fake.java b/src/me/confuser/barapi/nms/v1_8Fake.java index a5da361..180d37e 100644 --- a/src/me/confuser/barapi/nms/v1_8Fake.java +++ b/src/me/confuser/barapi/nms/v1_8Fake.java @@ -1,204 +1,210 @@ package me.confuser.barapi.nms; +import me.confuser.barapi.Util; +import org.bukkit.Location; + import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import me.confuser.barapi.Util; - -import org.bukkit.Location; - /** -* This is the FakeDragon class for BarAPI. -* It is based on the code by SoThatsIt. -* -* http://forums.bukkit.org/threads/tutorial-utilizing-the-boss-health-bar.158018/page-5#post-2053705 -* -* @author James Mortemore -*/ + * This is the FakeDragon class for BarAPI. + * It is based on the code by SoThatsIt. + *

+ * http://forums.bukkit.org/threads/tutorial-utilizing-the-boss-health-bar.158018/page-5#post-2053705 + * + * @author James Mortemore + */ public class v1_8Fake extends FakeDragon { - private Object dragon; - private int id; - - public v1_8Fake(String name, Location loc) { - super(name, loc); - } - - @Override - public Object getSpawnPacket() { - Class Entity = Util.getCraftClass("Entity"); - Class EntityLiving = Util.getCraftClass("EntityLiving"); - Class EntityEnderDragon = Util.getCraftClass("EntityEnderDragon"); - Object packet = null; - try { - dragon = EntityEnderDragon.getConstructor(Util.getCraftClass("World")).newInstance(getWorld()); - - Method setLocation = Util.getMethod(EntityEnderDragon, "setLocation", new Class[] { double.class, double.class, double.class, float.class, float.class }); - setLocation.invoke(dragon, getX(), getY(), getZ(), getPitch(), getYaw()); - - Method setInvisible = Util.getMethod(EntityEnderDragon, "setInvisible", new Class[] { boolean.class }); - setInvisible.invoke(dragon, true); - - Method setCustomName = Util.getMethod(EntityEnderDragon, "setCustomName", new Class[] { String.class }); - setCustomName.invoke(dragon, name); - - Method setHealth = Util.getMethod(EntityEnderDragon, "setHealth", new Class[] { float.class }); - setHealth.invoke(dragon, health); - - Field motX = Util.getField(Entity, "motX"); - motX.set(dragon, getXvel()); - - Field motY = Util.getField(Entity, "motY"); - motY.set(dragon, getYvel()); - - Field motZ = Util.getField(Entity, "motZ"); - motZ.set(dragon, getZvel()); - - Method getId = Util.getMethod(EntityEnderDragon, "getId", new Class[] {}); - this.id = (Integer) getId.invoke(dragon); - - Class PacketPlayOutSpawnEntityLiving = Util.getCraftClass("PacketPlayOutSpawnEntityLiving"); - - packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class[] { EntityLiving }).newInstance(dragon); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getDestroyPacket() { - Class PacketPlayOutEntityDestroy = Util.getCraftClass("PacketPlayOutEntityDestroy"); - - Object packet = null; - try { - packet = PacketPlayOutEntityDestroy.newInstance(); - Field a = PacketPlayOutEntityDestroy.getDeclaredField("a"); - a.setAccessible(true); - a.set(packet, new int[] { id }); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getMetaPacket(Object watcher) { - Class DataWatcher = Util.getCraftClass("DataWatcher"); - - Class PacketPlayOutEntityMetadata = Util.getCraftClass("PacketPlayOutEntityMetadata"); - - Object packet = null; - try { - packet = PacketPlayOutEntityMetadata.getConstructor(new Class[] { int.class, DataWatcher, boolean.class }).newInstance(id, watcher, true); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getTeleportPacket(Location loc) { - Class PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport"); - Object packet = null; - - try { - packet = PacketPlayOutEntityTeleport.getConstructor(new Class[] { int.class, int.class, int.class, int.class, byte.class, byte.class, boolean.class, boolean.class }).newInstance(this.id, loc.getBlockX() * 32, loc.getBlockY() * 32, loc.getBlockZ() * 32, (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360), false, false); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getWatcher() { - Class Entity = Util.getCraftClass("Entity"); - Class DataWatcher = Util.getCraftClass("DataWatcher"); - - Object watcher = null; - try { - watcher = DataWatcher.getConstructor(new Class[] { Entity }).newInstance(dragon); - Method a = Util.getMethod(DataWatcher, "a", new Class[] { int.class, Object.class }); - - a.invoke(watcher, 5, isVisible() ? (byte) 0 : (byte) 0x20); - a.invoke(watcher, 6, (Float) health); - a.invoke(watcher, 7, (Integer) 0); - a.invoke(watcher, 8, (Byte) (byte) 0); - a.invoke(watcher, 10, name); - a.invoke(watcher, 11, (Byte) (byte) 1); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - - return watcher; - } - - public static boolean isUsable() { - Class PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport"); - - try { - PacketPlayOutEntityTeleport.getConstructor(new Class[] { int.class, int.class, int.class, int.class, byte.class, byte.class, boolean.class, boolean.class }); - } catch (IllegalArgumentException e) { - return false; - } catch (SecurityException e) { - return false; - } catch (NoSuchMethodException e) { - return false; - } - - return true; - } + + private Object dragon; + private int id; + + public v1_8Fake(String name, Location loc) { + super(name, loc); + } + + public static boolean isUsable() { + Class PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport"); + + try { + PacketPlayOutEntityTeleport + .getConstructor(new Class[] { int.class, int.class, int.class, int.class, byte.class, byte.class, boolean.class, boolean.class }); + } catch (IllegalArgumentException e) { + return false; + } catch (SecurityException e) { + return false; + } catch (NoSuchMethodException e) { + return false; + } + + return true; + } + + @Override + public Object getSpawnPacket() { + Class Entity = Util.getCraftClass("Entity"); + Class EntityLiving = Util.getCraftClass("EntityLiving"); + Class EntityEnderDragon = Util.getCraftClass("EntityEnderDragon"); + Object packet = null; + try { + dragon = EntityEnderDragon.getConstructor(Util.getCraftClass("World")).newInstance(getWorld()); + + Method setLocation = Util + .getMethod(EntityEnderDragon, "setLocation", new Class[] { double.class, double.class, double.class, float.class, float.class }); + setLocation.invoke(dragon, getX(), getY(), getZ(), getPitch(), getYaw()); + + Method setInvisible = Util.getMethod(EntityEnderDragon, "setInvisible", new Class[] { boolean.class }); + setInvisible.invoke(dragon, true); + + Method setCustomName = Util.getMethod(EntityEnderDragon, "setCustomName", new Class[] { String.class }); + setCustomName.invoke(dragon, name); + + Method setHealth = Util.getMethod(EntityEnderDragon, "setHealth", new Class[] { float.class }); + setHealth.invoke(dragon, health); + + Field motX = Util.getField(Entity, "motX"); + motX.set(dragon, getXvel()); + + Field motY = Util.getField(Entity, "motY"); + motY.set(dragon, getYvel()); + + Field motZ = Util.getField(Entity, "motZ"); + motZ.set(dragon, getZvel()); + + Method getId = Util.getMethod(EntityEnderDragon, "getId", new Class[] {}); + this.id = (Integer) getId.invoke(dragon); + + Class PacketPlayOutSpawnEntityLiving = Util.getCraftClass("PacketPlayOutSpawnEntityLiving"); + + packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class[] { EntityLiving }).newInstance(dragon); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getDestroyPacket() { + Class PacketPlayOutEntityDestroy = Util.getCraftClass("PacketPlayOutEntityDestroy"); + + Object packet = null; + try { + packet = PacketPlayOutEntityDestroy.newInstance(); + Field a = PacketPlayOutEntityDestroy.getDeclaredField("a"); + a.setAccessible(true); + a.set(packet, new int[] { id }); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getMetaPacket(Object watcher) { + Class DataWatcher = Util.getCraftClass("DataWatcher"); + + Class PacketPlayOutEntityMetadata = Util.getCraftClass("PacketPlayOutEntityMetadata"); + + Object packet = null; + try { + packet = PacketPlayOutEntityMetadata.getConstructor(new Class[] { int.class, DataWatcher, boolean.class }) + .newInstance(id, watcher, true); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getTeleportPacket(Location loc) { + Class PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport"); + Object packet = null; + + try { + packet = PacketPlayOutEntityTeleport + .getConstructor(new Class[] { int.class, int.class, int.class, int.class, byte.class, byte.class, boolean.class, boolean.class }) + .newInstance(this.id, loc.getBlockX() * 32, loc.getBlockY() * 32, loc.getBlockZ() * 32, (byte) ((int) loc + .getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360), false, false); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getWatcher() { + Class Entity = Util.getCraftClass("Entity"); + Class DataWatcher = Util.getCraftClass("DataWatcher"); + + Object watcher = null; + try { + watcher = DataWatcher.getConstructor(new Class[] { Entity }).newInstance(dragon); + Method a = Util.getMethod(DataWatcher, "a", new Class[] { int.class, Object.class }); + + a.invoke(watcher, 5, isVisible() ? (byte) 0 : (byte) 0x20); + a.invoke(watcher, 6, (Float) health); + a.invoke(watcher, 7, (Integer) 0); + a.invoke(watcher, 8, (Byte) (byte) 0); + a.invoke(watcher, 10, name); + a.invoke(watcher, 11, (Byte) (byte) 1); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + + return watcher; + } } From 92634712e97cbdb6535c796ca9a9eb6dc8de79ad Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 19 Mar 2016 15:15:24 +0000 Subject: [PATCH 12/15] Updated to 3.4 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4c0c1be..ea24294 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ Allows plugins to control the boss health bar message me.confuser BarAPI - 3.4-SNAPSHOT + 3.4 BarAPI From acadfaa0dea7f34604be783173a90c88836e28f8 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Fri, 4 Nov 2016 15:36:15 +0000 Subject: [PATCH 13/15] Added 1.10+ support --- src/me/confuser/barapi/Util.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index d391b45..ab0f6e2 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -49,7 +49,7 @@ public static void detectVersion() { } else if (minor == 8) { fakeDragonClass = v1_8.class; isBelowGround = false; - } else if (minor == 9) { + } else if (minor >= 9) { fakeDragonClass = v1_9.class; } } From b1ab443fa1fafe422d7f259f69e9458623c46b8a Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Fri, 4 Nov 2016 15:36:36 +0000 Subject: [PATCH 14/15] Updated to v3.5 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ea24294..ab283dd 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ Allows plugins to control the boss health bar message me.confuser BarAPI - 3.4 + 3.5 BarAPI From c96e13ee9503333124b8530e4d73d9887c3993b8 Mon Sep 17 00:00:00 2001 From: Thomas Windt Date: Thu, 9 Aug 2018 15:00:43 +0200 Subject: [PATCH 15/15] Update README.md Link was not working --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e4b2807..2002436 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@

<repositories>
 	<repository>
 		<id>confuser-repo</id>
-		<url>http://ci.frostcast.net/plugin/repository/everything</url>
+		<url>https://ci.frostcast.net/plugin/repository/everything</url>
 	</repository>
 </repositories>
 
@@ -37,7 +37,7 @@
 	<dependency>
 		<groupId>me.confuser</groupId>
 		<artifactId>BarAPI</artifactId>
-		<version>3.0</version>
+		<version>3.5</version>
 	</dependency>
 </dependencies>