package omtteam.omlib.api.tile;

import net.minecraft.nbt.NBTTagCompound;
import omtteam.omlib.api.permission.EnumAccessLevel;
import omtteam.omlib.api.permission.TrustedPlayer;
import omtteam.omlib.handler.OMConfig;
import omtteam.omlib.util.player.Player;
import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class TrustedPlayersManagerTileTest {
    private final TrustedPlayersManagerTile tpm = new TrustedPlayersManagerTile(null);

    @Test
    void testTrustedPlayerAdditionAndRemoval() {
        Player player = new Player(UUID.randomUUID(), "test", "test");
        OMConfig.GENERAL.offlineModeSupport = true;
        for (EnumAccessLevel level : EnumAccessLevel.values()) {
            TrustedPlayer trustedPlayer = new TrustedPlayer(player, level);
            tpm.addTrustedPlayer(trustedPlayer);
            assertEquals(trustedPlayer, tpm.getTrustedPlayer(player.getName()), "trustedPlayer needs to be gettable by name");
            assertEquals(trustedPlayer, tpm.getTrustedPlayer(player.getUuid()), "trustedPlayer needs to be gettable by uuid");
            assertEquals(trustedPlayer, tpm.getTrustedPlayer(player), "trustedPlayer needs to be gettable by player");
            tpm.getTrustedPlayers().clear();
        }
        for (EnumAccessLevel level : EnumAccessLevel.values()) {
            TrustedPlayer trustedPlayer = new TrustedPlayer(player, level);
            tpm.addTrustedPlayer(trustedPlayer);
            tpm.removeTrustedPlayer(player.getName());
            assertNull(tpm.getTrustedPlayer(player), "removed trustedPlayer needs to be null");
            tpm.getTrustedPlayers().clear();
        }
        OMConfig.GENERAL.offlineModeSupport = false;
        for (EnumAccessLevel level : EnumAccessLevel.values()) {
            TrustedPlayer trustedPlayer = new TrustedPlayer(player, level);
            tpm.addTrustedPlayer(trustedPlayer);
            assertEquals(trustedPlayer, tpm.getTrustedPlayer(player.getName()), "trustedPlayer needs to be gettable by name");
            assertEquals(trustedPlayer, tpm.getTrustedPlayer(player), "trustedPlayer needs to be gettable by player");
            tpm.getTrustedPlayers().clear();
        }
        for (EnumAccessLevel level : EnumAccessLevel.values()) {
            TrustedPlayer trustedPlayer = new TrustedPlayer(player, level);
            tpm.addTrustedPlayer(trustedPlayer);
            tpm.removeTrustedPlayer(player.getName());
            assertNull(tpm.getTrustedPlayer(player), "removed trustedPlayer needs to be null");
            tpm.getTrustedPlayers().clear();
        }
    }

    @Test
    void checkNBT() {
        NBTTagCompound tag = new NBTTagCompound();
        tpm.writeToNBT(tag);
        TrustedPlayersManagerTile that = new TrustedPlayersManagerTile(null);
        that.readFromNBT(tag);
        assertEquals(tpm.trustedPlayers, that.trustedPlayers);
    }
}

@@ -210,16 +210,54 @@
 Config;%0A
+import omtteam.omlib.util.RandomUtil;%0A
 import o
@@ -2767,24 +2767,394 @@
 heckNBT() %7B%0A
+        tpm.getTrustedPlayers().clear();%0A        RandomUtil.init();%0A        for (EnumAccessLevel level : EnumAccessLevel.values()) %7B%0A            Player player = new Player(UUID.randomUUID(), RandomUtil.random.nextLong() + %22%22, %22test%22);%0A            TrustedPlayer trustedPlayer = new TrustedPlayer(player, level);%0A            tpm.addTrustedPlayer(trustedPlayer);%0A        %7D%0A
         NBTT

fix possible npe?