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/.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 - - 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>
 
diff --git a/pom.xml b/pom.xml index 41de64d..ab283dd 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.3-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.1 - - - 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.5 + + 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 28d8786..53bdafa 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -2,12 +2,14 @@ import me.confuser.barapi.nms.FakeDragon; 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; @@ -25,476 +27,509 @@ /** * 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) { - 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); - - 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); - - 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 (!useSpigotHack()) { - 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) + */ + @Deprecated + 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. + */ + @Deprecated + 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) + */ + @Deprecated + 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. + */ + @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); + + 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) + */ + @Deprecated + 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. + */ + @Deprecated + 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. + */ + @Deprecated + 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. + */ + @Deprecated + public static void removeBar(Player player) { + if (!hasBar(player)) + return; + + 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()); + + 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). + */ + @Deprecated + 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. + */ + @Deprecated + 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. + */ + @Deprecated + 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) { + 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) { + 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())); + + 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); + + return dragon; + } + + private static FakeDragon addDragon(Player player, Location loc, String message) { + FakeDragon dragon = Util.newDragon(message, getDragonLocation(loc)); + + 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); + + 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; + + final FakeDragon oldDragon = getDragon(player, ""); + + if (oldDragon instanceof v1_9) 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; + + 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); + } } diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index 8ec8f87..ab0f6e2 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -1,188 +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; - - static { - detectVersion(); - } - - public static void detectVersion() { - if (BarAPI.useSpigotHack()) { - fakeDragonClass = v1_8Fake.class; - version = "v1_7_R4."; - } 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; - } - - 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; - } + + 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; + } } \ No newline at end of file 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; + } +} 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; + } } 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; + } +}