Optimized UUID caching + made Async

This commit is contained in:
boy0001 2015-06-25 00:32:27 +10:00
parent 8401c0dbcd
commit 7c55aa488c
10 changed files with 1166 additions and 116 deletions

View File

@ -524,7 +524,6 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
FlagManager.removeFlag(FlagManager.getFlag("titles"));
} else {
AbstractTitle.TITLE_CLASS = new DefaultTitle();
if (UUIDHandler.uuidWrapper instanceof DefaultUUIDWrapper) {
Settings.TWIN_MODE_UUID = true;
}

View File

@ -718,9 +718,7 @@ public class PlotSquared {
for (final String flag : intFlags) {
FlagManager.addFlag(new AbstractFlag(flag, new FlagValue.UnsignedIntegerValue()));
}
if (Settings.PHYSICS_LISTENER) {
FlagManager.addFlag(new AbstractFlag("disable-physics", new FlagValue.BooleanValue()));
}
FlagManager.addFlag(new AbstractFlag("fly", new FlagValue.BooleanValue()));
FlagManager.addFlag(new AbstractFlag("explosion", new FlagValue.BooleanValue()));
FlagManager.addFlag(new AbstractFlag("hostile-interact", new FlagValue.BooleanValue()));
@ -806,7 +804,6 @@ public class PlotSquared {
options.put("protection.redstone.disable-offline", Settings.REDSTONE_DISABLER);
options.put("protection.tnt-listener.enabled", Settings.TNT_LISTENER);
options.put("protection.piston.falling-blocks", Settings.PISTON_FALLING_BLOCK_CHECK);
options.put("protection.physics-listener.enabled", Settings.PHYSICS_LISTENER);
// Clusters
options.put("clusters.enabled", Settings.ENABLE_CLUSTERS);
@ -890,7 +887,6 @@ public class PlotSquared {
Settings.REDSTONE_DISABLER = config.getBoolean("protection.tnt-listener.enabled");
Settings.TNT_LISTENER = config.getBoolean("protection.tnt-listener.enabled");
Settings.PISTON_FALLING_BLOCK_CHECK = config.getBoolean("protection.piston.falling-blocks");
Settings.PHYSICS_LISTENER = config.getBoolean("protection.physics-listener.enabled");
// Clusters
Settings.ENABLE_CLUSTERS = config.getBoolean("clusters.enabled");
@ -902,7 +898,7 @@ public class PlotSquared {
// UUID
Settings.OFFLINE_MODE = config.getBoolean("UUID.offline");
Settings.UUID_LOWERCASE = config.getBoolean("UUID.force-lowercase");
Settings.UUID_LOWERCASE = Settings.OFFLINE_MODE && config.getBoolean("UUID.force-lowercase");
Settings.UUID_FROM_DISK = config.getBoolean("uuid.read-from-disk");
// Mob stuff

View File

@ -0,0 +1,14 @@
package com.intellectualcrafters.plot.commands;
public abstract class NamedSubCommand extends SubCommand {
public NamedSubCommand(Command command, String description, String usage, CommandCategory category, boolean isPlayer) {
super(command, description, usage, category, isPlayer);
}
public NamedSubCommand(String cmd, String permission, String description, String usage, CommandCategory category, boolean isPlayer, String[] aliases) {
super(cmd, permission, description, usage, category, isPlayer, aliases);
}
public NamedSubCommand(String cmd, String permission, String description, String usage, String alias, CommandCategory category, boolean isPlayer) {
super(cmd, permission, description, usage, alias, category, isPlayer);
}
}

View File

@ -288,9 +288,9 @@ public class list extends SubCommand {
}
}
i++;
if (Settings.FANCY_CHAT) {
if (player != null && Settings.FANCY_CHAT) {
ChatColor color;
if (player == null) {
if (plot.owner == null) {
color = ChatColor.GOLD;
}
else if (plot.isOwner(player.getUUID())) {
@ -397,7 +397,7 @@ public class list extends SubCommand {
MainUtil.sendMessage(player, message);
}
}
if (Settings.FANCY_CHAT) {
if (player != null && Settings.FANCY_CHAT) {
if (page < totalPages && page > 0) {
// back | next
new FancyMessage("")

View File

@ -65,10 +65,6 @@ public class Settings {
* Check for falling blocks when pistons extend?
*/
public static boolean PISTON_FALLING_BLOCK_CHECK = true;
/**
* Physics listener
*/
public static boolean PHYSICS_LISTENER = false;
/**
* Max auto claiming size
*/

View File

@ -123,7 +123,6 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
if (plot == null) {
return;
}
if (Settings.REDSTONE_DISABLER) {
if (UUIDHandler.getPlayer(plot.owner) == null) {
boolean disable = true;
@ -225,7 +224,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
return;
}
}
if (Settings.PHYSICS_LISTENER && block.getType().hasGravity()) {
if (block.getType().hasGravity()) {
Plot plot = MainUtil.getPlot(loc);
if (plot == null) {
return;
@ -698,7 +697,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
if (MainUtil.isPlotRoad(loc)) {
e.setCancelled(true);
}
else if (Settings.PHYSICS_LISTENER) {
else {
Plot plot = MainUtil.getPlot(loc);
if (FlagManager.isPlotFlagTrue(plot, "disable-physics")) {
e.setCancelled(true);
@ -912,9 +911,6 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
@EventHandler(ignoreCancelled=true, priority=EventPriority.HIGHEST)
public void onEntityFall(EntityChangeBlockEvent event) {
if (!Settings.PHYSICS_LISTENER) {
return;
}
if (event.getEntityType() != EntityType.FALLING_BLOCK) {
return;
}
@ -1580,12 +1576,10 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
return;
}
}
if (Settings.PHYSICS_LISTENER) {
if (FlagManager.isPlotFlagTrue(plot, "disable-physics")) {
Block block = event.getBlockPlaced();
sendBlockChange(block.getLocation(), block.getType(), block.getData());
}
}
return;
}
else if (!Permissions.hasPermission(pp, "plots.admin.build.road")) {

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,13 @@
package com.intellectualcrafters.plot.util.bukkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -11,6 +15,9 @@ import org.bukkit.OfflinePlayer;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.common.io.OutputSupplier;
import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
@ -21,6 +28,10 @@ import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.NbtFactory;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.NbtFactory.NbtCompound;
import com.intellectualcrafters.plot.util.NbtFactory.StreamOptions;
import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper;
import com.intellectualcrafters.plot.uuid.OfflineUUIDWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
@ -111,12 +122,18 @@ public class UUIDHandler {
if (CACHED) {
return;
}
PlotSquared.log(C.PREFIX.s() + "&6Starting player data caching: " + world);
final File container = Bukkit.getWorldContainer();
UUIDHandler.CACHED = true;
add(new StringWrapper("*"), DBFunc.everyone);
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
PlotSquared.log(C.PREFIX.s() + "&6Starting player data caching for: " + world);
final HashMap<StringWrapper, UUID> toAdd = new HashMap<>();
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
if (Settings.TWIN_MODE_UUID) {
HashSet<UUID> all = getAllUUIDS();
final File playerdataFolder = new File(Bukkit.getWorldContainer(), world + File.separator + "playerdata");
PlotSquared.log("&aFast mod UUID caching enabled!");
final File playerdataFolder = new File(container, world + File.separator + "playerdata");
String[] dat = playerdataFolder.list(new FilenameFilter() {
@Override
public boolean accept(final File f, final String s) {
@ -128,18 +145,24 @@ public class UUIDHandler {
for (final String current : dat) {
final String s = current.replaceAll(".dat$", "");
try {
final UUID uuid = UUID.fromString(s);
UUID uuid = UUID.fromString(s);
if (check || all.contains(uuid)) {
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
ExpireManager.dates.put(uuid, op.getLastPlayed());
add(new StringWrapper(op.getName()), uuid);
File file = new File(playerdataFolder + File.separator + current);
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
NbtCompound compound = NbtFactory.fromStream(is, StreamOptions.GZIP_COMPRESSION);
NbtCompound bukkit = (NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");
long last = (long) bukkit.get("lastPlayed");
ExpireManager.dates.put(uuid, last);
toAdd.put(new StringWrapper(name), uuid);
}
} catch (final Exception e) {
e.printStackTrace();
PlotSquared.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
}
PlotSquared.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
cache(toAdd);
return;
}
final HashSet<String> worlds = new HashSet<>();
@ -147,16 +170,17 @@ public class UUIDHandler {
worlds.add("world");
final HashSet<UUID> uuids = new HashSet<>();
final HashSet<String> names = new HashSet<>();
File playerdataFolder = null;
for (final String worldname : worlds) {
// Getting UUIDs
final File playerdataFolder = new File(Bukkit.getWorldContainer(), worldname + File.separator + "playerdata");
playerdataFolder = new File(container, worldname + File.separator + "playerdata");
String[] dat = playerdataFolder.list(new FilenameFilter() {
@Override
public boolean accept(final File f, final String s) {
return s.endsWith(".dat");
}
});
if (dat != null) {
if (dat != null && dat.length != 0) {
for (final String current : dat) {
final String s = current.replaceAll(".dat$", "");
try {
@ -166,6 +190,7 @@ public class UUIDHandler {
PlotSquared.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
break;
}
// Getting names
final File playersFolder = new File(worldname + File.separator + "players");
@ -175,20 +200,30 @@ public class UUIDHandler {
return s.endsWith(".dat");
}
});
if (dat != null) {
if (dat != null && dat.length != 0) {
for (final String current : dat) {
names.add(current.replaceAll(".dat$", ""));
}
break;
}
}
final UUIDWrapper wrapper = new DefaultUUIDWrapper();
for (UUID uuid : uuids) {
try {
final OfflinePlotPlayer player = wrapper.getOfflinePlayer(uuid);
ExpireManager.dates.put(uuid, player.getLastPlayed());
uuid = UUIDHandler.uuidWrapper.getUUID(player);
final StringWrapper name = new StringWrapper(player.getName());
add(name, uuid);
File file = new File(playerdataFolder + File.separator + uuid.toString() + ".dat");
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
NbtCompound compound = NbtFactory.fromStream(is, StreamOptions.GZIP_COMPRESSION);
NbtCompound bukkit = (NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");
long last = (long) bukkit.get("lastPlayed");
if (Settings.OFFLINE_MODE) {
if (!Settings.UUID_LOWERCASE || !name.toLowerCase().equals(name)) {
long most = (long) compound.get("UUIDMost");
long least = (long) compound.get("UUIDLeast");
uuid = new UUID(most, least);
}
}
ExpireManager.dates.put(uuid, last);
toAdd.put(new StringWrapper(name), uuid);
} catch (final Throwable e) {
PlotSquared.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
}
@ -196,22 +231,35 @@ public class UUIDHandler {
for (final String name : names) {
final UUID uuid = uuidWrapper.getUUID(name);
final StringWrapper nameWrap = new StringWrapper(name);
add(nameWrap, uuid);
toAdd.put(nameWrap, uuid);
}
if (uuidMap.size() == 0) {
for (OfflinePlotPlayer op : uuidWrapper.getOfflinePlayers()) {
if (op.getLastPlayed() != 0) {
String name = op.getName();
StringWrapper wrap = new StringWrapper(name);
UUID uuid = uuidWrapper.getUUID(op);
add(wrap, uuid);
toAdd.put(wrap, uuid);
}
}
}
cache(toAdd);
}
});
}
public static void cache(final HashMap<StringWrapper, UUID> toAdd) {
TaskManager.runTask(new Runnable() {
@Override
public void run() {
for (Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
add(entry.getKey(), entry.getValue());
}
PlotSquared.log(C.PREFIX.s() + "&6Cached a total of: " + UUIDHandler.uuidMap.size() + " UUIDs");
}
});
}
public static UUID getUUID(final PlotPlayer player) {
return UUIDHandler.uuidWrapper.getUUID(player);