From 81f79f5ccd9da6774ccd02c88aa415888b09bc56 Mon Sep 17 00:00:00 2001 From: FirephoenixX02 Date: Fri, 7 Jul 2023 14:19:54 +0200 Subject: [PATCH] Fix Bugs and add Chest Filling System --- .../ps3minigames/commands/ForceStop.java | 5 + .../ps3minigames/listener/WorldListener.java | 51 ++-- .../ps3minigames/util/GameUtil.java | 231 +++++++++++++----- src/main/resources/config.yml | 38 ++- 4 files changed, 239 insertions(+), 86 deletions(-) diff --git a/src/main/java/me/firephoenix/ps3minigames/commands/ForceStop.java b/src/main/java/me/firephoenix/ps3minigames/commands/ForceStop.java index e1f81c3..c21e7f1 100644 --- a/src/main/java/me/firephoenix/ps3minigames/commands/ForceStop.java +++ b/src/main/java/me/firephoenix/ps3minigames/commands/ForceStop.java @@ -1,6 +1,7 @@ package me.firephoenix.ps3minigames.commands; import me.firephoenix.ps3minigames.PS3Minigames; +import me.firephoenix.ps3minigames.states.GameState; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -32,6 +33,10 @@ public class ForceStop implements CommandExecutor { sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-find-game"))); return true; } else { + if (Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID)).getGameState() != GameState.RUNNING) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-stop-game"))); + } + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.force-stopping-game").replace("%map%", Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID)).getMap().getName()))); PS3Minigames.INSTANCE.getGameUtil().stopGame(Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID))); return true; } diff --git a/src/main/java/me/firephoenix/ps3minigames/listener/WorldListener.java b/src/main/java/me/firephoenix/ps3minigames/listener/WorldListener.java index b082532..7660abe 100644 --- a/src/main/java/me/firephoenix/ps3minigames/listener/WorldListener.java +++ b/src/main/java/me/firephoenix/ps3minigames/listener/WorldListener.java @@ -8,9 +8,7 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.block.*; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.FoodLevelChangeEvent; @@ -54,17 +52,23 @@ public class WorldListener implements Listener { @EventHandler public void onDestroyCrops(PlayerInteractEvent e) { - if (e.getClickedBlock() == null) { - e.setCancelled(true); - return; - } - if (e.getClickedBlock().getType() == Material.AIR) { - e.setCancelled(true); - return; + if (e.getClickedBlock() != null) { + if (e.getClickedBlock().getType() == Material.AIR) { + e.setCancelled(true); + return; + } } if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) { - if (e.getPlayer().getWorld() != PS3Minigames.INSTANCE.getLobby() && e.getClickedBlock().getType() == Material.CHEST) return; - e.setCancelled(true); + if (e.getClickedBlock() == null) return; + Block clickedBlock = e.getClickedBlock(); + + //Left or Right click? + if (e.getAction() == Action.LEFT_CLICK_BLOCK) { + //Crops? + if (clickedBlock.getType() == Material.CROPS) { + e.setCancelled(true); + } + } } } @@ -89,15 +93,14 @@ public class WorldListener implements Listener { @EventHandler public void onPlayerDoorOpen(PlayerInteractEvent e) { - if (e.getClickedBlock() == null) { - e.setCancelled(true); - return; - } - if (e.getClickedBlock().getType() == Material.AIR) { - e.setCancelled(true); - return; + if (e.getClickedBlock() != null) { + if (e.getClickedBlock().getType() == Material.AIR) { + e.setCancelled(true); + return; + } } if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) { + if (e.getClickedBlock() == null) return; Block clickedBlock = e.getClickedBlock(); //Left or Right click? @@ -155,4 +158,14 @@ public class WorldListener implements Listener { } } + @EventHandler + public void onBlockSpread(BlockSpreadEvent e) { + e.setCancelled(true); + } + + @EventHandler + public void onBlockGrowth(BlockGrowEvent e) { + e.setCancelled(true); + } + } diff --git a/src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java b/src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java index defec5c..f9a58ca 100644 --- a/src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java +++ b/src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java @@ -3,14 +3,14 @@ package me.firephoenix.ps3minigames.util; import me.firephoenix.ps3minigames.PS3Minigames; import me.firephoenix.ps3minigames.game.Game; import me.firephoenix.ps3minigames.states.GameState; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.World; +import org.bukkit.*; +import org.bukkit.block.Chest; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.UUID; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.*; /** * @author NieGestorben @@ -19,68 +19,167 @@ import java.util.UUID; public class GameUtil { - public void startNewGame(ArrayList players, World map) { - int ID = PS3Minigames.INSTANCE.getGames().size() + 1; - String newName = map.getName() + "game" + ID; - boolean copydone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().cloneWorld(map.getName(), newName); - if (copydone) { - boolean loadingdone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().loadWorld(newName); - if (loadingdone) { - PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().getMVWorld(newName).setAlias(newName); - World gameWorld = Bukkit.getWorld(newName); - Game newGame = new Game(ID, players, gameWorld, GameState.STARTING); - PS3Minigames.INSTANCE.getGames().add(newGame); - PS3Minigames.INSTANCE.getWorldToGameHashMap().put(gameWorld, newGame); - int spawnnumber = 1; - for (UUID uuid : players) { - if (PS3Minigames.INSTANCE.getServer().getPlayer(uuid) == null) return; - PS3Minigames.INSTANCE.getServer().getPlayer(uuid).sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.teleporting"))); - String configpathtospawnloc = "maps." + map.getName() + ".spawn" + spawnnumber++; - Location location = new Location(gameWorld, PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".x"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".y"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".pitch")); - Bukkit.getServer().getPlayer(uuid).teleport(location); - PS3Minigames.INSTANCE.getFrozenPlayer().add(uuid); + public static int getRandomInRange(int min, int max) { + Random random = new Random(); + return random.nextInt(max - min) + min; } - Timer timer = new Timer(10, PS3Minigames.INSTANCE); - timer.start(); - timer.eachSecond(() -> { - for (UUID uuid : newGame.getPlayers()) { - if (Bukkit.getServer().getPlayer(uuid) == null) return; - Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&',"&6" + timer.getCounter()), ""); - } - }); - timer.whenComplete(() -> { - newGame.setGameState(GameState.RUNNING); - gameWorld.getPlayers().forEach(player -> { - PS3Minigames.INSTANCE.getFrozenPlayer().remove(player.getUniqueId()); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.game-start-no-countdown"))); - }); - }); - } else { - System.out.println("error while trying to load the world!"); - } - } else { - System.out.println("error while trying to copy the world!"); - } - } - public void stopGame(Game game) { - if (game.getGameState() != GameState.STOPPING) game.setGameState(GameState.STOPPING); - game.getPlayers().forEach(uuid -> Bukkit.getServer().getPlayer(uuid).teleport(new Location(PS3Minigames.INSTANCE.getServer().getWorld(PS3Minigames.INSTANCE.getConfig().getString("spawn-lobby.world")), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.x"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.y"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.pitch")))); - if (game.getMap().getPlayers().size() == 0) { - PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName()); - } - PS3Minigames.INSTANCE.getGames().remove(game); - } + public static float getRandomInRange(float min, float max) { + return (float) (min + Math.random() * (max - min)); + } - @Nullable - public Game getGameByWorld(World world) { - return PS3Minigames.INSTANCE.getWorldToGameHashMap().get(world); - } + public void startNewGame(ArrayList players, World map) { + int ID = PS3Minigames.INSTANCE.getGames().size() + 1; + String newName = map.getName() + "game" + ID; + boolean copydone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().cloneWorld(map.getName(), newName); + if (copydone) { + boolean loadingdone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().loadWorld(newName); + if (loadingdone) { + PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().getMVWorld(newName).setAlias(newName); + World gameWorld = Bukkit.getWorld(newName); + Game newGame = new Game(ID, players, gameWorld, GameState.STARTING); + PS3Minigames.INSTANCE.getGames().add(newGame); + PS3Minigames.INSTANCE.getWorldToGameHashMap().put(gameWorld, newGame); + int spawnnumber = 1; + for (UUID uuid : players) { + if (PS3Minigames.INSTANCE.getServer().getPlayer(uuid) == null) return; + PS3Minigames.INSTANCE.getServer().getPlayer(uuid).sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.teleporting"))); + String configpathtospawnloc = "maps." + map.getName() + ".spawn" + spawnnumber++; + Location location = new Location(gameWorld, PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".x"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".y"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".pitch")); + Bukkit.getServer().getPlayer(uuid).teleport(location); + PS3Minigames.INSTANCE.getFrozenPlayer().add(uuid); + } + Timer timer = new Timer(10, PS3Minigames.INSTANCE); + timer.start(); + fillChests(newGame); + timer.eachSecond(() -> { + for (UUID uuid : newGame.getPlayers()) { + if (Bukkit.getServer().getPlayer(uuid) == null) return; + Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&', "&6" + timer.getCounter()), ""); + } + }); + timer.whenComplete(() -> { + newGame.setGameState(GameState.RUNNING); + gameWorld.getPlayers().forEach(player -> { + PS3Minigames.INSTANCE.getFrozenPlayer().remove(player.getUniqueId()); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.game-start-no-countdown"))); + }); + }); + } else { + System.out.println("error while trying to load the world!"); + } + } else { + System.out.println("error while trying to copy the world!"); + } + } + + public void stopGame(Game game) { + if (game.getGameState() == GameState.STOPPING) { + System.out.println("Someone tried to stop a game which is currently stopping, not possible, ignoring."); + return; + } + game.setGameState(GameState.STOPPING); + // Teleport all players + Clear inv + Reset Effects + Unfreeze + game.getPlayers().forEach(uuid -> { + Bukkit.getServer().getPlayer(uuid).teleport(new Location(PS3Minigames.INSTANCE.getServer().getWorld(PS3Minigames.INSTANCE.getConfig().getString("spawn-lobby.world")), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.x"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.y"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.pitch"))); + Bukkit.getServer().getPlayer(uuid).getInventory().clear(); + Bukkit.getServer().getPlayer(uuid).getActivePotionEffects().clear(); + Bukkit.getServer().getPlayer(uuid).setHealth(20); + Bukkit.getServer().getPlayer(uuid).setFoodLevel(20); + PS3Minigames.INSTANCE.getFrozenPlayer().remove(uuid); + }); + if (game.getMap().getPlayers().size() == 0) { + PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName()); + } + PS3Minigames.INSTANCE.getGames().remove(game); + } + + @Nullable + public Game getGameByWorld(World world) { + return PS3Minigames.INSTANCE.getWorldToGameHashMap().get(world); + } + + @Nullable + public Game getGameByID(int id) { + //we use id - 1 because the id is the absolute size of the list which starts at 1 and the arraylist position starts at 0 + return PS3Minigames.INSTANCE.getGames().stream().anyMatch(game -> game.getGameid() == id) ? PS3Minigames.INSTANCE.getGames().get(id - 1) : null; + } + + public void fillChests(Game game) { + for (Chunk chunk : game.getMap().getLoadedChunks()) { + Arrays.stream(chunk.getTileEntities()).filter(tileentity -> tileentity instanceof Chest).forEach((chest) -> { + Chest chestblock = (Chest) chest; + fillChest(chestblock); + }); + } + } + + public void fillChest(Chest chest) { + int slots = getRandomInRange(4, chest.getBlockInventory().getSize() / 3); + for (int i = 0; i < slots; i++) { + int randomSlot = getRandomInRange(0, slots); + Random random = new Random(); + int chance = random.nextInt(10); + boolean potion = chance >= 1 && chance < 3; + if (potion) { + ItemStack[] possiblePotions = getPossiblePotionsFromConfig("chest-loot-potions").toArray(new ItemStack[0]); + ItemStack randomPotion = possiblePotions[random.nextInt((int) Arrays.stream(possiblePotions).count())]; + chest.getBlockInventory().setItem(randomSlot, randomPotion); + } else { + Material[] possibleItems = getPossibleMaterialsFromConfig("chest-loot").keySet().toArray(new Material[0]); + Material randomItem = possibleItems[random.nextInt((int) Arrays.stream(possibleItems).count())]; + int itemAmount = getPossibleMaterialsFromConfig("chest-loot").get(randomItem); + ItemStack item = new ItemBuilder(randomItem, itemAmount).toItemStack(); + chest.getBlockInventory().setItem(randomSlot, item); + } + } + } + + private Map getPossibleMaterialsFromConfig(String path) { + Collection entries = PS3Minigames.INSTANCE.getConfig().getStringList(path); + if (entries == null) throw new IllegalArgumentException("Cannot find path: " + path + "in config.yml!"); + + Map materialToMaxStackSize = new HashMap<>(); + + for (String entry : entries) { + String[] parts = entry.split("#", 2); + String materialName = parts[0]; + materialName = materialName.replaceAll("\\d+$", ""); + int maxStackSize = Integer.parseInt(parts[1]); + + Material material = Material.getMaterial(materialName); + if (material == null) + throw new IllegalArgumentException("Material with the name: " + materialName + " was not found!"); + + materialToMaxStackSize.put(material, maxStackSize); + } + + return materialToMaxStackSize; + } + + private ArrayList getPossiblePotionsFromConfig(String path) { + Collection entries = PS3Minigames.INSTANCE.getConfig().getIntegerList(path); + if (entries == null) throw new IllegalArgumentException("Cannot find path: " + path + "in config.yml!"); + + ArrayList potionToMaxStackSize = new ArrayList<>(); + + for (Integer entry : entries) { + int potionID = entry; + + ItemStack potion = new ItemStack(Material.POTION, 1, (short) potionID); + + potionToMaxStackSize.add(potion); + } + + return potionToMaxStackSize; + } + + public double roundTo(double value, int places) { + if (places < 0) throw new IllegalArgumentException(); + + BigDecimal bd = new BigDecimal(Double.toString(value)); + bd = bd.setScale(places, RoundingMode.HALF_UP); + return bd.doubleValue(); + } - @Nullable - public Game getGameByID(int id) { - //we use id - 1 because the id is the absolute size of the list which starts at 1 and the arraylist position starts at 0 - return PS3Minigames.INSTANCE.getGames().stream().anyMatch(game -> game.getGameid() == id) ? PS3Minigames.INSTANCE.getGames().get(id - 1) : null; - } - } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1248930..e30deb3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -28,6 +28,8 @@ messages: cant-find-game: "&6Game&7 couldn't be found!" available-maps: "&7Available &6Maps&7:" force-starting-game: "&7Starting game on map &6%map%&7..." + force-stopping-game: "&7Stopping game on map &6%map%&7..." + cant-stop-game: "&7Cant stop game because it's already &6stopping&7!" # Maps maps: @@ -80,4 +82,38 @@ maps: y: 63.0 z: 10.5 yaw: 135.0 - pitch: 0.0 \ No newline at end of file + pitch: 0.0 + +# Chest Loot +# Item list ---> https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html +# Note: Only <=1.8 Items ! +# BukkitMaterial#Amount +chest-loot: + - STONE_SWORD#1 + - APPLE#1 + - COOKED_MUTTON#1 + - WOOD_SWORD#1 + - CHAINMAIL_HELMET#1 + - CHAINMAIL_CHESTPLATE#1 + - LEATHER_LEGGINGS#1 + - WOOD_PICKAXE#1 + - BOW#1 + - ARROW#5 + - LEATHER_BOOTS#1 + - WOOD_AXE#1 + - GOLD_HELMET#1 + - LEATHER_CHESTPLATE#1 +# Chest Loot Potions +# Note: Only <=1.8 Items ! +# https://dev.bukkit.org/projects/etshop/pages/potion-data-value-table OR https://minecraftitemids.com/types/potion +# Usage: POTION_DATA_VALUE +chest-loot-potions: + - 8193 # Regen + - 8197 # Instant Heal + - 16396 # Harming Splash + - 16388 # Poison Splash + - 8194 # Speed I + - 16392 # Weakness Splash + - 8195 # Fire Res + +