21
21
import org .bukkit .event .player .PlayerInteractEvent ;
22
22
import org .bukkit .inventory .Inventory ;
23
23
24
- import java .util .Collections ;
25
- import java .util .HashMap ;
26
- import java .util .Map ;
24
+ import java .util .*;
27
25
28
26
public class PersonalChests implements Listener {
29
27
30
- private static final HashMap <Inventory , Arena > inventoryArenaHashMap = new HashMap <>();
28
+ private static final HashMap <Arena , List < Inventory > > inventoryArenaHashMap = new HashMap <>();
31
29
private static final HashMap <Player , Block > openChests = new HashMap <>();
32
30
33
31
@ EventHandler
34
- public void onRoundStart (RoundStartEvent event ){
35
- if (!ConfigValue .personal_ender_chests_enabled )
32
+ public void onRoundStart (RoundStartEvent event ) {
33
+ if (!ConfigValue .personal_ender_chests_enabled )
36
34
return ;
37
35
38
36
final Arena arena = event .getArena ();
39
- for (Player player : arena .getPlayers ()){
37
+ final List <Inventory > inventories = new ArrayList <>();
38
+
39
+ for (Player player : arena .getPlayers ()) {
40
40
final Team team = arena .getPlayerTeam (player );
41
41
final String teamName = team != null ? ChatColor .stripColor (team .getDisplayName ()) : "" ;
42
- final String teamColor = team != null ? "& " + team .getChatColor (). getChar () : "" ;
42
+ final String teamColor = team != null ? "" + team .getBungeeChatColor () : "" ;
43
43
final String chestName = Message .build (ConfigValue .personal_ender_chests_name )
44
44
.placeholder ("team-name" , teamName )
45
45
.placeholder ("team-color" , teamColor )
46
46
.done ();
47
47
48
- final Inventory inventory = Bukkit .createInventory (player , 27 , chestName );
49
- inventoryArenaHashMap .put (inventory , arena );
48
+ inventories .add (Bukkit .createInventory (player , 27 , chestName ));
50
49
}
50
+
51
+ inventoryArenaHashMap .put (arena , inventories );
51
52
}
52
53
53
54
@ EventHandler
54
- public void onRoundEnd (RoundEndEvent e ) {
55
- inventoryArenaHashMap .values (). removeAll ( Collections . singleton ( e . getArena () ));
55
+ public void onRoundEnd (RoundEndEvent event ) {
56
+ inventoryArenaHashMap .remove ( event . getArena ());
56
57
}
57
58
58
- // TODO Do we need to cancel this event?
59
-
60
59
@ EventHandler (priority = EventPriority .HIGH )
61
- public void onChestOpen (PlayerInteractEvent event ){
62
-
63
- if (!ConfigValue .personal_ender_chests_enabled )
60
+ public void onChestOpen (PlayerInteractEvent event ) {
61
+ if (!ConfigValue .personal_ender_chests_enabled )
64
62
return ;
65
63
66
64
final Player player = event .getPlayer ();
@@ -69,29 +67,31 @@ public void onChestOpen(PlayerInteractEvent event){
69
67
70
68
// Check if player is opening chest in an arena
71
69
if (arena == null || block == null ||
70
+ block .getType () != Material .ENDER_CHEST ||
72
71
arena .getStatus () != ArenaStatus .RUNNING ||
73
72
event .getAction () != Action .RIGHT_CLICK_BLOCK )
74
73
return ;
75
74
76
- if (block .getType () == Material .ENDER_CHEST ) {
77
- for (Map .Entry <Inventory , Arena > entry : inventoryArenaHashMap .entrySet ()){
75
+ final List <Inventory > inventories = inventoryArenaHashMap .get (arena );
78
76
79
- final Inventory inventory = entry .getKey ();
77
+ if (inventories == null )
78
+ return ;
80
79
81
- if ( inventory . getHolder () == player ) {
82
- BedwarsAPI . getNMSHelper (). simulateChestOpening ( block );
83
- player . openInventory ( inventory );
84
- openChests . put ( player , block );
85
- break ;
86
- }
80
+ for ( Inventory inventory : inventories ) {
81
+ if ( inventory . getHolder () == player ) {
82
+ BedwarsAPI . getNMSHelper (). simulateChestOpening ( block );
83
+ player . openInventory ( inventory );
84
+ openChests . put ( player , block ) ;
85
+ break ;
87
86
}
88
87
}
89
88
}
90
89
91
90
@ EventHandler
92
- public void onInventoryClose (InventoryCloseEvent e ){
91
+ public void onInventoryClose (InventoryCloseEvent e ) {
93
92
final Player player = (Player ) e .getPlayer ();
94
- if (openChests .containsKey (player )){
93
+
94
+ if (openChests .containsKey (player )) {
95
95
BedwarsAPI .getNMSHelper ().simulateChestClosing (openChests .get (player ));
96
96
openChests .remove (player );
97
97
}
0 commit comments