Improves and fixes comments for listeners

Removes the enableBungee check in the BungeeCordListener as it should only be listening if the option is enabled anyway
Improves the checking for players teleporting from the end
This commit is contained in:
Kristian Knarvik 2021-10-13 16:46:30 +02:00
parent bf7a10636e
commit 44325eeb6a
9 changed files with 53 additions and 31 deletions

View File

@ -44,4 +44,17 @@ public class FromTheEndTeleportation {
return this.exitPortal; return this.exitPortal;
} }
@Override
public int hashCode() {
return teleportingPlayer.hashCode();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof FromTheEndTeleportation otherTeleportation)) {
return false;
}
return teleportingPlayer.equals(otherTeleportation.teleportingPlayer);
}
} }

View File

@ -36,6 +36,9 @@ public class BlockEventListener implements Listener {
/** /**
* Detects snowmen ruining portals * Detects snowmen ruining portals
* *
* <p>If entrance protection or portal verification is enabled, the snowman will be prevented from placing snow in
* the portal entrance.</p>
*
* @param event <p>The triggered event</p> * @param event <p>The triggered event</p>
*/ */
@EventHandler @EventHandler
@ -71,7 +74,7 @@ public class BlockEventListener implements Listener {
} }
final Portal portal = PortalHandler.createPortal(event, player); final Portal portal = PortalHandler.createPortal(event, player);
// Not creating a gate, just placing a sign //Not creating a gate, just placing a sign
if (portal == null) { if (portal == null) {
return; return;
} }

View File

@ -1,6 +1,5 @@
package net.knarcraft.stargate.listener; package net.knarcraft.stargate.listener;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.BungeeHelper; import net.knarcraft.stargate.utility.BungeeHelper;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener; import org.bukkit.plugin.messaging.PluginMessageListener;
@ -16,7 +15,7 @@ import org.jetbrains.annotations.NotNull;
public class BungeeCordListener implements PluginMessageListener { public class BungeeCordListener implements PluginMessageListener {
/** /**
* Receive a plugin message * Receives plugin messages
* *
* @param channel <p>The channel the message was received on</p> * @param channel <p>The channel the message was received on</p>
* @param unused <p>Unused.</p> * @param unused <p>Unused.</p>
@ -24,16 +23,18 @@ public class BungeeCordListener implements PluginMessageListener {
*/ */
@Override @Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player unused, byte[] message) { public void onPluginMessageReceived(@NotNull String channel, @NotNull Player unused, byte[] message) {
//Ignore plugin messages if bungee support is not enabled or some other plugin message is received //Ignore plugin messages if some other plugin message is received
if (!Stargate.enableBungee || !channel.equals("BungeeCord")) { if (!channel.equals("BungeeCord")) {
return; return;
} }
//Try to read the plugin message
String receivedMessage = BungeeHelper.readPluginMessage(message); String receivedMessage = BungeeHelper.readPluginMessage(message);
if (receivedMessage == null) { if (receivedMessage == null) {
return; return;
} }
//Use the message to initiate teleportation
BungeeHelper.handleTeleportMessage(receivedMessage); BungeeHelper.handleTeleportMessage(receivedMessage);
} }

View File

@ -30,6 +30,7 @@ public class EntityEventListener implements Listener {
} }
Entity entity = event.getEntity(); Entity entity = event.getEntity();
//Cancel normal portal event is near a stargate
if (PortalHandler.getByAdjacentEntrance(event.getFrom(), EntityHelper.getEntityMaxSizeInt(entity)) != null) { if (PortalHandler.getByAdjacentEntrance(event.getFrom(), EntityHelper.getEntityMaxSizeInt(entity)) != null) {
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -44,6 +44,7 @@ public class PlayerEventListener implements Listener {
} }
Player player = event.getPlayer(); Player player = event.getPlayer();
//Check if the player is waiting to be teleported to a stargate
String destination = Stargate.bungeeQueue.remove(player.getName().toLowerCase()); String destination = Stargate.bungeeQueue.remove(player.getName().toLowerCase());
if (destination == null) { if (destination == null) {
return; return;
@ -54,6 +55,7 @@ public class PlayerEventListener implements Listener {
Stargate.debug("PlayerJoin", "Error fetching destination portal: " + destination); Stargate.debug("PlayerJoin", "Error fetching destination portal: " + destination);
return; return;
} }
//Teleport the player to the stargate
portal.teleport(player, portal, null); portal.teleport(player, portal, null);
} }
@ -85,13 +87,10 @@ public class PlayerEventListener implements Listener {
return; return;
} }
if (playerVehicle instanceof LivingEntity) { if (playerVehicle instanceof LivingEntity) {
//Make sure the horse can be sat on //Make sure any horses are properly tamed
if (playerVehicle instanceof AbstractHorse horse) { if (playerVehicle instanceof AbstractHorse horse && !horse.isTamed()) {
//Make sure the horse is properly tamed horse.setTamed(true);
if (!horse.isTamed()) { horse.setOwner(player);
horse.setTamed(true);
horse.setOwner(player);
}
} }
destination.teleport((Vehicle) playerVehicle, entrancePortal); destination.teleport((Vehicle) playerVehicle, entrancePortal);
} else { } else {
@ -110,7 +109,8 @@ public class PlayerEventListener implements Listener {
* @param toLocation <p>The location the player is moving to</p> * @param toLocation <p>The location the player is moving to</p>
* @return <p>True if the event is relevant</p> * @return <p>True if the event is relevant</p>
*/ */
private boolean isRelevantMoveEvent(PlayerMoveEvent event, Player player, BlockLocation fromLocation, BlockLocation toLocation) { private boolean isRelevantMoveEvent(PlayerMoveEvent event, Player player, BlockLocation fromLocation,
BlockLocation toLocation) {
//Check to see if the player moved to another block //Check to see if the player moved to another block
if (fromLocation.equals(toLocation)) { if (fromLocation.equals(toLocation)) {
return false; return false;

View File

@ -27,7 +27,7 @@ public class PortalEventListener implements Listener {
private static final List<FromTheEndTeleportation> playersFromTheEnd = new ArrayList<>(); private static final List<FromTheEndTeleportation> playersFromTheEnd = new ArrayList<>();
/** /**
* Listen for and abort vanilla portal creation caused by stargate creation * Listens for and aborts vanilla portal creation caused by stargate creation
* *
* @param event <p>The triggered event</p> * @param event <p>The triggered event</p>
*/ */
@ -55,7 +55,7 @@ public class PortalEventListener implements Listener {
Location location = event.getLocation(); Location location = event.getLocation();
World world = location.getWorld(); World world = location.getWorld();
Entity entity = event.getEntity(); Entity entity = event.getEntity();
//Block normal portal teleportation if teleporting from a stargate //Hijack normal portal teleportation if teleporting from a stargate
if (entity instanceof Player player && location.getBlock().getType() == Material.END_PORTAL && world != null && if (entity instanceof Player player && location.getBlock().getType() == Material.END_PORTAL && world != null &&
world.getEnvironment() == World.Environment.THE_END) { world.getEnvironment() == World.Environment.THE_END) {
Portal portal = PortalHandler.getByAdjacentEntrance(location); Portal portal = PortalHandler.getByAdjacentEntrance(location);
@ -79,16 +79,18 @@ public class PortalEventListener implements Listener {
@EventHandler @EventHandler
public void onRespawn(PlayerRespawnEvent event) { public void onRespawn(PlayerRespawnEvent event) {
Player respawningPlayer = event.getPlayer(); Player respawningPlayer = event.getPlayer();
playersFromTheEnd.forEach((teleportation) -> { int playerIndex = playersFromTheEnd.indexOf(new FromTheEndTeleportation(respawningPlayer, null));
//Check if player is actually teleporting from the end if (playerIndex == -1) {
if (teleportation.getPlayer() == respawningPlayer) { return;
Portal exitPortal = teleportation.getExit(); }
//Overwrite respawn location to respawn in front of the portal FromTheEndTeleportation teleportation = playersFromTheEnd.get(playerIndex);
event.setRespawnLocation(exitPortal.getExit(respawningPlayer, respawningPlayer.getLocation())); playersFromTheEnd.remove(playerIndex);
//Properly close the portal to prevent it from staying in a locked state until it times out
exitPortal.close(false); Portal exitPortal = teleportation.getExit();
} //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

@ -14,9 +14,9 @@ public class TeleportEventListener implements Listener {
/** /**
* This event handler handles some special teleportation events * 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 * <p>This event cancels nether portal, end gateway and end portal teleportation if the user teleported from a
* entrance. This prevents the user from just teleporting to the nether with the default portal design. * stargate entrance. This prevents the user from just teleporting to the nether or the end with portals using
* Additionally, this event teleports any vehicles not detected by the VehicleMove event together with the player.</p> * the special teleportation blocks.</p>
* *
* @param event <p>The event to check and possibly cancel</p> * @param event <p>The event to check and possibly cancel</p>
*/ */

View File

@ -69,7 +69,8 @@ public class VehicleEventListener implements Listener {
Stargate.logger.warning(Stargate.getString("prefix") + "Unable to find portal destination"); Stargate.logger.warning(Stargate.getString("prefix") + "Unable to find portal destination");
return; return;
} }
Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " + destinationPortal.getSignLocation()); Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " +
destinationPortal.getSignLocation());
destinationPortal.teleport(vehicle, entrancePortal); destinationPortal.teleport(vehicle, entrancePortal);
} }
} }

View File

@ -36,8 +36,9 @@ public class WorldEventListener implements Listener {
public void onWorldUnload(WorldUnloadEvent event) { public void onWorldUnload(WorldUnloadEvent event) {
Stargate.debug("onWorldUnload", "Reloading all Stargates"); Stargate.debug("onWorldUnload", "Reloading all Stargates");
World world = event.getWorld(); World world = event.getWorld();
if (Stargate.managedWorlds.contains(world.getName())) { String worldName = world.getName();
Stargate.managedWorlds.remove(world.getName()); if (Stargate.managedWorlds.contains(worldName)) {
Stargate.managedWorlds.remove(worldName);
PortalHandler.clearPortals(world); PortalHandler.clearPortals(world);
} }
} }