Fixes teleportation of players using end portals to and from the end

This commit is contained in:
Kristian Knarvik 2021-10-12 01:11:52 +02:00
parent 51afa1527f
commit 53cd55938b
5 changed files with 102 additions and 26 deletions

View File

@ -10,6 +10,7 @@ import net.knarcraft.stargate.listener.EntityEventListener;
import net.knarcraft.stargate.listener.PlayerEventListener;
import net.knarcraft.stargate.listener.PluginEventListener;
import net.knarcraft.stargate.listener.PortalEventListener;
import net.knarcraft.stargate.listener.TeleportEventListener;
import net.knarcraft.stargate.listener.VehicleEventListener;
import net.knarcraft.stargate.listener.WorldEventListener;
import net.knarcraft.stargate.portal.GateHandler;
@ -359,6 +360,7 @@ public class Stargate extends JavaPlugin {
pluginManager.registerEvents(new PortalEventListener(), this);
pluginManager.registerEvents(new WorldEventListener(), this);
pluginManager.registerEvents(new PluginEventListener(this), this);
pluginManager.registerEvents(new TeleportEventListener(), this);
}
/**

View File

@ -10,7 +10,6 @@ import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.MaterialHelper;
import net.knarcraft.stargate.utility.PermissionHelper;
import org.bukkit.GameMode;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.AbstractHorse;
@ -25,9 +24,6 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.Objects;
/**
* This listener listens to any player-related events related to stargates
@ -63,27 +59,6 @@ public class PlayerEventListener implements Listener {
portal.teleport(player, portal, null);
}
/**
* This event handler handles some special teleportation events
*
* <p>This event cancels nether portal and end gateway teleportation if the user teleported from a stargate
* entrance. This prevents the user from just teleporting to the nether with the default portal design.
* Additionally, this event teleports any vehicles not detected by the VehicleMove event together with the player.</p>
*
* @param event <p>The event to check and possibly cancel</p>
*/
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
// cancel portal and end gateway teleportation if it's from a stargate entrance
PlayerTeleportEvent.TeleportCause cause = event.getCause();
if (!event.isCancelled() && (cause == PlayerTeleportEvent.TeleportCause.NETHER_PORTAL
|| cause == PlayerTeleportEvent.TeleportCause.END_GATEWAY && World.Environment.THE_END ==
Objects.requireNonNull(event.getFrom().getWorld()).getEnvironment())
&& PortalHandler.getByAdjacentEntrance(event.getFrom()) != null) {
event.setCancelled(true);
}
}
/**
* This event handler detects if a player moves into a portal
*

View File

@ -1,16 +1,35 @@
package net.knarcraft.stargate.listener;
import net.knarcraft.stargate.container.TwoTuple;
import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.world.PortalCreateEvent;
import java.util.ArrayList;
import java.util.List;
/**
* Listens for and cancels relevant portal events
*/
public class PortalEventListener implements Listener {
private static final List<TwoTuple<Player, Portal>> playersFromTheEnd = new ArrayList<>();
/**
* Listen for and abort vanilla portal creation caused by stargate creation
*
* @param event <p>The triggered event</p>
*/
@EventHandler
public void onPortalCreation(PortalCreateEvent event) {
if (event.isCancelled()) {
@ -25,4 +44,48 @@ public class PortalEventListener implements Listener {
}
}
/**
* Listen for entities entering an artificial end portal
*
* @param event <p>The triggered event</p>
*/
@EventHandler
public void onEntityPortalEnter(EntityPortalEnterEvent event) {
Location location = event.getLocation();
World world = location.getWorld();
Entity entity = event.getEntity();
//Block normal portal teleportation if teleporting from a stargate
if (entity instanceof Player && location.getBlock().getType() == Material.END_PORTAL && world != null &&
world.getEnvironment() == World.Environment.THE_END) {
Portal portal = PortalHandler.getByAdjacentEntrance(location);
if (portal != null) {
playersFromTheEnd.add(new TwoTuple<>((Player) entity, portal.getDestination()));
}
}
}
/**
* Listen for the respawn event to catch players teleporting from the end in an artificial end portal
*
* @param event <p>The triggered event</p>
*/
@EventHandler
public void onRespawn(PlayerRespawnEvent event) {
Player respawningPlayer = event.getPlayer();
playersFromTheEnd.forEach((tuple) -> {
//Check if player is actually teleporting from the end
if (tuple.getFirstValue() == respawningPlayer) {
Portal exitPortal = tuple.getSecondValue();
//Need to make sure the player is allowed to exit from the portal
if (!exitPortal.isOpenFor(respawningPlayer)) {
return;
}
//Overwrite respawn location to respawn in front of the portal
event.setRespawnLocation(exitPortal.getExit(respawningPlayer, respawningPlayer.getLocation()));
//Properly close the portal to prevent it from staying in a locked state until it times out
exitPortal.close(false);
}
});
}
}

View File

@ -0,0 +1,36 @@
package net.knarcraft.stargate.listener;
import net.knarcraft.stargate.portal.PortalHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
/**
* This listener listens to teleportation-related events
*/
@SuppressWarnings("unused")
public class TeleportEventListener implements Listener {
/**
* This event handler handles some special teleportation events
*
* <p>This event cancels nether portal and end gateway teleportation if the user teleported from a stargate
* entrance. This prevents the user from just teleporting to the nether with the default portal design.
* Additionally, this event teleports any vehicles not detected by the VehicleMove event together with the player.</p>
*
* @param event <p>The event to check and possibly cancel</p>
*/
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
PlayerTeleportEvent.TeleportCause cause = event.getCause();
//Block normal portal teleportation if teleporting from a stargate
if (!event.isCancelled() && (cause == PlayerTeleportEvent.TeleportCause.NETHER_PORTAL ||
cause == PlayerTeleportEvent.TeleportCause.END_GATEWAY ||
cause == PlayerTeleportEvent.TeleportCause.END_PORTAL)
&& PortalHandler.getByAdjacentEntrance(event.getFrom()) != null) {
event.setCancelled(true);
}
}
}

View File

@ -646,7 +646,7 @@ public class Portal {
* @param traveller <p>The location of the entity travelling</p>
* @return <p>The location the entity should be teleported to.</p>
*/
private Location getExit(Entity entity, Location traveller) {
public Location getExit(Entity entity, Location traveller) {
Location exitLocation = null;
// Check if the gate has an exit block
RelativeBlockVector relativeExit = gate.getLayout().getExit();