diff --git a/src/main/java/de/fanta/challenges/CoordsTargeter.java b/src/main/java/de/fanta/challenges/CoordsTargeter.java new file mode 100644 index 0000000..4d5c40f --- /dev/null +++ b/src/main/java/de/fanta/challenges/CoordsTargeter.java @@ -0,0 +1,88 @@ +package de.fanta.challenges; + +import de.fanta.challenges.events.TimerChangedEvent; +import de.fanta.challenges.utils.ChatUtil; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.Vibration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.util.HashMap; +import java.util.UUID; + +public class CoordsTargeter implements Listener { + + private static final Challenges plugin = Challenges.getPlugin(); + private static final HashMap locationMap = new HashMap<>(); + + private int taskId = -1; + + @EventHandler + public void onTimer(TimerChangedEvent e) { + if (e.isRunning()) { + startUpdateTask(); + } else { + locationMap.clear(); + stopUpdateTask(); + } + } + + @EventHandler + public void onLeave(PlayerQuitEvent e) { + if (locationMap.containsKey(e.getPlayer().getUniqueId())) { + removeLocation(e.getPlayer().getUniqueId()); + } + } + + public void updateTargeter() { + for (Player p : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { + if (locationMap.containsKey(p.getUniqueId())) { + Location targetLocation = locationMap.get(p.getUniqueId()); + + if (targetLocation != null && targetLocation.getWorld() == p.getWorld()) { + Location playerlocation; + if (p.getVehicle() != null) { + playerlocation = p.getLocation().clone().add(0, 0.5, 0); + } else { + playerlocation = p.getLocation().clone().add(0, 1, 0); + } + + int distance = (int) playerlocation.toVector().subtract(targetLocation.toVector()).length(); + Vibration vibration = new Vibration(new Vibration.Destination.BlockDestination(targetLocation.getBlock()), distance * 2); + p.spawnParticle(Particle.VIBRATION, playerlocation, 1, vibration); + + if (distance < 5) { + removeLocation(p.getUniqueId()); + ChatUtil.sendNormalMessage(p, "Sie haben ihr Ziel erreicht!"); + } + } else { + removeLocation(p.getUniqueId()); + ChatUtil.sendWarningMessage(p, "Du bist nicht mehr in der Welt vom Waypoint, der Weg wird nicht mehr angezeigt!"); + } + } + } + } + + public void startUpdateTask() { + taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::updateTargeter, 5L, 5L); + } + + public void stopUpdateTask() { + plugin.getServer().getScheduler().cancelTask(taskId); + } + + public static void addLocation(UUID uuid, Location location) { + locationMap.put(uuid, location); + } + + public static void removeLocation(UUID uuid) { + locationMap.remove(uuid); + } + + public static boolean containsLocation(UUID uuid) { + return locationMap.containsKey(uuid); + } +} diff --git a/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java b/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java index 70d18bb..c77445e 100644 --- a/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java +++ b/src/main/java/de/fanta/challenges/challenges/AllItemsChallenge.java @@ -2,6 +2,7 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.guiutils.GUIUtils; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; @@ -33,9 +34,17 @@ items.add(mat.toString()); } } + + ItemStack configStack = plugin.getConfig().getItemStack("allitemscurrentitem"); + if (configStack != null) { + item = configStack.getType(); + } + if (item == null) { Random r = new Random(); item = Material.valueOf(items.get(r.nextInt((items.size() - 1) + 1))); + + GUIUtils.setConfig("allitemscurrentitem", new ItemStack(item)); } bossBar = Bukkit.createBossBar("", BarColor.GREEN, BarStyle.SOLID); bossBar.setVisible(true); @@ -77,6 +86,7 @@ if (items.size() != 0) { Random r = new Random(); item = Material.valueOf(items.get(r.nextInt((items.size() - 1) + 1))); + GUIUtils.setConfig("allitemscurrentitem", new ItemStack(item)); FileConfiguration config = Challenges.getPlugin().getAllItemsConfig(); if (skipped) { ChatUtil.sendBrodcastMessage("Item: " + ChatUtil.BLUE + old.toString().replace("_", " ") + ChatUtil.GREEN + " wurde von " + p.getName() + " übersprungen."); diff --git a/src/main/java/de/fanta/challenges/commands/CommandRegistration.java b/src/main/java/de/fanta/challenges/commands/CommandRegistration.java index e132f42..ce09463 100644 --- a/src/main/java/de/fanta/challenges/commands/CommandRegistration.java +++ b/src/main/java/de/fanta/challenges/commands/CommandRegistration.java @@ -16,6 +16,7 @@ import de.fanta.challenges.commands.coords.CoordsGetCommand; import de.fanta.challenges.commands.coords.CoordsSaveCommand; import de.fanta.challenges.commands.coords.CoordsShareCommand; +import de.fanta.challenges.commands.coords.CoordsTargetCommand; import de.fanta.challenges.commands.editor.EditorCommand; import de.fanta.challenges.commands.editor.EditorSetCommand; import de.fanta.challenges.commands.event.CreateTeamsCommand; @@ -73,6 +74,7 @@ coordsRouter.addCommandMapping(new CoordsGetCommand(plugin), "get"); coordsRouter.addCommandMapping(new CoordsSaveCommand(plugin), "save"); coordsRouter.addCommandMapping(new CoordsDeleteCommand(), "delete"); + coordsRouter.addCommandMapping(new CoordsTargetCommand(plugin), "target"); CommandRouter hpRouter = new CommandRouter(plugin.getCommand("hp")); hpRouter.addCommandMapping(new HPAddCommand(plugin), "add"); diff --git a/src/main/java/de/fanta/challenges/commands/coords/CoordsTargetCommand.java b/src/main/java/de/fanta/challenges/commands/coords/CoordsTargetCommand.java new file mode 100644 index 0000000..f227bbc --- /dev/null +++ b/src/main/java/de/fanta/challenges/commands/coords/CoordsTargetCommand.java @@ -0,0 +1,76 @@ +package de.fanta.challenges.commands.coords; + +import de.fanta.challenges.Challenges; +import de.fanta.challenges.CoordsTargeter; +import de.fanta.challenges.utils.ChatUtil; +import de.iani.cubesideutils.bukkit.commands.SubCommand; +import de.iani.cubesideutils.commands.ArgsParser; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Collection; + +public class CoordsTargetCommand extends SubCommand { + + private final Challenges plugin; + + public CoordsTargetCommand(Challenges plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String s1, ArgsParser args) { + if (!(sender instanceof Player player)) { + ChatUtil.sendErrorMessage(sender, "You are not a Player :>"); + return true; + } + + if (!plugin.getTimer().isRunning()) { + ChatUtil.sendErrorMessage(player, "Das kannst du nur machen wenn der Timer aktiv ist."); + return true; + } + + if (args.hasNext()) { + String next = args.getNext(); + if (plugin.getConfig().contains("Saved_Locations." + next)) { + World world = Bukkit.getWorld(plugin.getConfig().getString("Saved_Locations." + next + ".World")); + int x = plugin.getConfig().getInt("Saved_Locations." + next + ".BlockX"); + int y = plugin.getConfig().getInt("Saved_Locations." + next + ".BlockY"); + int z = plugin.getConfig().getInt("Saved_Locations." + next + ".BlockZ"); + if (world != null) { + if (world == player.getWorld()) { + Location location = new Location(world, x, y, z); + CoordsTargeter.addLocation(player.getUniqueId(), location); + ChatUtil.sendNormalMessage(player, "Der Weg zu " + next + " wird dir nun angezeigt!"); + ChatUtil.sendNormalMessage(player, "Du kannst dies mit '/coords target' wieder ausschalten."); + } else { + ChatUtil.sendErrorMessage(player, "Du befindest dich nicht in der gleichen Welt wie der Waypoint!"); + } + } else { + ChatUtil.sendWarningMessage(player, "Diese Position existiert nicht!"); + } + } else { + ChatUtil.sendWarningMessage(player, "Diese Position existiert nicht!"); + } + } else { + if (CoordsTargeter.containsLocation(player.getUniqueId())) { + CoordsTargeter.removeLocation(player.getUniqueId()); + ChatUtil.sendWarningMessage(player, "Der Weg wird nun nicht mehr angezeigt!"); + } else { + ChatUtil.sendWarningMessage(player, "Du hast keine Position gewählt. -> /coords target [Location]"); + } + } + + + return true; + } + + @Override + public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { + return plugin.getConfig().getConfigurationSection("Saved_Locations").getKeys(false); + } +} diff --git a/src/main/java/de/fanta/challenges/listeners/EventRegistration.java b/src/main/java/de/fanta/challenges/listeners/EventRegistration.java index 1eb65f8..b87e9f1 100644 --- a/src/main/java/de/fanta/challenges/listeners/EventRegistration.java +++ b/src/main/java/de/fanta/challenges/listeners/EventRegistration.java @@ -1,6 +1,7 @@ package de.fanta.challenges.listeners; import de.fanta.challenges.Challenges; +import de.fanta.challenges.CoordsTargeter; import de.fanta.challenges.challenges.BedrockWallChallenge; import de.fanta.challenges.challenges.ChallengeEvents.BingoChallengeEvent; import de.fanta.challenges.challenges.ChallengeEvents.DeathrunChallengeEvent; @@ -26,14 +27,11 @@ import de.fanta.challenges.challenges.XPChallenge; import de.fanta.challenges.gravestones.GravestoneListener; import de.fanta.challenges.guis.CheckItemsGUI; -import de.fanta.challenges.guis.ResetGui; import de.fanta.challenges.guis.TeleportGUI; -import de.fanta.challenges.guis.TimerGui; import de.fanta.challenges.guis.coordsgui.CoordsDeleteGUI; import de.fanta.challenges.guis.coordsgui.CoordsGUI; import de.fanta.challenges.guis.eventgui.EventGui; import de.fanta.challenges.guis.eventgui.EventItemsGui; -import de.fanta.challenges.guis.eventgui.SammelFieberSettingsGui; import de.fanta.challenges.guis.eventgui.TeamSelectGUI; import de.fanta.challenges.guis.eventgui.TeamSettingsGUI; import de.fanta.challenges.teams.TeamListener; @@ -53,6 +51,7 @@ pM.registerEvents(new EntityListener(), plugin); pM.registerEvents(new PlayerListener(), plugin); pM.registerEvents(new VoidListener(), plugin); + pM.registerEvents(new CoordsTargeter(), plugin); pM.registerEvents(new WindowManager(), plugin); pM.registerEvents(new EventItemsGui(), plugin);