From 9bef0841f8c7647548add19e200acbca426a26b7 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 30 Apr 2018 23:34:56 +0100 Subject: [PATCH 1/2] cleanup --- config.yml | 3 - pom.xml | 58 +- src/me/confuser/barapi/BarAPI.java | 691 ++++++++------------- src/me/confuser/barapi/Util.java | 296 ++++----- src/me/confuser/barapi/nms/FakeDragon.java | 262 ++++---- src/me/confuser/barapi/nms/v1_6.java | 365 ++++++----- src/me/confuser/barapi/nms/v1_7.java | 367 ++++++----- src/me/confuser/barapi/nms/v1_8.java | 343 +++++----- src/me/confuser/barapi/nms/v1_8Fake.java | 354 +++++------ src/me/confuser/barapi/nms/v1_9.java | 58 -- 10 files changed, 1229 insertions(+), 1568 deletions(-) delete mode 100644 src/me/confuser/barapi/nms/v1_9.java diff --git a/config.yml b/config.yml index 95bfdce..576b91e 100644 --- a/config.yml +++ b/config.yml @@ -1,5 +1,2 @@ -# Set to false to disable the auto updater -autoUpdate: true -testMode: false # For the latest spigot fake protocol hacks useSpigotHack: false diff --git a/pom.xml b/pom.xml index ab283dd..865eabf 100644 --- a/pom.xml +++ b/pom.xml @@ -6,6 +6,7 @@ me.confuser BarAPI 3.5 + BarAPI @@ -24,75 +25,26 @@ maven-compiler-plugin 3.1 - 1.6 - 1.6 + 1.8 + 1.8 - - 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 53bdafa..ec7bb38 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -2,14 +2,10 @@ 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; @@ -19,10 +15,9 @@ import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.plugin.java.JavaPlugin; -import org.mcstats.MetricsLite; -import java.io.IOException; import java.util.HashMap; +import java.util.Map; import java.util.UUID; /** @@ -33,503 +28,309 @@ 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 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()); - } + private static boolean useSpigotHack = false; + private Map players = new HashMap<>(); + private Map timers = new HashMap<>(); - 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); + public static boolean useSpigotHack() { + return useSpigotHack; } - } - private static void sendDragon(FakeDragon dragon, Player player) { - if (dragon instanceof v1_9) { - BossBar bar = ((v1_9) dragon).getBar(); + private static String cleanMessage(String message) { + if (message.length() > 64) + message = message.substring(0, 63); - 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()))); + return message; } - } - 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()); + 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; } - players.put(player.getUniqueId(), dragon); + @Override + public void onEnable() { + getConfig().options().copyDefaults(true); + saveConfig(); - return dragon; - } + useSpigotHack = getConfig().getBoolean("useSpigotHack", false); - private static FakeDragon addDragon(Player player, Location loc, String message) { - FakeDragon dragon = Util.newDragon(message, getDragonLocation(loc)); + if (!useSpigotHack()) { + if (v1_8Fake.isUsable()) { + useSpigotHack = true; + Util.detectVersion(); + getLogger().info("Detected spigot hack, enabling fake 1.8"); + } + } - if (dragon instanceof v1_9) { - BossBar bar = ((v1_9) dragon).getBar(); + getServer().getPluginManager().registerEvents(this, this); - bar.addPlayer(player); - } else { - Util.sendPacket(player, dragon.getSpawnPacket()); + if (useSpigotHack) { + getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { + for (UUID uuid : players.keySet()) { + Player p = Bukkit.getPlayer(uuid); + Util.sendPacket(p, players.get(uuid).getTeleportPacket(getDragonLocation(p.getLocation()))); + } + }, 0L, 5L); + } } - players.put(player.getUniqueId(), dragon); + @Override + public void onDisable() { + for (Player player : getServer().getOnlinePlayers()) { + quit(player); + } + + players.clear(); - return dragon; - } + for (int timerID : timers.values()) { + Bukkit.getScheduler().cancelTask(timerID); + } - private static Location getDragonLocation(Location loc) { - if (Util.isBelowGround) { - loc.subtract(0, 300, 0); - return loc; + timers.clear(); } - float pitch = loc.getPitch(); + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void PlayerLoggout(PlayerQuitEvent event) { + quit(event.getPlayer()); + } - 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(); + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerKick(PlayerKickEvent event) { + quit(event.getPlayer()); } - 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); + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(final PlayerTeleportEvent event) { + handleTeleport(event.getPlayer(), event.getTo().clone()); } - try { - MetricsLite metrics = new MetricsLite(this); - metrics.start(); - } catch (IOException e) { - // Failed to submit the stats :-( + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(final PlayerRespawnEvent event) { + handleTeleport(event.getPlayer(), event.getRespawnLocation().clone()); } - plugin = this; + private void handleTeleport(final Player player, final Location loc) { + if (!hasBar(player)) { + return; + } - useSpigotHack = getConfig().getBoolean("useSpigotHack", false); + final FakeDragon oldDragon = getDragon(player, ""); - if (!useSpigotHack) { - if (v1_8Fake.isUsable()) { - useSpigotHack = true; - Util.detectVersion(); - getLogger().info("Detected spigot hack, enabling fake 1.8"); - } - } + Bukkit.getScheduler().runTaskLater(this, () -> { + // Check if the player still has a dragon after the two ticks! ;) + if (!hasBar(player)) + return; - getServer().getPluginManager().registerEvents(this, this); + float health = oldDragon.health; + String message = oldDragon.name; - getLogger().info("Loaded"); + Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); - if (useSpigotHack) { - getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { + players.remove(player.getUniqueId()); - 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); + FakeDragon dragon = addDragon(player, loc, message); + dragon.health = health; + + sendDragon(dragon, player); + }, 2L); + } + + private void quit(Player player) { + removeBar(player); } - // TestMode - if (getConfig().getBoolean("testMode")) { - plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() { + /** + * 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 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); + } - @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); - } + /** + * 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 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); } - }, 30L, 300L); + FakeDragon dragon = getDragon(player, message); + + dragon.name = cleanMessage(message); + dragon.health = (percent / 100f) * dragon.getMaxHealth(); + + cancelTimer(player); + + sendDragon(dragon, player); } - } - public void onDisable() { - for (Player player : plugin.getServer().getOnlinePlayers()) { - quit(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 boolean hasBar(Player player) { + return players.get(player.getUniqueId()) != null; } - players.clear(); + /** + * 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 void removeBar(Player player) { + if (!hasBar(player)) + return; - for (int timerID : timers.values()) { - Bukkit.getScheduler().cancelTask(timerID); + FakeDragon dragon = getDragon(player, ""); + Util.sendPacket(player, dragon.getDestroyPacket()); + players.remove(player.getUniqueId()); + cancelTimer(player); } - timers.clear(); - } + /** + * 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 void setHealth(Player player, float percent) { + if (!hasBar(player)) + return; - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void PlayerLoggout(PlayerQuitEvent event) { - quit(event.getPlayer()); - } + FakeDragon dragon = getDragon(player, ""); + dragon.health = (percent / 100f) * dragon.getMaxHealth(); - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerKick(PlayerKickEvent event) { - quit(event.getPlayer()); - } + cancelTimer(player); - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerTeleport(final PlayerTeleportEvent event) { - handleTeleport(event.getPlayer(), event.getTo().clone()); - } + if (percent == 0) { + removeBar(player); + } else { + sendDragon(dragon, player); + } + } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerTeleport(final PlayerRespawnEvent event) { - handleTeleport(event.getPlayer(), event.getRespawnLocation().clone()); - } + /** + * 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 float getHealth(Player player) { + if (!hasBar(player)) + return -1; - private void handleTeleport(final Player player, final Location loc) { + return getDragon(player, "").health; + } - if (!hasBar(player)) - return; + /** + * 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 String getMessage(Player player) { + if (!hasBar(player)) + return ""; - final FakeDragon oldDragon = getDragon(player, ""); + return getDragon(player, "").name; + } - if (oldDragon instanceof v1_9) return; + private void cancelTimer(Player player) { + Integer timerID = timers.remove(player.getUniqueId()); - Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { + if (timerID != null) { + Bukkit.getScheduler().cancelTask(timerID); + } + } - @Override - public void run() { - // Check if the player still has a dragon after the two ticks! ;) - if (!hasBar(player)) - return; + private void sendDragon(FakeDragon dragon, Player player) { + Util.sendPacket(player, dragon.getMetaPacket(dragon.getWatcher())); + Util.sendPacket(player, dragon.getTeleportPacket(getDragonLocation(player.getLocation()))); + } - float health = oldDragon.health; - String message = oldDragon.name; + private FakeDragon getDragon(Player player, String message) { + if (hasBar(player)) { + return players.get(player.getUniqueId()); + } else + return addDragon(player, cleanMessage(message)); + } - Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); + private 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; + } - players.remove(player.getUniqueId()); + private 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; + } - FakeDragon dragon = addDragon(player, loc, message); - dragon.health = health; + private Location getDragonLocation(Location loc) { + if (Util.isBelowGround) { + loc.subtract(0, 300, 0); + return loc; + } - sendDragon(dragon, player); - } + float pitch = loc.getPitch(); - }, 2L); - } + if (pitch >= 55) { + loc.add(0, -300, 0); + } else if (pitch <= -55) { + loc.add(0, 300, 0); + } else { + loc = loc.getBlock().getRelative(getDirection(loc), getServer().getViewDistance() * 16).getLocation(); + } - private void quit(Player player) { - removeBar(player); - } + return loc; + } } diff --git a/src/me/confuser/barapi/Util.java b/src/me/confuser/barapi/Util.java index ab0f6e2..358f5de 100644 --- a/src/me/confuser/barapi/Util.java +++ b/src/me/confuser/barapi/Util.java @@ -1,6 +1,10 @@ package me.confuser.barapi; -import me.confuser.barapi.nms.*; +import me.confuser.barapi.nms.FakeDragon; +import me.confuser.barapi.nms.v1_6; +import me.confuser.barapi.nms.v1_7; +import me.confuser.barapi.nms.v1_8; +import me.confuser.barapi.nms.v1_8Fake; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -21,179 +25,149 @@ */ 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) { - fakeDragonClass = v1_9.class; - } - } + public static boolean newProtocol = false; + public static String version; + public static Class fakeDragonClass = v1_6.class; + public static boolean isBelowGround = true; - version = mcVersion + "."; + static { + detectVersion(); } - } - - 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(); + + 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; + } + } + + version = mcVersion + "."; + } } - 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 FakeDragon newDragon(String message, Location loc) { + FakeDragon fakeDragon = null; + + try { + fakeDragon = (FakeDragon) fakeDragonClass.getConstructor(String.class, Location.class).newInstance(message, loc); + } catch (IllegalArgumentException | SecurityException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + + return fakeDragon; } - } - - 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(); + + // 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 | IllegalArgumentException | InvocationTargetException | IllegalAccessException | NoSuchFieldException 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(); + + 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; } - 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(); + + 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 | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + return nms_entity; } - 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(); + + 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 | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + return nms_entity; } - 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; - } + + public static Field getField(Class cl, String field_name) { + try { + Field field = cl.getDeclaredField(field_name); + return field; + } catch (SecurityException | NoSuchFieldException e) { + e.printStackTrace(); + } + return null; } - 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; - } + + 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; } - return null; - } - - public static Method getMethod(Class cl, String method) { - for (Method m : cl.getMethods()) { - if (m.getName().equals(method)) { - return m; - } + + 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; } - 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; - } + + public static Method getMethod(Class cl, String method) { + for (Method m : cl.getMethods()) { + if (m.getName().equals(method)) { + return m; + } + } + return null; } - return equal; - } + 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/FakeDragon.java b/src/me/confuser/barapi/nms/FakeDragon.java index cfee43e..061fd34 100644 --- a/src/me/confuser/barapi/nms/FakeDragon.java +++ b/src/me/confuser/barapi/nms/FakeDragon.java @@ -1,145 +1,143 @@ package me.confuser.barapi.nms; import me.confuser.barapi.Util; - import org.bukkit.Location; public abstract class FakeDragon { - private float maxHealth = 200; - private int x; - private int y; - private int z; - - private int pitch = 0; - private int yaw = 0; - private byte xvel = 0; - private byte yvel = 0; - private byte zvel = 0; - public float health = 0; - private boolean visible = false; - public String name; - private Object world; - - public FakeDragon(String name, Location loc, int percent) { - this.name = name; - this.x = loc.getBlockX(); - this.y = loc.getBlockY(); - this.z = loc.getBlockZ(); - this.health = percent / 100F * maxHealth; - this.world = Util.getHandle(loc.getWorld()); - } - - public FakeDragon(String name, Location loc) { - this.name = name; - this.x = loc.getBlockX(); - this.y = loc.getBlockY(); - this.z = loc.getBlockZ(); - this.world = Util.getHandle(loc.getWorld()); - } - - public float getMaxHealth() { - return maxHealth; - } - - public void setHealth(int percent) { - this.health = percent / 100F * maxHealth; - } - - public void setName(String name) { - this.name = name; - } - - public int getX() { - return x; - } - - public void setX(int x) { - this.x = x; - } - - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public int getZ() { - return z; - } - - public void setZ(int z) { - this.z = z; - } - - public int getPitch() { - return pitch; - } - - public void setPitch(int pitch) { - this.pitch = pitch; - } - - public int getYaw() { - return yaw; - } - - public void setYaw(int yaw) { - this.yaw = yaw; - } - - public byte getXvel() { - return xvel; - } - - public void setXvel(byte xvel) { - this.xvel = xvel; - } - - public byte getYvel() { - return yvel; - } - - public void setYvel(byte yvel) { - this.yvel = yvel; - } - - public byte getZvel() { - return zvel; - } - - public void setZvel(byte zvel) { - this.zvel = zvel; - } - - public boolean isVisible() { - return visible; - } - - public void setVisible(boolean visible) { - this.visible = visible; - } - - public Object getWorld() { - return world; - } + public float health = 0; + public String name; + private float maxHealth = 200; + private int x; + private int y; + private int z; + private int pitch = 0; + private int yaw = 0; + private byte xvel = 0; + private byte yvel = 0; + private byte zvel = 0; + private boolean visible = false; + private Object world; + + public FakeDragon(String name, Location loc, int percent) { + this.name = name; + this.x = loc.getBlockX(); + this.y = loc.getBlockY(); + this.z = loc.getBlockZ(); + this.health = percent / 100F * maxHealth; + this.world = Util.getHandle(loc.getWorld()); + } + + public FakeDragon(String name, Location loc) { + this.name = name; + this.x = loc.getBlockX(); + this.y = loc.getBlockY(); + this.z = loc.getBlockZ(); + this.world = Util.getHandle(loc.getWorld()); + } + + public float getMaxHealth() { + return maxHealth; + } + + public void setMaxHealth(float max) { + maxHealth = max; + } + + public void setHealth(int percent) { + this.health = percent / 100F * maxHealth; + } + + public void setName(String name) { + this.name = name; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getZ() { + return z; + } + + public void setZ(int z) { + this.z = z; + } + + public int getPitch() { + return pitch; + } + + public void setPitch(int pitch) { + this.pitch = pitch; + } + + public int getYaw() { + return yaw; + } + + public void setYaw(int yaw) { + this.yaw = yaw; + } + + public byte getXvel() { + return xvel; + } + + public void setXvel(byte xvel) { + this.xvel = xvel; + } + + public byte getYvel() { + return yvel; + } + + public void setYvel(byte yvel) { + this.yvel = yvel; + } + + public byte getZvel() { + return zvel; + } + + public void setZvel(byte zvel) { + this.zvel = zvel; + } + + public boolean isVisible() { + return visible; + } + + public void setVisible(boolean visible) { + this.visible = visible; + } + + public Object getWorld() { + return world; + } - public void setWorld(Object world) { - this.world = world; - } - - public void setMaxHealth(float max) { - maxHealth = max; - } + public void setWorld(Object world) { + this.world = world; + } - public abstract Object getSpawnPacket(); + public abstract Object getSpawnPacket(); - public abstract Object getDestroyPacket(); + public abstract Object getDestroyPacket(); - public abstract Object getMetaPacket(Object watcher); + public abstract Object getMetaPacket(Object watcher); - public abstract Object getTeleportPacket(Location loc); + public abstract Object getTeleportPacket(Location loc); - public abstract Object getWatcher(); + public abstract Object getWatcher(); } diff --git a/src/me/confuser/barapi/nms/v1_6.java b/src/me/confuser/barapi/nms/v1_6.java index b937976..04dd50a 100644 --- a/src/me/confuser/barapi/nms/v1_6.java +++ b/src/me/confuser/barapi/nms/v1_6.java @@ -1,194 +1,193 @@ 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; import org.bukkit.entity.EntityType; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** -* 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-2#post-1760928 -* -* @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-2#post-1760928 + * + * @author James Mortemore + */ public class v1_6 extends FakeDragon { - private static final Integer EntityID = 6000; - - public v1_6(String name, Location loc) { - super(name, loc); - } - - @SuppressWarnings("deprecation") - @Override - public Object getSpawnPacket() { - Class mob_class = Util.getCraftClass("Packet24MobSpawn"); - Object mobPacket = null; - try { - mobPacket = mob_class.newInstance(); - - Field a = Util.getField(mob_class, "a"); - a.setAccessible(true); - a.set(mobPacket, EntityID);// Entity ID - Field b = Util.getField(mob_class, "b"); - b.setAccessible(true); - b.set(mobPacket, EntityType.ENDER_DRAGON.getTypeId());// Mob type - // (ID: 64) - Field c = Util.getField(mob_class, "c"); - c.setAccessible(true); - c.set(mobPacket, getX());// X position - Field d = Util.getField(mob_class, "d"); - d.setAccessible(true); - d.set(mobPacket, getY());// Y position - Field e = Util.getField(mob_class, "e"); - e.setAccessible(true); - e.set(mobPacket, getZ());// Z position - Field f = Util.getField(mob_class, "f"); - f.setAccessible(true); - f.set(mobPacket, (byte) ((int) (getPitch() * 256.0F / 360.0F)));// Pitch - Field g = Util.getField(mob_class, "g"); - g.setAccessible(true); - g.set(mobPacket, (byte) ((int) 0));// Head - // Pitch - Field h = Util.getField(mob_class, "h"); - h.setAccessible(true); - h.set(mobPacket, (byte) ((int) (getYaw() * 256.0F / 360.0F)));// Yaw - Field i = Util.getField(mob_class, "i"); - i.setAccessible(true); - i.set(mobPacket, getXvel());// X velocity - Field j = Util.getField(mob_class, "j"); - j.setAccessible(true); - j.set(mobPacket, getYvel());// Y velocity - Field k = Util.getField(mob_class, "k"); - k.setAccessible(true); - k.set(mobPacket, getZvel());// Z velocity - - Object watcher = getWatcher(); - Field t = Util.getField(mob_class, "t"); - t.setAccessible(true); - t.set(mobPacket, watcher); - } catch (InstantiationException e1) { - e1.printStackTrace(); - } catch (IllegalAccessException e1) { - e1.printStackTrace(); - } - - return mobPacket; - } - - @Override - public Object getDestroyPacket() { - Class packet_class = Util.getCraftClass("Packet29DestroyEntity"); - Object packet = null; - try { - packet = packet_class.newInstance(); - - Field a = Util.getField(packet_class, "a"); - a.setAccessible(true); - a.set(packet, new int[] { EntityID }); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getMetaPacket(Object watcher) { - Class packet_class = Util.getCraftClass("Packet40EntityMetadata"); - Object packet = null; - try { - packet = packet_class.newInstance(); - - Field a = Util.getField(packet_class, "a"); - a.setAccessible(true); - a.set(packet, EntityID); - - Method watcher_c = Util.getMethod(watcher.getClass(), "c"); - Field b = Util.getField(packet_class, "b"); - b.setAccessible(true); - b.set(packet, watcher_c.invoke(watcher)); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - - return packet; - } - - @Override - public Object getTeleportPacket(Location loc) { - Class packet_class = Util.getCraftClass("Packet34EntityTeleport"); - Object packet = null; - try { - packet = packet_class.newInstance(); - - Field a = Util.getField(packet_class, "a"); - a.setAccessible(true); - a.set(packet, EntityID); - Field b = Util.getField(packet_class, "b"); - b.setAccessible(true); - b.set(packet, (int) Math.floor(loc.getX() * 32.0D)); - Field c = Util.getField(packet_class, "c"); - c.setAccessible(true); - c.set(packet, (int) Math.floor(loc.getY() * 32.0D)); - Field d = Util.getField(packet_class, "d"); - d.setAccessible(true); - d.set(packet, (int) Math.floor(loc.getZ() * 32.0D)); - Field e = Util.getField(packet_class, "e"); - e.setAccessible(true); - e.set(packet, (byte) ((int) (loc.getYaw() * 256.0F / 360.0F))); - Field f = Util.getField(packet_class, "f"); - f.setAccessible(true); - f.set(packet, (byte) ((int) (loc.getPitch() * 256.0F / 360.0F))); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return packet; - } - - @Override - public Object getWatcher() { - Class watcher_class = Util.getCraftClass("DataWatcher"); - Object watcher = null; - try { - watcher = watcher_class.newInstance(); - - Method a = Util.getMethod(watcher_class, "a", new Class[] { int.class, Object.class }); - a.setAccessible(true); - - a.invoke(watcher, 0, isVisible() ? (byte) 0 : (byte) 0x20); - a.invoke(watcher, 6, (Float) (float) health); - a.invoke(watcher, 7, (Integer) (int) 0); - a.invoke(watcher, 8, (Byte) (byte) 0); - a.invoke(watcher, 10, (String) name); - a.invoke(watcher, 11, (Byte) (byte) 1); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - - return watcher; - } + private static final Integer EntityID = 6000; + + public v1_6(String name, Location loc) { + super(name, loc); + } + + @SuppressWarnings("deprecation") + @Override + public Object getSpawnPacket() { + Class mob_class = Util.getCraftClass("Packet24MobSpawn"); + Object mobPacket = null; + try { + mobPacket = mob_class.newInstance(); + + Field a = Util.getField(mob_class, "a"); + a.setAccessible(true); + a.set(mobPacket, EntityID);// Entity ID + Field b = Util.getField(mob_class, "b"); + b.setAccessible(true); + b.set(mobPacket, EntityType.ENDER_DRAGON.getTypeId());// Mob type + // (ID: 64) + Field c = Util.getField(mob_class, "c"); + c.setAccessible(true); + c.set(mobPacket, getX());// X position + Field d = Util.getField(mob_class, "d"); + d.setAccessible(true); + d.set(mobPacket, getY());// Y position + Field e = Util.getField(mob_class, "e"); + e.setAccessible(true); + e.set(mobPacket, getZ());// Z position + Field f = Util.getField(mob_class, "f"); + f.setAccessible(true); + f.set(mobPacket, (byte) ((int) (getPitch() * 256.0F / 360.0F)));// Pitch + Field g = Util.getField(mob_class, "g"); + g.setAccessible(true); + g.set(mobPacket, (byte) ((int) 0));// Head + // Pitch + Field h = Util.getField(mob_class, "h"); + h.setAccessible(true); + h.set(mobPacket, (byte) ((int) (getYaw() * 256.0F / 360.0F)));// Yaw + Field i = Util.getField(mob_class, "i"); + i.setAccessible(true); + i.set(mobPacket, getXvel());// X velocity + Field j = Util.getField(mob_class, "j"); + j.setAccessible(true); + j.set(mobPacket, getYvel());// Y velocity + Field k = Util.getField(mob_class, "k"); + k.setAccessible(true); + k.set(mobPacket, getZvel());// Z velocity + + Object watcher = getWatcher(); + Field t = Util.getField(mob_class, "t"); + t.setAccessible(true); + t.set(mobPacket, watcher); + } catch (InstantiationException e1) { + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + e1.printStackTrace(); + } + + return mobPacket; + } + + @Override + public Object getDestroyPacket() { + Class packet_class = Util.getCraftClass("Packet29DestroyEntity"); + Object packet = null; + try { + packet = packet_class.newInstance(); + + Field a = Util.getField(packet_class, "a"); + a.setAccessible(true); + a.set(packet, new int[]{EntityID}); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getMetaPacket(Object watcher) { + Class packet_class = Util.getCraftClass("Packet40EntityMetadata"); + Object packet = null; + try { + packet = packet_class.newInstance(); + + Field a = Util.getField(packet_class, "a"); + a.setAccessible(true); + a.set(packet, EntityID); + + Method watcher_c = Util.getMethod(watcher.getClass(), "c"); + Field b = Util.getField(packet_class, "b"); + b.setAccessible(true); + b.set(packet, watcher_c.invoke(watcher)); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return packet; + } + + @Override + public Object getTeleportPacket(Location loc) { + Class packet_class = Util.getCraftClass("Packet34EntityTeleport"); + Object packet = null; + try { + packet = packet_class.newInstance(); + + Field a = Util.getField(packet_class, "a"); + a.setAccessible(true); + a.set(packet, EntityID); + Field b = Util.getField(packet_class, "b"); + b.setAccessible(true); + b.set(packet, (int) Math.floor(loc.getX() * 32.0D)); + Field c = Util.getField(packet_class, "c"); + c.setAccessible(true); + c.set(packet, (int) Math.floor(loc.getY() * 32.0D)); + Field d = Util.getField(packet_class, "d"); + d.setAccessible(true); + d.set(packet, (int) Math.floor(loc.getZ() * 32.0D)); + Field e = Util.getField(packet_class, "e"); + e.setAccessible(true); + e.set(packet, (byte) ((int) (loc.getYaw() * 256.0F / 360.0F))); + Field f = Util.getField(packet_class, "f"); + f.setAccessible(true); + f.set(packet, (byte) ((int) (loc.getPitch() * 256.0F / 360.0F))); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return packet; + } + + @Override + public Object getWatcher() { + Class watcher_class = Util.getCraftClass("DataWatcher"); + Object watcher = null; + try { + watcher = watcher_class.newInstance(); + + Method a = Util.getMethod(watcher_class, "a", new Class[]{int.class, Object.class}); + a.setAccessible(true); + + a.invoke(watcher, 0, isVisible() ? (byte) 0 : (byte) 0x20); + a.invoke(watcher, 6, (Float) (float) health); + a.invoke(watcher, 7, (Integer) (int) 0); + a.invoke(watcher, 8, (Byte) (byte) 0); + a.invoke(watcher, 10, (String) name); + a.invoke(watcher, 11, (Byte) (byte) 1); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return watcher; + } } diff --git a/src/me/confuser/barapi/nms/v1_7.java b/src/me/confuser/barapi/nms/v1_7.java index 7aa685e..f30151c 100644 --- a/src/me/confuser/barapi/nms/v1_7.java +++ b/src/me/confuser/barapi/nms/v1_7.java @@ -1,194 +1,193 @@ 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_7 extends FakeDragon { - private Object dragon; - private int id; - - public v1_7(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, isVisible()); - - 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 }).newInstance(this.id, loc.getBlockX() * 32, loc.getBlockY() * 32, loc.getBlockZ() * 32, (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360)); - } 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, 0, 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; - } + private Object dragon; + private int id; + + public v1_7(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, isVisible()); + + 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}).newInstance(this.id, loc.getBlockX() * 32, loc.getBlockY() * 32, loc.getBlockZ() * 32, (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360)); + } 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, 0, 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_8.java b/src/me/confuser/barapi/nms/v1_8.java index ff1e3dc..a808d56 100644 --- a/src/me/confuser/barapi/nms/v1_8.java +++ b/src/me/confuser/barapi/nms/v1_8.java @@ -1,180 +1,179 @@ 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; - 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; - } + 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 180d37e..3c60f11 100644 --- a/src/me/confuser/barapi/nms/v1_8Fake.java +++ b/src/me/confuser/barapi/nms/v1_8Fake.java @@ -18,193 +18,193 @@ public class v1_8Fake extends FakeDragon { - 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; + private Object dragon; + private int id; + + public v1_8Fake(String name, Location loc) { + super(name, loc); } - 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(); + 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; } - 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(); + @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; } - 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(); + @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; } - 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(); + @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; } - 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(); + @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; } - return watcher; - } + @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 deleted file mode 100644 index ef5a811..0000000 --- a/src/me/confuser/barapi/nms/v1_9.java +++ /dev/null @@ -1,58 +0,0 @@ -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 8d6e803301307ebb0b1fc2482b9f0d16830f4795 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 1 May 2018 00:02:35 +0100 Subject: [PATCH 2/2] Implement helper's BossBarFactory --- plugin.yml | 4 - pom.xml | 12 + src/me/confuser/barapi/BarAPI.java | 254 +++++++++++------- .../confuser/barapi/MixedBossBarFactory.java | 182 +++++++++++++ 4 files changed, 354 insertions(+), 98 deletions(-) delete mode 100644 plugin.yml create mode 100644 src/me/confuser/barapi/MixedBossBarFactory.java diff --git a/plugin.yml b/plugin.yml deleted file mode 100644 index 96c3991..0000000 --- a/plugin.yml +++ /dev/null @@ -1,4 +0,0 @@ -main: me.confuser.barapi.BarAPI -name: ${project.name} -version: ${project.version} -description: ${project.description} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 865eabf..1f4130f 100644 --- a/pom.xml +++ b/pom.xml @@ -46,5 +46,17 @@ 1.9-R0.1-SNAPSHOT provided + + me.lucko + helper + 5.2.0 + provided + + + us.myles + viaversion + 1.3.0 + provided + diff --git a/src/me/confuser/barapi/BarAPI.java b/src/me/confuser/barapi/BarAPI.java index ec7bb38..aa3a99d 100644 --- a/src/me/confuser/barapi/BarAPI.java +++ b/src/me/confuser/barapi/BarAPI.java @@ -2,6 +2,13 @@ import me.confuser.barapi.nms.FakeDragon; import me.confuser.barapi.nms.v1_8Fake; +import me.lucko.helper.bossbar.BossBar; +import me.lucko.helper.bossbar.BossBarColor; +import me.lucko.helper.bossbar.BossBarFactory; +import me.lucko.helper.bossbar.BossBarStyle; +import me.lucko.helper.plugin.ExtendedJavaPlugin; +import me.lucko.helper.plugin.ap.Plugin; +import me.lucko.helper.text.Text; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -14,10 +21,15 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.plugin.ServicePriority; +import javax.annotation.Nonnull; +import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; /** @@ -25,8 +37,8 @@ * * @author James Mortemore */ - -public class BarAPI extends JavaPlugin implements Listener { +@Plugin(name = "BarAPI", hardDepends = {"helper", "ViaVersion"}) +public class BarAPI extends ExtendedJavaPlugin implements Listener, BossBarFactory { private static boolean useSpigotHack = false; private Map players = new HashMap<>(); @@ -57,7 +69,7 @@ private static BlockFace getDirection(Location loc) { } @Override - public void onEnable() { + public void enable() { getConfig().options().copyDefaults(true); saveConfig(); @@ -75,27 +87,31 @@ public void onEnable() { if (useSpigotHack) { getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { - for (UUID uuid : players.keySet()) { + for (UUID uuid : this.players.keySet()) { Player p = Bukkit.getPlayer(uuid); - Util.sendPacket(p, players.get(uuid).getTeleportPacket(getDragonLocation(p.getLocation()))); + Util.sendPacket(p, this.players.get(uuid).getTeleportPacket(getDragonLocation(p.getLocation()))); } }, 0L, 5L); } + + // provide helper boss bar service + BossBarFactory service = getService(BossBarFactory.class); + provideService(BossBarFactory.class, new MixedBossBarFactory(this, service), ServicePriority.High); } @Override - public void onDisable() { + public void disable() { for (Player player : getServer().getOnlinePlayers()) { quit(player); } - players.clear(); + this.players.clear(); - for (int timerID : timers.values()) { + for (int timerID : this.timers.values()) { Bukkit.getScheduler().cancelTask(timerID); } - timers.clear(); + this.timers.clear(); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -135,7 +151,7 @@ private void handleTeleport(final Player player, final Location loc) { Util.sendPacket(player, getDragon(player, "").getDestroyPacket()); - players.remove(player.getUniqueId()); + this.players.remove(player.getUniqueId()); FakeDragon dragon = addDragon(player, loc, message); dragon.health = health; @@ -148,31 +164,6 @@ private void quit(Player player) { removeBar(player); } - /** - * 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 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 the given player.
* It will remain there until the player logs off or another plugin overrides it.
@@ -210,7 +201,7 @@ public void setMessage(Player player, String message, float percent) { * @return True, if the player has a bar, False otherwise. */ public boolean hasBar(Player player) { - return players.get(player.getUniqueId()) != null; + return this.players.get(player.getUniqueId()) != null; } /** @@ -225,64 +216,12 @@ public void removeBar(Player player) { FakeDragon dragon = getDragon(player, ""); Util.sendPacket(player, dragon.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 void setHealth(Player player, float percent) { - if (!hasBar(player)) - return; - - FakeDragon dragon = getDragon(player, ""); - dragon.health = (percent / 100f) * dragon.getMaxHealth(); - + this.players.remove(player.getUniqueId()); 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 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 String getMessage(Player player) { - if (!hasBar(player)) - return ""; - - return getDragon(player, "").name; } private void cancelTimer(Player player) { - Integer timerID = timers.remove(player.getUniqueId()); + Integer timerID = this.timers.remove(player.getUniqueId()); if (timerID != null) { Bukkit.getScheduler().cancelTask(timerID); @@ -296,7 +235,7 @@ private void sendDragon(FakeDragon dragon, Player player) { private FakeDragon getDragon(Player player, String message) { if (hasBar(player)) { - return players.get(player.getUniqueId()); + return this.players.get(player.getUniqueId()); } else return addDragon(player, cleanMessage(message)); } @@ -304,14 +243,14 @@ private FakeDragon getDragon(Player player, String message) { private FakeDragon addDragon(Player player, String message) { FakeDragon dragon = Util.newDragon(message, getDragonLocation(player.getLocation())); Util.sendPacket(player, dragon.getSpawnPacket()); - players.put(player.getUniqueId(), dragon); + this.players.put(player.getUniqueId(), dragon); return dragon; } private 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); + this.players.put(player.getUniqueId(), dragon); return dragon; } @@ -333,4 +272,131 @@ private Location getDragonLocation(Location loc) { return loc; } + + @Nonnull + @Override + public BossBar newBossBar() { + return new BarApiBossBar(); + } + + private final class BarApiBossBar implements BossBar { + private String title = "null"; + private double progress = 1d; + private boolean visible = true; + private final Set players = new HashSet<>(); + + private void update() { + for (Player p : this.players) { + update(p); + } + } + + private void update(Player p) { + setMessage(p, this.title, (float) (this.progress * 100d)); + } + + @Nonnull + @Override + public String title() { + return this.title; + } + + @Nonnull + @Override + public BossBar title(@Nonnull String title) { + this.title = Text.colorize(title); + update(); + return this; + } + + @Override + public double progress() { + return this.progress; + } + + @Nonnull + @Override + public BossBar progress(double progress) { + this.progress = progress; + update(); + return this; + } + + @Nonnull + @Override + public BossBarColor color() { + return BossBarColor.defaultColor(); + } + + @Nonnull + @Override + public BossBar color(@Nonnull BossBarColor color) { + return this; + } + + @Nonnull + @Override + public BossBarStyle style() { + return BossBarStyle.defaultStyle(); + } + + @Nonnull + @Override + public BossBar style(@Nonnull BossBarStyle style) { + return this; + } + + @Override + public boolean visible() { + return this.visible; + } + + @Nonnull + @Override + public BossBar visible(boolean visible) { + this.visible = visible; + + if (!visible) { + for (Player p : this.players) { + removeBar(p); + } + } else { + update(); + } + + return this; + } + + @Nonnull + @Override + public List players() { + return new ArrayList<>(this.players); + } + + @Override + public void addPlayer(@Nonnull Player player) { + if (this.players.add(player)) { + update(player); + } + } + + @Override + public void removePlayer(@Nonnull Player player) { + if (this.players.remove(player)) { + removeBar(player); + } + } + + @Override + public void removeAll() { + for (Player p : this.players) { + removePlayer(p); + } + } + + @Override + public void close() { + removeAll(); + } + } } diff --git a/src/me/confuser/barapi/MixedBossBarFactory.java b/src/me/confuser/barapi/MixedBossBarFactory.java new file mode 100644 index 0000000..154c2be --- /dev/null +++ b/src/me/confuser/barapi/MixedBossBarFactory.java @@ -0,0 +1,182 @@ +/* + * This file is part of BarAPI, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.confuser.barapi; + +import me.lucko.helper.bossbar.BossBar; +import me.lucko.helper.bossbar.BossBarColor; +import me.lucko.helper.bossbar.BossBarFactory; +import me.lucko.helper.bossbar.BossBarStyle; +import org.bukkit.entity.Player; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.ViaAPI; +import us.myles.ViaVersion.api.protocol.ProtocolVersion; + +import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.List; + +public class MixedBossBarFactory implements BossBarFactory { + + @SuppressWarnings("unchecked") + private final ViaAPI viaApi = Via.getAPI(); + + // used for players on 1.8 + private final BarAPI legacyFactory; + // used for players on 1.9+ + private final BossBarFactory newFactory; + + public MixedBossBarFactory(BarAPI legacyFactory, BossBarFactory newFactory) { + this.legacyFactory = legacyFactory; + this.newFactory = newFactory; + } + + @Nonnull + @Override + public BossBar newBossBar() { + return new MixedBossBar(this.viaApi, this.legacyFactory.newBossBar(), this.newFactory.newBossBar()); + } + + private static final class MixedBossBar implements BossBar { + private final ViaAPI viaApi; + private final BossBar legacyBar; + private final BossBar newBar; + + private MixedBossBar(ViaAPI viaApi, BossBar legacyBar, BossBar newBar) { + this.viaApi = viaApi; + this.legacyBar = legacyBar; + this.newBar = newBar; + } + + @Nonnull + @Override + public String title() { + return this.newBar.title(); + } + + @Nonnull + @Override + public BossBar title(@Nonnull String title) { + this.legacyBar.title(title); + this.newBar.title(title); + return this; + } + + @Override + public double progress() { + return this.newBar.progress(); + } + + @Nonnull + @Override + public BossBar progress(double progress) { + this.legacyBar.progress(progress); + this.newBar.progress(progress); + return this; + } + + @Nonnull + @Override + public BossBarColor color() { + return this.newBar.color(); + } + + @Nonnull + @Override + public BossBar color(@Nonnull BossBarColor color) { + this.legacyBar.color(color); + this.newBar.color(color); + return this; + } + + @Nonnull + @Override + public BossBarStyle style() { + return this.newBar.style(); + } + + @Nonnull + @Override + public BossBar style(@Nonnull BossBarStyle style) { + this.legacyBar.style(style); + this.newBar.style(style); + return this; + } + + @Override + public boolean visible() { + return this.newBar.visible(); + } + + @Nonnull + @Override + public BossBar visible(boolean visible) { + this.legacyBar.visible(visible); + this.newBar.visible(visible); + return this; + } + + @Nonnull + @Override + public List players() { + List ret = new ArrayList<>(); + ret.addAll(this.legacyBar.players()); + ret.addAll(this.newBar.players()); + return ret; + } + + @Override + public void addPlayer(@Nonnull Player player) { + int version = this.viaApi.getPlayerVersion(player); + if (version < ProtocolVersion.v1_9.getId()) { + this.legacyBar.addPlayer(player); + } else { + this.newBar.addPlayer(player); + } + } + + @Override + public void removePlayer(@Nonnull Player player) { + int version = this.viaApi.getPlayerVersion(player); + if (version < ProtocolVersion.v1_9.getId()) { + this.legacyBar.removePlayer(player); + } else { + this.newBar.removePlayer(player); + } + } + + @Override + public void removeAll() { + this.legacyBar.removeAll(); + this.newBar.removeAll(); + } + + @Override + public void close() { + this.legacyBar.close(); + this.newBar.close(); + } + } +}