From f0f3740a2cdd73b550af912f995e2e4ae9d8e03a Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 31 Jan 2020 09:47:04 +0100 Subject: [PATCH] [CI skip] Minor refactoring --- .../subcommands/TeleporterCommand.java | 2 +- .../listeners/TeleporterListener.java | 2 +- .../Slimefun/GPS/GPSNetwork.java | 46 +----- .../Slimefun/GPS/GPSTeleportation.java | 143 ++++++++++++++++++ .../Slimefun/GPS/TeleportationSequence.java | 85 ----------- .../Slimefun/utils/Utilities.java | 2 - 6 files changed, 148 insertions(+), 132 deletions(-) create mode 100644 src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSTeleportation.java delete mode 100644 src/main/java/me/mrCookieSlime/Slimefun/GPS/TeleportationSequence.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java index 3fc25b911..a8c1f7122 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java @@ -30,7 +30,7 @@ public class TeleporterCommand extends SubCommand { OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); if (player.getName() != null) { - SlimefunPlugin.getGPSNetwork().openTeleporterGUI((Player) sender, player.getUniqueId(), ((Player) sender).getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); + SlimefunPlugin.getGPSNetwork().getTeleleportationService().openTeleporterGUI((Player) sender, player.getUniqueId(), ((Player) sender).getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); } else SlimefunPlugin.getLocal().sendMessage(sender, "messages.unknown-player", msg -> msg.replace("%player%", args[1])); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TeleporterListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TeleporterListener.java index 359a57b50..04d769363 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TeleporterListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TeleporterListener.java @@ -41,7 +41,7 @@ public class TeleporterListener implements Listener { Block block = e.getClickedBlock().getRelative(BlockFace.DOWN); UUID owner = UUID.fromString(BlockStorage.getLocationInfo(block.getLocation(), "owner")); - SlimefunPlugin.getGPSNetwork().openTeleporterGUI(e.getPlayer(), owner, block, SlimefunPlugin.getGPSNetwork().getNetworkComplexity(owner)); + SlimefunPlugin.getGPSNetwork().getTeleleportationService().openTeleporterGUI(e.getPlayer(), owner, block, SlimefunPlugin.getGPSNetwork().getNetworkComplexity(owner)); } } else if (id.equals("ELEVATOR_PLATE")) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSNetwork.java b/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSNetwork.java index 335c31743..643ea5b68 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSNetwork.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSNetwork.java @@ -11,7 +11,6 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Sound; import org.bukkit.World.Environment; -import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -35,10 +34,8 @@ public class GPSNetwork { private final int[] border = {0, 1, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53}; private final int[] inventory = {19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43}; - private final int[] teleporterBorder = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53}; - private final int[] teleporterInventory = {19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43}; - private final Map> transmitters = new HashMap<>(); + private final GPSTeleportation teleportation = new GPSTeleportation(this); private final ItemStack deathpointIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWFlMzg1NWY5NTJjZDRhMDNjMTQ4YTk0NmUzZjgxMmE1OTU1YWQzNWNiY2I1MjYyN2VhNGFjZDQ3ZDMwODEifX19"); private final ItemStack netherIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDgzNTcxZmY1ODlmMWE1OWJiMDJiODA4MDBmYzczNjExNmUyN2MzZGNmOWVmZWJlZGU4Y2YxZmRkZSJ9fX0="); @@ -230,45 +227,8 @@ public class GPSNetwork { return transmitters.getOrDefault(uuid, new HashSet<>()); } - public void openTeleporterGUI(Player p, UUID uuid, Block b, int complexity) { - if (SlimefunPlugin.getUtilities().teleporterUsers.contains(p.getUniqueId())) { - return; - } - - p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1F, 1F); - SlimefunPlugin.getUtilities().teleporterUsers.add(p.getUniqueId()); - - ChestMenu menu = new ChestMenu("&3Teleporter"); - menu.addMenuCloseHandler(pl -> SlimefunPlugin.getUtilities().teleporterUsers.remove(pl.getUniqueId())); - - for (int slot : teleporterBorder) { - menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); - } - - menu.addItem(4, new CustomItem(worldIcon, "&7Waypoint Overview &e(Select a Destination)")); - menu.addMenuClickHandler(4, ChestMenuUtils.getEmptyClickHandler()); - - Location source = new Location(b.getWorld(), b.getX() + 0.5D, b.getY() + 2D, b.getZ() + 0.5D); - int index = 0; - - for (Map.Entry entry : SlimefunPlugin.getGPSNetwork().getWaypoints(uuid).entrySet()) { - if (index >= teleporterInventory.length) break; - int slot = teleporterInventory[index]; - - Location l = entry.getValue(); - ItemStack globe = getIcon(entry); - - menu.addItem(slot, new CustomItem(globe, entry.getKey(), "&8\u21E8 &7World: &r" + l.getWorld().getName(), "&8\u21E8 &7X: &r" + l.getX(), "&8\u21E8 &7Y: &r" + l.getY(), "&8\u21E8 &7Z: &r" + l.getZ(), "&8\u21E8 &7Estimated Teleportation Time: &r" + (50 / TeleportationSequence.getSpeed(SlimefunPlugin.getGPSNetwork().getNetworkComplexity(uuid), source, l)) + "s", "", "&8\u21E8 &cClick to select")); - menu.addMenuClickHandler(slot, (pl, slotn, item, action) -> { - pl.closeInventory(); - TeleportationSequence.start(pl.getUniqueId(), complexity, source, l, false); - return false; - }); - - index++; - } - - menu.open(p); + public GPSTeleportation getTeleleportationService() { + return teleportation; } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSTeleportation.java b/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSTeleportation.java new file mode 100644 index 000000000..f7a4d61e3 --- /dev/null +++ b/src/main/java/me/mrCookieSlime/Slimefun/GPS/GPSTeleportation.java @@ -0,0 +1,143 @@ +package me.mrCookieSlime.Slimefun.GPS; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.api.Slimefun; + +public final class GPSTeleportation { + + private final Set teleporterUsers = new HashSet<>(); + private final int[] teleporterBorder = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53}; + private final int[] teleporterInventory = {19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43}; + + private final GPSNetwork network; + + public GPSTeleportation(GPSNetwork gpsNetwork) { + network = gpsNetwork; + } + + public void openTeleporterGUI(Player p, UUID uuid, Block b, int complexity) { + if (teleporterUsers.contains(p.getUniqueId())) { + return; + } + + p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1F, 1F); + teleporterUsers.add(p.getUniqueId()); + + ChestMenu menu = new ChestMenu("&3Teleporter"); + menu.addMenuCloseHandler(pl -> teleporterUsers.remove(pl.getUniqueId())); + + for (int slot : teleporterBorder) { + menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); + } + + menu.addItem(4, new CustomItem(SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzljODg4MWU0MjkxNWE5ZDI5YmI2MWExNmZiMjZkMDU5OTEzMjA0ZDI2NWRmNWI0MzliM2Q3OTJhY2Q1NiJ9fX0="), "&7Waypoint Overview &e(Select a Destination)")); + menu.addMenuClickHandler(4, ChestMenuUtils.getEmptyClickHandler()); + + Location source = new Location(b.getWorld(), b.getX() + 0.5D, b.getY() + 2D, b.getZ() + 0.5D); + int index = 0; + + for (Map.Entry entry : network.getWaypoints(uuid).entrySet()) { + if (index >= teleporterInventory.length) break; + int slot = teleporterInventory[index]; + + Location l = entry.getValue(); + ItemStack globe = network.getIcon(entry); + + menu.addItem(slot, new CustomItem(globe, entry.getKey(), "&8\u21E8 &7World: &r" + l.getWorld().getName(), "&8\u21E8 &7X: &r" + l.getX(), "&8\u21E8 &7Y: &r" + l.getY(), "&8\u21E8 &7Z: &r" + l.getZ(), "&8\u21E8 &7Estimated Teleportation Time: &r" + (50 / getSpeed(network.getNetworkComplexity(uuid), source, l)) + "s", "", "&8\u21E8 &cClick to select")); + menu.addMenuClickHandler(slot, (pl, slotn, item, action) -> { + pl.closeInventory(); + start(pl.getUniqueId(), complexity, source, l, false); + return false; + }); + + index++; + } + + menu.open(p); + } + + public void start(UUID uuid, int complexity, Location source, Location destination, boolean resistance) { + teleporterUsers.add(uuid); + + updateProgress(uuid, getSpeed(complexity, source, destination), 1, source, destination, resistance); + } + + public int getSpeed(int complexity, Location source, Location destination) { + int speed = complexity / 200; + if (speed > 50) speed = 50; + speed = speed - (distance(source, destination) / 200); + + return speed < 1 ? 1: speed; + } + + private int distance(Location source, Location destination) { + if (source.getWorld().getName().equals(destination.getWorld().getName())) { + int distance = (int) source.distance(destination); + return distance > 8000 ? 8000: distance; + } + else return 8000; + } + + private boolean isValid(Player p, Location source) { + return p != null && p.getLocation().distanceSquared(source) < 2.0; + } + + private void cancel(UUID uuid, Player p) { + teleporterUsers.remove(uuid); + + if (p != null) { + p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.cancelled")), ChatColors.color("&c&k40&r&c%"), 20, 60, 20); + } + } + + private void updateProgress(UUID uuid, int speed, int progress, Location source, Location destination, boolean resistance) { + Player p = Bukkit.getPlayer(uuid); + + if (isValid(p, source)) { + if (progress > 99) { + p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.teleported")), ChatColors.color("&b100%"), 20, 60, 20); + + p.teleport(destination); + + if (resistance) { + p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 600, 20)); + SlimefunPlugin.getLocal().sendMessage(p, "machines.TELEPORTER.invulnerability"); + } + + destination.getWorld().spawnParticle(Particle.PORTAL,new Location(destination.getWorld(), destination.getX(), destination.getY() + 1, destination.getZ()),progress * 2, 0.2F, 0.8F, 0.2F ); + destination.getWorld().playSound(destination, Sound.BLOCK_BEACON_ACTIVATE, 1F, 1F); + teleporterUsers.remove(uuid); + } + else { + p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.teleporting")), ChatColors.color("&b" + progress + "%"), 0, 60, 0); + + source.getWorld().spawnParticle(Particle.PORTAL, source, progress * 2, 0.2F, 0.8F, 0.2F); + source.getWorld().playSound(source, Sound.BLOCK_BEACON_AMBIENT, 1F, 0.6F); + + Slimefun.runSync(() -> updateProgress(uuid, speed, progress + speed, source, destination, resistance), 10L); + } + } + else cancel(uuid, p); + } + +} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/GPS/TeleportationSequence.java b/src/main/java/me/mrCookieSlime/Slimefun/GPS/TeleportationSequence.java deleted file mode 100644 index f3c9a9e80..000000000 --- a/src/main/java/me/mrCookieSlime/Slimefun/GPS/TeleportationSequence.java +++ /dev/null @@ -1,85 +0,0 @@ -package me.mrCookieSlime.Slimefun.GPS; - -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Particle; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; - -import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; -import me.mrCookieSlime.Slimefun.SlimefunPlugin; -import me.mrCookieSlime.Slimefun.api.Slimefun; - -public final class TeleportationSequence { - - private TeleportationSequence() {} - - public static void start(UUID uuid, int complexity, Location source, Location destination, boolean resistance) { - SlimefunPlugin.getUtilities().teleporterUsers.add(uuid); - - updateProgress(uuid, getSpeed(complexity, source, destination), 1, source, destination, resistance); - } - - public static int getSpeed(int complexity, Location source, Location destination) { - int speed = complexity / 200; - if (speed > 50) speed = 50; - speed = speed - (distance(source, destination) / 200); - - return speed < 1 ? 1: speed; - } - - private static int distance(Location source, Location destination) { - if (source.getWorld().getName().equals(destination.getWorld().getName())) { - int distance = (int) source.distance(destination); - return distance > 8000 ? 8000: distance; - } - else return 8000; - } - - private static boolean isValid(Player p, Location source) { - return p != null && p.getLocation().distanceSquared(source) < 2.0; - } - - private static void cancel(UUID uuid, Player p) { - SlimefunPlugin.getUtilities().teleporterUsers.remove(uuid); - - if (p != null) { - p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.cancelled")), ChatColors.color("&c&k40&r&c%"), 20, 60, 20); - } - } - - private static void updateProgress(UUID uuid, int speed, int progress, Location source, Location destination, boolean resistance) { - Player p = Bukkit.getPlayer(uuid); - - if (isValid(p, source)) { - if (progress > 99) { - p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.teleported")), ChatColors.color("&b100%"), 20, 60, 20); - - p.teleport(destination); - - if (resistance) { - p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 600, 20)); - SlimefunPlugin.getLocal().sendMessage(p, "machines.TELEPORTER.invulnerability"); - } - - destination.getWorld().spawnParticle(Particle.PORTAL,new Location(destination.getWorld(), destination.getX(), destination.getY() + 1, destination.getZ()),progress * 2, 0.2F, 0.8F, 0.2F ); - destination.getWorld().playSound(destination, Sound.BLOCK_BEACON_ACTIVATE, 1F, 1F); - SlimefunPlugin.getUtilities().teleporterUsers.remove(uuid); - } - else { - p.sendTitle(ChatColors.color(SlimefunPlugin.getLocal().getMessage(p, "machines.TELEPORTER.teleporting")), ChatColors.color("&b" + progress + "%"), 0, 60, 0); - - source.getWorld().spawnParticle(Particle.PORTAL, source, progress * 2, 0.2F, 0.8F, 0.2F); - source.getWorld().playSound(source, Sound.BLOCK_BEACON_AMBIENT, 1F, 0.6F); - - Slimefun.runSync(() -> updateProgress(uuid, speed, progress + speed, source, destination, resistance), 10L); - } - } - else cancel(uuid, p); - } - -} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/utils/Utilities.java b/src/main/java/me/mrCookieSlime/Slimefun/utils/Utilities.java index 94a0242f1..6fc9f11ee 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/utils/Utilities.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/utils/Utilities.java @@ -102,8 +102,6 @@ public final class Utilities { public final List blocks = new ArrayList<>(); public final List cancelPlace = new ArrayList<>(); - public final Set teleporterUsers = new HashSet<>(); - public final Map resources = new HashMap<>(); public final Map resourceConfigs = new HashMap<>();