Skip to content

Commit

Permalink
Implement ProfileWhitelistVerifyEvent.
Browse files Browse the repository at this point in the history
Related to #922.

* Fixed /whitelist remove <player> showing usage error even on success
  • Loading branch information
aramperes committed Jun 3, 2018
1 parent a40541d commit 4c7e8c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/main/java/net/glowstone/EventFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.glowstone;

import com.destroystokyo.paper.event.profile.ProfileWhitelistVerifyEvent;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashSet;
Expand Down Expand Up @@ -144,9 +145,8 @@ public PlayerLoginEvent onPlayerLogin(GlowPlayer player, String hostname) {
} else if (ipBans.isBanned(addressString)) {
event.disallow(Result.KICK_BANNED,
Kick.BANNED.get(ipBans.getBanEntry(addressString).getReason()));
} else if (server.hasWhitelist() && !player.isWhitelisted()) {
event.disallow(Result.KICK_WHITELIST, Kick.WHITELIST.get());
} else if (server.getOnlinePlayers().size() >= server.getMaxPlayers()) {
} else if (checkWhitelisted(player, event)
&& server.getOnlinePlayers().size() >= server.getMaxPlayers()) {
event.disallow(Result.KICK_FULL, Kick.FULL.get(server.getMaxPlayers()));
}

Expand Down Expand Up @@ -247,4 +247,36 @@ public <T extends EntityDamageEvent> T onEntityDamage(T event) {
}
return result;
}

/**
* Checks whether a player is whitelisted when joining the server,
* and fires the {@link ProfileWhitelistVerifyEvent} in the process.
*
* <p>The supplied {@link PlayerLoginEvent} will be disallowed by this method
* if the player is not whitelisted.
*
* @param player the player joining the server
* @param loginEvent the {@link PlayerLoginEvent} that will follow this check
* @return true if the player is whitelisted, false otherwise
*/
private boolean checkWhitelisted(GlowPlayer player, PlayerLoginEvent loginEvent) {
// check whether the player is whitelisted (explicitly or implicitly)
boolean whitelisted = player.isOp()
|| !player.getServer().hasWhitelist()
|| player.isWhitelisted();
// fire the event to allow plugins to change this behavior
ProfileWhitelistVerifyEvent event = callEvent(new ProfileWhitelistVerifyEvent(
player.getProfile(),
player.getServer().hasWhitelist(),
whitelisted,
player.isOp(),
Kick.WHITELIST.get()
));
if (event.isWhitelisted()) {
return true;
}
// note: the kick message is mutable by plugins
loginEvent.disallow(Result.KICK_WHITELIST, event.getKickMessage());
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
player.setWhitelisted(false);
sender.sendMessage("Removed " + player.getName() + " from the whitelist");
});
return true;
}
if (subcommand.equals("reload")) {
sender.getServer().reloadWhitelist();
Expand Down

0 comments on commit 4c7e8c0

Please sign in to comment.