Skip to content

Commit

Permalink
Teleport based on protection center and world height.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 27, 2021
1 parent c6e2a9b commit 4b9b677
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Location getTo(PlayerEntityPortalEvent e, Environment env, World toWorld) {
// Find the maximum x and z corner
for (; (i < x + 5) && e.getWorld().getBlockAt(i, k, z).getType().equals(Material.END_PORTAL); i++);
for (; (j < z + 5) && e.getWorld().getBlockAt(x, k, j).getType().equals(Material.END_PORTAL); j++);

// Mojang end platform generation is:
// AIR
// AIR
Expand Down Expand Up @@ -422,8 +422,8 @@ else if (e.getEntity() instanceof Player){
void setSeachRadius(PlayerEntityPortalEvent e, Island i) {
if (!i.onIsland(e.getFrom())) return;
// Find max x or max z
int x = Math.abs(i.getCenter().getBlockX() - e.getFrom().getBlockX());
int z = Math.abs(i.getCenter().getBlockZ() - e.getFrom().getBlockZ());
int x = Math.abs(i.getProtectionCenter().getBlockX() - e.getFrom().getBlockX());
int z = Math.abs(i.getProtectionCenter().getBlockZ() - e.getFrom().getBlockZ());
int diff = i.getProtectionRange() - Math.max(x, z);
if (diff > 0 && diff < 128) {
e.setSearchRadius(diff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class SafeSpotTeleport {
private static final int MAX_CHUNKS = 6;
private static final long SPEED = 1;
private static final int MAX_RADIUS = 50;
private static final int MAX_HEIGHT = 235;
private boolean notChecking;
private BukkitTask task;

Expand All @@ -52,6 +51,7 @@ public class SafeSpotTeleport {
private final Runnable runnable;
private final CompletableFuture<Boolean> result;
private final String homeName;
private final int maxHeight;

/**
* Teleports and entity to a safe spot on island
Expand All @@ -66,12 +66,13 @@ public class SafeSpotTeleport {
this.homeName = builder.getHomeName();
this.runnable = builder.getRunnable();
this.result = builder.getResult();

// If there is no portal scan required, try the desired location immediately
Util.getChunkAtAsync(location).thenRun(()-> tryTogo(builder.getFailureMessage()));
this.maxHeight = location.getWorld().getMaxHeight() - 20;
BentoBox.getInstance().logDebug("World = " + location.getWorld().getName() + " " + maxHeight);
// Try to go
Util.getChunkAtAsync(location).thenRun(()-> tryToGo(builder.getFailureMessage()));
}

private void tryTogo(String failureMessage) {
private void tryToGo(String failureMessage) {
if (plugin.getIslands().isSafeLocation(location)) {
if (portal) {
// If the desired location is safe, then that's where you'll go if there's no portal
Expand Down Expand Up @@ -226,7 +227,7 @@ private boolean scanChunk(ChunkSnapshot chunk) {
for (int x = 0; x< 16; x++) {
for (int z = 0; z < 16; z++) {
// Work down from the entry point up
for (int y = Math.min(chunk.getHighestBlockYAt(x, z), MAX_HEIGHT); y >= 0; y--) {
for (int y = Math.min(chunk.getHighestBlockYAt(x, z), maxHeight); y >= 0; y--) {
if (checkBlock(chunk, x,y,z)) {
return true;
}
Expand Down Expand Up @@ -265,8 +266,8 @@ private void teleportEntity(final Location loc) {
boolean checkBlock(ChunkSnapshot chunk, int x, int y, int z) {
World world = location.getWorld();
Material type = chunk.getBlockType(x, y, z);
Material space1 = chunk.getBlockType(x, Math.min(y + 1, SafeSpotTeleport.MAX_HEIGHT), z);
Material space2 = chunk.getBlockType(x, Math.min(y + 2, SafeSpotTeleport.MAX_HEIGHT), z);
Material space1 = chunk.getBlockType(x, Math.min(y + 1, maxHeight), z);
Material space2 = chunk.getBlockType(x, Math.min(y + 2, maxHeight), z);
if (space1.equals(Material.NETHER_PORTAL) || space2.equals(Material.NETHER_PORTAL)) {
// A portal has been found, switch to non-portal mode now
portal = false;
Expand Down

0 comments on commit 4b9b677

Please sign in to comment.