Fixed online matching preventing exact offline access, closes #9
Restored some old OpenInv behavior - Offline name partial completion. Currently there's no minimum similarity to consider a match a success, and the first name with the same similarity will take precedence. As a result, you may see some very odd results on occasion.
This commit is contained in:
		@@ -201,6 +201,72 @@ public class OpenInv extends JavaPlugin {
 | 
				
			|||||||
        saveConfig();
 | 
					        saveConfig();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Get an OfflinePlayer by name.
 | 
				
			||||||
 | 
					     * 
 | 
				
			||||||
 | 
					     * @param name the name of the Player
 | 
				
			||||||
 | 
					     * @return the OfflinePlayer, or null if no players have ever logged in
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public OfflinePlayer matchPlayer(String name) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Warn if called on the main thread - if we resort to searching offline players, this may take several seconds.
 | 
				
			||||||
 | 
					        if (getServer().isPrimaryThread()) {
 | 
				
			||||||
 | 
					            getLogger().warning("Call to OpenInv#matchPlayer made on the main thread!");
 | 
				
			||||||
 | 
					            getLogger().warning("This can cause the server to hang, potentially severely.");
 | 
				
			||||||
 | 
					            getLogger().warning("Trace:");
 | 
				
			||||||
 | 
					            for (StackTraceElement element : new Throwable().fillInStackTrace().getStackTrace()) {
 | 
				
			||||||
 | 
					                getLogger().warning(element.toString());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        OfflinePlayer player = getServer().getPlayerExact(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (player != null) {
 | 
				
			||||||
 | 
					            return player;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        player = getServer().getOfflinePlayer(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /*
 | 
				
			||||||
 | 
					         * Compatibility: Pre-UUID, getOfflinePlayer always returns an OfflinePlayer. Post-UUID,
 | 
				
			||||||
 | 
					         * getOfflinePlayer will return null if no matching player is found. To preserve
 | 
				
			||||||
 | 
					         * compatibility, only return the player if they have played before. Ignoring current online
 | 
				
			||||||
 | 
					         * status is fine, they'd have been found by getPlayerExact otherwise.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if (player != null && player.hasPlayedBefore()) {
 | 
				
			||||||
 | 
					            return player;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        player = getServer().getPlayer(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (player != null) {
 | 
				
			||||||
 | 
					            return player;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int bestMatch = Integer.MAX_VALUE;
 | 
				
			||||||
 | 
					        for (OfflinePlayer offline : getServer().getOfflinePlayers()) {
 | 
				
			||||||
 | 
					            if (offline.getName() == null) {
 | 
				
			||||||
 | 
					                // Loaded by UUID only, name has never been looked up.
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Compatibility: Lang3 is only bundled with 1.8+
 | 
				
			||||||
 | 
					            int currentMatch = org.apache.commons.lang.StringUtils.getLevenshteinDistance(name, offline.getName());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (currentMatch == 0) {
 | 
				
			||||||
 | 
					                return offline;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (currentMatch < bestMatch) {
 | 
				
			||||||
 | 
					                bestMatch = currentMatch;
 | 
				
			||||||
 | 
					                player = offline;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Only null if no players have played ever, otherwise even the worst match will do.
 | 
				
			||||||
 | 
					        return player;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static void ShowHelp(Player player) {
 | 
					    public static void ShowHelp(Player player) {
 | 
				
			||||||
        player.sendMessage(ChatColor.GREEN + "/openinv <Player> - Open a player's inventory");
 | 
					        player.sendMessage(ChatColor.GREEN + "/openinv <Player> - Open a player's inventory");
 | 
				
			||||||
        player.sendMessage(ChatColor.GREEN + "   (aliases: oi, inv, open)");
 | 
					        player.sendMessage(ChatColor.GREEN + "   (aliases: oi, inv, open)");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,9 +17,7 @@
 | 
				
			|||||||
package com.lishid.openinv.commands;
 | 
					package com.lishid.openinv.commands;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.bukkit.Bukkit;
 | 
					 | 
				
			||||||
import org.bukkit.ChatColor;
 | 
					import org.bukkit.ChatColor;
 | 
				
			||||||
import org.bukkit.OfflinePlayer;
 | 
					import org.bukkit.OfflinePlayer;
 | 
				
			||||||
import org.bukkit.command.Command;
 | 
					import org.bukkit.command.Command;
 | 
				
			||||||
@@ -75,20 +73,13 @@ public class OpenEnderPluginCommand implements CommandExecutor {
 | 
				
			|||||||
        new BukkitRunnable() {
 | 
					        new BukkitRunnable() {
 | 
				
			||||||
            @Override
 | 
					            @Override
 | 
				
			||||||
            public void run() {
 | 
					            public void run() {
 | 
				
			||||||
                List<Player> matches = Bukkit.matchPlayer(name);
 | 
					                final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);
 | 
				
			||||||
                final OfflinePlayer offlinePlayer;
 | 
					
 | 
				
			||||||
                if (!matches.isEmpty()) {
 | 
					 | 
				
			||||||
                    offlinePlayer = matches.get(0);
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    offlinePlayer = Bukkit.getOfflinePlayer(name);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (!player.isOnline()) {
 | 
					 | 
				
			||||||
                    return;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
 | 
					                if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
 | 
				
			||||||
                    player.sendMessage(ChatColor.RED + "Player not found!");
 | 
					                    player.sendMessage(ChatColor.RED + "Player not found!");
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                new BukkitRunnable() {
 | 
					                new BukkitRunnable() {
 | 
				
			||||||
                    @Override
 | 
					                    @Override
 | 
				
			||||||
                    public void run() {
 | 
					                    public void run() {
 | 
				
			||||||
@@ -98,6 +89,7 @@ public class OpenEnderPluginCommand implements CommandExecutor {
 | 
				
			|||||||
                        openInventory(player, offlinePlayer);
 | 
					                        openInventory(player, offlinePlayer);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }.runTask(plugin);
 | 
					                }.runTask(plugin);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }.runTaskAsynchronously(plugin);
 | 
					        }.runTaskAsynchronously(plugin);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,9 +17,7 @@
 | 
				
			|||||||
package com.lishid.openinv.commands;
 | 
					package com.lishid.openinv.commands;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.bukkit.Bukkit;
 | 
					 | 
				
			||||||
import org.bukkit.ChatColor;
 | 
					import org.bukkit.ChatColor;
 | 
				
			||||||
import org.bukkit.OfflinePlayer;
 | 
					import org.bukkit.OfflinePlayer;
 | 
				
			||||||
import org.bukkit.command.Command;
 | 
					import org.bukkit.command.Command;
 | 
				
			||||||
@@ -75,20 +73,13 @@ public class OpenInvPluginCommand implements CommandExecutor {
 | 
				
			|||||||
        new BukkitRunnable() {
 | 
					        new BukkitRunnable() {
 | 
				
			||||||
            @Override
 | 
					            @Override
 | 
				
			||||||
            public void run() {
 | 
					            public void run() {
 | 
				
			||||||
                List<Player> matches = Bukkit.matchPlayer(name);
 | 
					                final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);
 | 
				
			||||||
                final OfflinePlayer offlinePlayer;
 | 
					
 | 
				
			||||||
                if (!matches.isEmpty()) {
 | 
					 | 
				
			||||||
                    offlinePlayer = matches.get(0);
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    offlinePlayer = Bukkit.getOfflinePlayer(name);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (!player.isOnline()) {
 | 
					 | 
				
			||||||
                    return;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
 | 
					                if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
 | 
				
			||||||
                    player.sendMessage(ChatColor.RED + "Player not found!");
 | 
					                    player.sendMessage(ChatColor.RED + "Player not found!");
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                new BukkitRunnable() {
 | 
					                new BukkitRunnable() {
 | 
				
			||||||
                    @Override
 | 
					                    @Override
 | 
				
			||||||
                    public void run() {
 | 
					                    public void run() {
 | 
				
			||||||
@@ -98,6 +89,7 @@ public class OpenInvPluginCommand implements CommandExecutor {
 | 
				
			|||||||
                        openInventory(player, offlinePlayer);
 | 
					                        openInventory(player, offlinePlayer);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }.runTask(plugin);
 | 
					                }.runTask(plugin);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }.runTaskAsynchronously(plugin);
 | 
					        }.runTaskAsynchronously(plugin);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
name: OpenInv
 | 
					name: OpenInv
 | 
				
			||||||
main: com.lishid.openinv.OpenInv
 | 
					main: com.lishid.openinv.OpenInv
 | 
				
			||||||
version: 2.4.8
 | 
					version: 2.4.9
 | 
				
			||||||
author: lishid
 | 
					author: lishid
 | 
				
			||||||
authors: [Jikoo]
 | 
					authors: [Jikoo]
 | 
				
			||||||
description: >
 | 
					description: >
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user