1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

[CI skip] Minor refactoring

This commit is contained in:
TheBusyBiscuit 2020-01-31 09:47:04 +01:00
parent 4cca09f6c9
commit f0f3740a2c
6 changed files with 148 additions and 132 deletions

View File

@ -30,7 +30,7 @@ public class TeleporterCommand extends SubCommand {
OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
if (player.getName() != null) { 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])); else SlimefunPlugin.getLocal().sendMessage(sender, "messages.unknown-player", msg -> msg.replace("%player%", args[1]));
} }

View File

@ -41,7 +41,7 @@ public class TeleporterListener implements Listener {
Block block = e.getClickedBlock().getRelative(BlockFace.DOWN); Block block = e.getClickedBlock().getRelative(BlockFace.DOWN);
UUID owner = UUID.fromString(BlockStorage.getLocationInfo(block.getLocation(), "owner")); 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")) { else if (id.equals("ELEVATOR_PLATE")) {

View File

@ -11,7 +11,6 @@ import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; 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[] 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[] 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<UUID, Set<Location>> transmitters = new HashMap<>(); private final Map<UUID, Set<Location>> transmitters = new HashMap<>();
private final GPSTeleportation teleportation = new GPSTeleportation(this);
private final ItemStack deathpointIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWFlMzg1NWY5NTJjZDRhMDNjMTQ4YTk0NmUzZjgxMmE1OTU1YWQzNWNiY2I1MjYyN2VhNGFjZDQ3ZDMwODEifX19"); private final ItemStack deathpointIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWFlMzg1NWY5NTJjZDRhMDNjMTQ4YTk0NmUzZjgxMmE1OTU1YWQzNWNiY2I1MjYyN2VhNGFjZDQ3ZDMwODEifX19");
private final ItemStack netherIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDgzNTcxZmY1ODlmMWE1OWJiMDJiODA4MDBmYzczNjExNmUyN2MzZGNmOWVmZWJlZGU4Y2YxZmRkZSJ9fX0="); private final ItemStack netherIcon = SkullItem.fromBase64("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDgzNTcxZmY1ODlmMWE1OWJiMDJiODA4MDBmYzczNjExNmUyN2MzZGNmOWVmZWJlZGU4Y2YxZmRkZSJ9fX0=");
@ -230,45 +227,8 @@ public class GPSNetwork {
return transmitters.getOrDefault(uuid, new HashSet<>()); return transmitters.getOrDefault(uuid, new HashSet<>());
} }
public void openTeleporterGUI(Player p, UUID uuid, Block b, int complexity) { public GPSTeleportation getTeleleportationService() {
if (SlimefunPlugin.getUtilities().teleporterUsers.contains(p.getUniqueId())) { return teleportation;
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<String, Location> 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);
} }
} }

View File

@ -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<UUID> 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<String, Location> 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);
}
}

View File

@ -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);
}
}

View File

@ -102,8 +102,6 @@ public final class Utilities {
public final List<UUID> blocks = new ArrayList<>(); public final List<UUID> blocks = new ArrayList<>();
public final List<UUID> cancelPlace = new ArrayList<>(); public final List<UUID> cancelPlace = new ArrayList<>();
public final Set<UUID> teleporterUsers = new HashSet<>();
public final Map<String, OreGenResource> resources = new HashMap<>(); public final Map<String, OreGenResource> resources = new HashMap<>();
public final Map<String, Config> resourceConfigs = new HashMap<>(); public final Map<String, Config> resourceConfigs = new HashMap<>();