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("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));

View file

@ -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")) {

View file

@ -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;

View file

@ -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);

View file

@ -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();
}

View file

@ -15,6 +15,7 @@ import org.json.simple.parser.JSONParser;
import com.google.common.collect.ImmutableList;
public class UUIDFetcher implements Callable<Map<String, UUID>> {
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<Map<String, UUID>> {
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
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<Map<String, UUID>> {
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<Map<String, UUID>> {
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<Map<String, UUID>> {
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<Map<String, UUID>> {
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);
}

View file

@ -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 {