This commit is contained in:
boy0001 2014-12-30 17:51:20 +11:00
parent 84bb45a88d
commit fef01de974
6 changed files with 0 additions and 691 deletions

View File

@ -1,215 +0,0 @@
package com.intellectualcrafters.plot.listeners;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper;
import org.bukkit.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.scheduler.BukkitScheduler;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author Empire92
*/
@SuppressWarnings({"unused", "deprecation"}) public class EntityListener implements Listener {
public final static HashMap<String, HashMap<Plot, HashSet<Integer>>> entityMap = new HashMap<>();
public EntityListener() {
final BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
scheduler.scheduleSyncRepeatingTask(PlotMain.getMain(), new Runnable() {
@Override
public void run() {
final Iterator<Entry<String, HashMap<Plot, HashSet<Integer>>>> worldIt = entityMap.entrySet().iterator();
final Set<Plot> plots = PlotMain.getPlots();
while (worldIt.hasNext()) {
final Entry<String, HashMap<Plot, HashSet<Integer>>> entry = worldIt.next();
final String worldname = entry.getKey();
if (!PlotMain.isPlotWorld(worldname)) {
worldIt.remove();
continue;
}
final World world = Bukkit.getWorld(worldname);
if ((world == null) || (entry.getValue().size() == 0)) {
worldIt.remove();
continue;
}
final Iterator<Entry<Plot, HashSet<Integer>>> it = entry.getValue().entrySet().iterator();
while (it.hasNext()) {
final Entry<Plot, HashSet<Integer>> plotEntry = it.next();
final Plot plot = plotEntry.getKey();
if (!plots.contains(plot)) {
it.remove();
continue;
}
boolean loaded = false;
final Location pos1 = PlotHelper.getPlotBottomLoc(world, plot.id).add(1, 0, 1);
final Location pos2 = PlotHelper.getPlotTopLoc(world, plot.id);
try {
loops:
for (int i = (pos1.getBlockX() / 16) * 16; i < (16 + ((pos2.getBlockX() / 16) * 16)); i += 16) {
for (int j = (pos1.getBlockZ() / 16) * 16; j < (16 + ((pos2.getBlockZ() / 16) * 16)); j += 16) {
final Chunk chunk = world.getChunkAt(i, j);
if (chunk.isLoaded()) {
loaded = true;
break loops;
}
}
}
} catch (final Exception e) {
it.remove();
continue;
}
if (!loaded) {
it.remove();
}
}
}
}
}, 24000L, 48000L);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public static void onPlayerInteract(final PlayerInteractEvent e) {
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
final Player p = e.getPlayer();
final World w = p.getWorld();
final String n = w.getName();
if ((e.getMaterial() == Material.MONSTER_EGG) || (e.getMaterial() == Material.MONSTER_EGGS)) {
if (entityMap.containsKey(n)) {
final Location l = e.getClickedBlock().getLocation();
final Plot plot = PlotHelper.getCurrentPlot(l);
if ((plot != null) && plot.hasRights(p)) {
int mobs;
if (entityMap.get(n).containsKey(plot)) {
mobs = entityMap.get(n).get(plot).size();
} else {
mobs = 0;
}
if (!(PlotMain.hasPermissionRange(p, "plots.mobcap", Settings.MOB_CAP) > mobs)) {
PlayerFunctions.sendMessage(p, C.NO_PERMISSION, "plots.mobcap." + (mobs + 1));
e.setCancelled(true);
}
}
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onCreatureSpawn(final CreatureSpawnEvent e) {
final Location l = e.getLocation();
final String w = l.getWorld().getName();
if (PlotMain.isPlotWorld(w)) {
final Plot plot = PlotHelper.getCurrentPlot(l);
if ((plot != null) && plot.hasOwner()) {
addEntity(e.getEntity(), plot);
}
}
}
@EventHandler
public static void onChunkLoad(final ChunkLoadEvent e) {
if (PlotMain.isPlotWorld(e.getWorld())) {
for (final Entity entity : e.getChunk().getEntities()) {
if (entity instanceof LivingEntity) {
if (!(entity instanceof Player)) {
final Plot plot = PlotHelper.getCurrentPlot(entity.getLocation());
if (plot != null) {
if (plot.hasOwner()) {
addEntity(entity, plot);
}
}
}
}
}
}
}
public static void addEntity(final Entity entity, final Plot plot) {
if (!entityMap.containsKey(plot.world)) {
entityMap.put(plot.world, new HashMap<Plot, HashSet<Integer>>());
}
if (!entityMap.containsKey(plot.world)) {
entityMap.put(plot.world, new HashMap<Plot, HashSet<Integer>>());
}
final HashMap<Plot, HashSet<Integer>> section = entityMap.get(plot.world);
if (!section.containsKey(plot)) {
section.put(plot, new HashSet<Integer>());
}
section.get(plot).add(entity.getEntityId());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onEntityDeath(final EntityDeathEvent e) {
final Entity entity = e.getEntity();
final Location l = entity.getLocation();
final String w = l.getWorld().getName();
remove(w, entity);
}
public static void remove(String w, Entity entity) {
if (entityMap.containsKey(w)) {
final int id = entity.getEntityId();
final Plot plot = PlotHelper.getCurrentPlot(entity.getLocation());
if (plot != null) {
if (entityMap.get(w).containsKey(plot)) {
entityMap.get(w).get(plot).remove(id);
}
} else {
for (final Entry<Plot, HashSet<Integer>> n : entityMap.get(w).entrySet()) {
n.getValue().remove(id);
}
}
}
}
@EventHandler
public static void onChunkDespawn(final ChunkUnloadEvent e) {
final String w = e.getWorld().getName();
if (entityMap.containsKey(w)) {
for (final Entity entity : e.getChunk().getEntities()) {
if (entity instanceof LivingEntity) {
if (!(entity instanceof Player)) {
final Plot plot = PlotHelper.getCurrentPlot(entity.getLocation());
if (plot != null) {
if (plot.hasOwner()) {
if (entityMap.get(w).containsKey(plot)) {
if (entityMap.get(w).get(plot).size() == 1) {
entityMap.get(w).remove(plot);
} else {
entityMap.get(w).get(plot).remove(entity.getEntityId());
}
}
}
}
}
}
}
}
}
}

View File

@ -1,71 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.uuid;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
/**
* Name Fetcher Class From Bukkit
*/
public class NameFetcher implements Callable<Map<UUID, String>> {
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
private final JSONParser jsonParser = new JSONParser();
private final List<UUID> uuids;
public NameFetcher(final List<UUID> uuids) {
this.uuids = ImmutableList.copyOf(uuids);
}
@Override
public Map<UUID, String> call() throws Exception {
final Map<UUID, String> uuidStringMap = new HashMap<>();
for (final UUID uuid : this.uuids) {
if (uuidStringMap.containsKey(uuid)) {
continue;
}
final HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection();
final JSONObject response = (JSONObject) this.jsonParser.parse(new InputStreamReader(connection.getInputStream()));
final String name = (String) response.get("name");
if (name == null) {
continue;
}
final String cause = (String) response.get("cause");
final String errorMessage = (String) response.get("errorMessage");
if ((cause != null) && (cause.length() > 0)) {
throw new IllegalStateException(errorMessage);
}
uuidStringMap.put(uuid, name);
}
return uuidStringMap;
}
}

View File

@ -1,120 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.uuid;
import com.google.common.collect.BiMap;
import com.intellectualcrafters.json.JSONObject;
import com.intellectualcrafters.json.JSONTokener;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.UUIDHandler;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
/**
* Plot UUID Saver/Fetcher
*
* @author Citymonstret
* @author Empire92
*/
public class PlotUUIDSaver implements UUIDSaver {
@Override
public void globalPopulate() {
Bukkit.getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), new Runnable() {
@Override
public void run() {
final OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers();
final int length = offlinePlayers.length;
final long start = System.currentTimeMillis();
String name;
UUID uuid;
for (final OfflinePlayer player : offlinePlayers) {
uuid = UUIDHandler.getUUID(player);
if (!UUIDHandler.uuidExists(uuid)) {
name = player.getName();
if (name != null) {
UUIDHandler.add(new StringWrapper(name), uuid);
}
}
}
final long time = System.currentTimeMillis() - start;
final int size = UUIDHandler.getUuidMap().size();
double ups;
if ((time == 0l) || (size == 0)) {
ups = size;
} else {
ups = size / time;
}
// Plot Squared Only...
PlotMain.sendConsoleSenderMessage("&cFinished caching of offline player UUIDs! Took &6" + time + "&cms (&6" + ups + "&c per millisecond), &6" + length + " &cUUIDs were cached" + " and there is now a grand total of &6" + size + " &ccached.");
}
});
}
@Override
public void globalSave(final BiMap<StringWrapper, UUID> map) {
}
@Override
public void save(final UUIDSet set) {
}
@Override
public UUID mojangUUID(final String name) throws Exception {
final URLConnection connection = new URL(Settings.API_URL + "?user=" + name).openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
final JSONTokener tokener = new JSONTokener(connection.getInputStream());
final JSONObject root = new JSONObject(tokener);
final String uuid = root.getJSONObject(name).getString("dashed");
return UUID.fromString(uuid);
}
@Override
public String mojangName(final UUID uuid) throws Exception {
final URLConnection connection = new URL(Settings.API_URL + "?user=" + uuid.toString().replace("-", "")).openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
final JSONTokener tokener = new JSONTokener(connection.getInputStream());
final JSONObject root = new JSONObject(tokener);
return root.getJSONObject(uuid.toString().replace("-", "")).getString("username");
}
@Override
public UUIDSet get(final String name) {
return null;
}
@Override
public UUIDSet get(final UUID uuid) {
return null;
}
}

View File

@ -1,124 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.uuid;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.Callable;
/**
* UUID Fetcher From Bukkit
*/
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();
private final List<String> names;
private final boolean rateLimiting;
public UUIDFetcher(final List<String> names, final boolean rateLimiting) {
this.names = ImmutableList.copyOf(names);
this.rateLimiting = rateLimiting;
}
public UUIDFetcher(final List<String> names) {
this(names, true);
}
private static void writeBody(final HttpURLConnection connection, final String body) throws Exception {
final OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
}
private static HttpURLConnection createConnection() throws Exception {
final URL url = new URL(PROFILE_URL);
final 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;
}
public static UUID getUUID(final 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));
}
@SuppressWarnings("unused")
public static byte[] toBytes(final UUID uuid) {
final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
@SuppressWarnings("unused")
public static UUID fromBytes(final byte[] array) {
if (array.length != 16) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
}
final ByteBuffer byteBuffer = ByteBuffer.wrap(array);
final long mostSignificant = byteBuffer.getLong();
final long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant);
}
@SuppressWarnings("unused")
public static UUID getUUIDOf(final String name) throws Exception {
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
}
@Override
public Map<String, UUID> call() throws Exception {
final Map<String, UUID> uuidMap = new HashMap<>();
final int requests = (int) Math.ceil(this.names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
final HttpURLConnection connection = createConnection();
final String body = JSONArray.toJSONString(this.names.subList(i * 100, Math.min((i + 1) * 100, this.names.size())));
writeBody(connection, body);
final JSONArray array = (JSONArray) this.jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (final Object profile : array) {
final JSONObject jsonProfile = (JSONObject) profile;
final String id = (String) jsonProfile.get("id");
final String name = (String) jsonProfile.get("name");
final UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}
if (this.rateLimiting && (i != (requests - 1))) {
Thread.sleep(100L);
}
}
return uuidMap;
}
}

View File

@ -1,92 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.uuid;
import com.google.common.collect.BiMap;
import com.intellectualcrafters.plot.object.StringWrapper;
import java.util.UUID;
/**
* @author Citymonstret
*/
public interface UUIDSaver {
/**
* Populate the default list
*/
public void globalPopulate();
/**
* Save the UUIDs
*
* @param biMap Map containing names and UUIDs
*/
public void globalSave(final BiMap<StringWrapper, UUID> biMap);
/**
* Save a single UUIDSet
*
* @param set Set to save
*/
public void save(final UUIDSet set);
/**
* Get a single UUIDSet
*
* @param name Username
*
* @return UUID Set
*/
public UUIDSet get(final String name);
/**
* Get a single UUIDSet
*
* @param uuid UUID
*
* @return UUID Set
*/
public UUIDSet get(final UUID uuid);
/**
* Fetch uuid from mojang servers
*
* @param name Username
*
* @return uuid
*
* @throws Exception
*/
public UUID mojangUUID(final String name) throws Exception;
/**
* Fetch username from mojang servers
*
* @param uuid UUID
*
* @return username
*
* @throws Exception
*/
public String mojangName(final UUID uuid) throws Exception;
}

View File

@ -1,69 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.uuid;
import java.util.UUID;
/**
* @author Citymonstret
*/
public class UUIDSet {
/**
* Player Name
*/
private final String name;
/**
* Player UUID
*/
private final UUID uuid;
/**
* Constructor
*
* @param name Username
* @param uuid UUID
*/
public UUIDSet(final String name, final UUID uuid) {
this.name = name;
this.uuid = uuid;
}
@Override
public String toString() {
return getName();
}
/**
* Return the name
*
* @return Name
*/
public String getName() {
return this.name;
}
public UUID getUUID() {
return this.uuid;
}
}