|
91 | 91 | import java.util.Locale; |
92 | 92 | import java.util.Map; |
93 | 93 | import java.util.UUID; |
| 94 | +import java.util.concurrent.atomic.AtomicInteger; |
94 | 95 | import java.util.logging.Level; |
95 | 96 | import java.util.logging.Logger; |
96 | 97 | import java.util.regex.Matcher; |
@@ -761,6 +762,7 @@ public static MCItemStack ParseItemNotation(String functionName, String notation |
761 | 762 | } |
762 | 763 |
|
763 | 764 | private static final Map<String, MCCommandSender> INJECTED_PLAYERS = new HashMap<>(); |
| 765 | + private static final Map<String, AtomicInteger> PLAYER_INJECTION_COUNT_MAP = new HashMap<>(); |
764 | 766 | private static MCEntity injectedEntity; |
765 | 767 | 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})$"); |
766 | 768 |
|
@@ -1221,26 +1223,50 @@ public static MCCommandSender GetInjectedPlayer(String name) { |
1221 | 1223 | return INJECTED_PLAYERS.get(name.toLowerCase()); |
1222 | 1224 | } |
1223 | 1225 |
|
| 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 | + */ |
1224 | 1231 | public static void InjectPlayer(MCCommandSender player) { |
1225 | 1232 | String name = player.getName(); |
1226 | 1233 | if("CONSOLE".equals(name)) { |
1227 | 1234 | name = "~console"; |
1228 | 1235 | } |
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 | + } |
1230 | 1246 | } |
1231 | 1247 |
|
1232 | 1248 | /** |
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. |
1237 | 1253 | */ |
1238 | 1254 | public static MCCommandSender UninjectPlayer(MCCommandSender player) { |
1239 | 1255 | String name = player.getName(); |
1240 | 1256 | if("CONSOLE".equals(name)) { |
1241 | 1257 | name = "~console"; |
1242 | 1258 | } |
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 | + } |
1244 | 1270 | } |
1245 | 1271 |
|
1246 | 1272 | public static void InjectEntity(MCEntity entity) { |
|
0 commit comments