From 26226f40f58313162b430fa3882e61b5996be57f Mon Sep 17 00:00:00 2001 From: Marcel <8513034+MrEAlderson@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:15:23 +0100 Subject: [PATCH] Chest locking: Fixed not working if team chests are configured wrong --- .../tweaksaddon/config/MainConfig.java | 2 +- .../tweaks/misc/LockTeamChest.java | 41 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java b/src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java index 033ddf4..d7d5356 100644 --- a/src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java +++ b/src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java @@ -333,7 +333,7 @@ public class MainConfig { } ) public static boolean lock_team_chest_enabled = true; - public static Set lock_team_chest_materials = new HashSet<>(Arrays.asList(Material.CHEST)); + @Config public static Set lock_team_chest_materials = new HashSet<>(Arrays.asList(Material.CHEST)); @Config public static double lock_team_chest_range = 8; @Config public static String lock_team_chest_fail_open = "&cYou cannot open this chest until {team} &chas been eliminated."; diff --git a/src/main/java/me/metallicgoat/tweaksaddon/tweaks/misc/LockTeamChest.java b/src/main/java/me/metallicgoat/tweaksaddon/tweaks/misc/LockTeamChest.java index 05d58af..c0f3cdf 100644 --- a/src/main/java/me/metallicgoat/tweaksaddon/tweaks/misc/LockTeamChest.java +++ b/src/main/java/me/metallicgoat/tweaksaddon/tweaks/misc/LockTeamChest.java @@ -1,6 +1,8 @@ package me.metallicgoat.tweaksaddon.tweaks.misc; +import de.marcely.bedwars.api.GameAPI; import de.marcely.bedwars.api.arena.Arena; +import de.marcely.bedwars.api.arena.ArenaStatus; import de.marcely.bedwars.api.arena.Team; import de.marcely.bedwars.api.event.player.PlayerOpenArenaChestEvent; import de.marcely.bedwars.api.message.Message; @@ -11,23 +13,46 @@ 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.player.PlayerInteractEvent; +import org.jetbrains.annotations.Nullable; public class LockTeamChest implements Listener { - // Many people will want team chests disabled, use this with only regular chests @EventHandler public void playerOpenArenaChest(PlayerOpenArenaChestEvent event) { if (!MainConfig.lock_team_chest_enabled) return; - final Block block = event.getChestBlock(); + if (check(event.getPlayer(), event.getArena(), event.getTeam(), event.getChestBlock())) + event.setCancelled(true); + } - if (!MainConfig.lock_team_chest_materials.contains(block.getType())) + @EventHandler + public void playerInteract(PlayerInteractEvent event) { + // in case the given material was configured to not be a personal chest, + // but shall stil be locked + if (!MainConfig.lock_team_chest_enabled) + return; + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; - final Arena arena = event.getArena(); final Player player = event.getPlayer(); - final Team playerTeam = event.getTeam(); + final Arena arena = GameAPI.get().getArenaByPlayer(player); + + if (arena == null || arena.getStatus() != ArenaStatus.RUNNING) + return; + + final Team playerTeam = arena.getPlayerTeam(player); + + if (check(player, arena, playerTeam, event.getClickedBlock())) + event.setCancelled(true); + } + + private boolean check(Player player, Arena arena, Team playerTeam, Block block) { + if (!MainConfig.lock_team_chest_materials.contains(block.getType())) + return false; + final Team chestTeam = getChestTeam(arena, block); if (chestTeam != null && !arena.getPlayersInTeam(chestTeam).isEmpty() && chestTeam != playerTeam) { @@ -35,11 +60,13 @@ public void playerOpenArenaChest(PlayerOpenArenaChestEvent event) { .placeholder("team-name", chestTeam.getDisplayName()) .placeholder("team", chestTeam.getDisplayName()) .send(player); - - event.setCancelled(true); + return true; } + + return false; } + @Nullable private Team getChestTeam(Arena arena, Block chest) { if (arena.getGameWorld() == chest.getWorld()) { for (Team team : arena.getEnabledTeams()) {