Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize access to player team map #456

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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