From a7d001d578209d67d4b8a99087a2db4466fc5da8 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Mon, 3 Sep 2018 15:53:16 +0200 Subject: [PATCH] Added User#spawnParticle(Particle, Particle.DustOptions, int, int, int) --- .../admin/range/AdminRangeDisplayCommand.java | 34 ++++--------- .../bentobox/bentobox/api/user/User.java | 50 +++++++++++++------ 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java index b8d1a9aef..6c2a11293 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java @@ -86,15 +86,15 @@ public class AdminRangeDisplayCommand extends CompositeCommand { getIslands().getIslandAt(user.getLocation()).ifPresent(island -> { // Draw the island protected area - drawZone(user.getPlayer(), Particle.BARRIER, null, island.getCenter(), island.getProtectionRange()); + drawZone(user, Particle.BARRIER, null, island.getCenter(), island.getProtectionRange()); // Draw the default protected area if island protected zone is different if (island.getProtectionRange() != getPlugin().getIWM().getIslandProtectionRange(getWorld())) { - drawZone(user.getPlayer(), Particle.VILLAGER_HAPPY, null, island.getCenter(), getPlugin().getIWM().getIslandProtectionRange(getWorld())); + drawZone(user, Particle.VILLAGER_HAPPY, null, island.getCenter(), getPlugin().getIWM().getIslandProtectionRange(getWorld())); } // Draw the island area - drawZone(user.getPlayer(), Particle.REDSTONE, new Particle.DustOptions(Color.GRAY, 1.0F),island.getCenter(), island.getRange()); + drawZone(user, Particle.REDSTONE, new Particle.DustOptions(Color.GRAY, 1.0F),island.getCenter(), island.getRange()); }); }, 20, 30)); } @@ -105,33 +105,17 @@ public class AdminRangeDisplayCommand extends CompositeCommand { display.remove(user); } - private void drawZone(Player player, Particle particle, Particle.DustOptions dustOptions, Location center, int range) { - if (particle.equals(Particle.REDSTONE) && dustOptions == null) { - // Security check that will avoid later unexpected exceptions. - throw new IllegalArgumentException("A non-null Particle.DustOptions must be provided when using Particle.REDSTONE."); - } - + private void drawZone(User user, Particle particle, Particle.DustOptions dustOptions, Location center, int range) { // Get player Y coordinate - int playerY = player.getLocation().getBlockY() + 1; + int playerY = user.getPlayer().getLocation().getBlockY() + 1; // Draw 3 "stages" (one line below, at and above player's y coordinate) for (int stage = -1 ; stage <= 1 ; stage++) { for (int i = -range ; i <= range ; i++) { - spawnParticle(player, particle, dustOptions, center.getBlockX() + i, playerY + stage, center.getBlockZ() + range); - spawnParticle(player, particle, dustOptions, center.getBlockX() + i, playerY + stage, center.getBlockZ() - range); - spawnParticle(player, particle, dustOptions, center.getBlockX() + range, playerY + stage, center.getBlockZ() + i); - spawnParticle(player, particle, dustOptions, center.getBlockX() - range, playerY + stage, center.getBlockZ() + i); - } - } - } - - private void spawnParticle(Player player, Particle particle, Particle.DustOptions dustOptions, int x, int y, int z) { - // Check if this particle is beyond the viewing distance of the server - if (player.getLocation().toVector().distanceSquared(new Vector(x,y,z)) < (Bukkit.getServer().getViewDistance()*256*Bukkit.getServer().getViewDistance())) { - if (particle.equals(Particle.REDSTONE)) { - player.spawnParticle(particle, x, y, z, 1, 0, 0, 0, 1, dustOptions); - } else { - player.spawnParticle(particle, x, y, z, 1); + user.spawnParticle(particle, dustOptions, center.getBlockX() + i, playerY + stage, center.getBlockZ() + range); + user.spawnParticle(particle, dustOptions, center.getBlockX() + i, playerY + stage, center.getBlockZ() - range); + user.spawnParticle(particle, dustOptions, center.getBlockX() + range, playerY + stage, center.getBlockZ() + i); + user.spawnParticle(particle, dustOptions, center.getBlockX() - range, playerY + stage, center.getBlockZ() + i); } } } diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 24e6e32fb..54df1a2af 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -6,17 +6,13 @@ import java.util.Map; import java.util.Set; import java.util.UUID; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.util.Vector; import world.bentobox.bentobox.BentoBox; /** @@ -329,6 +325,40 @@ public class User { return player.performCommand(command); } + /** + * Checks if a user is in one of the game worlds + * @return true if user is, false if not + */ + public boolean inWorld() { + return plugin.getIWM().inWorld(getLocation()); + } + + /** + * Spawn particles to the player. + * They are only displayed if they are within the server's view distance. + * @param particle Particle to display. + * @param dustOptions Particle.DustOptions for the particle to display. + * Cannot be null when particle is {@link Particle#REDSTONE}. + * @param x X coordinate of the particle to display. + * @param y Y coordinate of the particle to display. + * @param z Z coordinate of the particle to display. + */ + public void spawnParticle(Particle particle, Particle.DustOptions dustOptions, int x, int y, int z) { + if (particle.equals(Particle.REDSTONE) && dustOptions == null) { + // Security check that will avoid later unexpected exceptions. + throw new IllegalArgumentException("A non-null Particle.DustOptions must be provided when using Particle.REDSTONE as particle."); + } + + // Check if this particle is beyond the viewing distance of the server + if (player.getLocation().toVector().distanceSquared(new Vector(x,y,z)) < (Bukkit.getServer().getViewDistance()*256*Bukkit.getServer().getViewDistance())) { + if (particle.equals(Particle.REDSTONE)) { + player.spawnParticle(particle, x, y, z, 1, 0, 0, 0, 1, dustOptions); + } else { + player.spawnParticle(particle, x, y, z, 1); + } + } + } + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @@ -358,12 +388,4 @@ public class User { return other.playerUUID == null; } else return playerUUID.equals(other.playerUUID); } - - /** - * Checks if a user is in one of the game worlds - * @return true if user is, false if not - */ - public boolean inWorld() { - return plugin.getIWM().inWorld(getLocation()); - } }