From 2c99b3a9e47e00e3486fb6e30d283b1185fd6c5f Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Mon, 5 Jan 2015 21:05:05 -0800 Subject: [PATCH] Nether portal fixes and perf optimizations. Not checking when it's safe to not check. Checking maximum one location. Unfortunately, asking the portal travel agent to find any possible existing portal seems both expensive, and unavoidable in the common case. --- .../GriefPrevention/PlayerEventHandler.java | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java index 544a766..44c95f1 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java @@ -34,6 +34,7 @@ import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.TravelAgent; import org.bukkit.World.Environment; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -776,28 +777,43 @@ class PlayerEventHandler implements Listener if(!GriefPrevention.instance.claimsEnabledForWorld(event.getTo().getWorld())) return; Player player = event.getPlayer(); - - //FEATURE: when players get trapped in a nether portal, send them back through to the other side + if(event.getCause() == TeleportCause.NETHER_PORTAL) { + //FEATURE: when players get trapped in a nether portal, send them back through to the other side CheckForPortalTrapTask task = new CheckForPortalTrapTask(player, event.getFrom()); GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 100L); - } - //FEATURE: if the player teleporting doesn't have permission to build a nether portal and none already exists at the destination, cancel the teleportation - Location existingPortalLocation = event.getPortalTravelAgent().findPortal(event.getTo()); - boolean creatingPortal = event.getPortalTravelAgent().getCanCreatePortal() && (existingPortalLocation == null); - - //if creating a new portal - if(creatingPortal) - { - //and it goes to a land claim where the player doesn't have permission to build - Claim claim = this.dataStore.getClaimAt(event.getTo(), false, null); - if(claim != null && claim.allowBuild(player, Material.PORTAL) != null) + //FEATURE: if the player teleporting doesn't have permission to build a nether portal and none already exists at the destination, cancel the teleportation + Location destination = event.getTo(); + if(event.useTravelAgent()) { - //cancel and inform about the reason - event.setCancelled(true); - GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoBuildPortalPermission, claim.getOwnerName()); + if(event.getPortalTravelAgent().getCanCreatePortal()) + { + //hypothetically find where the portal would be created if it were + TravelAgent agent = event.getPortalTravelAgent(); + agent.setCanCreatePortal(false); + destination = agent.findOrCreate(destination); + agent.setCanCreatePortal(true); + } + else + { + //if not able to create a portal, we don't have to do anything here + return; + } + } + + //if creating a new portal + if(destination.getBlock().getType() != Material.PORTAL) + { + //check for a land claim and the player's permission that land claim + Claim claim = this.dataStore.getClaimAt(destination, false, null); + if(claim != null && claim.allowBuild(player, Material.PORTAL) != null) + { + //cancel and inform about the reason + event.setCancelled(true); + GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoBuildPortalPermission, claim.getOwnerName()); + } } } }