Fix Bugs and add Chest Filling System

This commit is contained in:
FirephoenixX02 2023-07-07 14:19:54 +02:00
parent 397e61823a
commit 81f79f5ccd
4 changed files with 239 additions and 86 deletions

View file

@ -1,6 +1,7 @@
package me.firephoenix.ps3minigames.commands; package me.firephoenix.ps3minigames.commands;
import me.firephoenix.ps3minigames.PS3Minigames; import me.firephoenix.ps3minigames.PS3Minigames;
import me.firephoenix.ps3minigames.states.GameState;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -32,6 +33,10 @@ public class ForceStop implements CommandExecutor {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-find-game"))); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-find-game")));
return true; return true;
} else { } 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))); PS3Minigames.INSTANCE.getGameUtil().stopGame(Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID)));
return true; return true;
} }

View file

@ -8,9 +8,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.*;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.entity.FoodLevelChangeEvent;
@ -54,19 +52,25 @@ public class WorldListener implements Listener {
@EventHandler @EventHandler
public void onDestroyCrops(PlayerInteractEvent e) { public void onDestroyCrops(PlayerInteractEvent e) {
if (e.getClickedBlock() == null) { if (e.getClickedBlock() != null) {
e.setCancelled(true);
return;
}
if (e.getClickedBlock().getType() == Material.AIR) { if (e.getClickedBlock().getType() == Material.AIR) {
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
}
if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) { if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) {
if (e.getPlayer().getWorld() != PS3Minigames.INSTANCE.getLobby() && e.getClickedBlock().getType() == Material.CHEST) return; 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); e.setCancelled(true);
} }
} }
}
}
@EventHandler @EventHandler
@ -89,15 +93,14 @@ public class WorldListener implements Listener {
@EventHandler @EventHandler
public void onPlayerDoorOpen(PlayerInteractEvent e) { public void onPlayerDoorOpen(PlayerInteractEvent e) {
if (e.getClickedBlock() == null) { if (e.getClickedBlock() != null) {
e.setCancelled(true);
return;
}
if (e.getClickedBlock().getType() == Material.AIR) { if (e.getClickedBlock().getType() == Material.AIR) {
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
}
if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) { if (!PS3Minigames.INSTANCE.getBuildModePlayer().contains(e.getPlayer().getUniqueId())) {
if (e.getClickedBlock() == null) return;
Block clickedBlock = e.getClickedBlock(); Block clickedBlock = e.getClickedBlock();
//Left or Right click? //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);
}
} }

View file

@ -3,14 +3,14 @@ package me.firephoenix.ps3minigames.util;
import me.firephoenix.ps3minigames.PS3Minigames; import me.firephoenix.ps3minigames.PS3Minigames;
import me.firephoenix.ps3minigames.game.Game; import me.firephoenix.ps3minigames.game.Game;
import me.firephoenix.ps3minigames.states.GameState; import me.firephoenix.ps3minigames.states.GameState;
import org.bukkit.Bukkit; import org.bukkit.*;
import org.bukkit.ChatColor; import org.bukkit.block.Chest;
import org.bukkit.Location; import org.bukkit.inventory.ItemStack;
import org.bukkit.World;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.math.BigDecimal;
import java.util.UUID; import java.math.RoundingMode;
import java.util.*;
/** /**
* @author NieGestorben * @author NieGestorben
@ -19,6 +19,15 @@ import java.util.UUID;
public class GameUtil { public class GameUtil {
public static int getRandomInRange(int min, int max) {
Random random = new Random();
return random.nextInt(max - min) + min;
}
public static float getRandomInRange(float min, float max) {
return (float) (min + Math.random() * (max - min));
}
public void startNewGame(ArrayList<UUID> players, World map) { public void startNewGame(ArrayList<UUID> players, World map) {
int ID = PS3Minigames.INSTANCE.getGames().size() + 1; int ID = PS3Minigames.INSTANCE.getGames().size() + 1;
String newName = map.getName() + "game" + ID; String newName = map.getName() + "game" + ID;
@ -42,10 +51,11 @@ public class GameUtil {
} }
Timer timer = new Timer(10, PS3Minigames.INSTANCE); Timer timer = new Timer(10, PS3Minigames.INSTANCE);
timer.start(); timer.start();
fillChests(newGame);
timer.eachSecond(() -> { timer.eachSecond(() -> {
for (UUID uuid : newGame.getPlayers()) { for (UUID uuid : newGame.getPlayers()) {
if (Bukkit.getServer().getPlayer(uuid) == null) return; if (Bukkit.getServer().getPlayer(uuid) == null) return;
Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&',"&6" + timer.getCounter()), ""); Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&', "&6" + timer.getCounter()), "");
} }
}); });
timer.whenComplete(() -> { timer.whenComplete(() -> {
@ -64,8 +74,20 @@ public class GameUtil {
} }
public void stopGame(Game game) { public void stopGame(Game game) {
if (game.getGameState() != GameState.STOPPING) game.setGameState(GameState.STOPPING); if (game.getGameState() == 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")))); 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) { if (game.getMap().getPlayers().size() == 0) {
PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName()); PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName());
} }
@ -83,4 +105,81 @@ public class GameUtil {
return PS3Minigames.INSTANCE.getGames().stream().anyMatch(game -> game.getGameid() == id) ? PS3Minigames.INSTANCE.getGames().get(id - 1) : null; 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<Material, Integer> getPossibleMaterialsFromConfig(String path) {
Collection<String> entries = PS3Minigames.INSTANCE.getConfig().getStringList(path);
if (entries == null) throw new IllegalArgumentException("Cannot find path: " + path + "in config.yml!");
Map<Material, Integer> 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<ItemStack> getPossiblePotionsFromConfig(String path) {
Collection<Integer> entries = PS3Minigames.INSTANCE.getConfig().getIntegerList(path);
if (entries == null) throw new IllegalArgumentException("Cannot find path: " + path + "in config.yml!");
ArrayList<ItemStack> 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();
}
} }

View file

@ -28,6 +28,8 @@ messages:
cant-find-game: "&6Game&7 couldn't be found!" cant-find-game: "&6Game&7 couldn't be found!"
available-maps: "&7Available &6Maps&7:" available-maps: "&7Available &6Maps&7:"
force-starting-game: "&7Starting game on map &6%map%&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
maps: maps:
@ -81,3 +83,37 @@ maps:
z: 10.5 z: 10.5
yaw: 135.0 yaw: 135.0
pitch: 0.0 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