Skip to content

Commit

Permalink
Change the way waypoints are validated/activated. This will allow a w…
Browse files Browse the repository at this point in the history
…aypoint to claim a previously used signature, as long as the old waypoint has been altered (auto-deleting the altered waypoint).

This also fixes a bug where a new waypoint could take a signature from a valid non-altered waypoint.
  • Loading branch information
BLuFeNiX committed Jul 18, 2021
1 parent 23c4f3b commit 4f1d539
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,26 @@ private void handleBookOfEnderAction(Player player, Location blockLocation) {
player.sendMessage(StringResources.WAYPOINT_ALREADY_ACTIVE);
break;
case Waypoint.EXISTS_MODIFIED_CONFLICT:
Log.d("waypoint with this signature already exists, not registering this one");
player.sendMessage(StringResources.WAYPOINT_SIGNATURE_EXISTS);
// the player has modified a waypoint, so, let's remove the existing record from the DB
waypointDB.removeWaypointByLocation(waypoint.loc);
// the rest of this case is now the same as NOT_EXISTS_CONFLICT,
// so fall through without a break
case Waypoint.NOT_EXISTS_CONFLICT:
Waypoint conflictFromDB = waypointDB.getWaypointFromSignature(waypoint.sig);
Waypoint conflictInWorld = Waypoint.fromLocation(conflictFromDB.loc);
if (conflictInWorld.status == Waypoint.EXISTS_VERIFIED) {
// conflicting waypoint still exists and has not been modified
Log.d("waypoint with this signature already exists, not registering this one");
player.sendMessage(StringResources.WAYPOINT_SIGNATURE_EXISTS);
} else {
Log.d("conflicting waypoint was altered, so removing that one and registering this one");
waypointDB.removeWaypointBySignature(conflictFromDB.sig);
waypointDB.addWaypoint(waypoint);
player.sendMessage(StringResources.WAYPOINT_ACTIVATED);
if (Config.consumeBook) {
player.getInventory().getItemInMainHand().setAmount(0);
}
}
break;
case Waypoint.EXISTS_MODIFIED:
// no conflict with new signature
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/net/blufenix/teleportationrunes/Waypoint.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.blufenix.teleportationrunes;

import net.blufenix.common.Log;
import org.bukkit.Location;

import java.util.Arrays;

/**
* Created by blufenix on 7/29/15.
*/
Expand All @@ -11,6 +14,7 @@ public class Waypoint {
public static final int EXISTS_MODIFIED = 1;
public static final int EXISTS_VERIFIED = 2;
public static final int EXISTS_MODIFIED_CONFLICT = 3;
public static final int NOT_EXISTS_CONFLICT = 4;

public final Location loc;
public final Signature sig;
Expand Down Expand Up @@ -42,23 +46,31 @@ public static Waypoint fromLocation(Location loc) {

WaypointDB db = TeleportationRunes.getInstance().getWaypointDB();

Signature expectedSignature = Signature.fromLocation(loc, Config.waypointBlueprint.atRotation(rotation));
Waypoint waypoint = db.getWaypointFromLocation(loc);
Signature sigAtLoc = Signature.fromLocation(loc, Config.waypointBlueprint.atRotation(rotation));
Waypoint waypointFromDBLoc = db.getWaypointFromLocation(loc);
Waypoint ret = new Waypoint(loc, sigAtLoc);

if (waypoint != null) {
if (waypointFromDBLoc != null) {
// we know the location matches, so if the signature matches as well, we are verified
if (waypoint.sig.equals(expectedSignature)) {
waypoint.status = EXISTS_VERIFIED;
} else if (!db.getWaypointFromSignature(waypoint.sig).loc.equals(waypoint.loc)) {
if (waypointFromDBLoc.sig.equals(sigAtLoc)) {
ret.status = EXISTS_VERIFIED;
} else if (db.getWaypointFromSignature(sigAtLoc) != null) {
// another waypoint exists in the DB with the same signature
waypoint.status = EXISTS_MODIFIED_CONFLICT;
ret.status = EXISTS_MODIFIED_CONFLICT;
} else {
waypoint = new Waypoint(waypoint.loc, expectedSignature, EXISTS_MODIFIED);
ret.status = EXISTS_MODIFIED;
}
} else {
waypoint = new Waypoint(loc, expectedSignature, NOT_EXISTS);
// no waypoint has been registered at this location,
// but we need to see if the desired signature is available
if (db.getWaypointFromSignature(sigAtLoc) == null) {
// signature not used
ret.status = NOT_EXISTS;
} else {
ret.status = NOT_EXISTS_CONFLICT;
}
}

return waypoint;
return ret;
}
}

0 comments on commit 4f1d539

Please sign in to comment.