Java Code Examples for org.bukkit.event.player.PlayerLoginEvent#setResult()

The following examples show how to use org.bukkit.event.player.PlayerLoginEvent#setResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerEvents.java    From AntiVPN with MIT License 6 votes vote down vote up
private void tryKickPlayer(String message, Player player, PlayerLoginEvent event) {
    Optional<PlaceholderAPIHook> placeholderapi;
    try {
        placeholderapi = ServiceLocator.getOptional(PlaceholderAPIHook.class);
    } catch (InstantiationException | IllegalAccessException ex) {
        logger.error(ex.getMessage(), ex);
        placeholderapi = Optional.empty();
    }

    event.setResult(PlayerLoginEvent.Result.KICK_OTHER);

    if (placeholderapi.isPresent()) {
        event.setKickMessage(placeholderapi.get().withPlaceholders(player, message));
    } else {
        event.setKickMessage(message);
    }
}
 
Example 2
Source File: PlayerConnectionListener.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority=EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent event){
	GameManager gm = GameManager.getGameManager();

	// Player is not allowed to join so don't create UhcPlayer. (Server full, whitelist, ban, ...)
	if (event.getResult() != Result.ALLOWED){
		return;
	}
	
	try{
		boolean allowedToJoin = gm.getPlayersManager().isPlayerAllowedToJoin(event.getPlayer());

		if (allowedToJoin){
			// Create player if not existent.
			gm.getPlayersManager().getOrCreateUhcPlayer(event.getPlayer());
		}else{
			throw new UhcPlayerJoinException("An unexpected error as occured.");
		}
	}catch(final UhcPlayerJoinException e){
		event.setKickMessage(e.getMessage());
		event.setResult(Result.KICK_OTHER);
	}
}
 
Example 3
Source File: PlayerListener.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event) {
    final Player player = event.getPlayer();
    final String name = player.getName();

    try {
        onJoinVerifier.checkSingleSession(name);
    } catch (FailedVerificationException e) {
        event.setKickMessage(messages.retrieveSingle(name, e.getReason(), e.getArgs()));
        event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
        return;
    }

    if (validationService.isUnrestricted(name)) {
        return;
    }

    onJoinVerifier.refusePlayerForFullServer(event);
}
 
Example 4
Source File: PlayerListenerTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldStopHandlingEventForBadResult() throws FailedVerificationException {
    // given
    String name = "someone";
    Player player = mockPlayerWithName(name);
    PlayerLoginEvent event = new PlayerLoginEvent(player, "", null);
    event.setResult(PlayerLoginEvent.Result.KICK_BANNED);
    event = spy(event);
    given(validationService.isUnrestricted(name)).willReturn(false);
    given(onJoinVerifier.refusePlayerForFullServer(event)).willReturn(false);

    // when
    listener.onPlayerLogin(event);

    // then
    verify(validationService).isUnrestricted(name);
    verify(onJoinVerifier).checkSingleSession(name);
    verify(onJoinVerifier).refusePlayerForFullServer(event);
    verifyNoModifyingCalls(event);
}
 
Example 5
Source File: OnJoinVerifierTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@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(player, 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));
    verifyNoInteractions(bukkitService, dataSource);
}
 
Example 6
Source File: OnJoinVerifierTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldKickNonVipForJoiningVipPlayer() {
    // 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(true);
    List<Player> onlinePlayers = Arrays.asList(mock(Player.class), mock(Player.class));
    given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
    given(permissionsManager.hasPermission(onlinePlayers.get(1), PlayerStatePermission.IS_VIP)).willReturn(false);
    given(bukkitService.getOnlinePlayers()).willReturn(onlinePlayers);
    given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
    given(messages.retrieveSingle(player, MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");

    // when
    boolean result = onJoinVerifier.refusePlayerForFullServer(event);

    // then
    assertThat(result, equalTo(false));
    assertThat(event.getResult(), equalTo(PlayerLoginEvent.Result.ALLOWED));
    // First player is VIP, so expect no interactions there and second player to have been kicked
    verifyNoInteractions(onlinePlayers.get(0));
    verify(onlinePlayers.get(1)).kickPlayer("kick for vip");
}
 
Example 7
Source File: OnJoinVerifierTest.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldKickVipPlayerIfNoPlayerCanBeKicked() {
    // 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(true);
    List<Player> onlinePlayers = Collections.singletonList(mock(Player.class));
    given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
    given(bukkitService.getOnlinePlayers()).willReturn(onlinePlayers);
    given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
    given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");

    // when
    boolean result = onJoinVerifier.refusePlayerForFullServer(event);

    // then
    assertThat(result, equalTo(true));
    assertThat(event.getResult(), equalTo(PlayerLoginEvent.Result.KICK_FULL));
    assertThat(event.getKickMessage(), equalTo("kick full server"));
    verifyNoInteractions(onlinePlayers.get(0));
}
 
Example 8
Source File: NPCMain.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onLogin(PlayerLoginEvent event)
{
    if(isTagged(event.getPlayer().getUniqueId()))
    {
        Boolean b = taggedPlayers.get(event.getPlayer().getUniqueId()).wasKilled();
        if(b == null)
        {
            event.setResult(Result.KICK_OTHER);
            event.setKickMessage(Lang.NPCALIVE.toString());
        }
    }
}
 
Example 9
Source File: UHPluginListener.java    From KTP with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onPlayerLogin(PlayerLoginEvent ev) {
	if (this.p.isPlayerDead(ev.getPlayer().getName()) && !this.p.getConfig().getBoolean("allow-reconnect", true)) {
		ev.setResult(Result.KICK_OTHER);
		ev.setKickMessage("Vous ĂȘtes mort !");
	}
}
 
Example 10
Source File: PlayerLoginListener.java    From ExploitFixer with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerLogin(final PlayerLoginEvent event) {
	final Player player = event.getPlayer();
	final InetAddress address = event.getAddress();
	final String locale = VersionUtil.getLocale(player);

	if (connectionModule.isNullAddressEnabled() && address == null) {
		final String nullAddressKickMessage = messagesModule.getKickMessage("nulladdress", locale);

		event.setKickMessage(nullAddressKickMessage);
		event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
	} else {
		final UUID uuid = player.getUniqueId();
		final String playerName = player.getName();
		final ExploitPlayer exploitPlayer = exploitPlayerManager.get(player);

		if (connectionModule.isUUIDSpoofEnabled()) {
			final BukkitScheduler scheduler = plugin.getServer().getScheduler();

			scheduler.runTaskAsynchronously(plugin, () -> {
				final String originalUUID = uuid.toString().replace("-", "");
				final String offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes())
						.toString().replace("-", "");

				if (!originalUUID.contains(offlineUUID)) {
					final String onlineUUID = exploitPlayer.getOnlineUUID();

					if (onlineUUID == null || !onlineUUID.contains(originalUUID)) {
						final HamsterPlayer hamsterPlayer = HamsterAPI.getInstance().getHamsterPlayerManager()
								.get(player);
						final String uuidSpoofKickMessage = messagesModule.getKickMessage("uuidspoof", locale);

						event.setKickMessage(uuidSpoofKickMessage);
						event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
						hamsterPlayer.disconnect(uuidSpoofKickMessage);
						hamsterPlayer.closeChannel();
					}
				}

				exploitPlayer.setLogged(true);
			});
		} else {
			exploitPlayer.setLogged(true);
		}
	}

	if (player.hasPermission("exploitfixer.notifications")) {
		notificationsModule.setNotifications(player, true);
	}
}
 
Example 11
Source File: LoginListener.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void login(PlayerLoginEvent event) {
    try {
        final Player player = event.getPlayer();
        final UUID uuid = player.getUniqueId();

        this.logins.cleanUp();
        final LoginResponse response = this.logins.getIfPresent(uuid);
        this.logins.invalidate(uuid);

        if(response == null) {
            this.logger.warning("No login info for " + player.getName() + " " + uuid);
            event.disallow(PlayerLoginEvent.Result.KICK_OTHER, INTERNAL_SERVER_ERROR);
            return;
        }

        // TODO: Consider creating a PreUserLoginEvent that can be cancelled,
        // before things like sessions are started.

        userStore.addUser(player, response.user());

        applyPermissions(player, response.user());

        if(response.punishment() != null) {
            rejectLogin(event, punishmentFormatter.screen(response.punishment()));
        }

        if(!player.hasPermission(Permissions.LOGIN)) {
            rejectLogin(event, new TranslatableComponent("servers.notAllowed"));
        }

        if(event.getResult() == PlayerLoginEvent.Result.KICK_FULL) {
            // Allow privileged players to join when the server is full
            if(player.hasPermission("pgm.fullserver")) {
                event.allow();
            } else {
                rejectLogin(event, new TranslatableComponent("serverFull"));
            }
        }

        if(response.user().mc_locale() != null) {
            // If we have a saved locale for the player, apply it.
            // This should ensure that text displayed on join is properly
            // localized, as long as the player has connected once before.
            player.setLocale(response.user().mc_locale());
        }

        userService.update(response.user(), new UserDoc.ClientDetails() {
            @Override public String mc_client_version() {
                return MinecraftVersion.describeProtocol(player.getProtocolVersion());
            }

            @Override public String skin_blob() {
                return player.getSkin().getData();
            }
        });

        if(event.getResult() == PlayerLoginEvent.Result.KICK_OTHER) return;

        final UserLoginEvent ourEvent = new UserLoginEvent(
            player, response, event.getResult(),
            event.getKickMessage() == null || "".equals(event.getKickMessage()) ? null : new Component(event.getKickMessage())
        );

        eventBus.callEvent(ourEvent);

        event.setResult(ourEvent.getResult());
        event.setKickMessage(ourEvent.getKickMessage() == null ? "" : renderer.renderLegacy(ourEvent.getKickMessage(), player));
    }
    catch(Exception e) {
        this.logger.log(Level.SEVERE, e.toString(), e);
        event.disallow(PlayerLoginEvent.Result.KICK_OTHER, INTERNAL_SERVER_ERROR);
    }
}