diff --git a/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java b/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java index 5c3345c..40bb1e2 100644 --- a/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java +++ b/src/main/java/de/fanta/challenges/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java @@ -27,6 +27,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerAttemptPickupItemEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; @@ -48,7 +49,6 @@ private static boolean running; private static final HashMap playerSpawnLocations = new HashMap<>(); private static final Map bossBarMap = new HashMap<>(); - private int updateTaskID = -1; private int spawnItemTaskID = -1; private static final ArrayList materials = new ArrayList<>(); private static Location lastSpawnLocation = null; @@ -67,17 +67,17 @@ } } materials.remove(Material.ELYTRA); + materials.remove(Material.LAVA_BUCKET); for (Player pp : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { plugin.getScoreManager().join(new ChallengePlayer(pp.getUniqueId())); + plugin.getScoreManager().setScore(new ChallengePlayer(pp.getUniqueId()), 1); } - - startUpdateTask(); + updateBossBar(); startItemSpawnTask(); setRunning(true); } else { - stopUpdateTask(); stopItemSpawnTask(); for (Player pp : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { pp.setGameMode(GameMode.SPECTATOR); @@ -113,7 +113,13 @@ if (plugin.getVanish().isVanish(e.getPlayer())) { return; } - plugin.getScoreManager().join(new ChallengePlayer(e.getPlayer().getUniqueId())); + ChallengePlayer challengePlayer = new ChallengePlayer(e.getPlayer().getUniqueId()); + plugin.getScoreManager().join(challengePlayer); + if (plugin.getScoreManager().getScore(challengePlayer) >= 1) { + return; + } + plugin.getScoreManager().setScore(challengePlayer, 1); + updateBossBar(); } if (isLoaded()) { @@ -137,7 +143,7 @@ int BARRIER_POS = 20; int BARRIER_SIZE = 6; if (distanceToCenter >= BARRIER_POS) { //Outside border - p.teleport(getPlayerLocation(p).clone().add(0.5,2,0.5)); + p.teleport(getPlayerLocation(p).clone().add(0.5, 2, 0.5)); } else if (distanceToCenter >= BARRIER_POS - 6) { //Inside border //Send barrier double barrierZ = spawnLocation.getZ() + BARRIER_POS * (posRelatedToCenter > 0 ? 1 : -1); @@ -157,6 +163,12 @@ resetBarrier(p, world, barrierLocations); } } + + if (isLoaded() && !isRunning()) { + if (p.getLocation().getY() <= 0) { + teleportPlayer(p); + } + } } @EventHandler @@ -178,6 +190,28 @@ } } + @EventHandler + public void onBlockPlace(BlockPlaceEvent e) { + if (isRunning()) { + Player placer = e.getPlayer(); + + double posRelatedToCenter = placer.getLocation().getZ() - getPlayerLocation(placer).clone().getZ(); + double distanceToCenter = Math.abs(posRelatedToCenter); + if (distanceToCenter >= 20) { + e.setCancelled(true); + return; + } + + ScoreManager scoreManager = plugin.getScoreManager(); + int score = scoreManager.getScore(new ChallengePlayer(placer.getUniqueId())); + if (e.getBlock().getX() == score + 1) { + scoreManager.updateScore(new ChallengePlayer(placer.getUniqueId()), 1); + } + + updateBossBar(); + } + } + public static boolean load(Player player) { if (!load) { voidWorld = Bukkit.getWorld("VOID"); @@ -188,6 +222,7 @@ wc.generateStructures(false); wc.generator(new EmptyChunkGenerator()); voidWorld = Bukkit.getServer().createWorld(wc); + voidWorld.setPVP(false); } for (Player pp : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { @@ -241,23 +276,15 @@ } } - public void startUpdateTask() { - updateTaskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::tick, 1L, 1L); - } - - public void stopUpdateTask() { - plugin.getServer().getScheduler().cancelTask(updateTaskID); - } - - public void startItemSpawnTask() { + private void startItemSpawnTask() { spawnItemTaskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this::itemSpawn, 6 * 20, 6 * 20); } - public void stopItemSpawnTask() { + private void stopItemSpawnTask() { plugin.getServer().getScheduler().cancelTask(spawnItemTaskID); } - public void itemSpawn() { + private void itemSpawn() { Random random = new Random(); for (Player player : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { ItemStack randomItem = new ItemStack(materials.get(random.nextInt(materials.size()))); @@ -266,31 +293,17 @@ } } - private void tick() { - if (Objects.equals(Config.getString("event.type"), "bridgerace") && plugin.getTimer().isRunning()) { - ScoreManager scoreManager = plugin.getScoreManager(); - for (Player p : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { - if (!plugin.getVanish().isVanish(p)) { - if (p.getGameMode() != GameMode.SPECTATOR && p.getLocation().getY() >= 60 && p.getLocation().getX() >= 0 && p.getWorld() == voidWorld) { - double posRelatedToCenter = p.getLocation().getZ() - getPlayerLocation(p).clone().getZ(); - double distanceToCenter = Math.abs(posRelatedToCenter); - if (distanceToCenter >= 20) { - return; - } - - int distance = (int) p.getLocation().toVector().subtract(getPlayerLocation(p).clone().toVector()).length(); - if (distance > scoreManager.getScore(new ChallengePlayer(p.getUniqueId()))) { - scoreManager.setScore(new ChallengePlayer(p.getUniqueId()), distance); - } - } - BossBar bossBar = bossBarMap.computeIfAbsent(p, player -> { - BossBar newBossBar = Bukkit.createBossBar(ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + scoreManager.getPosition(new ChallengePlayer(p.getUniqueId())) + ChatUtil.RED + " | " + ChatUtil.GREEN + "Länge deiner Brücke: " + ChatUtil.BLUE + scoreManager.getScore(new ChallengePlayer(p.getUniqueId())) + " Blöcke", BarColor.GREEN, BarStyle.SOLID); - newBossBar.setVisible(true); - newBossBar.addPlayer(p); - return newBossBar; - }); - bossBar.setTitle(ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + scoreManager.getPosition(new ChallengePlayer(p.getUniqueId())) + ChatUtil.RED + " | " + ChatUtil.GREEN + "Länge deiner Brücke: " + ChatUtil.BLUE + scoreManager.getScore(new ChallengePlayer(p.getUniqueId())) + " Blöcke"); - } + private void updateBossBar() { + ScoreManager scoreManager = plugin.getScoreManager(); + for (Player p : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { + if (!plugin.getVanish().isVanish(p)) { + BossBar bossBar = bossBarMap.computeIfAbsent(p, player -> { + BossBar newBossBar = Bukkit.createBossBar(ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + scoreManager.getPosition(new ChallengePlayer(p.getUniqueId())) + ChatUtil.RED + " | " + ChatUtil.GREEN + "Länge deiner Brücke: " + ChatUtil.BLUE + scoreManager.getScore(new ChallengePlayer(p.getUniqueId())) + " Blöcke", BarColor.GREEN, BarStyle.SOLID); + newBossBar.setVisible(true); + newBossBar.addPlayer(p); + return newBossBar; + }); + bossBar.setTitle(ChatUtil.GREEN + "Position: " + ChatUtil.BLUE + scoreManager.getPosition(new ChallengePlayer(p.getUniqueId())) + ChatUtil.RED + " | " + ChatUtil.GREEN + "Länge deiner Brücke: " + ChatUtil.BLUE + scoreManager.getScore(new ChallengePlayer(p.getUniqueId())) + " Blöcke"); } } }