Always return a TravelAgent; Fixes BUKKIT-3541

Recent changes caused PlayerPortalEvent to suddenly return null
unexpectedly and could end up in NPEs resulting that did not before.
This commit addresses that situation by always ensuring a TravelAgent
instance is returned.

The TravelAgent for world 0 is returned arbitrarily in an effort to
compensate for plugins that are implementation dependent and expect some
form of a TravelAgent to be accessible in the event at all times.
This commit is contained in:
EdGruberman 2013-02-03 12:55:51 -07:00 committed by feildmaster
parent 4d3865a036
commit efe8e9a7f5
3 changed files with 7 additions and 2 deletions

View File

@ -1764,7 +1764,7 @@ public abstract class Entity {
Location exit = exitWorld != null ? minecraftserver.getPlayerList().calculateTarget(enter, minecraftserver.getWorldServer(i)) : null; Location exit = exitWorld != null ? minecraftserver.getPlayerList().calculateTarget(enter, minecraftserver.getWorldServer(i)) : null;
boolean useTravelAgent = exitWorld != null && !(this.dimension == 1 && exitWorld.dimension == 1); // don't use agent for custom worlds or return from THE_END boolean useTravelAgent = exitWorld != null && !(this.dimension == 1 && exitWorld.dimension == 1); // don't use agent for custom worlds or return from THE_END
TravelAgent agent = exit != null ? (TravelAgent) ((CraftWorld) exit.getWorld()).getHandle().s() : null; TravelAgent agent = exit != null ? (TravelAgent) ((CraftWorld) exit.getWorld()).getHandle().s() : org.bukkit.craftbukkit.CraftTravelAgent.DEFAULT; // return arbitrary TA to compensate for implementation dependent plugins
EntityPortalEvent event = new EntityPortalEvent(this.getBukkitEntity(), enter, exit, agent); EntityPortalEvent event = new EntityPortalEvent(this.getBukkitEntity(), enter, exit, agent);
event.useTravelAgent(useTravelAgent); event.useTravelAgent(useTravelAgent);
event.getEntity().getServer().getPluginManager().callEvent(event); event.getEntity().getServer().getPluginManager().callEvent(event);

View File

@ -451,7 +451,7 @@ public abstract class PlayerList {
} }
} }
TravelAgent agent = exit != null ? (TravelAgent) ((CraftWorld) exit.getWorld()).getHandle().s() : null; TravelAgent agent = exit != null ? (TravelAgent) ((CraftWorld) exit.getWorld()).getHandle().s() : org.bukkit.craftbukkit.CraftTravelAgent.DEFAULT; // return arbitrary TA to compensate for implementation dependent plugins
PlayerPortalEvent event = new PlayerPortalEvent(entityplayer.getBukkitEntity(), enter, exit, agent, cause); PlayerPortalEvent event = new PlayerPortalEvent(entityplayer.getBukkitEntity(), enter, exit, agent, cause);
event.useTravelAgent(useTravelAgent); event.useTravelAgent(useTravelAgent);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);

View File

@ -9,12 +9,17 @@ import org.bukkit.TravelAgent;
public class CraftTravelAgent extends PortalTravelAgent implements TravelAgent { public class CraftTravelAgent extends PortalTravelAgent implements TravelAgent {
public static TravelAgent DEFAULT = null;
private int searchRadius = 128; private int searchRadius = 128;
private int creationRadius = 16; private int creationRadius = 16;
private boolean canCreatePortal = true; private boolean canCreatePortal = true;
public CraftTravelAgent(WorldServer worldserver) { public CraftTravelAgent(WorldServer worldserver) {
super(worldserver); super(worldserver);
if (DEFAULT == null && worldserver.dimension == 0) {
DEFAULT = (TravelAgent) worldserver.s();
}
} }
public Location findOrCreate(Location target) { public Location findOrCreate(Location target) {