Merge pull request #44 from ShadowRanger/master

Minor code formatting changes
This commit is contained in:
Brett Flannigan 2015-03-08 05:00:27 -05:00
commit c8edf9f898
6 changed files with 55 additions and 30 deletions

View File

@ -8,7 +8,9 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
public class BlockPlaceListener implements Listener {
public class BlockPlaceListener implements Listener
{
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) public void onBlockPlace(BlockPlaceEvent event)
{ {
@ -20,12 +22,14 @@ public class BlockPlaceListener implements Listener {
BorderData border = Config.Border(world.getName()); BorderData border = Config.Border(world.getName());
if (border == null) return; if (border == null) return;
if (!border.insideBorder(loc.getX(), loc.getZ(), Config.ShapeRound())) { if (!border.insideBorder(loc.getX(), loc.getZ(), Config.ShapeRound()))
{
event.setCancelled(true); event.setCancelled(true);
} }
} }
public void unregister() { public void unregister()
{
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }
} }

View File

@ -19,31 +19,37 @@ import java.util.concurrent.Callable;
* from http://forums.bukkit.org/threads/player-name-uuid-fetcher.250926/ * from http://forums.bukkit.org/threads/player-name-uuid-fetcher.250926/
*/ */
public class NameFetcher implements Callable<Map<UUID, String>> { public class NameFetcher implements Callable<Map<UUID, String>>
{
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
private final JSONParser jsonParser = new JSONParser(); private final JSONParser jsonParser = new JSONParser();
private final List<UUID> uuids; private final List<UUID> uuids;
public NameFetcher(List<UUID> uuids) { public NameFetcher(List<UUID> uuids)
{
this.uuids = ImmutableList.copyOf(uuids); this.uuids = ImmutableList.copyOf(uuids);
} }
@Override @Override
public Map<UUID, String> call() throws Exception { public Map<UUID, String> call() throws Exception
{
Map<UUID, String> uuidStringMap = new HashMap<UUID, String>(); Map<UUID, String> uuidStringMap = new HashMap<UUID, String>();
for (UUID uuid: uuids) { for (UUID uuid: uuids)
{
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL+uuid.toString().replace("-", "")).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL+uuid.toString().replace("-", "")).openConnection();
JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
String name = (String) response.get("name"); String name = (String) response.get("name");
if (name == null) { if (name == null)
{
continue; continue;
} }
String cause = (String) response.get("cause"); String cause = (String) response.get("cause");
String errorMessage = (String) response.get("errorMessage"); String errorMessage = (String) response.get("errorMessage");
if (cause != null && cause.length() > 0) { if (cause != null && cause.length() > 0)
{
throw new IllegalStateException(errorMessage); throw new IllegalStateException(errorMessage);
} }
uuidStringMap.put(uuid, name); uuidStringMap.put(uuid, name);
} }
return uuidStringMap; return uuidStringMap;
} }
} }

View File

@ -20,52 +20,61 @@ import java.util.concurrent.Callable;
* slightly modified to fix name case mismatches for single name lookup * slightly modified to fix name case mismatches for single name lookup
*/ */
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();
private final List<String> names; private final List<String> names;
private final boolean rateLimiting; private final boolean rateLimiting;
public UUIDFetcher(List<String> names, boolean rateLimiting) { public UUIDFetcher(List<String> names, boolean rateLimiting)
{
this.names = ImmutableList.copyOf(names); this.names = ImmutableList.copyOf(names);
this.rateLimiting = rateLimiting; this.rateLimiting = rateLimiting;
} }
public UUIDFetcher(List<String> names) { public UUIDFetcher(List<String> names)
{
this(names, true); this(names, true);
} }
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");
String name = (String) jsonProfile.get("name"); String name = (String) jsonProfile.get("name");
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;
} }
private static void writeBody(HttpURLConnection connection, String body) throws Exception { private static void writeBody(HttpURLConnection connection, String body) throws Exception
{
OutputStream stream = connection.getOutputStream(); OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes()); stream.write(body.getBytes());
stream.flush(); stream.flush();
stream.close(); stream.close();
} }
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");
@ -76,18 +85,21 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
return connection; return connection;
} }
private static UUID getUUID(String id) { private static UUID getUUID(String id)
{
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32)); return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
} }
public static byte[] toBytes(UUID uuid) { public static byte[] toBytes(UUID 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();
} }
public static UUID fromBytes(byte[] array) { public static UUID fromBytes(byte[] array)
{
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);
} }
@ -97,7 +109,8 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
return new UUID(mostSignificant, leastSignificant); return new UUID(mostSignificant, leastSignificant);
} }
public static UUID getUUIDOf(String name) throws Exception { public static UUID getUUIDOf(String name) throws Exception
{
return new UUIDFetcher(Arrays.asList(name)).call().get(name.toLowerCase()); return new UUIDFetcher(Arrays.asList(name)).call().get(name.toLowerCase());
} }
} }

View File

@ -209,5 +209,4 @@ public class WBCommand implements CommandExecutor
commands.remove("commands"); commands.remove("commands");
return commands; return commands;
} }
}
}

View File

@ -27,7 +27,8 @@ public class WorldBorder extends JavaPlugin
// keep an eye on teleports, to redirect them to a spot inside the border if necessary // keep an eye on teleports, to redirect them to a spot inside the border if necessary
getServer().getPluginManager().registerEvents(new WBListener(), this); getServer().getPluginManager().registerEvents(new WBListener(), this);
if (Config.preventBlockPlace()) { if (Config.preventBlockPlace())
{
enableBlockPlaceListener(true); enableBlockPlaceListener(true);
} }
@ -64,11 +65,13 @@ public class WorldBorder extends JavaPlugin
} }
public void enableBlockPlaceListener(boolean enable) { public void enableBlockPlaceListener(boolean enable) {
if (enable) { if (enable)
{
getServer().getPluginManager().registerEvents(this.blockPlaceListener = new BlockPlaceListener(), this); getServer().getPluginManager().registerEvents(this.blockPlaceListener = new BlockPlaceListener(), this);
} else { }
else
{
blockPlaceListener.unregister(); blockPlaceListener.unregister();
} }
} }
} }

View File

@ -259,7 +259,7 @@ public class WorldFileData
} }
// crude chunk map PNG image output, for debugging // crude chunk map PNG image output, for debugging
private void testImage(CoordXZ region, List<Boolean> data) { private void testImage(CoordXZ region, List<Boolean> data) {
int width = 32; int width = 32;
int height = 32; int height = 32;