Skip to content

Commit

Permalink
Synchronise access to player team map (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen authored Aug 27, 2020
1 parent faebd51 commit aea1130
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

Expand All @@ -27,14 +26,16 @@
*/
public class ModulePlayerCollisions extends Module {

private CollisionPacketListener collisionPacketListener = new CollisionPacketListener();
private Map<Player, TeamPacket> playerTeamMap = Collections.synchronizedMap(new WeakHashMap<>());

private final CollisionPacketListener collisionPacketListener;
private final Map<Player, TeamPacket> playerTeamMap;

public ModulePlayerCollisions(OCMMain plugin){
super(plugin, "disable-player-collisions");

// inject all players at startup, so the plugin still works properly after a reload
collisionPacketListener = new CollisionPacketListener();
playerTeamMap = new WeakHashMap<>();

OCMMain.getInstance().addEnableListener(() -> {
for(Player player : Bukkit.getOnlinePlayers()){
PacketManager.getInstance().addListener(collisionPacketListener, player);
Expand Down Expand Up @@ -65,14 +66,16 @@ private void createOrUpdateTeam(Player player){
? CollisionRule.NEVER
: CollisionRule.ALWAYS;

if(playerTeamMap.containsKey(player)){
TeamPacket teamPacket = playerTeamMap.get(player);
teamPacket.setTeamAction(TeamAction.UPDATE);
teamPacket.setCollisionRule(collisionRule);
teamPacket.send(player);
} else {
debug("Fake collision team created for you.", player);
createAndSendNewTeam(player, collisionRule);
synchronized(playerTeamMap){
if(playerTeamMap.containsKey(player)){
TeamPacket teamPacket = playerTeamMap.get(player);
teamPacket.setTeamAction(TeamAction.UPDATE);
teamPacket.setCollisionRule(collisionRule);
teamPacket.send(player);
} else {
debug("Fake collision team created for you.", player);
createAndSendNewTeam(player, collisionRule);
}
}
}

Expand All @@ -84,7 +87,9 @@ private void createOrUpdateTeam(Player player){
*/
private void createAndSendNewTeam(Player player, CollisionRule collisionRule){
TeamPacket newTeamPacket = TeamUtils.craftTeamCreatePacket(player, collisionRule);
playerTeamMap.put(player, newTeamPacket);
synchronized(playerTeamMap){
playerTeamMap.put(player, newTeamPacket);
}

newTeamPacket.send(player);
}
Expand All @@ -110,6 +115,12 @@ public void onPacketSend(PacketEvent packetEvent){
return;
}

synchronized(playerTeamMap){
handlePacket(packetEvent);
}
}

private void handlePacket(PacketEvent packetEvent){
Object nmsPacket = packetEvent.getPacket().getNMSPacket();

CollisionRule collisionRule = isEnabled(packetEvent.getPlayer().getWorld())
Expand Down

0 comments on commit aea1130

Please sign in to comment.