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;
}
@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
*
* <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>
*/
@EventHandler
@ -71,7 +74,7 @@ public class BlockEventListener implements Listener {
}
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) {
return;
}

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ public class PortalEventListener implements Listener {
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>
*/
@ -55,7 +55,7 @@ public class PortalEventListener implements Listener {
Location location = event.getLocation();
World world = location.getWorld();
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 &&
world.getEnvironment() == World.Environment.THE_END) {
Portal portal = PortalHandler.getByAdjacentEntrance(location);
@ -79,16 +79,18 @@ public class PortalEventListener implements Listener {
@EventHandler
public void onRespawn(PlayerRespawnEvent event) {
Player respawningPlayer = event.getPlayer();
playersFromTheEnd.forEach((teleportation) -> {
//Check if player is actually teleporting from the end
if (teleportation.getPlayer() == respawningPlayer) {
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);
}
});
int playerIndex = playersFromTheEnd.indexOf(new FromTheEndTeleportation(respawningPlayer, null));
if (playerIndex == -1) {
return;
}
FromTheEndTeleportation teleportation = playersFromTheEnd.get(playerIndex);
playersFromTheEnd.remove(playerIndex);
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
*
* <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>
* <p>This event cancels nether portal, end gateway and end portal teleportation if the user teleported from a
* stargate entrance. This prevents the user from just teleporting to the nether or the end with portals using
* the special teleportation blocks.</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");
return;
}
Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " + destinationPortal.getSignLocation());
Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " +
destinationPortal.getSignLocation());
destinationPortal.teleport(vehicle, entrancePortal);
}
}

View File

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