diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index d6b18f0..162ac72 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -78,7 +78,7 @@ public class OpenInv extends JavaPlugin { getCommand("openinv").setExecutor(new OpenInvCommand(this)); getCommand("openender").setExecutor(new OpenEnderCommand(this)); getCommand("searchinv").setExecutor(new SearchInvCommand(this)); - getCommand("searchender").setExecutor(new SearchEnderCommand(this)); + getCommand("searchender").setExecutor(new SearchEnderCommand()); getCommand("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this)); getCommand("anychest").setExecutor(new AnyChestCommand(this)); getCommand("silentchest").setExecutor(new SilentChestCommand(this)); diff --git a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java index 544b353..778a762 100644 --- a/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java +++ b/src/main/java/com/lishid/openinv/commands/SearchEnderCommand.java @@ -13,12 +13,6 @@ import com.lishid.openinv.Permissions; public class SearchEnderCommand implements CommandExecutor { - private OpenInv plugin; - - public SearchEnderCommand(OpenInv plugin) { - this.plugin = plugin; - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("searchender")) { diff --git a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java index 2f2f8d9..87bab09 100644 --- a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java +++ b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java @@ -32,7 +32,7 @@ import net.minecraft.server.v1_9_R1.PlayerInteractManager; public class PlayerDataManager { - private OpenInv plugin; + private final OpenInv plugin; public PlayerDataManager(OpenInv plugin) { this.plugin = plugin; diff --git a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java index 528a789..18adb68 100644 --- a/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java +++ b/src/main/java/com/lishid/openinv/internal/SpecialPlayerInventory.java @@ -18,17 +18,19 @@ package com.lishid.openinv.internal; import java.lang.reflect.Field; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -// Volatile -import net.minecraft.server.v1_9_R1.*; - -import org.bukkit.craftbukkit.v1_9_R1.entity.*; -import org.bukkit.craftbukkit.v1_9_R1.inventory.*; - import com.lishid.openinv.OpenInv; +import net.minecraft.server.v1_9_R1.ContainerUtil; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.ItemStack; +import net.minecraft.server.v1_9_R1.PlayerInventory; + public class SpecialPlayerInventory extends PlayerInventory { private final CraftInventory inventory = new CraftInventory(this); diff --git a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java index 7db5c9b..9c2f9e4 100644 --- a/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/listeners/OpenInvPlayerListener.java @@ -56,9 +56,9 @@ public class OpenInvPlayerListener implements Listener { inventory.playerOnline(event.getPlayer()); } - SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); - if (chest != null) { - chest.playerOnline(event.getPlayer()); + SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + if (enderChest != null) { + enderChest.playerOnline(event.getPlayer()); } } @@ -71,9 +71,9 @@ public class OpenInvPlayerListener implements Listener { inventory.playerOffline(); } - SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); - if (chest != null) { - chest.playerOffline(); + SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId()); + if (enderChest != null) { + enderChest.playerOffline(); } } @@ -118,8 +118,7 @@ public class OpenInvPlayerListener implements Listener { if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) { try { anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z); - } - catch (Exception e) { + } catch (Exception e) { player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); e.printStackTrace(); } @@ -139,13 +138,13 @@ public class OpenInvPlayerListener implements Listener { if (block.getState() instanceof Sign) { try { Sign sign = (Sign) block.getState(); + if (OpenInv.hasPermission(player, Permissions.PERM_OPENINV) && sign.getLine(0).equalsIgnoreCase("[openinv]")) { String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(3).trim(); player.performCommand("openinv " + text); } - } - catch (Exception e) { - player.sendMessage("Internal Error."); + } catch (Exception e) { + player.sendMessage(ChatColor.RED + "An internal error occured."); e.printStackTrace(); } diff --git a/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java b/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java index 2784fb4..cc0499a 100644 --- a/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java +++ b/src/main/java/com/lishid/openinv/utils/UUIDFetcher.java @@ -15,6 +15,7 @@ import org.json.simple.parser.JSONParser; import com.google.common.collect.ImmutableList; public class UUIDFetcher implements Callable> { + private static final double PROFILES_PER_REQUEST = 100; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private final JSONParser jsonParser = new JSONParser(); @@ -34,11 +35,13 @@ public class UUIDFetcher implements Callable> { public Map call() throws Exception { Map uuidMap = new HashMap(); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); + for (int i = 0; i < requests; i++) { HttpURLConnection connection = createConnection(); String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); writeBody(connection, body); JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); + for (Object profile : array) { JSONObject jsonProfile = (JSONObject) profile; String id = (String) jsonProfile.get("id"); @@ -46,10 +49,12 @@ public class UUIDFetcher implements Callable> { UUID uuid = UUIDFetcher.getUUID(id); uuidMap.put(name.toLowerCase(), uuid); } + if (rateLimiting && i != requests - 1) { Thread.sleep(100L); } } + return uuidMap; } @@ -62,12 +67,14 @@ public class UUIDFetcher implements Callable> { private static HttpURLConnection createConnection() throws Exception { URL url = new URL(PROFILE_URL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); + return connection; } @@ -79,6 +86,7 @@ public class UUIDFetcher implements Callable> { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); + return byteBuffer.array(); } @@ -86,9 +94,11 @@ public class UUIDFetcher implements Callable> { if (array.length != 16) { throw new IllegalArgumentException("Illegal byte array length: " + array.length); } + ByteBuffer byteBuffer = ByteBuffer.wrap(array); long mostSignificant = byteBuffer.getLong(); long leastSignificant = byteBuffer.getLong(); + return new UUID(mostSignificant, leastSignificant); } diff --git a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java index ea7c038..f7faad1 100644 --- a/src/main/java/com/lishid/openinv/utils/UUIDUtil.java +++ b/src/main/java/com/lishid/openinv/utils/UUIDUtil.java @@ -10,7 +10,10 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -public class UUIDUtil { +public final class UUIDUtil { + + private UUIDUtil() {} + private static Player getPlayer(String name) { Validate.notNull(name, "Name cannot be null"); @@ -22,10 +25,12 @@ public class UUIDUtil { for (Player player : players) { if (player.getName().toLowerCase().startsWith(lowerName)) { int curDelta = player.getName().length() - lowerName.length(); + if (curDelta < delta) { found = player; delta = curDelta; } + if (curDelta == 0) break; } } @@ -39,14 +44,19 @@ public class UUIDUtil { return offlinePlayer.hasPlayedBefore() ? offlinePlayer.getUniqueId() : null; } + /** + * Returns the UUID of a player by their name. + * + * @param name the name of the player to get the UUID of + * @return the player's UUID or null + */ public static UUID getUUIDOf(String name) { UUID uuid; Player player = getPlayer(name); if (player != null) { uuid = player.getUniqueId(); - } - else { + } else { if (Bukkit.getServer().getOnlineMode()) { if (!Bukkit.getServer().isPrimaryThread()) { UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name)); @@ -55,8 +65,7 @@ public class UUIDUtil { try { response = fetcher.call(); uuid = response.get(name.toLowerCase()); - } - catch (Exception e) { + } catch (Exception e) { uuid = getUUIDLocally(name); } } else {