added dropped items in game and fixed some generator issues

This commit is contained in:
Luca 2021-11-23 13:36:48 +01:00
parent 031874bc41
commit f58f2260a5
6 changed files with 101 additions and 24 deletions

View File

@ -9,6 +9,7 @@ import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.github.paperspigot.Title; import org.github.paperspigot.Title;
import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.arena.Arena; 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.CC;
import rip.tilly.bedwars.utils.TimeUtils; import rip.tilly.bedwars.utils.TimeUtils;
import java.awt.*;
import java.util.*; import java.util.*;
import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
@Getter @Getter
@ -30,6 +33,7 @@ public class Game {
private final Set<Location> placedBlocksLocations = new ConcurrentSet<>(); private final Set<Location> placedBlocksLocations = new ConcurrentSet<>();
private final Set<UUID> spectators = new ConcurrentSet<>(); private final Set<UUID> spectators = new ConcurrentSet<>();
private final Set<Integer> runnables = new HashSet<>(); private final Set<Integer> runnables = new HashSet<>();
private final Set<ItemStack> droppedItems = new ConcurrentSet<>();
private final List<GameTeam> teams; private final List<GameTeam> teams;
private final Arena arena; private final Arena arena;

View File

@ -7,6 +7,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.EulerAngle; import org.bukkit.util.EulerAngle;
import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.Game;
import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.CC;
public class Generator { public class Generator {
@ -24,12 +25,15 @@ public class Generator {
private int secondsSinceActivation; private int secondsSinceActivation;
private boolean activated; private boolean activated;
public Generator(Location location, GeneratorType generatorType, boolean isIslandGenerator) { private final Game game;
public Generator(Location location, GeneratorType generatorType, boolean isIslandGenerator, Game game) {
this.location = location; this.location = location;
this.generatorType = generatorType; this.generatorType = generatorType;
this.isIslandGenerator = isIslandGenerator; this.isIslandGenerator = isIslandGenerator;
this.secondsSinceActivation = this.getActivationTime(); this.secondsSinceActivation = this.getActivationTime();
this.activated = false; this.activated = false;
this.game = game;
} }
public void spawn() { public void spawn() {
@ -87,7 +91,9 @@ public class Generator {
throw new IllegalStateException("Unexcepted value: " + this.generatorType); 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) { public void setActivated(boolean activated) {

View File

@ -7,11 +7,9 @@ import org.bukkit.event.block.*;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent; import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.event.weather.WeatherChangeEvent; import org.bukkit.event.weather.WeatherChangeEvent;
import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.playerdata.PlayerData;
import rip.tilly.bedwars.playerdata.PlayerState; import rip.tilly.bedwars.playerdata.PlayerState;
public class RandomListeners implements Listener { public class RandomListeners implements Listener {
@ -23,15 +21,6 @@ public class RandomListeners implements Listener {
event.setCancelled(true); 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 @EventHandler
public void onWeatherChange(WeatherChangeEvent event) { public void onWeatherChange(WeatherChangeEvent event) {
if (event.toWeatherState()) { if (event.toWeatherState()) {

View File

@ -42,21 +42,21 @@ public class GameStartListener implements Listener {
CopiedArena currentArena = game.getCopiedArena(); CopiedArena currentArena = game.getCopiedArena();
for (CustomLocation location : currentArena.getTeamGenerators()) { 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); teamGen.setActivated(true);
game.getActivatedGenerators().add(teamGen); game.getActivatedGenerators().add(teamGen);
} }
for (CustomLocation location : currentArena.getDiamondGenerators()) { 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); diaGen.setActivated(true);
game.getActivatedGenerators().add(diaGen); game.getActivatedGenerators().add(diaGen);
} }
for (CustomLocation location : currentArena.getEmeraldGenerators()) { 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); emeGen.setActivated(true);
game.getActivatedGenerators().add(emeGen); game.getActivatedGenerators().add(emeGen);
@ -83,7 +83,7 @@ public class GameStartListener implements Listener {
player.teleport(team.getId() == 1 ? locationA.toBukkitLocation() : locationB.toBukkitLocation()); player.teleport(team.getId() == 1 ? locationA.toBukkitLocation() : locationB.toBukkitLocation());
player.getInventory().setArmorContents(this.plugin.getGameManager().getGameArmor(playerData)); 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); player.getInventory().addItem(stack);
} }
})); }));

View File

@ -2,11 +2,16 @@ package rip.tilly.bedwars.listeners.game;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.Game; import rip.tilly.bedwars.game.Game;
@ -25,7 +30,7 @@ public class WorldListener implements Listener {
return playerData.getPlayerState() == PlayerState.PLAYING; return playerData.getPlayerState() == PlayerState.PLAYING;
} }
@EventHandler @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) { if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) {
@ -67,18 +72,17 @@ public class WorldListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
event.setCancelled(true);
block.setType(Material.AIR);
GameTeam opposingTeam = playerData.getTeamId() == 0 ? game.getTeams().get(1) : game.getTeams().get(0); GameTeam opposingTeam = playerData.getTeamId() == 0 ? game.getTeams().get(1) : game.getTeams().get(0);
opposingTeam.destroyBed(); opposingTeam.destroyBed();
game.broadcastWithSound(playerTeam.getPlayerTeam().getChatColor() + player.getName() + " &ehas destroyed " + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getName() + "'s &ebed!", Sound.ENDERDRAGON_GROWL); 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); game.broadcastSound(Sound.ENDERDRAGON_GROWL);
playerData.setGameBedsDestroyed(playerData.getGameBedsDestroyed() + 1); playerData.getCurrentGameData().setGameBedsDestroyed(playerData.getCurrentGameData().getGameBedsDestroyed() + 1);
playerData.setBedsDestroyed(playerData.getBedsDestroyed() + 1); playerData.setBedsDestroyed(playerData.getBedsDestroyed() + 1);
block.getDrops().clear();
block.getDrops().removeIf(Objects::nonNull);
Location location = block.getLocation(); Location location = block.getLocation();
World world = location.getWorld(); World world = location.getWorld();
world.playEffect(location, Effect.CRIT, 1, 400); world.playEffect(location, Effect.CRIT, 1, 400);
@ -92,12 +96,24 @@ public class WorldListener implements Listener {
return; return;
} }
player.getInventory().addItem(block.getDrops().toArray(new ItemStack[0]));
game.removePlacedBlock(block); 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); block.setType(Material.AIR);
} }
@EventHandler @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onBlockPlace(BlockPlaceEvent event) { public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) { if (player.getGameMode() == GameMode.CREATIVE && player.isOp()) {
@ -145,4 +161,64 @@ public class WorldListener implements Listener {
game.addPlacedBlock(block); 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;
}
}
} }

View File

@ -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.getTeams().forEach(team -> team.playingPlayers().forEach(player -> this.plugin.getPlayerDataManager().resetPlayer(player, true)));
this.game.spectatorPlayers().forEach(this.plugin.getGameManager()::removeSpectator); this.game.spectatorPlayers().forEach(this.plugin.getGameManager()::removeSpectator);
this.game.getDroppedItems().clear();
this.plugin.getChunkClearingManager().resetArena(this.game.getCopiedArena()); this.plugin.getChunkClearingManager().resetArena(this.game.getCopiedArena());
this.game.getArena().addAvailableArena(this.game.getCopiedArena()); this.game.getArena().addAvailableArena(this.game.getCopiedArena());
this.plugin.getArenaManager().removeArenaGameUUID(this.game.getCopiedArena()); this.plugin.getArenaManager().removeArenaGameUUID(this.game.getCopiedArena());