Skip to content

Commit

Permalink
Improve Personal EnderChests
Browse files Browse the repository at this point in the history
  • Loading branch information
MetallicGoat committed Mar 26, 2023
1 parent 3075689 commit 84aca9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@
<dependency>
<groupId>de.marcely.bedwars</groupId>
<artifactId>API</artifactId>
<version>5.0.15</version>
<version>5.2</version>
<scope>system</scope>
<systemPath>${basedir}/libs/private/MBedwars.jar</systemPath>
</dependency>
<dependency>
<groupId>de.marcely.bedwars</groupId>
<artifactId>API</artifactId>
<version>5.1</version>
<version>5.2</version>
</dependency>
-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,44 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

public class PersonalChests implements Listener {

private static final HashMap<Inventory, Arena> inventoryArenaHashMap = new HashMap<>();
private static final HashMap<Arena, List<Inventory>> inventoryArenaHashMap = new HashMap<>();
private static final HashMap<Player, Block> openChests = new HashMap<>();

@EventHandler
public void onRoundStart(RoundStartEvent event){
if(!ConfigValue.personal_ender_chests_enabled)
public void onRoundStart(RoundStartEvent event) {
if (!ConfigValue.personal_ender_chests_enabled)
return;

final Arena arena = event.getArena();
for(Player player : arena.getPlayers()){
final List<Inventory> inventories = new ArrayList<>();

for (Player player : arena.getPlayers()) {
final Team team = arena.getPlayerTeam(player);
final String teamName = team != null ? ChatColor.stripColor(team.getDisplayName()) : "";
final String teamColor = team != null ? "&" + team.getChatColor().getChar() : "";
final String teamColor = team != null ? "" + team.getBungeeChatColor() : "";
final String chestName = Message.build(ConfigValue.personal_ender_chests_name)
.placeholder("team-name", teamName)
.placeholder("team-color", teamColor)
.done();

final Inventory inventory = Bukkit.createInventory(player, 27, chestName);
inventoryArenaHashMap.put(inventory, arena);
inventories.add(Bukkit.createInventory(player, 27, chestName));
}

inventoryArenaHashMap.put(arena, inventories);
}

@EventHandler
public void onRoundEnd(RoundEndEvent e){
inventoryArenaHashMap.values().removeAll(Collections.singleton(e.getArena()));
public void onRoundEnd(RoundEndEvent event) {
inventoryArenaHashMap.remove(event.getArena());
}

// TODO Do we need to cancel this event?

@EventHandler(priority = EventPriority.HIGH)
public void onChestOpen(PlayerInteractEvent event){

if(!ConfigValue.personal_ender_chests_enabled)
public void onChestOpen(PlayerInteractEvent event) {
if (!ConfigValue.personal_ender_chests_enabled)
return;

final Player player = event.getPlayer();
Expand All @@ -69,29 +67,31 @@ public void onChestOpen(PlayerInteractEvent event){

// Check if player is opening chest in an arena
if (arena == null || block == null ||
block.getType() != Material.ENDER_CHEST ||
arena.getStatus() != ArenaStatus.RUNNING ||
event.getAction() != Action.RIGHT_CLICK_BLOCK)
return;

if (block.getType() == Material.ENDER_CHEST) {
for(Map.Entry<Inventory, Arena> entry : inventoryArenaHashMap.entrySet()){
final List<Inventory> inventories = inventoryArenaHashMap.get(arena);

final Inventory inventory = entry.getKey();
if (inventories == null)
return;

if(inventory.getHolder() == player){
BedwarsAPI.getNMSHelper().simulateChestOpening(block);
player.openInventory(inventory);
openChests.put(player, block);
break;
}
for (Inventory inventory : inventories) {
if (inventory.getHolder() == player) {
BedwarsAPI.getNMSHelper().simulateChestOpening(block);
player.openInventory(inventory);
openChests.put(player, block);
break;
}
}
}

@EventHandler
public void onInventoryClose(InventoryCloseEvent e){
public void onInventoryClose(InventoryCloseEvent e) {
final Player player = (Player) e.getPlayer();
if(openChests.containsKey(player)){

if (openChests.containsKey(player)) {
BedwarsAPI.getNMSHelper().simulateChestClosing(openChests.get(player));
openChests.remove(player);
}
Expand Down

0 comments on commit 84aca9b

Please sign in to comment.