Fixed portals. Still has debug in it.

Need to add nether->overworld portal
This commit is contained in:
Tastybento 2018-02-11 09:28:52 -08:00
parent f2f8917bf8
commit b9c262fd5c
6 changed files with 101 additions and 55 deletions

View File

@ -26,6 +26,7 @@ general:
offline-player: "&cThat player is offline or doesn't exist."
unknown-player: "&cUnknown player!"
general: "&cThat command is not ready yet - contact admin"
warp-not-safe: "&cThat warp is not safe right now!"
commands:
help:

View File

@ -49,6 +49,7 @@ public class NetherPortals implements Listener {
Vector p = location.toVector().multiply(new Vector(1, 0, 1));
Vector spawn = location.getWorld().getSpawnLocation().toVector().multiply(new Vector(1, 0, 1));
if (spawn.distanceSquared(p) < (plugin.getSettings().getNetherSpawnRadius() * plugin.getSettings().getNetherSpawnRadius())) {
plugin.getLogger().info("not away from spawn");
return false;
} else {
return true;
@ -56,6 +57,7 @@ public class NetherPortals implements Listener {
}
private boolean inWorlds(Location from) {
plugin.getLogger().info("In world = " + (from.getWorld().equals(world) || from.getWorld().equals(nether) || from.getWorld().equals(the_end)));
return (from.getWorld().equals(world) || from.getWorld().equals(nether) || from.getWorld().equals(the_end)) ? true : false;
}
@ -64,8 +66,10 @@ public class NetherPortals implements Listener {
|| (!player.getWorld().equals(nether) && !player.getWorld().equals(the_end))
|| (player.getWorld().equals(nether) && plugin.getSettings().isNetherIslands())
|| (player.getWorld().equals(the_end) && plugin.getSettings().isEndIslands())) {
plugin.getLogger().info("No legacy nether or end");
return true;
}
plugin.getLogger().info("Action!");
return false;
}
@ -105,6 +109,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEndIslandPortal(PlayerPortalEvent event) {
plugin.getLogger().info("End portal event Is end generated? " + plugin.getSettings().isEndGenerate());
if (!event.getCause().equals(TeleportCause.END_PORTAL) || !plugin.getSettings().isEndGenerate()) {
return;
}
@ -112,15 +117,19 @@ public class NetherPortals implements Listener {
return;
}
// If entering a portal in the end, teleport home if you have one, else do nothing
if (event.getFrom().getWorld().equals(the_end)) {
if (event.getFrom().getWorld().equals(the_end)) {
plugin.getLogger().info("In end world");
if (plugin.getIslands().hasIsland(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
plugin.getIslands().homeTeleport(event.getPlayer());
}
return;
}
plugin.getLogger().info("In other world going through end portal");
// If this is island end, then go to the same location, otherwise try spawn
Location to = plugin.getSettings().isEndIslands() ? event.getFrom().toVector().toLocation(the_end) : the_end.getSpawnLocation();
// Else other worlds teleport to the end
event.setCancelled(true);
new SafeTeleportBuilder(plugin)
.entity(event.getPlayer())
.location(to)
@ -136,6 +145,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEntityPortal(EntityPortalEvent event) {
plugin.getLogger().info(event.getEventName());
if (inWorlds(event.getFrom())) {
// Disable entity portal transfer due to dupe glitching
event.setCancelled(true);
@ -177,6 +187,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onNetherPortal(PlayerPortalEvent event) {
plugin.getLogger().info(event.getEventName() + " " + event.getCause() + " nether");
if (!event.getCause().equals(TeleportCause.NETHER_PORTAL)) {
return;
}
@ -185,13 +196,19 @@ public class NetherPortals implements Listener {
}
// If entering a portal in the nether or end, teleport home if you have one, else do nothing
if (!event.getFrom().getWorld().equals(world)) {
plugin.getLogger().info("Entered portal in nether or end");
if (plugin.getIslands().hasIsland(event.getPlayer().getUniqueId())) {
plugin.getLogger().info("player has island - teleporting home");
event.setCancelled(true);
plugin.getIslands().homeTeleport(event.getPlayer());
}
return;
}
plugin.getLogger().info("Entering nether portal in overworld");
// If this is island nether, then go to the same vector, otherwise try spawn
Location to = plugin.getSettings().isNetherIslands() ? event.getFrom().toVector().toLocation(nether) : nether.getSpawnLocation();
plugin.getLogger().info("Going to " + to);
event.setCancelled(true);
// Else other worlds teleport to the nether
new SafeTeleportBuilder(plugin)
.entity(event.getPlayer())

View File

@ -135,7 +135,7 @@ public abstract class AbstractFlagListener implements Listener {
}
/**
* Generic place blocks checker
* Generic flag checker
* @param e
* @param loc
* @param breakBlocks

View File

@ -6,6 +6,7 @@ package us.tastybento.bskyblock.listeners.flags;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import us.tastybento.bskyblock.lists.Flags;
@ -18,6 +19,14 @@ public class PortalListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerPortal(PlayerPortalEvent e) {
checkIsland(e, e.getFrom(), Flags.PORTAL);
if (e.getPlayer().isOp()) {
return;
}
if (e.getCause().equals(TeleportCause.NETHER_PORTAL)) {
checkIsland(e, e.getFrom(), Flags.PORTAL);
} else if (e.getCause().equals(TeleportCause.END_PORTAL)) {
// Silent check because it's spammy
checkIsland(e, e.getFrom(), Flags.PORTAL, true);
}
}
}

View File

@ -61,8 +61,10 @@ public class SafeSpotTeleport {
this.portal = portal;
this.homeNumber = homeNumber;
plugin.getLogger().info("Safe teleport called");
// Put player into spectator mode
if (entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SURVIVAL)) {
Bukkit.getLogger().info("Put player into spectator mode");
((Player)entity).setGameMode(GameMode.SPECTATOR);
}
@ -76,8 +78,10 @@ public class SafeSpotTeleport {
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
List<ChunkSnapshot> chunkSnapshot = new ArrayList<>();
if (checking) {
Bukkit.getLogger().info("Checking");
Iterator<Pair<Integer, Integer>> it = chunksToScan.iterator();
if (!it.hasNext()) {
// TODO REMOVE
Bukkit.getLogger().info("Nothing left!");
// Nothing left
tidyUp(entity, failureMessage);
@ -92,6 +96,8 @@ public class SafeSpotTeleport {
// Move to next step
checking = false;
checkChunks(chunkSnapshot);
} else {
Bukkit.getLogger().info("Not checking");
}
}, 0L, SPEED);
}
@ -103,6 +109,7 @@ public class SafeSpotTeleport {
if (portal && bestSpot != null) {
// No portals found, teleport to the best spot we found
teleportEntity(bestSpot);
return;
}
// Failed - no safe spot
if (entity instanceof Player && !failureMessage.isEmpty()) {
@ -157,14 +164,18 @@ public class SafeSpotTeleport {
* @param chunkSnapshot
*/
private void checkChunks(List<ChunkSnapshot> chunkSnapshot) {
Bukkit.getLogger().info("Chunksnapshot = " + chunkSnapshot.size());
// Run async task to scan chunks
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
for (ChunkSnapshot chunk: chunkSnapshot) {
if (scanChunk(chunk)) {
Bukkit.getLogger().info("Safe sport found!");
task.cancel();
return;
}
}
Bukkit.getLogger().info("not found in initial list");
// Nothing happened, change state
checking = true;
});
@ -176,6 +187,7 @@ public class SafeSpotTeleport {
* @return true if a safe spot was found
*/
private boolean scanChunk(ChunkSnapshot chunk) {
Bukkit.getLogger().info("Scanning chunk " + chunk.getX() + " " + chunk.getZ());
// Max height
int maxHeight = location.getWorld().getMaxHeight() - 20;
// Run through the chunk
@ -184,6 +196,7 @@ public class SafeSpotTeleport {
// Work down from the entry point up
for (int y = Math.min(chunk.getHighestBlockYAt(x, z), maxHeight); y >= 0; y--) {
if (checkBlock(chunk, x,y,z, maxHeight)) {
Bukkit.getLogger().info("Check block returned true");
return true;
}
} // end y
@ -196,14 +209,17 @@ public class SafeSpotTeleport {
* Teleports entity to the safe spot
*/
private void teleportEntity(Location loc) {
Bukkit.getLogger().info("Teleporting!");
task.cancel();
// Return to main thread and teleport the player
plugin.getServer().getScheduler().runTask(plugin, () -> {
if (!portal && entity instanceof Player) {
Bukkit.getLogger().info("Setting home");
// Set home
plugin.getPlayers().setHomeLocation(entity.getUniqueId(), loc, homeNumber);
}
Vector velocity = entity.getVelocity();
Bukkit.getLogger().info("Teleported!");
entity.teleport(loc);
// Exit spectator mode if in it
if (entity instanceof Player) {
@ -261,62 +277,65 @@ public class SafeSpotTeleport {
if (!type.equals(Material.AIR)) { // AIR
Material space1 = chunk.getBlockType(x, Math.min(y + 1, worldHeight), z);
Material space2 = chunk.getBlockType(x, Math.min(y + 2, worldHeight), z);
if ((space1.equals(Material.AIR) && space2.equals(Material.AIR))
|| (space1.equals(Material.PORTAL) && space2.equals(Material.PORTAL))
&& (!type.toString().contains("FENCE")
&& !type.toString().contains("DOOR")
&& !type.toString().contains("GATE")
&& !type.toString().contains("PLATE"))) {
switch (type) {
// Unsafe
case ANVIL:
case BARRIER:
case BOAT:
case CACTUS:
case DOUBLE_PLANT:
case ENDER_PORTAL:
case FIRE:
case FLOWER_POT:
case LADDER:
case LAVA:
case LEVER:
case LONG_GRASS:
case PISTON_EXTENSION:
case PISTON_MOVING_PIECE:
case PORTAL:
case SIGN_POST:
case SKULL:
case STANDING_BANNER:
case STATIONARY_LAVA:
case STATIONARY_WATER:
case STONE_BUTTON:
case TORCH:
case TRIPWIRE:
case WATER:
case WEB:
case WOOD_BUTTON:
//Block is dangerous
break;
default:
// Safe
Vector newSpot = new Vector(chunk.getX() * 16 + x + 0.5D, y + 1, chunk.getZ() * 16 + z + 0.5D);
// Check for portal
if (portal) {
if (chunk.getBlockType(x, y, z).equals(Material.PORTAL)) {
if ((space1.equals(Material.AIR) && space2.equals(Material.AIR)) || (space1.equals(Material.PORTAL) && space2.equals(Material.PORTAL))) {
if (!type.toString().contains("FENCE") && !type.toString().contains("DOOR") && !type.toString().contains("GATE") && !type.toString().contains("PLATE")) {
Bukkit.getLogger().info("Checking " + type);
switch (type) {
// Unsafe
case ANVIL:
case BARRIER:
case BOAT:
case CACTUS:
case DOUBLE_PLANT:
case ENDER_PORTAL:
case FIRE:
case FLOWER_POT:
case LADDER:
case LAVA:
case LEVER:
case LONG_GRASS:
case PISTON_EXTENSION:
case PISTON_MOVING_PIECE:
case SIGN_POST:
case SKULL:
case STANDING_BANNER:
case STATIONARY_LAVA:
case STATIONARY_WATER:
case STONE_BUTTON:
case TORCH:
case TRIPWIRE:
case WATER:
case WEB:
case WOOD_BUTTON:
//Block is dangerous
break;
case PORTAL:
if (portal) {
Bukkit.getLogger().info("Portal found");
Vector newSpot = new Vector(chunk.getX() * 16 + x + 0.5D, y + 1, chunk.getZ() * 16 + z + 0.5D);
// Teleport as soon as we find a portal
teleportEntity(newSpot.toLocation(world));
return true;
} else if (bestSpot == null) {
// Stash the best spot
bestSpot = newSpot.toLocation(world);
return false;
}
} else {
// Regular search - teleport as soon as we find something
teleportEntity(newSpot.toLocation(world));
return true;
break;
default:
// Safe
Vector newSpot = new Vector(chunk.getX() * 16 + x + 0.5D, y + 1, chunk.getZ() * 16 + z + 0.5D);
// Check for portal
if (portal) {
if (bestSpot == null) {
Bukkit.getLogger().info("Best spot found = " + bestSpot);
// Stash the best spot
bestSpot = newSpot.toLocation(world);
return false;
}
} else {
// Regular search - teleport as soon as we find something
Bukkit.getLogger().info("Safe spot found, teleporting to new spot");
teleportEntity(newSpot.toLocation(world));
return true;
}
}
return true;
}
}
}

View File

@ -12,7 +12,7 @@ public class SafeTeleportBuilder {
private Entity entity;
private int homeNumber = 0;
private boolean portal = false;
private String failureMessage = "";
private String failureMessage = "general.errors.warp-not-safe";
private Location location;