Fix advancement exception on player disconnect (#724)

This commit is contained in:
EpicPlayerA10 2022-03-03 17:11:42 +01:00 committed by GitHub
parent fff0e3d97a
commit f1465804c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Represents a tab which can be shared between multiple players. Created using {@link AdvancementManager#createTab(String, AdvancementRoot)}.
@ -158,7 +159,7 @@ public class AdvancementTab implements Viewable {
* @param player the player
*/
private void addPlayer(@NotNull Player player) {
Set<AdvancementTab> tabs = PLAYER_TAB_MAP.computeIfAbsent(player.getUuid(), p -> new HashSet<>());
Set<AdvancementTab> tabs = PLAYER_TAB_MAP.computeIfAbsent(player.getUuid(), p -> new CopyOnWriteArraySet<>());
tabs.add(this);
}

View File

@ -0,0 +1,83 @@
package net.minestom.server.advancements;
import net.kyori.adventure.text.Component;
import net.minestom.server.api.Env;
import net.minestom.server.api.EnvTest;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.item.Material;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@EnvTest
public class AdvancementIntegrationTest {
@Test
public void addAndRemoveViewer(Env env) {
var instance = env.createFlatInstance();
var player = env.createPlayer(instance, new Pos(0, 42, 0));
AdvancementRoot root = new AdvancementRoot(
Component.text("title"),
Component.text("description"),
Material.DIAMOND,
FrameType.TASK,
0,
0,
"minecraft:textures/block/stone.png"
);
AdvancementTab tab = env.process().advancement().createTab("minestom:minestom_tab", root);
// Add viewer
tab.addViewer(player);
assertEquals(1, tab.getViewers().size());
assertTrue(tab.getViewers().contains(player));
assertNotNull(AdvancementTab.getTabs(player));
assertEquals(1, AdvancementTab.getTabs(player).size());
assertTrue(AdvancementTab.getTabs(player).contains(tab));
// Remove viewer
tab.removeViewer(player);
assertEquals(0, tab.getViewers().size());
assertNull(AdvancementTab.getTabs(player));
}
@Test
public void removeViewerOnDisconnect(Env env) {
var instance = env.createFlatInstance();
var player = env.createPlayer(instance, new Pos(0, 42, 0));
AdvancementRoot root1 = new AdvancementRoot(
Component.text("title"),
Component.text("description"),
Material.DIAMOND,
FrameType.TASK,
0,
0,
"minecraft:textures/block/stone.png"
);
AdvancementRoot root2 = new AdvancementRoot(
Component.text("title2"),
Component.text("description"),
Material.DIAMOND,
FrameType.TASK,
0,
0,
"minecraft:textures/block/stone.png"
);
AdvancementTab tab1 = env.process().advancement().createTab("minestom:minestom_tab1", root1);
AdvancementTab tab2 = env.process().advancement().createTab("minestom:minestom_tab2", root2);
tab1.addViewer(player);
tab2.addViewer(player);
player.remove(); // Disconnect
assertEquals(0, tab1.getViewers().size());
assertEquals(0, tab2.getViewers().size());
assertNull(AdvancementTab.getTabs(player));
}
}