Work on directions and merging

This commit is contained in:
MattBDev 2019-08-13 22:01:23 -04:00
parent 56e055c671
commit 3e6b0b8394
4 changed files with 52 additions and 83 deletions

View File

@ -6,8 +6,7 @@ import lombok.Setter;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
/** /**
* Event called when several plots are merged * Event called when several plots are merged
@ -29,8 +28,8 @@ public final class PlotMergeEvent extends PlotEvent implements Cancellable {
* @param dir The direction of the merge * @param dir The direction of the merge
* @param max Max merge size * @param max Max merge size
*/ */
public PlotMergeEvent(@Nonnull final World world, @Nonnull final Plot plot, public PlotMergeEvent(@NotNull final World world, @NotNull final Plot plot,
@Nonnull final int dir, @Nonnull final int max) { @NotNull final int dir, @NotNull final int max) {
super(plot); super(plot);
this.world = world; this.world = world;
this.dir = dir; this.dir = dir;

View File

@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.commands;
import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.object.Direction;
import com.github.intellectualsites.plotsquared.plot.object.Expression; import com.github.intellectualsites.plotsquared.plot.object.Expression;
import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.Plot;
@ -17,6 +18,8 @@ import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import java.util.UUID; import java.util.UUID;
import static com.github.intellectualsites.plotsquared.plot.object.Direction.getFromIndex;
@CommandDeclaration(command = "merge", aliases = "m", @CommandDeclaration(command = "merge", aliases = "m",
description = "Merge the plot you are standing on, with another plot", description = "Merge the plot you are standing on, with another plot",
permission = "plots.merge", usage = "/plot merge <all|n|e|s|w> [removeroads]", permission = "plots.merge", usage = "/plot merge <all|n|e|s|w> [removeroads]",
@ -82,20 +85,20 @@ public class Merge extends SubCommand {
MainUtil.sendMessage(player, Captions.NO_PERMISSION, "plots.merge." + (size + 1)); MainUtil.sendMessage(player, Captions.NO_PERMISSION, "plots.merge." + (size + 1));
return false; return false;
} }
int direction = -1; Direction direction = Direction.ALL;
if (args.length == 0) { if (args.length == 0) {
switch (direction(player.getLocationFull().getYaw())) { switch (direction(player.getLocationFull().getYaw())) {
case "NORTH": case "NORTH":
direction = 0; direction = Direction.NORTH;
break; break;
case "EAST": case "EAST":
direction = 1; direction = Direction.EAST;
break; break;
case "SOUTH": case "SOUTH":
direction = 2; direction = Direction.SOUTH;
break; break;
case "WEST": case "WEST":
direction = 3; direction = Direction.WEST;
break; break;
} }
} else { } else {
@ -110,7 +113,7 @@ public class Merge extends SubCommand {
Captions.PERMISSION_MERGE_KEEP_ROAD.getTranslated()); Captions.PERMISSION_MERGE_KEEP_ROAD.getTranslated());
return true; return true;
} }
if (plot.autoMerge(-1, maxSize, uuid, terrain)) { if (plot.autoMerge(Direction.ALL, maxSize, uuid, terrain)) {
if (EconHandler.manager != null && plotArea.USE_ECONOMY && price > 0d) { if (EconHandler.manager != null && plotArea.USE_ECONOMY && price > 0d) {
EconHandler.manager.withdrawMoney(player, price); EconHandler.manager.withdrawMoney(player, price);
sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price)); sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price));
@ -124,12 +127,12 @@ public class Merge extends SubCommand {
} }
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
if (args[0].equalsIgnoreCase(values[i]) || args[0].equalsIgnoreCase(aliases[i])) { if (args[0].equalsIgnoreCase(values[i]) || args[0].equalsIgnoreCase(aliases[i])) {
direction = i; direction = getFromIndex(i);
break; break;
} }
} }
} }
if (direction == -1) { if (direction == Direction.ALL) {
MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX,
"/plot merge <" + StringMan.join(values, "|") + "> [removeroads]"); "/plot merge <" + StringMan.join(values, "|") + "> [removeroads]");
MainUtil.sendMessage(player, MainUtil.sendMessage(player,
@ -156,7 +159,8 @@ public class Merge extends SubCommand {
return true; return true;
} }
Plot adjacent = plot.getRelative(direction); Plot adjacent = plot.getRelative(direction);
if (adjacent == null || !adjacent.hasOwner() || adjacent.getMerged((direction + 2) % 4) if (adjacent == null || !adjacent.hasOwner() || adjacent
.getMerged((direction.getIndex() + 2) % 4)
|| adjacent.isOwner(uuid)) { || adjacent.isOwner(uuid)) {
MainUtil.sendMessage(player, Captions.NO_AVAILABLE_AUTOMERGE); MainUtil.sendMessage(player, Captions.NO_AVAILABLE_AUTOMERGE);
return false; return false;
@ -173,7 +177,7 @@ public class Merge extends SubCommand {
continue; continue;
} }
isOnline = true; isOnline = true;
final int dir = direction; final Direction dir = direction;
Runnable run = () -> { Runnable run = () -> {
MainUtil.sendMessage(accepter, Captions.MERGE_ACCEPTED); MainUtil.sendMessage(accepter, Captions.MERGE_ACCEPTED);
plot.autoMerge(dir, maxSize - size, owner, terrain); plot.autoMerge(dir, maxSize - size, owner, terrain);

View File

@ -16,6 +16,15 @@ public enum Direction {
this.name = name; this.name = name;
} }
public static Direction getFromIndex(int index) {
for (Direction value : values()) {
if (value.getIndex() == index) {
return value;
}
}
return NORTH;
}
public int getIndex() { public int getIndex() {
return index; return index;
} }

View File

@ -1545,7 +1545,7 @@ public class Plot {
DBFunc.createPlotAndSettings(this, () -> { DBFunc.createPlotAndSettings(this, () -> {
PlotArea plotworld = Plot.this.area; PlotArea plotworld = Plot.this.area;
if (notify && plotworld.AUTO_MERGE) { if (notify && plotworld.AUTO_MERGE) {
Plot.this.autoMerge(-1, Integer.MAX_VALUE, uuid, true); Plot.this.autoMerge(Direction.ALL, Integer.MAX_VALUE, uuid, true);
} }
}); });
return true; return true;
@ -2022,56 +2022,12 @@ public class Plot {
* Sets the raw merge data<br> * Sets the raw merge data<br>
* - Updates DB<br> * - Updates DB<br>
* - Does not modify terrain<br> * - Does not modify terrain<br>
* ----------<br>
* 0 = north<br>
* 1 = east<br>
* 2 = south<br>
* 3 = west<br>
* ----------<br>
*
* @param direction
* @param value
*/
public void setMerged(int direction, boolean value) {
if (this.getSettings().setMerged(direction, value)) {
if (value) {
Plot other = this.getRelative(direction).getBasePlot(false);
if (!other.equals(this.getBasePlot(false))) {
Plot base = other.id.y < this.id.y
|| other.id.y == this.id.y && other.id.x < this.id.x ? other : this.origin;
this.origin.origin = base;
other.origin = base;
this.origin = base;
connected_cache = null;
}
} else {
if (this.origin != null) {
this.origin.origin = null;
this.origin = null;
}
connected_cache = null;
}
DBFunc.setMerged(this, this.getSettings().getMerged());
regions_cache = null;
}
}
/**
* Sets the raw merge data<br>
* - Updates DB<br>
* - Does not modify terrain<br>
* ----------<br>
* 0 = north<br>
* 1 = east<br>
* 2 = south<br>
* 3 = west<br>
* ----------<br>
* *
* @param direction * @param direction
* @param value * @param value
*/ */
public void setMerged(Direction direction, boolean value) { public void setMerged(Direction direction, boolean value) {
if (this.getSettings().setMerged(direction.getIndex(), value)) { if (this.getSettings().setMerged(direction, value)) {
if (value) { if (value) {
Plot other = this.getRelative(direction).getBasePlot(false); Plot other = this.getRelative(direction).getBasePlot(false);
if (!other.equals(this.getBasePlot(false))) { if (!other.equals(this.getBasePlot(false))) {
@ -2249,24 +2205,21 @@ public class Plot {
} }
/** /**
* Auto merge a plot in a specific direction<br> * Auto merge a plot in a specific direction.
* *
* @param dir The direction to merge<br> * @param dir the direction to merge
* -1 = All directions<br> * @param max the max number of merges to do
* 0 = north<br> * @param uuid the UUID it is allowed to merge with
* 1 = east<br>
* 2 = south<br>
* 3 = west<br>
* @param max The max number of merges to do
* @param uuid The UUID it is allowed to merge with
* @param removeRoads whether to remove roads * @param removeRoads whether to remove roads
* @return true if a merge takes place * @return true if a merge takes place
*/ */
public boolean autoMerge(int dir, int max, UUID uuid, boolean removeRoads) { public boolean autoMerge(Direction dir, int max, UUID uuid, boolean removeRoads) {
//Ignore merging if there is no owner for the plot
if (this.owner == null) { if (this.owner == null) {
return false; return false;
} }
if (!EventUtil.manager.callMerge(this, dir, max)) { //Call the merge event
if (!EventUtil.manager.callMerge(this, dir.getIndex(), max)) {
return false; return false;
} }
Set<Plot> connected = this.getConnectedPlots(); Set<Plot> connected = this.getConnectedPlots();
@ -2282,7 +2235,7 @@ public class Plot {
} }
visited.add(current); visited.add(current);
Set<Plot> plots; Set<Plot> plots;
if ((dir == -1 || dir == 0) && !getMerged(Direction.NORTH)) { if ((dir == Direction.ALL || dir == Direction.NORTH) && !getMerged(Direction.NORTH)) {
Plot other = current.getRelative(Direction.NORTH); Plot other = current.getRelative(Direction.NORTH);
if (other != null && other.isOwner(uuid) && ( if (other != null && other.isOwner(uuid) && (
other.getBasePlot(false).equals(current.getBasePlot(false)) other.getBasePlot(false).equals(current.getBasePlot(false))
@ -2301,7 +2254,8 @@ public class Plot {
} }
} }
} }
if (max >= 0 && (dir == -1 || dir == 1) && !current.getMerged(Direction.EAST)) { if (max >= 0 && (dir == Direction.ALL || dir == Direction.EAST) && !current
.getMerged(Direction.EAST)) {
Plot other = current.getRelative(Direction.EAST); Plot other = current.getRelative(Direction.EAST);
if (other != null && other.isOwner(uuid) && ( if (other != null && other.isOwner(uuid) && (
other.getBasePlot(false).equals(current.getBasePlot(false)) other.getBasePlot(false).equals(current.getBasePlot(false))
@ -2320,7 +2274,8 @@ public class Plot {
} }
} }
} }
if (max >= 0 && (dir == -1 || dir == 2) && !getMerged(Direction.SOUTH)) { if (max >= 0 && (dir == Direction.ALL || dir == Direction.SOUTH) && !getMerged(
Direction.SOUTH)) {
Plot other = current.getRelative(Direction.SOUTH); Plot other = current.getRelative(Direction.SOUTH);
if (other != null && other.isOwner(uuid) && ( if (other != null && other.isOwner(uuid) && (
other.getBasePlot(false).equals(current.getBasePlot(false)) other.getBasePlot(false).equals(current.getBasePlot(false))
@ -2339,7 +2294,8 @@ public class Plot {
} }
} }
} }
if (max >= 0 && (dir == -1 || dir == 3) && !getMerged(Direction.WEST)) { if (max >= 0 && (dir == Direction.ALL || dir == Direction.WEST) && !getMerged(
Direction.WEST)) {
Plot other = current.getRelative(Direction.WEST); Plot other = current.getRelative(Direction.WEST);
if (other != null && other.isOwner(uuid) && ( if (other != null && other.isOwner(uuid) && (
other.getBasePlot(false).equals(current.getBasePlot(false)) other.getBasePlot(false).equals(current.getBasePlot(false))
@ -2502,10 +2458,10 @@ public class Plot {
// invalid merge // invalid merge
PlotSquared.debug("Fixing invalid merge: " + this); PlotSquared.debug("Fixing invalid merge: " + this);
if (tmp.isOwnerAbs(this.owner)) { if (tmp.isOwnerAbs(this.owner)) {
tmp.getSettings().setMerged(2, true); tmp.getSettings().setMerged(Direction.SOUTH, true);
DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); DBFunc.setMerged(tmp, tmp.getSettings().getMerged());
} else { } else {
this.getSettings().setMerged(0, false); this.getSettings().setMerged(Direction.NORTH, false);
DBFunc.setMerged(this, this.getSettings().getMerged()); DBFunc.setMerged(this, this.getSettings().getMerged());
} }
} }
@ -2518,10 +2474,10 @@ public class Plot {
// invalid merge // invalid merge
PlotSquared.debug("Fixing invalid merge: " + this); PlotSquared.debug("Fixing invalid merge: " + this);
if (tmp.isOwnerAbs(this.owner)) { if (tmp.isOwnerAbs(this.owner)) {
tmp.getSettings().setMerged(3, true); tmp.getSettings().setMerged(Direction.WEST, true);
DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); DBFunc.setMerged(tmp, tmp.getSettings().getMerged());
} else { } else {
this.getSettings().setMerged(1, false); this.getSettings().setMerged(Direction.EAST, false);
DBFunc.setMerged(this, this.getSettings().getMerged()); DBFunc.setMerged(this, this.getSettings().getMerged());
} }
} }
@ -2534,10 +2490,10 @@ public class Plot {
// invalid merge // invalid merge
PlotSquared.debug("Fixing invalid merge: " + this); PlotSquared.debug("Fixing invalid merge: " + this);
if (tmp.isOwnerAbs(this.owner)) { if (tmp.isOwnerAbs(this.owner)) {
tmp.getSettings().setMerged(0, true); tmp.getSettings().setMerged(Direction.NORTH, true);
DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); DBFunc.setMerged(tmp, tmp.getSettings().getMerged());
} else { } else {
this.getSettings().setMerged(2, false); this.getSettings().setMerged(Direction.SOUTH, false);
DBFunc.setMerged(this, this.getSettings().getMerged()); DBFunc.setMerged(this, this.getSettings().getMerged());
} }
} }
@ -2550,10 +2506,10 @@ public class Plot {
// invalid merge // invalid merge
PlotSquared.debug("Fixing invalid merge: " + this); PlotSquared.debug("Fixing invalid merge: " + this);
if (tmp.isOwnerAbs(this.owner)) { if (tmp.isOwnerAbs(this.owner)) {
tmp.getSettings().setMerged(1, true); tmp.getSettings().setMerged(Direction.EAST, true);
DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); DBFunc.setMerged(tmp, tmp.getSettings().getMerged());
} else { } else {
this.getSettings().setMerged(3, false); this.getSettings().setMerged(Direction.WEST, false);
DBFunc.setMerged(this, this.getSettings().getMerged()); DBFunc.setMerged(this, this.getSettings().getMerged());
} }
} }
@ -2951,6 +2907,7 @@ public class Plot {
} }
} }
} }
} }
/** /**