Fixed plot merging and made some optimizations to move events and plot lookups

This commit is contained in:
boy0001 2015-02-28 11:55:52 +11:00
parent 03dcca451c
commit a43cf8833c
3 changed files with 47 additions and 59 deletions

View File

@ -34,39 +34,39 @@ public abstract class SquarePlotManager extends GridPlotManager {
@Override @Override
public PlotId getPlotIdAbs(final PlotWorld plotworld, int x, final int y, int z) { public PlotId getPlotIdAbs(final PlotWorld plotworld, int x, final int y, int z) {
final SquarePlotWorld dpw = ((SquarePlotWorld) plotworld); final SquarePlotWorld dpw = ((SquarePlotWorld) plotworld);
// get plot size
final int size = dpw.PLOT_WIDTH + dpw.ROAD_WIDTH;
// get size of path on bottom part, and top part of plot
// (As 0,0 is in the middle of a road, not the very start)
int pathWidthLower; int pathWidthLower;
if ((dpw.ROAD_WIDTH % 2) == 0) { if ((dpw.ROAD_WIDTH % 2) == 0) {
pathWidthLower = (int) (Math.floor(dpw.ROAD_WIDTH / 2) - 1); pathWidthLower = (dpw.ROAD_WIDTH / 2) - 1;
} else { } else {
pathWidthLower = (int) Math.floor(dpw.ROAD_WIDTH / 2); pathWidthLower = dpw.ROAD_WIDTH / 2;
} }
// calulating how many shifts need to be done final int size = dpw.PLOT_WIDTH + dpw.ROAD_WIDTH;
int dx = x / size; int idx;
int dz = z / size; int idz;
if (x < 0) { if (x < 0) {
dx--; idx = (x/size);
x += ((-dx) * size); x = size + (x % size);
}
else {
idx = (x/size) + 1;
x = (x % size);
} }
if (z < 0) { if (z < 0) {
dz--; idz = (z/size);
z += ((-dz) * size); z = size + (z % size);
}
else {
idz = (z/size) + 1;
z = (z % size);
} }
// reducing to first plot
final int rx = (x) % size;
final int rz = (z) % size;
// checking if road (return null if so)
final int end = pathWidthLower + dpw.PLOT_WIDTH; final int end = pathWidthLower + dpw.PLOT_WIDTH;
final boolean northSouth = (rz <= pathWidthLower) || (rz > end); final boolean northSouth = (z <= pathWidthLower) || (z > end);
final boolean eastWest = (rx <= pathWidthLower) || (rx > end); final boolean eastWest = (x <= pathWidthLower) || (x > end);
if (northSouth || eastWest) { if (northSouth || eastWest) {
return null; return null;
} }
// returning the plot id (based on the number of shifts required) return new PlotId(idx, idz);
return new PlotId(dx + 1, dz + 1);
} }
@Override @Override
@ -78,28 +78,37 @@ public abstract class SquarePlotManager extends GridPlotManager {
final int size = dpw.PLOT_WIDTH + dpw.ROAD_WIDTH; final int size = dpw.PLOT_WIDTH + dpw.ROAD_WIDTH;
int pathWidthLower; int pathWidthLower;
if ((dpw.ROAD_WIDTH % 2) == 0) { if ((dpw.ROAD_WIDTH % 2) == 0) {
pathWidthLower = (int) (Math.floor(dpw.ROAD_WIDTH / 2) - 1); pathWidthLower = (dpw.ROAD_WIDTH / 2) - 1;
} else { } else {
pathWidthLower = (int) Math.floor(dpw.ROAD_WIDTH / 2); pathWidthLower = dpw.ROAD_WIDTH / 2;
} }
int dx = x / size; int dx;
int dz = z / size; int dz;
int rx;
int rz;
if (x < 0) { if (x < 0) {
dx--; dx = (x/size);
x += ((-dx) * size); rx = size + (x % size);
}
else {
dx = (x/size) + 1;
rx = (x % size);
} }
if (z < 0) { if (z < 0) {
dz--; dz = (z/size);
z += ((-dz) * size); rz = size + (z % size);
}
else {
dz = (z/size) + 1;
rz = (z % size);
} }
final int rx = (x) % size;
final int rz = (z) % size;
final int end = pathWidthLower + dpw.PLOT_WIDTH; final int end = pathWidthLower + dpw.PLOT_WIDTH;
final boolean northSouth = (rz <= pathWidthLower) || (rz > end); final boolean northSouth = (rz <= pathWidthLower) || (rz > end);
final boolean eastWest = (rx <= pathWidthLower) || (rx > end); final boolean eastWest = (rx <= pathWidthLower) || (rx > end);
if (northSouth && eastWest) { if (northSouth && eastWest) {
// This means you are in the intersection // This means you are in the intersection
final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, y, z + dpw.ROAD_WIDTH); final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, 0, z + dpw.ROAD_WIDTH);
final PlotId id = MainUtil.getPlotAbs(loc); final PlotId id = MainUtil.getPlotAbs(loc);
final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id); final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id);
if (plot == null) { if (plot == null) {
@ -112,7 +121,7 @@ public abstract class SquarePlotManager extends GridPlotManager {
} }
if (northSouth) { if (northSouth) {
// You are on a road running West to East (yeah, I named the var poorly) // You are on a road running West to East (yeah, I named the var poorly)
final Location loc = new Location(plotworld.worldname, x, y, z + dpw.ROAD_WIDTH); final Location loc = new Location(plotworld.worldname, x, 0, z + dpw.ROAD_WIDTH);
final PlotId id = MainUtil.getPlotAbs(loc); final PlotId id = MainUtil.getPlotAbs(loc);
final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id); final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id);
if (plot == null) { if (plot == null) {
@ -125,7 +134,7 @@ public abstract class SquarePlotManager extends GridPlotManager {
} }
if (eastWest) { if (eastWest) {
// This is the road separating an Eastern and Western plot // This is the road separating an Eastern and Western plot
final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, y, z); final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, 0, z);
final PlotId id = MainUtil.getPlotAbs(loc); final PlotId id = MainUtil.getPlotAbs(loc);
final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id); final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id);
if (plot == null) { if (plot == null) {
@ -136,7 +145,7 @@ public abstract class SquarePlotManager extends GridPlotManager {
} }
return null; return null;
} }
final PlotId id = new PlotId(dx + 1, dz + 1); final PlotId id = new PlotId(dx, dz);
final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id); final Plot plot = PlotSquared.getPlots(plotworld.worldname).get(id);
if (plot == null) { if (plot == null) {
return id; return id;

View File

@ -28,14 +28,13 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent;
import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions; import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions;
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
@ -97,11 +96,11 @@ public class ForceFieldListener implements Listener {
} }
@EventHandler @EventHandler
public void onPlotEntry(final PlayerMoveEvent event) { public void onPlotEntry(final PlayerEnterPlotEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
final PlotPlayer pp = BukkitUtil.getPlayer(player); final PlotPlayer pp = BukkitUtil.getPlayer(player);
final Location loc = pp.getLocation(); final Location loc = pp.getLocation();
final Plot plot = MainUtil.getPlot(loc); final Plot plot = event.getPlot();
if (plot == null) { if (plot == null) {
return; return;
} }

View File

@ -1,23 +1,3 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// 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.listeners; package com.intellectualcrafters.plot.listeners;
import java.util.Collection; import java.util.Collection;