Skip to content

Commit

Permalink
Merge pull request #32 from MrEAlderson/master
Browse files Browse the repository at this point in the history
Improvements to chest locking
  • Loading branch information
MetallicGoat authored Nov 11, 2024
2 parents 30b3526 + 26226f4 commit b4b029a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.Getter;
import me.metallicgoat.tweaksaddon.config.ConfigManager.Config;
import me.metallicgoat.tweaksaddon.config.ConfigManager.SectionTitle;
Expand Down Expand Up @@ -324,11 +326,14 @@ public class MainConfig {

@Config(
description = {
"Prevents player from opening a bases' chest if bases team is still alive",
"Note: You may want to disable team chests in MBedwars, as it works like a shared team Ender Chest"
"Prevents player from opening an enemies' base chest if its team is still alive (has players)",
"Materials is a list of chest types to lock, you may use CHEST or ENDER_CHEST",
"Note: In case you want to lock ENDER_CHEST, make sure it's configured as a team chest within MBedwars' config.yml",
"Range is the radius around a team's spawn point in which the chest shall get locked"
}
)
public static boolean lock_team_chest_enabled = true;
@Config public static Set<Material> 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.";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,72 @@
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;
import de.marcely.bedwars.tools.location.XYZYP;
import me.metallicgoat.tweaksaddon.config.MainConfig;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
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;

// THIS IS NOT FOR MBEDWARS TEAM CHESTS
final boolean isStandardChest = !event.isTeamChest() && event.getChestBlock().getType() == Material.CHEST;
if (check(event.getPlayer(), event.getArena(), event.getTeam(), event.getChestBlock()))
event.setCancelled(true);
}

if (!MainConfig.lock_team_chest_enabled || !isStandardChest)
@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 Team chestTeam = getChestTeam(arena, event.getChestBlock());
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) {
Message.build(MainConfig.lock_team_chest_fail_open)
.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()) {
Expand Down

0 comments on commit b4b029a

Please sign in to comment.