Search in sources :

Example 6 with PlayerLoginEvent

use of org.bukkit.event.player.PlayerLoginEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAbortPlayerJoinForInvalidName.

@Test
public void shouldAbortPlayerJoinForInvalidName() throws FailedVerificationException {
    // given
    String name = "inval!dName";
    Player player = mockPlayerWithName(name);
    PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", null));
    given(validationService.isUnrestricted(name)).willReturn(false);
    given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);
    FailedVerificationException exception = new FailedVerificationException(MessageKey.INVALID_NAME_CHARACTERS, "[a-z]");
    doThrow(exception).when(onJoinVerifier).checkIsValidName(name);
    String message = "Invalid characters!";
    given(messages.retrieveSingle(exception.getReason(), exception.getArgs())).willReturn(message);
    // when
    listener.onPlayerLogin(event);
    // then
    verify(validationService).isUnrestricted(name);
    verify(onJoinVerifier).refusePlayerForFullServer(event);
    verify(onJoinVerifier).checkSingleSession(name);
    verify(onJoinVerifier).checkIsValidName(name);
    // Check that we don't talk with the data source before performing checks that don't require it
    verifyZeroInteractions(dataSource);
    verify(event).setKickMessage(message);
    verify(event).setResult(PlayerLoginEvent.Result.KICK_OTHER);
}
Also used : Player(org.bukkit.entity.Player) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with PlayerLoginEvent

use of org.bukkit.event.player.PlayerLoginEvent in project AuthMeReloaded by AuthMe.

the class OnJoinVerifierTest method shouldRefuseNonVipPlayerForFullServer.

@Test
public void shouldRefuseNonVipPlayerForFullServer() {
    // given
    Player player = mock(Player.class);
    PlayerLoginEvent event = new PlayerLoginEvent(player, "hostname", null);
    event.setResult(PlayerLoginEvent.Result.KICK_FULL);
    given(permissionsManager.hasPermission(player, PlayerStatePermission.IS_VIP)).willReturn(false);
    String serverFullMessage = "server is full";
    given(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER)).willReturn(serverFullMessage);
    // when
    boolean result = onJoinVerifier.refusePlayerForFullServer(event);
    // then
    assertThat(result, equalTo(true));
    assertThat(event.getResult(), equalTo(PlayerLoginEvent.Result.KICK_FULL));
    assertThat(event.getKickMessage(), equalTo(serverFullMessage));
    verifyZeroInteractions(bukkitService, dataSource);
}
Also used : Player(org.bukkit.entity.Player) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) Test(org.junit.Test)

Example 8 with PlayerLoginEvent

use of org.bukkit.event.player.PlayerLoginEvent in project Dragonet-Legacy by DragonetMC.

the class DragonetSession method setPlayer.

/**
     * Sets the player associated with this session.
     *
     * @param profile The player's profile with name and UUID information.
     * @throws IllegalStateException if there is already a player associated
     * with this session.
     */
@Override
public void setPlayer(PlayerProfile profile) {
    if (this.getPlayer() != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // initialize the player
    PlayerDataService.PlayerReader reader = this.getServer().getPlayerDataService().beginReadingData(profile.getUniqueId());
    //this.player = new GlowPlayer(this, profile, reader);
    this.player = new DragonetPlayer(this, profile, reader);
    // login event
    PlayerLoginEvent event = EventFactory.onPlayerLogin(player, this.getHostname());
    if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    // Dragonet-Add: or same username
    for (GlowPlayer other : getServer().getOnlinePlayers()) {
        if ((other != player && other.getUniqueId().equals(player.getUniqueId())) || other.getName().equalsIgnoreCase(player.getName())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    player.join(this, reader);
    WindowItemsPacket creativeInv = null;
    //Send the StartGamePacket
    StartGamePacket pkStartGame = new StartGamePacket();
    pkStartGame.seed = 0;
    pkStartGame.generator = 1;
    if (this.player.getGameMode().equals(GameMode.CREATIVE)) {
        pkStartGame.gamemode = 1;
        creativeInv = WindowItemsPacket.CREATIVE_INVENTORY;
    } else if (this.player.getGameMode().equals(GameMode.SURVIVAL) && this.player.getGameMode().equals(GameMode.ADVENTURE)) {
        pkStartGame.gamemode = 0;
    } else {
        creativeInv = new WindowItemsPacket();
        creativeInv.windowID = PEWindowConstantID.PLAYER_CREATIVE;
    }
    pkStartGame.eid = (long) this.player.getEntityId();
    pkStartGame.spawnX = this.player.getWorld().getSpawnLocation().getBlockX();
    pkStartGame.spawnY = this.player.getWorld().getSpawnLocation().getBlockY();
    pkStartGame.spawnZ = this.player.getWorld().getSpawnLocation().getBlockZ();
    pkStartGame.x = (float) this.player.getLocation().getX();
    pkStartGame.y = (float) this.player.getLocation().getY();
    pkStartGame.z = (float) this.player.getLocation().getZ();
    this.send(pkStartGame);
    if (creativeInv != null) {
        this.send(creativeInv);
    }
    //Send crafting recipie list
    sendRecipies();
    //Send Time
    SetTimePacket pkTime = new SetTimePacket((int) (this.getPlayer().getWorld().getTime() & 0xFFFFFFFF), false);
    this.send(pkTime);
    //Send Spawn Position
    SetSpawnPositionPacket pkSpawnPos = new SetSpawnPositionPacket();
    pkSpawnPos.x = this.player.getLocation().getBlockX();
    pkSpawnPos.y = this.player.getLocation().getBlockY();
    pkSpawnPos.z = this.player.getLocation().getBlockZ();
    this.send(pkSpawnPos);
    //Send Health
    SetHealthPacket pkHealth = new SetHealthPacket((int) Math.floor(this.getPlayer().getHealth()));
    this.send(pkHealth);
    //Send Difficulty Packet
    SetDifficultyPacket pkDifficulty = new SetDifficultyPacket();
    pkDifficulty.difficulty = this.getServer().getDifficulty().getValue();
    this.send(pkDifficulty);
    player.getWorld().getRawPlayers().add(player);
    GlowServer.logger.log(Level.INFO, "{0} [{1}] connected from Minecraft PE, UUID: {2}", new Object[] { player.getName(), this.getAddress(), player.getUniqueId() });
    //Preprare chunks
    this.chunkManager.prepareLoginChunks();
    // message and user list
    String message = EventFactory.onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        this.getServer().broadcastMessage(message);
    }
    // todo: display names are included in the outgoing messages here, but
    // don't show up on the client. A workaround or proper fix is needed.
    Message addMessage = new UserListItemMessage(UserListItemMessage.Action.ADD_PLAYER, player.getUserListEntry());
    List<UserListItemMessage.Entry> entries = new ArrayList<>();
    for (GlowPlayer other : this.getServer().getOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(UserListItemMessage.Action.ADD_PLAYER, entries));
    RakNetInterface.sendName();
}
Also used : DragonetPlayer(org.dragonet.entity.DragonetPlayer) Message(com.flowpowered.networking.Message) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) SetHealthPacket(org.dragonet.net.packet.minecraft.SetHealthPacket) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) WindowItemsPacket(org.dragonet.net.packet.minecraft.WindowItemsPacket) ArrayList(java.util.ArrayList) StartGamePacket(org.dragonet.net.packet.minecraft.StartGamePacket) SetTimePacket(org.dragonet.net.packet.minecraft.SetTimePacket) PlayerDataService(net.glowstone.io.PlayerDataService) SetDifficultyPacket(org.dragonet.net.packet.minecraft.SetDifficultyPacket) SetSpawnPositionPacket(org.dragonet.net.packet.minecraft.SetSpawnPositionPacket) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent)

Example 9 with PlayerLoginEvent

use of org.bukkit.event.player.PlayerLoginEvent in project AuthMeReloaded by AuthMe.

the class OnJoinVerifierTest method shouldNotDoAnythingForNormalEvent.

@Test
public void shouldNotDoAnythingForNormalEvent() {
    // given
    PlayerLoginEvent event = mock(PlayerLoginEvent.class);
    given(event.getResult()).willReturn(PlayerLoginEvent.Result.ALLOWED);
    // when
    boolean result = onJoinVerifier.refusePlayerForFullServer(event);
    // then
    assertThat(result, equalTo(false));
    verify(event).getResult();
    verifyNoMoreInteractions(event);
    verifyZeroInteractions(bukkitService, dataSource, permissionsManager);
}
Also used : PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) Test(org.junit.Test)

Example 10 with PlayerLoginEvent

use of org.bukkit.event.player.PlayerLoginEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldPerformAllJoinVerificationsSuccessfully.

@Test
public void shouldPerformAllJoinVerificationsSuccessfully() throws FailedVerificationException {
    // given
    String name = "someone";
    Player player = mockPlayerWithName(name);
    String ip = "12.34.56.78";
    PlayerLoginEvent event = spy(new PlayerLoginEvent(player, "", mockAddrWithIp(ip)));
    given(validationService.isUnrestricted(name)).willReturn(false);
    given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);
    PlayerAuth auth = PlayerAuth.builder().name(name).build();
    given(dataSource.getAuth(name)).willReturn(auth);
    // when
    listener.onPlayerLogin(event);
    // then
    verify(validationService).isUnrestricted(name);
    verify(onJoinVerifier).refusePlayerForFullServer(event);
    verify(onJoinVerifier).checkSingleSession(name);
    verify(onJoinVerifier).checkIsValidName(name);
    verify(onJoinVerifier).checkAntibot(player, true);
    verify(onJoinVerifier).checkKickNonRegistered(true);
    verify(onJoinVerifier).checkNameCasing(player, auth);
    verify(onJoinVerifier).checkPlayerCountry(true, ip);
    verify(teleportationService).teleportOnJoin(player);
    verifyNoModifyingCalls(event);
}
Also used : Player(org.bukkit.entity.Player) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) Test(org.junit.Test)

Aggregations

PlayerLoginEvent (org.bukkit.event.player.PlayerLoginEvent)11 Test (org.junit.Test)9 Player (org.bukkit.entity.Player)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 GlowPlayer (net.glowstone.entity.GlowPlayer)2 UserListItemMessage (net.glowstone.net.message.play.game.UserListItemMessage)2 AsyncableMessage (com.flowpowered.network.AsyncableMessage)1 Message (com.flowpowered.network.Message)1 Message (com.flowpowered.networking.Message)1 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)1 ArrayList (java.util.ArrayList)1 PlayerDataService (net.glowstone.io.PlayerDataService)1 PlayerReader (net.glowstone.io.PlayerDataService.PlayerReader)1 KickMessage (net.glowstone.net.message.KickMessage)1 SetCompressionMessage (net.glowstone.net.message.SetCompressionMessage)1 LoginSuccessMessage (net.glowstone.net.message.login.LoginSuccessMessage)1 DestroyEntitiesMessage (net.glowstone.net.message.play.entity.DestroyEntitiesMessage)1 PingMessage (net.glowstone.net.message.play.game.PingMessage)1 Entry (net.glowstone.net.message.play.game.UserListItemMessage.Entry)1 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)1