Skip to content

Commit b539342

Browse files
authored
Merge pull request #269 from Multiverse/ben/fix-entity-teleport
Fix wrong destination for entity portal teleport
2 parents 9b2c1fc + c5e53ad commit b539342

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

src/main/java/com/onarandombox/MultiverseNetherPortals/listeners/MVNPEntityListener.java

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onarandombox.MultiverseNetherPortals.listeners;
22

33
import com.dumptruckman.minecraft.util.Logging;
4+
import com.onarandombox.MultiverseCore.MVWorld;
45
import com.onarandombox.MultiverseCore.api.LocationManipulation;
56
import com.onarandombox.MultiverseCore.api.MVWorldManager;
67
import com.onarandombox.MultiverseCore.api.MultiverseMessaging;
@@ -299,26 +300,13 @@ public void onEntityPortal(EntityPortalEvent event) {
299300
return;
300301
}
301302

302-
// Some shortcuts for later
303-
Entity entity = event.getEntity();
304-
305-
@Nullable Location toLocation = event.getTo();
306-
Location fromLocation = event.getFrom();
307-
308-
if (toLocation == null) {
309-
Logging.warning("ToLocation in EntityPortalEvent is null.");
303+
if (event.getTo() == null) {
304+
Logging.warning("getTo() location in EntityPortalEvent is null.");
310305
return;
311306
}
312307

313-
MultiverseWorld fromWorld = this.worldManager.getMVWorld(fromLocation.getWorld().getName());
314-
MultiverseWorld toWorld = this.worldManager.getMVWorld(toLocation.getWorld().getName());
315-
316-
Location originalTo = this.locationManipulation.getBlockLocation(toLocation);
317-
Location currentLocation = this.locationManipulation.getBlockLocation(fromLocation);
318-
319-
320308
// Don't mess with other people's stuff
321-
if (!plugin.isHandledByNetherPortals(currentLocation)) {
309+
if (!plugin.isHandledByNetherPortals(event.getFrom())) {
322310
return;
323311
}
324312

@@ -328,12 +316,26 @@ public void onEntityPortal(EntityPortalEvent event) {
328316
return;
329317
}
330318

319+
// Some shortcuts for later
320+
Entity entity = event.getEntity();
321+
322+
Location fromLocation = this.locationManipulation.getBlockLocation(event.getFrom());
323+
Location originalToLocation = this.locationManipulation.getBlockLocation(event.getTo());
324+
325+
World fromWorld = fromLocation.getWorld();
326+
World originalToWorld = originalToLocation.getWorld();
327+
328+
if (fromWorld == null || originalToWorld == null) {
329+
Logging.warning("from/to world is null in EntityPortalEvent for %s", entity.getName());
330+
return;
331+
}
332+
331333
PortalType type;
332-
if (originalTo.getWorld().getEnvironment() == World.Environment.NETHER
333-
|| (currentLocation.getWorld().getEnvironment() == World.Environment.NETHER && originalTo.getWorld().getEnvironment() == World.Environment.NORMAL)) {
334+
if (originalToWorld.getEnvironment() == World.Environment.NETHER
335+
|| (fromWorld.getEnvironment() == World.Environment.NETHER && originalToWorld.getEnvironment() == World.Environment.NORMAL)) {
334336
type = PortalType.NETHER;
335-
} else if (originalTo.getWorld().getEnvironment() == World.Environment.THE_END
336-
|| (currentLocation.getWorld().getEnvironment() == World.Environment.THE_END && originalTo.getWorld().getEnvironment() == World.Environment.NORMAL)) {
337+
} else if (originalToWorld.getEnvironment() == World.Environment.THE_END
338+
|| (fromWorld.getEnvironment() == World.Environment.THE_END && originalToWorld.getEnvironment() == World.Environment.NORMAL)) {
337339
type = PortalType.ENDER;
338340
} else {
339341
return;
@@ -349,51 +351,46 @@ public void onEntityPortal(EntityPortalEvent event) {
349351
}
350352
}
351353

352-
String currentWorld = currentLocation.getWorld().getName();
353-
String linkedWorld = this.plugin.getWorldLink(currentWorld, type);
354-
Location newTo = getLocation(entity, currentLocation, type, currentWorld, linkedWorld); // Gets the player spawn location from the portal spawn location
354+
String fromWorldName = fromWorld.getName();
355+
String linkedWorldName = this.plugin.getWorldLink(fromWorldName, type);
356+
Location newToLocation = getLocation(entity, fromLocation, type, fromWorldName, linkedWorldName); // Gets the player spawn location from the portal spawn location
355357

356-
if (newTo != null) {
357-
event.setTo(newTo);
358-
} else {
358+
// If we can't get a valid location, cancel the event
359+
if (newToLocation == null) {
359360
event.setCancelled(true);
360361
return;
361362
}
362363

364+
event.setTo(newToLocation);
365+
MultiverseWorld newToWorld = this.worldManager.getMVWorld(newToLocation.getWorld());
366+
363367
// If we are going to the overworld from the end
364368
if (fromWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
365-
Logging.fine("Entity '" + entity.getName() + "' will be teleported to the spawn of '" + toWorld.getName() + "' since they used an end exit portal.");
369+
Logging.fine("Entity '" + entity.getName() + "' will be teleported to the spawn of '" + newToWorld.getName() + "' since they used an end exit portal.");
366370
try {
367371
Class.forName("org.bukkit.TravelAgent");
368372
event.getPortalTravelAgent().setCanCreatePortal(false);
369373
} catch (ClassNotFoundException ignore) {
370374
Logging.fine("TravelAgent not available for EntityPortalEvent for " + entity.getName() + ". There may be a portal created at spawn.");
371375
}
372-
373-
event.setTo(toWorld.getSpawnLocation());
374-
return;
376+
event.setTo(newToWorld.getSpawnLocation());
375377
}
376-
377378
// If we are going to the overworld from the nether
378-
if (fromWorld.getEnvironment() == World.Environment.NETHER && type == PortalType.NETHER) {
379+
else if (fromWorld.getEnvironment() == World.Environment.NETHER && type == PortalType.NETHER) {
379380
try {
380381
Class.forName("org.bukkit.TravelAgent");
381382
event.getPortalTravelAgent().setCanCreatePortal(true);
382-
event.setTo(event.getPortalTravelAgent().findOrCreate(toLocation));
383+
event.setTo(event.getPortalTravelAgent().findOrCreate(newToLocation));
383384
} catch (ClassNotFoundException ignore) {
384385
Logging.fine("TravelAgent not available for EntityPortalEvent for " + entity.getName() + ". Their destination may not be correct.");
385-
event.setTo(toLocation);
386+
event.setTo(newToLocation);
386387
}
387-
388-
return;
389388
}
390-
391389
// If we are going to the end from anywhere
392-
if (toWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
393-
Location spawnLocation = EndPlatformCreator.getVanillaLocation(toWorld);
390+
else if (newToWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
391+
Location spawnLocation = EndPlatformCreator.getVanillaLocation(newToWorld);
394392
event.setTo(spawnLocation);
395393
EndPlatformCreator.createEndPlatform(spawnLocation, plugin.isEndPlatformDropBlocks());
396-
return;
397394
}
398395
}
399396

0 commit comments

Comments
 (0)