Skip to content

Commit

Permalink
Fixed online matching preventing exact offline access, closes #9
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jikoo committed Jun 15, 2016
1 parent 9edac80 commit 1bbdde6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 25 deletions.
66 changes: 66 additions & 0 deletions src/main/java/com/lishid/openinv/OpenInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,72 @@ public void setPlayerAnyChestStatus(OfflinePlayer player, boolean status) {
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) {
player.sendMessage(ChatColor.GREEN + "/openinv <Player> - Open a player's inventory");
player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.lishid.openinv.commands;

import java.util.HashMap;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -75,20 +73,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
new BukkitRunnable() {
@Override
public void run() {
List<Player> matches = Bukkit.matchPlayer(name);
final OfflinePlayer offlinePlayer;
if (!matches.isEmpty()) {
offlinePlayer = matches.get(0);
} else {
offlinePlayer = Bukkit.getOfflinePlayer(name);
}
if (!player.isOnline()) {
return;
}
final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);

if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;
}

new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -98,6 +89,7 @@ public void run() {
openInventory(player, offlinePlayer);
}
}.runTask(plugin);

}
}.runTaskAsynchronously(plugin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.lishid.openinv.commands;

import java.util.HashMap;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -75,20 +73,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
new BukkitRunnable() {
@Override
public void run() {
List<Player> matches = Bukkit.matchPlayer(name);
final OfflinePlayer offlinePlayer;
if (!matches.isEmpty()) {
offlinePlayer = matches.get(0);
} else {
offlinePlayer = Bukkit.getOfflinePlayer(name);
}
if (!player.isOnline()) {
return;
}
final OfflinePlayer offlinePlayer = plugin.matchPlayer(name);

if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
player.sendMessage(ChatColor.RED + "Player not found!");
return;
}

new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -98,6 +89,7 @@ public void run() {
openInventory(player, offlinePlayer);
}
}.runTask(plugin);

}
}.runTaskAsynchronously(plugin);

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: OpenInv
main: com.lishid.openinv.OpenInv
version: 2.4.8
version: 2.4.9
author: lishid
authors: [Jikoo]
description: >
Expand Down

0 comments on commit 1bbdde6

Please sign in to comment.