Made getCurrentPlot(location) 23.07 times faster

I apologize if you wrote the code, but it was REALLY poorly done (brute
force is bad btw). Was it taken from PlotMe?
This commit is contained in:
boy0001 2014-09-25 21:25:53 +10:00
parent 3f4c275d64
commit d54336f536
11 changed files with 319 additions and 74 deletions

View File

@ -62,44 +62,106 @@ public class PlayerFunctions {
* @return
*/
public static PlotId getPlot(Location loc) {
int valx = loc.getBlockX();
int valz = loc.getBlockZ();
int x = loc.getBlockX();
int z = loc.getBlockZ();
PlotWorld plotworld = PlotMain.getWorldSettings(loc.getWorld());
int size = plotworld.PLOT_WIDTH + plotworld.ROAD_WIDTH;
int pathsize = plotworld.ROAD_WIDTH;
int pathWidthLower;
if ((plotworld.ROAD_WIDTH % 2) == 0) {
pathWidthLower = (int) (Math.floor(plotworld.ROAD_WIDTH / 2)-1);
}
else {
pathWidthLower = (int) Math.floor(plotworld.ROAD_WIDTH / 2);
}
int dx = x/size;
int dz = z/size;
boolean road = false;
double n3;
int mod2 = 0;
int mod1 = 1;
int x = (int) Math.ceil((double) valx / size);
int z = (int) Math.ceil((double) valz / size);
if ((pathsize % 2) == 1) {
n3 = Math.ceil(((double) pathsize) / 2);
mod2 = -1;
} else {
n3 = Math.floor(((double) pathsize) / 2);
if (x<0) {
dx--;
x+=((-dx) * size);
}
if (z<0) {
dz--;
z+=((-dz) * size);
}
for (double i = n3; i >= 0; i--) {
if (((((valx - i) + mod1) % size) == 0) || (((valx + i + mod2) % size) == 0)) {
road = true;
x = (int) Math.ceil((valx - n3) / size);
}
if (((((valz - i) + mod1) % size) == 0) || (((valz + i + mod2) % size) == 0)) {
road = true;
z = (int) Math.ceil((valz - n3) / size);
}
}
if (road) {
int rx = (x)%size;
int rz = (z)%size;
int end = pathWidthLower+plotworld.PLOT_WIDTH;
if (rx<=pathWidthLower) {
// west > return null for now
return null;
} else {
return new PlotId(x, z);
}
if (rx>end) {
// east > return null for now
return null;
}
if (rz<=pathWidthLower) {
// north > return null for now
return null;
}
if (rz>pathWidthLower+plotworld.PLOT_WIDTH) {
// south > return null for now
return null;
}
return new PlotId(dx,dz);
//
// double n3;
//
// int mod2 = 0;
// int mod1 = 1;
//
// int x = (int) Math.ceil((double) valx / size);
// int z = (int) Math.ceil((double) valz / size);
//
// if ((pathsize % 2) == 1) {
// n3 = Math.ceil(((double) pathsize) / 2);
// mod2 = -1;
// } else {
// n3 = Math.floor(((double) pathsize) / 2);
// }
//
// /*
// * If Road 1 + Road 2 are true, then it is in the middle between 4 plots and more checks might be required.
// */
// boolean road1 = false, road2 = false;
//
// for (double i = n3; i >= 0; i--) {
// if (((((valx - i) + mod1) % size) == 0) || (((valx + i + mod2) % size) == 0)) {
//
// /*
// * Road 1
// */
//
// road1 = true;
// x = (int) Math.ceil((valx - n3) / size);
// }
// if (((((valz - i) + mod1) % size) == 0) || (((valz + i + mod2) % size) == 0)) {
// /*
// * Road 2
// */
//
// road2 = true;
// z = (int) Math.ceil((valz - n3) / size);
// }
// }
// if (road1 && road2) {
// return null;
// }
// else if (road1) {
// return null;
// }
// else if (road2) {
// return null;
// }
// else {
// return new PlotId(x, z);
// }
}
/**

View File

@ -102,8 +102,9 @@ public class Plot implements Cloneable {
* @param denied
* @param changeTime
* @param time
* @param merged
*/
public Plot(PlotId id, UUID owner, Biome plotBiome, ArrayList<UUID> helpers, ArrayList<UUID> denied, boolean changeTime, long time, boolean rain, String alias, PlotHomePosition position, Flag[] flags, String world) {
public Plot(PlotId id, UUID owner, Biome plotBiome, ArrayList<UUID> helpers, ArrayList<UUID> denied, boolean changeTime, long time, boolean rain, String alias, PlotHomePosition position, Flag[] flags, String world, boolean[] merged) {
this.id = id;
this.settings = new PlotSettings(this);
this.settings.setBiome(plotBiome);
@ -116,6 +117,7 @@ public class Plot implements Cloneable {
this.settings.setTimeChange(changeTime);
this.settings.setAlias(alias);
this.settings.setPosition(position);
this.settings.setMerged(merged);
this.delete = false;
if (flags != null) {
this.settings.setFlags(flags);

View File

@ -463,6 +463,12 @@ public class PlotHelper {
* @param plot
*/
public static void clear(final Player requester, final Plot plot) {
/*
* TODO unlink any adjacent plots
*/
final long start = System.nanoTime();
final PlotWorld plotworld = PlotMain.getWorldSettings(Bukkit.getWorld(plot.world));
PlotHelper.setBiome(requester.getWorld(), plot, Biome.FOREST);

View File

@ -730,6 +730,9 @@ public class PlotMain extends JavaPlugin {
if (!PlayerEvents.isInPlot(this.location)) {
boolean tamed = false;
if (Settings.MOB_PATHFINDING) {
// TODO make this more efficient
if (entity instanceof Tameable) {
Tameable tameable = (Tameable) entity;
if (tameable.isTamed()) {

View File

@ -15,6 +15,8 @@ import java.util.Set;
import org.bukkit.block.Biome;
import com.intellectualcrafters.plot.RUtils.Direction;
/**
* plot settings
*
@ -22,7 +24,13 @@ import org.bukkit.block.Biome;
*
*/
public class PlotSettings {
/**
* merged plots
*/
private boolean[] merged = new boolean[] {false,false,false,false}; // 1111
/**
* plot alias
*/
private String alias;
/**
* plot biome
@ -54,7 +62,27 @@ public class PlotSettings {
public PlotSettings(Plot plot) {
this.alias = "";
}
/**
* <b>Check if the plot is merged in a direction</b><br>
* 0 = North<br>
* 1 = East<br>
* 2 = South<br>
* 3 = West<br>
* @param direction
* @return boolean
*/
public boolean getMerged(int direction) {
return merged[direction];
}
public boolean[] getMerged() {
return this.merged;
}
public void setMerged(boolean[] merged) {
this.merged = merged;
}
/**
*
* @param b

View File

@ -207,9 +207,13 @@ public class WorldGenerator extends ChunkGenerator {
int maxY = world.getMaxHeight();
this.result = new short[maxY / 16][];
double pathWidthLower;
pathWidthLower = Math.floor(this.pathsize / 2);
if ((pathsize % 2) == 0) {
pathWidthLower = Math.floor(this.pathsize / 2)-1;
}
else {
pathWidthLower = Math.floor(this.pathsize / 2);
}
final int prime = 31;
int h = 1;
h = (prime * h) + cx;

View File

@ -125,7 +125,12 @@ public class XPopulator extends BlockPopulator {
this.f_id[i] = result[0];
this.f_v[i] = result[1];
}
this.pathWidthLower = Math.floor(this.pathsize / 2);
if ((pathsize % 2) == 0) {
pathWidthLower = Math.floor(this.pathsize / 2)-1;
}
else {
this.pathWidthLower = Math.floor(this.pathsize / 2);
}
}
@Override

View File

@ -98,49 +98,20 @@ public class DBFunc {
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INT(11) NOT NULL AUTO_INCREMENT," + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)," + " UNIQUE KEY `unique_alias` (`alias`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `merged` INT(11) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)," + " UNIQUE KEY `unique_alias` (`alias`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8");
stmt.addBatch("ALTER TABLE `plot_settings` ADD CONSTRAINT `plot_settings_ibfk_1` FOREIGN KEY (`plot_plot_id`) REFERENCES `plot` (`id`) ON DELETE CASCADE");
} else {
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot` (" + "`id` INTEGER(11) PRIMARY KEY," + "`plot_id_x` INT(11) NOT NULL," + "`plot_id_z` INT(11) NOT NULL," + "`owner` VARCHAR(45) NOT NULL," + "`world` VARCHAR(45) NOT NULL," + "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_denied` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_helpers` (" + "`plot_plot_id` INT(11) NOT NULL," + "`user_uuid` VARCHAR(40) NOT NULL" + ")");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)" + ")");
stmt.addBatch("CREATE TABLE IF NOT EXISTS `plot_settings` (" + " `plot_plot_id` INT(11) NOT NULL," + " `biome` VARCHAR(45) DEFAULT 'FOREST'," + " `rain` INT(1) DEFAULT 0," + " `custom_time` TINYINT(1) DEFAULT '0'," + " `time` INT(11) DEFAULT '8000'," + " `deny_entry` TINYINT(1) DEFAULT '0'," + " `alias` VARCHAR(50) DEFAULT NULL," + " `flags` VARCHAR(512) DEFAULT NULL," + " `merged` INT(11) DEFAULT NULL," + " `position` VARCHAR(50) NOT NULL DEFAULT 'DEFAULT'," + " PRIMARY KEY (`plot_plot_id`)" + ")");
}
stmt.executeBatch();
stmt.clearBatch();
stmt.close();
/**
* Adding missing columns (for older versions) + get current columns
* (continue if they do not match the current number of columns) + get
* data from plot_id column - create column (plot_id_x,plot_id_z,world)
* - populate plot_id_x, plot_id_z with data from plot_id - populate
* world column with PlotMain.config.getString("plot_world") - which
* will be set from previous release;
*/
/**
* `plot`
*/
// int target_len = 6;
// ArrayList<String> ids = new ArrayList<String>();
// stmt = connection.createStatement();
// String table = "plot";
// ResultSet rs = stmt.executeQuery("SELECT * FROM `"+table+"`");
// ResultSetMetaData md = rs.getMetaData();
// int len = md.getColumnCount();
// if (len<target_len) {
// HashSet<String> cols = new HashSet<String>();
// for (int i = 1; i <= len; i++) {
// cols.add(md.getColumnName(i));
// }
// while (rs.next()) {
// ids.add(rs.getString("plot_id"));
// }
// }
// stmt.close();
}
/**
@ -149,7 +120,7 @@ public class DBFunc {
* @param plot
*/
public static void delete(final String world, final Plot plot) {
boolean result = PlotMain.removePlot(world, plot.id, false);
PlotMain.removePlot(world, plot.id, false);
runTask(new Runnable() {
@Override
public void run() {
@ -252,6 +223,7 @@ public class DBFunc {
statement.addBatch("UPDATE `plot` SET\n" + " `plot_id_x` = IF(" + " LOCATE(';', `plot_id`) > 0," + " SUBSTRING(`plot_id`, 1, LOCATE(';', `plot_id`) - 1)," + " `plot_id`" + " )," + " `plot_id_z` = IF(" + " LOCATE(';', `plot_id`) > 0," + " SUBSTRING(`plot_id`, LOCATE(';', `plot_id`) + 1)," + " NULL" + " )");
statement.addBatch("ALTER TABLE `plot` DROP `plot_id`");
statement.addBatch("ALTER IGNORE TABLE `plot_settings` ADD `flags` VARCHAR(512) DEFAULT NULL");
statement.addBatch("ALTER IGNORE TABLE `plot_settings` ADD `merged` int(11) DEFAULT 0");
statement.executeBatch();
statement.close();
}
@ -271,8 +243,6 @@ public class DBFunc {
plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z"));
id = r.getInt("id");
String worldname = r.getString("world");
// Quicker to get cache the UUID to the World than to convert
// each time.
HashMap<String, Object> settings = getSettings(id);
UUID owner = UUID.fromString(r.getString("owner"));
Biome plotBiome = Biome.FOREST;
@ -292,6 +262,8 @@ public class DBFunc {
}
}
ArrayList<UUID> helpers = plotHelpers(id);
ArrayList<UUID> denied = plotDenied(id);
// boolean changeTime = ((Short) settings.get("custom_time") ==
@ -303,7 +275,7 @@ public class DBFunc {
// boolean rain =
// Integer.parseInt(settings.get("rain").toString()) == 1 ? true
// : false;
boolean rain = false;
boolean rain = (int) settings.get("rain") == 1 ? true : false;
String alias = (String) settings.get("alias");
if ((alias == null) || alias.equalsIgnoreCase("NEW")) {
alias = "";
@ -321,8 +293,13 @@ public class DBFunc {
if (position == null) {
position = PlotHomePosition.DEFAULT;
}
p = new Plot(plot_id, owner, plotBiome, helpers, denied, /* changeTime */false, time, rain, alias, position, flags, worldname);
int merged_int = settings.get("merged") == null ? 0 : (int) settings.get("merged");
boolean[] merged = new boolean[7];
for (int i = 6; i >= 0; i--) {
merged[i] = (merged_int & (1 << i)) != 0;
}
p = new Plot(plot_id, owner, plotBiome, helpers, denied, /* changeTime */false, time, rain, alias, position, flags, worldname, merged);
if (plots.containsKey(worldname)) {
plots.get(worldname).put((plot_id), p);
} else {
@ -362,6 +339,29 @@ public class DBFunc {
}
});
}
public static void setMerged(final String world, final Plot plot, final boolean[] merged) {
plot.settings.setMerged(merged);
runTask(new Runnable() {
@Override
public void run() {
try {
int n = 0, l = merged.length;
for (int i = 0; i < l; ++i) {
n = (n << 1) + (merged[i] ? 1 : 0);
}
PreparedStatement stmt = connection.prepareStatement("UPDATE `plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?");
stmt.setInt(1, n);
stmt.setInt(2, getId(world, plot.id));
stmt.execute();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
Logger.add(LogLevel.WARNING, "Could not set merged for plot " + plot.id);
}
}
});
}
public static void setFlags(final String world, final Plot plot, final Flag[] flags) {
plot.settings.setFlags(flags);
@ -485,6 +485,9 @@ public class DBFunc {
var = "flags";
val = r.getObject(var);
h.put(var, val);
var = "merged";
val = r.getObject(var);
h.put(var, val);
}
stmt.close();
;

View File

@ -77,7 +77,7 @@ public class PlotMeConverter {
}
stream.println(eR3040bl230);
PlotId id = new PlotId(Integer.parseInt(plot.id.split(";")[0]), Integer.parseInt(plot.id.split(";")[1]));
com.intellectualcrafters.plot.Plot pl = new com.intellectualcrafters.plot.Plot(id, plot.getOwnerId(), plot.getBiome(), psAdded, psDenied, false, 8000l, false, "", PlotHomePosition.DEFAULT, null, world.getName());
com.intellectualcrafters.plot.Plot pl = new com.intellectualcrafters.plot.Plot(id, plot.getOwnerId(), plot.getBiome(), psAdded, psDenied, false, 8000l, false, "", PlotHomePosition.DEFAULT, null, world.getName(), new boolean[] {false, false, false, false} );
DBFunc.createPlot(pl);
DBFunc.createPlotSettings(DBFunc.getId(world.getName(), pl.id), pl);
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) IntellectualCrafters - 2014.
* You are not allowed to distribute and/or monetize any of our intellectual property.
* IntellectualCrafters is not affiliated with Mojang AB. Minecraft is a trademark of Mojang AB.
*
* >> File = PlayerClaimPlotEvent.java
* >> Generated by: Citymonstret at 2014-08-09 15:21
*/
package com.intellectualcrafters.plot.events;
import java.util.Set;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import com.intellectualcrafters.plot.Plot;
/**
* Created by Citymonstret on 2014-08-09.
*/
public class PlotMergeEvent extends Event implements Cancellable {
private static HandlerList handlers = new HandlerList();
private boolean cancelled;
private Set<Plot> plots;
/**
* PlotMergeEvent: Called when a plot is merged
* @param player
* @param plot
*/
public PlotMergeEvent(Set<Plot> plots) {
this.plots = plots;
}
/**
* Get the plots involved
* @return Plot
*/
public Set<Plot> getPlost() {
return this.plots;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) IntellectualCrafters - 2014.
* You are not allowed to distribute and/or monetize any of our intellectual property.
* IntellectualCrafters is not affiliated with Mojang AB. Minecraft is a trademark of Mojang AB.
*
* >> File = PlayerClaimPlotEvent.java
* >> Generated by: Citymonstret at 2014-08-09 15:21
*/
package com.intellectualcrafters.plot.events;
import java.util.Set;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import com.intellectualcrafters.plot.Plot;
/**
* Created by Citymonstret on 2014-08-09.
*/
public class PlotUnmergeEvent extends Event implements Cancellable {
private static HandlerList handlers = new HandlerList();
private boolean cancelled;
private Set<Plot> plots;
/**
* PlotUnmergeEvent: Called when a plot is unmerged
* @param player
* @param plot
*/
public PlotUnmergeEvent(Set<Plot> plots) {
this.plots = plots;
}
/**
* Get the plots involved
* @return Plot
*/
public Set<Plot> getPlost() {
return this.plots;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
}