From 3f792cb367ac9d5e9f4232bf52c410c4e166ef0a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Nov 2022 17:36:29 +0100 Subject: [PATCH] Add long right click compass to reset --- src/de/steamwar/lobby/LobbySystem.properties | 1 + .../steamwar/lobby/jumpandrun/JumpAndRun.java | 65 ++++++++++++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/de/steamwar/lobby/LobbySystem.properties b/src/de/steamwar/lobby/LobbySystem.properties index 9b83870..78e8745 100644 --- a/src/de/steamwar/lobby/LobbySystem.properties +++ b/src/de/steamwar/lobby/LobbySystem.properties @@ -84,5 +84,6 @@ PARTICLE_EVENT_WGS = §fWGS PARTICLE_EVENT_WARGEARCLASH = §fClash JUMP_AND_RUN_PROGRESS = §e{0}§8/§f{1} §c{2} §7{3} +JUMP_AND_RUN_CANCEL = {0} JUMP_AND_RUN_TIME = mm:ss SSS JUMP_AND_RUN_FINISHED = §aFinished in {0} with {1} fails \ No newline at end of file diff --git a/src/de/steamwar/lobby/jumpandrun/JumpAndRun.java b/src/de/steamwar/lobby/jumpandrun/JumpAndRun.java index c86bc42..2c40d41 100644 --- a/src/de/steamwar/lobby/jumpandrun/JumpAndRun.java +++ b/src/de/steamwar/lobby/jumpandrun/JumpAndRun.java @@ -10,6 +10,8 @@ import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; @@ -20,15 +22,27 @@ import java.util.*; public class JumpAndRun extends BasicListener { + private static final String BAR_EMPTY = "||||||||||||||||||||||||||||||"; + public static List points = new ArrayList<>(); private static final Map CURRENT_POS = new HashMap<>(); private static final Map FAILS = new HashMap<>(); private static final Map START = new HashMap<>(); + private static final Map CLICKED = new HashMap<>(); + private static final Map CLICKED_COUNT = new HashMap<>(); - static { + { Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { + Set toReset = new HashSet<>(); CURRENT_POS.forEach((player, index) -> { + if (System.currentTimeMillis() - CLICKED.getOrDefault(player, 0L) > 500) { + CLICKED.remove(player); + CLICKED_COUNT.remove(player); + } else { + CLICKED_COUNT.put(player, CLICKED_COUNT.getOrDefault(player, -1) + 1); + } + Location location = player.getLocation(); Vector point = points.get(index); if (index < points.size() - 1) { @@ -44,10 +58,24 @@ public class JumpAndRun extends BasicListener { } } - long time = System.currentTimeMillis() - START.get(player); - SimpleDateFormat format = new SimpleDateFormat(LobbySystem.getMessage().parse("JUMP_AND_RUN_TIME", player), Locale.ROOT); - String parsed = format.format(new Date(time)); - LobbySystem.getMessage().sendPrefixless("JUMP_AND_RUN_PROGRESS", player, ChatMessageType.ACTION_BAR, index + 1, points.size(), FAILS.get(player), parsed.substring(0, parsed.length() - 2)); + int count = CLICKED_COUNT.getOrDefault(player, -1); + if (count >= 0) { + if (count > 60) { + toReset.add(player); + return; + } + count = Math.min(count / 2, 30); + LobbySystem.getMessage().sendPrefixless("JUMP_AND_RUN_CANCEL", player, ChatMessageType.ACTION_BAR, "§e" + BAR_EMPTY.substring(0, count) + "§7" + BAR_EMPTY.substring(count)); + } else { + long time = System.currentTimeMillis() - START.get(player); + SimpleDateFormat format = new SimpleDateFormat(LobbySystem.getMessage().parse("JUMP_AND_RUN_TIME", player), Locale.ROOT); + String parsed = format.format(new Date(time)); + LobbySystem.getMessage().sendPrefixless("JUMP_AND_RUN_PROGRESS", player, ChatMessageType.ACTION_BAR, index + 1, points.size(), FAILS.get(player), parsed.substring(0, parsed.length() - 2)); + } + }); + toReset.forEach(player -> { + reset(player); + player.teleport(Bukkit.getWorlds().get(0).getSpawnLocation().clone().add(0.5, 0, 0.5)); }); }, 1, 1); } @@ -93,11 +121,7 @@ public class JumpAndRun extends BasicListener { SimpleDateFormat format = new SimpleDateFormat(LobbySystem.getMessage().parse("JUMP_AND_RUN_TIME", event.getPlayer()), Locale.ROOT); String parsed = format.format(new Date(time)); LobbySystem.getMessage().sendPrefixless("JUMP_AND_RUN_FINISHED", event.getPlayer(), parsed.substring(0, parsed.length() - 2), FAILS.get(event.getPlayer())); - PlayerSpawn.giveItems(event.getPlayer()); - event.getPlayer().setAllowFlight(true); - CURRENT_POS.remove(event.getPlayer()); - START.remove(event.getPlayer()); - FAILS.remove(event.getPlayer()); + reset(event.getPlayer()); } } @@ -107,5 +131,26 @@ public class JumpAndRun extends BasicListener { CURRENT_POS.remove(player); START.remove(player); FAILS.remove(player); + CLICKED.remove(event.getPlayer()); + CLICKED_COUNT.remove(event.getPlayer()); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getItem() == null) return; + if (event.getItem().getType() != Material.COMPASS) return; + if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) return; + event.setCancelled(true); + CLICKED.put(event.getPlayer(), System.currentTimeMillis()); + } + + private void reset(Player player) { + PlayerSpawn.giveItems(player); + player.setAllowFlight(true); + CURRENT_POS.remove(player); + START.remove(player); + FAILS.remove(player); + CLICKED.remove(player); + CLICKED_COUNT.remove(player); } }