From f58f2260a5ba44b2f0c43e08673d9cba75038f75 Mon Sep 17 00:00:00 2001 From: Luca Date: Tue, 23 Nov 2021 13:36:48 +0100 Subject: [PATCH] added dropped items in game and fixed some generator issues --- .../java/rip/tilly/bedwars/game/Game.java | 4 + .../tilly/bedwars/generators/Generator.java | 10 ++- .../bedwars/listeners/RandomListeners.java | 11 --- .../listeners/game/GameStartListener.java | 8 +- .../bedwars/listeners/game/WorldListener.java | 90 +++++++++++++++++-- .../tilly/bedwars/runnables/GameRunnable.java | 2 + 6 files changed, 101 insertions(+), 24 deletions(-) diff --git a/src/main/java/rip/tilly/bedwars/game/Game.java b/src/main/java/rip/tilly/bedwars/game/Game.java index 2a8f876..ef18457 100644 --- a/src/main/java/rip/tilly/bedwars/game/Game.java +++ b/src/main/java/rip/tilly/bedwars/game/Game.java @@ -9,6 +9,7 @@ import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.github.paperspigot.Title; import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.game.arena.Arena; @@ -17,7 +18,9 @@ import rip.tilly.bedwars.generators.Generator; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.TimeUtils; +import java.awt.*; import java.util.*; +import java.util.List; import java.util.stream.Stream; @Getter @@ -30,6 +33,7 @@ public class Game { private final Set placedBlocksLocations = new ConcurrentSet<>(); private final Set spectators = new ConcurrentSet<>(); private final Set runnables = new HashSet<>(); + private final Set droppedItems = new ConcurrentSet<>(); private final List teams; private final Arena arena; diff --git a/src/main/java/rip/tilly/bedwars/generators/Generator.java b/src/main/java/rip/tilly/bedwars/generators/Generator.java index 768caf2..5c7c755 100644 --- a/src/main/java/rip/tilly/bedwars/generators/Generator.java +++ b/src/main/java/rip/tilly/bedwars/generators/Generator.java @@ -7,6 +7,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.EulerAngle; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.game.Game; import rip.tilly.bedwars.utils.CC; public class Generator { @@ -23,13 +24,16 @@ public class Generator { private BukkitTask rotateIndicatorTask = null; private int secondsSinceActivation; private boolean activated; + + private final Game game; - public Generator(Location location, GeneratorType generatorType, boolean isIslandGenerator) { + public Generator(Location location, GeneratorType generatorType, boolean isIslandGenerator, Game game) { this.location = location; this.generatorType = generatorType; this.isIslandGenerator = isIslandGenerator; this.secondsSinceActivation = this.getActivationTime(); this.activated = false; + this.game = game; } public void spawn() { @@ -87,7 +91,9 @@ public class Generator { throw new IllegalStateException("Unexcepted value: " + this.generatorType); } - this.location.getWorld().dropItemNaturally(this.location, new ItemStack(material, 1)); + ItemStack drop = new ItemStack(material, 1); + this.location.getWorld().dropItemNaturally(this.location, drop); + this.game.getDroppedItems().add(drop); } public void setActivated(boolean activated) { diff --git a/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java b/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java index dcef73f..727263a 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java +++ b/src/main/java/rip/tilly/bedwars/listeners/RandomListeners.java @@ -7,11 +7,9 @@ import org.bukkit.event.block.*; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.hanging.HangingBreakEvent; -import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerToggleFlightEvent; import org.bukkit.event.weather.WeatherChangeEvent; import rip.tilly.bedwars.BedWars; -import rip.tilly.bedwars.playerdata.PlayerData; import rip.tilly.bedwars.playerdata.PlayerState; public class RandomListeners implements Listener { @@ -23,15 +21,6 @@ public class RandomListeners implements Listener { event.setCancelled(true); } - @EventHandler - public void onPlayerDropItem(PlayerDropItemEvent event) { - PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(event.getPlayer().getUniqueId()); - - if (playerData.getPlayerState() != PlayerState.PLAYING) { - event.setCancelled(true); - } - } - @EventHandler public void onWeatherChange(WeatherChangeEvent event) { if (event.toWeatherState()) { diff --git a/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java b/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java index 24f4514..39ca7a3 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java +++ b/src/main/java/rip/tilly/bedwars/listeners/game/GameStartListener.java @@ -42,21 +42,21 @@ public class GameStartListener implements Listener { CopiedArena currentArena = game.getCopiedArena(); for (CustomLocation location : currentArena.getTeamGenerators()) { - Generator teamGen = new Generator(location.toBukkitLocation(), GeneratorType.IRON, false); + Generator teamGen = new Generator(location.toBukkitLocation(), GeneratorType.IRON, true, game); teamGen.setActivated(true); game.getActivatedGenerators().add(teamGen); } for (CustomLocation location : currentArena.getDiamondGenerators()) { - Generator diaGen = new Generator(location.toBukkitLocation(), GeneratorType.DIAMOND, true); + Generator diaGen = new Generator(location.toBukkitLocation(), GeneratorType.DIAMOND, false, game); diaGen.setActivated(true); game.getActivatedGenerators().add(diaGen); } for (CustomLocation location : currentArena.getEmeraldGenerators()) { - Generator emeGen = new Generator(location.toBukkitLocation(), GeneratorType.EMERALD, true); + Generator emeGen = new Generator(location.toBukkitLocation(), GeneratorType.EMERALD, false, game); emeGen.setActivated(true); game.getActivatedGenerators().add(emeGen); @@ -83,7 +83,7 @@ public class GameStartListener implements Listener { player.teleport(team.getId() == 1 ? locationA.toBukkitLocation() : locationB.toBukkitLocation()); player.getInventory().setArmorContents(this.plugin.getGameManager().getGameArmor(playerData)); - for (ItemStack stack : this.plugin.getGameManager().getGameItems()) { + for (ItemStack stack : this.plugin.getGameManager().getGameItems(playerData.getCurrentGameData())) { player.getInventory().addItem(stack); } })); diff --git a/src/main/java/rip/tilly/bedwars/listeners/game/WorldListener.java b/src/main/java/rip/tilly/bedwars/listeners/game/WorldListener.java index 7f55bd3..98bd0f1 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/game/WorldListener.java +++ b/src/main/java/rip/tilly/bedwars/listeners/game/WorldListener.java @@ -2,11 +2,16 @@ package rip.tilly.bedwars.listeners.game; import org.bukkit.*; import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.inventory.ItemStack; import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.game.Game; @@ -25,7 +30,7 @@ public class WorldListener implements Listener { return playerData.getPlayerState() == PlayerState.PLAYING; } - @EventHandler + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) public void onBlockBreak(BlockBreakEvent event) { Player player = event.getPlayer(); if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) { @@ -67,18 +72,17 @@ public class WorldListener implements Listener { event.setCancelled(true); return; } + event.setCancelled(true); + block.setType(Material.AIR); GameTeam opposingTeam = playerData.getTeamId() == 0 ? game.getTeams().get(1) : game.getTeams().get(0); opposingTeam.destroyBed(); game.broadcastWithSound(playerTeam.getPlayerTeam().getChatColor() + player.getName() + " &ehas destroyed " + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getName() + "'s &ebed!", Sound.ENDERDRAGON_GROWL); game.broadcastSound(Sound.ENDERDRAGON_GROWL); - playerData.setGameBedsDestroyed(playerData.getGameBedsDestroyed() + 1); + playerData.getCurrentGameData().setGameBedsDestroyed(playerData.getCurrentGameData().getGameBedsDestroyed() + 1); playerData.setBedsDestroyed(playerData.getBedsDestroyed() + 1); - block.getDrops().clear(); - block.getDrops().removeIf(Objects::nonNull); - Location location = block.getLocation(); World world = location.getWorld(); world.playEffect(location, Effect.CRIT, 1, 400); @@ -92,12 +96,24 @@ public class WorldListener implements Listener { return; } - player.getInventory().addItem(block.getDrops().toArray(new ItemStack[0])); game.removePlacedBlock(block); + + ItemStack[] itemStack = block.getDrops().toArray(new ItemStack[0]); + for (ItemStack drops : itemStack) { + block.getWorld().dropItemNaturally(block.getLocation(), drops); + game.getDroppedItems().add(drops); + } + + for (Entity entity : block.getWorld().getNearbyEntities(block.getLocation(), 1, 1, 1)) { + if (entity instanceof Item) { + game.addEntityToRemove(entity); + } + } + block.setType(Material.AIR); } - @EventHandler + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) public void onBlockPlace(BlockPlaceEvent event) { Player player = event.getPlayer(); if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) { @@ -145,4 +161,64 @@ public class WorldListener implements Listener { game.addPlacedBlock(block); } + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + Player player = event.getPlayer(); + if (player.getGameMode() == GameMode.CREATIVE) { + return; + } + + PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId()); + if (!this.isInGame(playerData)) { + event.setCancelled(true); + return; + } + + Game game = this.plugin.getGameManager().getGame(playerData); + if (game == null) { + event.setCancelled(true); + return; + } + + if (game.getGameState() != GameState.FIGHTING) { + event.setCancelled(true); + return; + } + + ItemStack itemStack = event.getItemDrop().getItemStack(); + game.getDroppedItems().add(itemStack); + game.addEntityToRemove(event.getItemDrop()); + } + + @EventHandler + public void onPlayerPickupItem(PlayerPickupItemEvent event) { + Player player = event.getPlayer(); + if (player.getGameMode() == GameMode.CREATIVE) { + return; + } + + PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId()); + if (!this.isInGame(playerData)) { + event.setCancelled(true); + return; + } + + Game game = this.plugin.getGameManager().getGame(playerData); + if (game == null) { + event.setCancelled(true); + return; + } + + if (game.getGameState() != GameState.FIGHTING) { + event.setCancelled(true); + return; + } + + if (game.getDroppedItems().contains(event.getItem().getItemStack())) { + game.getDroppedItems().remove(event.getItem().getItemStack()); + game.removeEntityToRemove(event.getItem()); + return; + } + } } diff --git a/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java b/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java index f4b96dc..5bf791c 100644 --- a/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java +++ b/src/main/java/rip/tilly/bedwars/runnables/GameRunnable.java @@ -36,6 +36,8 @@ public class GameRunnable extends BukkitRunnable { this.game.getTeams().forEach(team -> team.playingPlayers().forEach(player -> this.plugin.getPlayerDataManager().resetPlayer(player, true))); this.game.spectatorPlayers().forEach(this.plugin.getGameManager()::removeSpectator); + this.game.getDroppedItems().clear(); + this.plugin.getChunkClearingManager().resetArena(this.game.getCopiedArena()); this.game.getArena().addAvailableArena(this.game.getCopiedArena()); this.plugin.getArenaManager().removeArenaGameUUID(this.game.getCopiedArena());