Skip to content

Commit b12caff

Browse files
committed
Support player injection for nested events
1 parent e4e3918 commit b12caff

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/main/java/com/laytonsmith/core/Static.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import java.util.Locale;
9292
import java.util.Map;
9393
import java.util.UUID;
94+
import java.util.concurrent.atomic.AtomicInteger;
9495
import java.util.logging.Level;
9596
import java.util.logging.Logger;
9697
import java.util.regex.Matcher;
@@ -761,6 +762,7 @@ public static MCItemStack ParseItemNotation(String functionName, String notation
761762
}
762763

763764
private static final Map<String, MCCommandSender> INJECTED_PLAYERS = new HashMap<>();
765+
private static final Map<String, AtomicInteger> PLAYER_INJECTION_COUNT_MAP = new HashMap<>();
764766
private static MCEntity injectedEntity;
765767
private static final Pattern DASHLESS_PATTERN = Pattern.compile("^([A-Fa-f0-9]{8})([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})([A-Fa-f0-9]{4})([A-Fa-f0-9]{12})$");
766768

@@ -1221,26 +1223,50 @@ public static MCCommandSender GetInjectedPlayer(String name) {
12211223
return INJECTED_PLAYERS.get(name.toLowerCase());
12221224
}
12231225

1226+
/**
1227+
* Injects the given player into the global player proxy system,
1228+
* or increments the injection count of the already injected player with that name.
1229+
* @param player - The player to inject.
1230+
*/
12241231
public static void InjectPlayer(MCCommandSender player) {
12251232
String name = player.getName();
12261233
if("CONSOLE".equals(name)) {
12271234
name = "~console";
12281235
}
1229-
INJECTED_PLAYERS.put(name.toLowerCase(), player);
1236+
name = name.toLowerCase();
1237+
synchronized(INJECTED_PLAYERS) {
1238+
AtomicInteger count = PLAYER_INJECTION_COUNT_MAP.get(name);
1239+
if(count == null) {
1240+
INJECTED_PLAYERS.put(name, player);
1241+
PLAYER_INJECTION_COUNT_MAP.put(name, new AtomicInteger(1));
1242+
} else {
1243+
count.incrementAndGet();
1244+
}
1245+
}
12301246
}
12311247

12321248
/**
1233-
* Removes a player into the global player proxy system. Returns the player removed (or null if none were injected).
1234-
*
1235-
* @param player
1236-
* @return
1249+
* Decrements the injection count of the injected player and
1250+
* removes it from the global player proxy system if the count reaches 0.
1251+
* @param player - The player to uninject.
1252+
* @return The player, or {@code null} if the player was not injected.
12371253
*/
12381254
public static MCCommandSender UninjectPlayer(MCCommandSender player) {
12391255
String name = player.getName();
12401256
if("CONSOLE".equals(name)) {
12411257
name = "~console";
12421258
}
1243-
return INJECTED_PLAYERS.remove(name.toLowerCase());
1259+
name = name.toLowerCase();
1260+
synchronized(INJECTED_PLAYERS) {
1261+
AtomicInteger count = PLAYER_INJECTION_COUNT_MAP.get(name);
1262+
if(count == null) {
1263+
return null;
1264+
} else if(count.decrementAndGet() <= 0) {
1265+
PLAYER_INJECTION_COUNT_MAP.remove(name);
1266+
return INJECTED_PLAYERS.remove(name);
1267+
}
1268+
return INJECTED_PLAYERS.get(name);
1269+
}
12441270
}
12451271

12461272
public static void InjectEntity(MCEntity entity) {

0 commit comments

Comments
 (0)