General refactoring

This commit is contained in:
ShadowRanger 2016-03-10 14:01:56 +11:00
parent b8f4589b87
commit d42cc3e275
7 changed files with 44 additions and 30 deletions

View file

@ -78,7 +78,7 @@ public class OpenInv extends JavaPlugin {
getCommand("openinv").setExecutor(new OpenInvCommand(this)); getCommand("openinv").setExecutor(new OpenInvCommand(this));
getCommand("openender").setExecutor(new OpenEnderCommand(this)); getCommand("openender").setExecutor(new OpenEnderCommand(this));
getCommand("searchinv").setExecutor(new SearchInvCommand(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("toggleopeninv").setExecutor(new ToggleOpenInvCommand(this));
getCommand("anychest").setExecutor(new AnyChestCommand(this)); getCommand("anychest").setExecutor(new AnyChestCommand(this));
getCommand("silentchest").setExecutor(new SilentChestCommand(this)); getCommand("silentchest").setExecutor(new SilentChestCommand(this));

View file

@ -13,12 +13,6 @@ import com.lishid.openinv.Permissions;
public class SearchEnderCommand implements CommandExecutor { public class SearchEnderCommand implements CommandExecutor {
private OpenInv plugin;
public SearchEnderCommand(OpenInv plugin) {
this.plugin = plugin;
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("searchender")) { if (command.getName().equalsIgnoreCase("searchender")) {

View file

@ -32,7 +32,7 @@ import net.minecraft.server.v1_9_R1.PlayerInteractManager;
public class PlayerDataManager { public class PlayerDataManager {
private OpenInv plugin; private final OpenInv plugin;
public PlayerDataManager(OpenInv plugin) { public PlayerDataManager(OpenInv plugin) {
this.plugin = plugin; this.plugin = plugin;

View file

@ -18,17 +18,19 @@ package com.lishid.openinv.internal;
import java.lang.reflect.Field; 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.entity.Player;
import org.bukkit.inventory.Inventory; 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 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 { public class SpecialPlayerInventory extends PlayerInventory {
private final CraftInventory inventory = new CraftInventory(this); private final CraftInventory inventory = new CraftInventory(this);

View file

@ -56,9 +56,9 @@ public class OpenInvPlayerListener implements Listener {
inventory.playerOnline(event.getPlayer()); inventory.playerOnline(event.getPlayer());
} }
SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId());
if (chest != null) { if (enderChest != null) {
chest.playerOnline(event.getPlayer()); enderChest.playerOnline(event.getPlayer());
} }
} }
@ -71,9 +71,9 @@ public class OpenInvPlayerListener implements Listener {
inventory.playerOffline(); inventory.playerOffline();
} }
SpecialEnderChest chest = OpenInv.enderChests.get(player.getUniqueId()); SpecialEnderChest enderChest = OpenInv.enderChests.get(player.getUniqueId());
if (chest != null) { if (enderChest != null) {
chest.playerOffline(); enderChest.playerOffline();
} }
} }
@ -118,8 +118,7 @@ public class OpenInvPlayerListener implements Listener {
if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) { if (OpenInv.hasPermission(player, Permissions.PERM_ANYCHEST) && configuration.getPlayerAnyChestStatus(player)) {
try { try {
anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z); anyChest = plugin.getAnySilentChest().isAnyChestNeeded(player, x, y, z);
} } catch (Exception e) {
catch (Exception e) {
player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); player.sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit.");
e.printStackTrace(); e.printStackTrace();
} }
@ -139,13 +138,13 @@ public class OpenInvPlayerListener implements Listener {
if (block.getState() instanceof Sign) { if (block.getState() instanceof Sign) {
try { try {
Sign sign = (Sign) block.getState(); Sign sign = (Sign) block.getState();
if (OpenInv.hasPermission(player, Permissions.PERM_OPENINV) && sign.getLine(0).equalsIgnoreCase("[openinv]")) { 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(); String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(3).trim();
player.performCommand("openinv " + text); player.performCommand("openinv " + text);
} }
} } catch (Exception e) {
catch (Exception e) { player.sendMessage(ChatColor.RED + "An internal error occured.");
player.sendMessage("Internal Error.");
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -15,6 +15,7 @@ import org.json.simple.parser.JSONParser;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public class UUIDFetcher implements Callable<Map<String, UUID>> { public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final double PROFILES_PER_REQUEST = 100; private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser(); private final JSONParser jsonParser = new JSONParser();
@ -34,11 +35,13 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
public Map<String, UUID> call() throws Exception { public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>(); Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) { for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection(); HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body); writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) { for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile; JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id"); String id = (String) jsonProfile.get("id");
@ -46,10 +49,12 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
UUID uuid = UUIDFetcher.getUUID(id); UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name.toLowerCase(), uuid); uuidMap.put(name.toLowerCase(), uuid);
} }
if (rateLimiting && i != requests - 1) { if (rateLimiting && i != requests - 1) {
Thread.sleep(100L); Thread.sleep(100L);
} }
} }
return uuidMap; return uuidMap;
} }
@ -62,12 +67,14 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static HttpURLConnection createConnection() throws Exception { private static HttpURLConnection createConnection() throws Exception {
URL url = new URL(PROFILE_URL); URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false); connection.setUseCaches(false);
connection.setDoInput(true); connection.setDoInput(true);
connection.setDoOutput(true); connection.setDoOutput(true);
return connection; return connection;
} }
@ -79,6 +86,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array(); return byteBuffer.array();
} }
@ -86,9 +94,11 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
if (array.length != 16) { if (array.length != 16) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length); throw new IllegalArgumentException("Illegal byte array length: " + array.length);
} }
ByteBuffer byteBuffer = ByteBuffer.wrap(array); ByteBuffer byteBuffer = ByteBuffer.wrap(array);
long mostSignificant = byteBuffer.getLong(); long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong(); long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant); return new UUID(mostSignificant, leastSignificant);
} }

View file

@ -10,7 +10,10 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class UUIDUtil { public final class UUIDUtil {
private UUIDUtil() {}
private static Player getPlayer(String name) { private static Player getPlayer(String name) {
Validate.notNull(name, "Name cannot be null"); Validate.notNull(name, "Name cannot be null");
@ -22,10 +25,12 @@ public class UUIDUtil {
for (Player player : players) { for (Player player : players) {
if (player.getName().toLowerCase().startsWith(lowerName)) { if (player.getName().toLowerCase().startsWith(lowerName)) {
int curDelta = player.getName().length() - lowerName.length(); int curDelta = player.getName().length() - lowerName.length();
if (curDelta < delta) { if (curDelta < delta) {
found = player; found = player;
delta = curDelta; delta = curDelta;
} }
if (curDelta == 0) break; if (curDelta == 0) break;
} }
} }
@ -39,14 +44,19 @@ public class UUIDUtil {
return offlinePlayer.hasPlayedBefore() ? offlinePlayer.getUniqueId() : null; 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) { public static UUID getUUIDOf(String name) {
UUID uuid; UUID uuid;
Player player = getPlayer(name); Player player = getPlayer(name);
if (player != null) { if (player != null) {
uuid = player.getUniqueId(); uuid = player.getUniqueId();
} } else {
else {
if (Bukkit.getServer().getOnlineMode()) { if (Bukkit.getServer().getOnlineMode()) {
if (!Bukkit.getServer().isPrimaryThread()) { if (!Bukkit.getServer().isPrimaryThread()) {
UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name)); UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(name));
@ -55,8 +65,7 @@ public class UUIDUtil {
try { try {
response = fetcher.call(); response = fetcher.call();
uuid = response.get(name.toLowerCase()); uuid = response.get(name.toLowerCase());
} } catch (Exception e) {
catch (Exception e) {
uuid = getUUIDLocally(name); uuid = getUUIDLocally(name);
} }
} else { } else {