Skip to content

Commit

Permalink
Merge pull request #1 from Polda18/bugfixes
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
Polda18 authored May 29, 2022
2 parents a76090a + 4a0f2a8 commit bfc1ec6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ whitelist takes their IGN and their current UUID and adds them to the whitelist.
That's fully expected. But when the player is *NOT* online, then whitelist checks
Mojang database to find that player in list of registered Minecraft players.
If that player cannot be found, whitelist refuses to add that player to whitelist.
If that player *IS* found in Mojang's database, then their online UUID is pulled.

That is no issue on online mode servers as this is fully expected and there's no
need to check if the player online does indeed exist or not, because otherwise server
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.polda18</groupId>
<artifactId>BetterWhitelist</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<name>BetterWhitelist</name>
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/me/polda18/betterwhitelist/events/EventsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
import me.polda18.betterwhitelist.utils.OnlineUUIDException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Level;

/**
* Core function of this plugin - event listener for player joining the game
* Core function of this plugin - event listener for player connecting
*/
public class EventsListener implements Listener {
private BetterWhitelist plugin;
Expand All @@ -39,7 +38,7 @@ public EventsListener(BetterWhitelist plugin) {
* @throws InvalidEntryException Fired when specified old player wasn't found in the whitelist (online mode update, failsafe mechanism)
*/
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) throws IOException, AlreadyInWhitelistException, OnlineUUIDException, InvalidEntryException {
public void onPlayerConnect(AsyncPlayerPreLoginEvent event) throws IOException, AlreadyInWhitelistException, OnlineUUIDException, InvalidEntryException {
// Check if the server runs in an online mode or not
boolean online_mode = Bukkit.getOnlineMode();

Expand All @@ -49,18 +48,19 @@ public void onPlayerJoin(PlayerJoinEvent event) throws IOException, AlreadyInWhi
}

// Get the player and his UUID
Player player = event.getPlayer();
String uuid = plugin.getWhitelist().getConfig()
.getString(player.getName() + ((online_mode) ? ".online_uuid" : ".offline_uuid"));
UUID uuid_joined = event.getUniqueId();
String player = event.getName();
String uuid_whitelisted = plugin.getWhitelist().getConfig()
.getString(player + ((online_mode) ? ".online_uuid" : ".offline_uuid"));

// Check other players if not found in online mode
if(uuid == null && online_mode) {
if(!uuid_joined.toString().equals(uuid_whitelisted) && online_mode) {
boolean uuid_match = false;
String old_player_name = "";
for(String entry : plugin.getWhitelist().getConfig().getKeys(false)) {
uuid = plugin.getWhitelist().getConfig().getString(entry + ".online_uuid");
assert uuid != null;
uuid_match = (player.getUniqueId().compareTo(UUID.fromString(uuid)) == 0);
uuid_whitelisted = plugin.getWhitelist().getConfig().getString(entry + ".online_uuid");
assert uuid_whitelisted != null;
uuid_match = (uuid_joined.compareTo(UUID.fromString(uuid_whitelisted)) == 0);

if(uuid_match) {
old_player_name = entry;
Expand All @@ -70,21 +70,22 @@ public void onPlayerJoin(PlayerJoinEvent event) throws IOException, AlreadyInWhi

// If match is found, update entries and announce name change to the console.
if(uuid_match) {
plugin.getWhitelist().addEntry(player.getName());
plugin.getWhitelist().addEntry(player);
plugin.getWhitelist().deleteEntry(old_player_name);

plugin.getLogger().log(Level.INFO, ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(plugin.getLanguage().getConfig()
.getString("messages.online_player_update"))
.replace("(old_player)", old_player_name)
.replace("(new_player)", player.getName())));
.replace("(new_player)", player)));
}
}

// Kick the player if the name/UUID is not found in the whitelist or there's a mismatch
if(uuid == null || player.getUniqueId().compareTo(UUID.fromString(uuid)) != 0) {
player.kickPlayer(ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(plugin.getLanguage().getConfig().getString("messages.kick"))));
if(uuid_whitelisted == null || uuid_joined.compareTo(UUID.fromString(uuid_whitelisted)) != 0) {
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_WHITELIST,
ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(plugin.getLanguage().getConfig().getString("messages.kick"))));
}
}
}

0 comments on commit bfc1ec6

Please sign in to comment.