Skip to content

Commit 34974a1

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents f6a296b + a782199 commit 34974a1

File tree

1 file changed

+44
-81
lines changed

1 file changed

+44
-81
lines changed
Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package dev.simplix.protocolize.velocity.adventure;
22

33
import java.util.Collection;
4-
import java.util.stream.Collectors;
4+
import java.util.HashSet;
5+
import java.util.Objects;
56

67
import com.velocitypowered.api.proxy.Player;
7-
import com.velocitypowered.api.proxy.ProxyServer;
8-
import com.velocitypowered.api.proxy.server.RegisteredServer;
98

109
import dev.simplix.protocolize.api.Protocolize;
1110
import dev.simplix.protocolize.api.SoundCategory;
@@ -22,63 +21,36 @@
2221
* @author 4drian3d
2322
*/
2423
public class ProtocolizeAudience {
25-
private AudienceType TYPE;
26-
private ProtocolizePlayer PLAYER;
27-
private Collection<ProtocolizePlayer> PLAYERS;
24+
private final Collection<ProtocolizePlayer> players = new HashSet<>();
2825
private static final ProtocolizePlayerProvider PLAYER_PROVIDER = Protocolize.playerProvider();
2926

30-
private enum AudienceType{
31-
COLLECTION, SINGLE, NOTSUPPORTED
32-
}
33-
3427
/**
3528
* Obtain a {@link ProtocolizeAudience} from an {@link Audience}
3629
* @param audience The Audience
3730
*/
3831
public ProtocolizeAudience(Audience audience){
39-
if(audience instanceof RegisteredServer){
40-
this.TYPE = AudienceType.COLLECTION;
41-
this.PLAYERS = ((RegisteredServer)audience).getPlayersConnected().stream()
42-
.map(player -> PLAYER_PROVIDER.player(player.getUniqueId()))
43-
.collect(Collectors.toList());
44-
} else if(audience instanceof ProxyServer){
45-
this.TYPE = AudienceType.COLLECTION;
46-
this.PLAYERS = ((ProxyServer)audience).getAllPlayers().stream()
47-
.map(player -> PLAYER_PROVIDER.player(player.getUniqueId()))
48-
.collect(Collectors.toList());
49-
} else if(audience instanceof ForwardingAudience){
50-
this.TYPE = AudienceType.COLLECTION;
51-
for(Audience singleAudience : ((ForwardingAudience)audience).audiences()){
52-
if(singleAudience instanceof Player){
53-
this.PLAYERS.add(getPlayer(singleAudience));
54-
} else if(singleAudience instanceof RegisteredServer){
55-
((RegisteredServer)singleAudience).getPlayersConnected().stream()
56-
.map(player -> PLAYER_PROVIDER.player(player.getUniqueId()))
57-
.forEach(this.PLAYERS::add);
58-
} else if(singleAudience instanceof ForwardingAudience) {
59-
for(Audience miniAudience : ((ForwardingAudience)singleAudience).audiences()){
60-
if(miniAudience instanceof Player){
61-
this.PLAYERS.add(getPlayer(singleAudience));
62-
} else if(miniAudience instanceof RegisteredServer){
63-
((RegisteredServer)miniAudience).getPlayersConnected().stream()
64-
.map(player -> PLAYER_PROVIDER.player(player.getUniqueId()))
65-
.forEach(this.PLAYERS::add);
66-
} else {
67-
/*
68-
It does not make sense to include the ProxyServer in a FowardingAudience
69-
*/
70-
}
71-
}
72-
}
32+
Objects.requireNonNull(audience, "the audience cannot be null");
33+
if(audience instanceof Player){
34+
players.add(getPlayer(audience));
35+
} else if(audience instanceof ForwardingAudience.Single){
36+
Audience singleAudience = ((ForwardingAudience.Single)audience).audience();
37+
if(singleAudience instanceof Player){
38+
players.add(getPlayer(singleAudience));
7339
}
74-
} else if(audience instanceof Player){
75-
this.TYPE = AudienceType.SINGLE;
76-
this.PLAYER = getPlayer(audience);
77-
} else {
78-
this.TYPE = AudienceType.NOTSUPPORTED;
40+
} else if(audience instanceof ForwardingAudience){
41+
checkAndAddPlayers((ForwardingAudience)audience);
7942
}
8043
}
8144

45+
/**
46+
* Obtain a {@link ProtocolizeAudience} from an {@link Audience}
47+
* @param audience The Audience
48+
* @return The {@link ProtocolizeAudience}
49+
*/
50+
public static ProtocolizeAudience audience(Audience audience){
51+
return new ProtocolizeAudience(audience);
52+
}
53+
8254
/**
8355
* Plays a {@link Sound} to the specified {@link ProtocolizeAudience}
8456
* @param sound The sound to be played
@@ -87,14 +59,8 @@ public ProtocolizeAudience(Audience audience){
8759
* @param pitch Sound pitch
8860
*/
8961
public void playSound(Sound sound, SoundCategory category, float volume, float pitch){
90-
switch(TYPE){
91-
case COLLECTION:
92-
this.PLAYERS.forEach(pPlayer -> pPlayer.playSound(sound, category, volume, pitch));
93-
break;
94-
case SINGLE:
95-
this.PLAYER.playSound(sound, category, volume, pitch);
96-
break;
97-
case NOTSUPPORTED: break;
62+
for(ProtocolizePlayer player : players){
63+
player.playSound(sound, category, volume, pitch);
9864
}
9965
}
10066

@@ -103,29 +69,17 @@ public void playSound(Sound sound, SoundCategory category, float volume, float p
10369
* @param inventory The inventory
10470
*/
10571
public void openInventory(Inventory inventory){
106-
switch(TYPE){
107-
case COLLECTION:
108-
this.PLAYERS.forEach(pPlayer -> pPlayer.openInventory(inventory));
109-
break;
110-
case SINGLE:
111-
this.PLAYER.openInventory(inventory);
112-
break;
113-
case NOTSUPPORTED: break;
72+
for(ProtocolizePlayer player : players){
73+
player.openInventory(inventory);
11474
}
11575
}
11676

11777
/**
11878
* Closes the open inventories of the {@link ProtocolizeAudience}
11979
*/
12080
public void closeInventory(){
121-
switch(TYPE){
122-
case COLLECTION:
123-
this.PLAYERS.forEach(pPlayer -> pPlayer.closeInventory());
124-
break;
125-
case SINGLE:
126-
this.PLAYER.closeInventory();
127-
break;
128-
case NOTSUPPORTED: break;
81+
for(ProtocolizePlayer player : players){
82+
player.closeInventory();
12983
}
13084
}
13185

@@ -135,14 +89,8 @@ public void closeInventory(){
13589
* @param inventory The Inventory
13690
*/
13791
public void registerInventory(int windowId, Inventory inventory){
138-
switch(TYPE){
139-
case COLLECTION:
140-
this.PLAYERS.forEach(pPlayer -> pPlayer.registerInventory(windowId, inventory));
141-
break;
142-
case SINGLE:
143-
this.PLAYER.registerInventory(windowId, inventory);
144-
break;
145-
case NOTSUPPORTED: break;
92+
for(ProtocolizePlayer player : players){
93+
player.registerInventory(windowId, inventory);
14694
}
14795
}
14896

@@ -155,4 +103,19 @@ private static ProtocolizePlayer getPlayer(Audience audience){
155103
Player player = (Player) audience;
156104
return PLAYER_PROVIDER.player(player.getUniqueId());
157105
}
106+
107+
private void checkAndAddPlayers(ForwardingAudience fAudience){
108+
for(Audience audience : fAudience.audiences()){
109+
if(audience instanceof Player){
110+
players.add(getPlayer(audience));
111+
} else if(audience instanceof ForwardingAudience.Single){
112+
Audience singleAudience = ((ForwardingAudience.Single)audience).audience();
113+
if(singleAudience instanceof Player){
114+
players.add(getPlayer(singleAudience));
115+
}
116+
} else if(audience instanceof ForwardingAudience){
117+
checkAndAddPlayers((ForwardingAudience)audience);
118+
}
119+
}
120+
}
158121
}

0 commit comments

Comments
 (0)