Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,14 @@ private void registerEvents() {
if (portalsConfigProvider.get().getTeleportVehicles()) {
pluginManager.registerEvents(serviceLocator.getService(MVPVehicleListener.class), this);
}
if (portalsConfigProvider.get().getTeleportEntities()) {
pluginManager.registerEvents(serviceLocator.getService(MVPEntityPortalListener.class), this);
}
if (portalsConfigProvider.get().getUseOnMove()) {
pluginManager.registerEvents(serviceLocator.getService(MVPPlayerMoveListener.class), this);
if (portalsConfigProvider.get().getTeleportEntities()) {
pluginManager.registerEvents(serviceLocator.getService(MVPEntityMoveListener.class), this);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Date;

import com.dumptruckman.minecraft.util.Logging;
import org.bukkit.entity.LivingEntity;
import org.mvplugins.multiverse.core.economy.MVEconomist;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
Expand All @@ -30,7 +31,6 @@ public class PortalPlayerSession {
private final PortalManager portalManager;
private final WorldManager worldManager;
private final DisplayUtils displayUtils;
private final MVEconomist economist;
private final Player player;

private MVPortal portalSelection = null;
Expand All @@ -51,7 +51,6 @@ public PortalPlayerSession(MultiversePortals plugin, Player p) {
this.portalManager = plugin.getServiceLocator().getService(PortalManager.class);
this.worldManager = plugin.getServiceLocator().getService(WorldManager.class);
this.displayUtils = plugin.getServiceLocator().getService(DisplayUtils.class);
this.economist = plugin.getServiceLocator().getService(MVEconomist.class);
this.player = p;
this.setLocation(p.getLocation());
this.lastTeleportTime = new Date(new Date().getTime() - this.portalsConfig.getPortalCooldown());
Expand Down Expand Up @@ -107,7 +106,7 @@ private void setStandingInLocation() {
}

public boolean doTeleportPlayer(MoveType eventType) {
if (eventType == MoveType.PLAYER_MOVE && this.player.isInsideVehicle()) {
if (eventType == MoveType.PLAYER_MOVE && shouldLetVehicleHandle()) {
return false;
}
return this.hasMovedOutOfPortal && this.standingIn != null;
Expand All @@ -122,7 +121,7 @@ public void setStaleLocation(Location loc, MoveType moveType) {
// This should never happen, but seems to when someone gets kicked.
return;
}
if (this.player.isInsideVehicle() && moveType != MoveType.VEHICLE_MOVE) {
if (shouldLetVehicleHandle() && moveType != MoveType.VEHICLE_MOVE) {
return;
}
// If the player has not moved, they have a stale location
Expand All @@ -136,6 +135,10 @@ public void setStaleLocation(Location loc, MoveType moveType) {
}
}

private boolean shouldLetVehicleHandle() {
return this.player.isInsideVehicle() && !(this.player.getVehicle() instanceof LivingEntity);
}

public boolean setLeftClickSelection(Vector v, LoadedMultiverseWorld world) {
if(!this.plugin.isWandEnabled()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ public Try<Void> setTeleportVehicles(boolean teleportVehicles) {
return configHandle.set(configNodes.teleportVehicles, teleportVehicles);
}

@ApiStatus.AvailableSince("5.2")
public boolean getTeleportEntities() {
return configHandle.get(configNodes.teleportEntities);
}

@ApiStatus.AvailableSince("5.2")
public Try<Void> setTeleportEntities(boolean teleportEntities) {
return configHandle.set(configNodes.teleportEntities, teleportEntities);
}

/**
*
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,29 @@
final ConfigNode<Boolean> teleportVehicles = node(ConfigNode.builder("portal-usage.teleport-vehicles", Boolean.class)
.comment("")
.comment("If enabled, mvportals will teleport all vehicles along with its passengers when the vehicle enters the portal.")
.comment("Vehicles are usually boats, minecarts, pigs and horses.")
.comment("Vehicles are usually boats and minecarts.")
.comment("(NOTE: please turn off the server before changing this config option)")

Check failure on line 155 in src/main/java/org/mvplugins/multiverse/portals/config/PortalsConfigNodes.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "(NOTE: please turn off the server before changing this config option)" 3 times.

See more on https://sonarcloud.io/project/issues?id=Multiverse_Multiverse-Portals&issues=AZrprE_oi3w47dWof0mk&open=AZrprE_oi3w47dWof0mk&pullRequest=700
.defaultValue(false)
.name("teleport-vehicles")
.aliases("teleportvehicles")
.onSetValue((oldValue, newValue) -> MultiversePortals.TeleportVehicles = newValue)
.build());

final ConfigNode<Boolean> teleportEntities = node(ConfigNode.builder("portal-usage.teleport-entities", Boolean.class)
.comment("")
.comment("If enabled, all living entities can use the portal if property `teleport-non-players` is true.")
.comment("There may be some performance overhead if your server has a large amount of entities moving around.")
.comment("(NOTE: please turn off the server before changing this config option)")
.defaultValue(false)
.name("teleport-entities")
.build());

final ConfigNode<Boolean> useOnMove = node(ConfigNode.builder("portal-usage.use-on-move", Boolean.class)
.comment("")
.comment("If enabled, player movement will be tracked to determine if the player has entered a portal.")
.comment("If enabled, player movement will be tracked to determine if the player/entity has entered a portal.")
.comment("Disabling this will cause mvportals without nether or end fill to not work.")
.comment("Only disable this if all your portals have nether or end fill and want to slight enhance performance.")
.comment("(NOTE: please turn off the server before changing this config option)")
.defaultValue(true)
.name("use-on-move")
.aliases("useonmove")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.mvplugins.multiverse.portals.listeners;

import io.papermc.paper.event.entity.EntityMoveEvent;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.destination.DestinationInstance;
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.teleportation.PassengerModes;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.portals.MVPortal;
import org.mvplugins.multiverse.portals.config.PortalsConfig;
import org.mvplugins.multiverse.portals.utils.PortalManager;

@Service
public final class MVPEntityMoveListener implements Listener {

private final PlayerListenerHelper helper;
private final PortalManager portalManager;
private final AsyncSafetyTeleporter teleporter;
private final PortalsConfig portalsConfig;

@Inject
MVPEntityMoveListener(@NotNull PlayerListenerHelper helper,
@NotNull PortalManager portalManager,
@NotNull AsyncSafetyTeleporter teleporter,
@NotNull PortalsConfig portalsConfig) {
this.helper = helper;
this.portalManager = portalManager;
this.teleporter = teleporter;
this.portalsConfig = portalsConfig;
}

@EventHandler(ignoreCancelled = true)
void entityMove(EntityMoveEvent event) {
if (helper.isWithinSameBlock(event.getFrom(), event.getTo())) {
return;
}

LivingEntity entity = event.getEntity();
Location location = entity.getLocation();

MVPortal portal = portalManager.getPortal(location);
if (portal == null
|| !portal.getTeleportNonPlayers()
|| (portalsConfig.getNetherAnimation() && !portal.isLegacyPortal())) {
return;
}

DestinationInstance<?, ?> destination = portal.getDestination();
if (destination == null) {
return;
}

teleporter.to(destination)
.checkSafety(portal.getCheckDestinationSafety() && destination.checkTeleportSafety())
.passengerMode(PassengerModes.RETAIN_ALL)
.teleportSingle(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.mvplugins.multiverse.portals.listeners;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEvent;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.destination.DestinationInstance;
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.teleportation.PassengerModes;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.portals.MVPortal;
import org.mvplugins.multiverse.portals.utils.PortalManager;

@Service
public final class MVPEntityPortalListener implements Listener {

private final PortalManager portalManager;
private final AsyncSafetyTeleporter teleporter;

@Inject
MVPEntityPortalListener(@NotNull PortalManager portalManager,
@NotNull AsyncSafetyTeleporter teleporter) {
this.portalManager = portalManager;
this.teleporter = teleporter;
}

@EventHandler(ignoreCancelled = true)
void entityPortal(EntityPortalEvent event) {
Entity entity = event.getEntity();
Location location = entity.getLocation();

MVPortal portal = portalManager.getPortal(location);
if (portal == null || !portal.getTeleportNonPlayers()) {
return;
}

DestinationInstance<?, ?> destination = portal.getDestination();
if (destination == null) {
return;
}

event.setCancelled(true);

teleporter.to(destination)
.checkSafety(portal.getCheckDestinationSafety() && destination.checkTeleportSafety())
.passengerMode(PassengerModes.RETAIN_ALL)
.teleportSingle(entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.dumptruckman.minecraft.util.Logging;
import org.bukkit.event.Listener;
import org.mvplugins.multiverse.core.destination.DestinationInstance;
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.teleportation.PassengerModes;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
Expand All @@ -27,28 +29,33 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.Date;

@Service
public final class MVPPlayerMoveListener implements Listener {

private final MultiversePortals plugin;
private final PortalsConfig portalsConfig;
private final PlayerListenerHelper helper;
private final WorldManager worldManager;
private final AsyncSafetyTeleporter teleporter;

@Inject
MVPPlayerMoveListener(
@NotNull MultiversePortals plugin,
@NotNull PortalsConfig portalsConfig,
@NotNull PlayerListenerHelper helper,
@NotNull WorldManager worldManager) {
@NotNull WorldManager worldManager,
@NotNull AsyncSafetyTeleporter teleporter) {
this.plugin = plugin;
this.portalsConfig = portalsConfig;
this.helper = helper;
this.worldManager = worldManager;
this.teleporter = teleporter;
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
void playerMove(PlayerMoveEvent event) {

Check failure on line 58 in src/main/java/org/mvplugins/multiverse/portals/listeners/MVPPlayerMoveListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Multiverse_Multiverse-Portals&issues=AZrprE-di3w47dWof0mj&open=AZrprE-di3w47dWof0mj&pullRequest=700
Player player = event.getPlayer(); // Grab Player
Location loc = player.getLocation(); // Grab Location

Expand All @@ -63,58 +70,72 @@
MVPortal portal = ps.getStandingInPortal();
// If the portal is not null, and it's a legacy portal,
// and we didn't show debug info (the debug is meant to toggle), do the stuff.
if (portal != null
&& (!portalsConfig.getNetherAnimation() || portal.isLegacyPortal())
&& ps.doTeleportPlayer(MoveType.PLAYER_MOVE)
&& !ps.showDebugInfo()) {

DestinationInstance<?, ?> d = portal.getDestination();
if (d == null) {
Logging.fine("Invalid Destination!");
return;
}
player.setFallDistance(0);

Location destLocation = d.getLocation(player).getOrNull();
if (destLocation == null) {
Logging.fine("Unable to teleport player because destination is null!");
return;
}

if (!this.worldManager.isLoadedWorld(destLocation.getWorld())) {
Logging.fine("Unable to teleport player because the destination world is not managed by Multiverse!");
return;
}
if (!portal.isFrameValid(loc)) {
player.sendMessage("This portal's frame is made of an " + ChatColor.RED + "incorrect material. You should exit it now.");
return;
}
if (ps.checkAndSendCooldownMessage()) {
return;
}

PlayerListenerHelper.PortalUseResult portalUseResult = helper.checkPlayerCanUsePortal(portal, player);
if (!portalUseResult.canUse()) {
return;
}

// If they're using Access and they don't have permission and they're NOT excempt, return, they're not allowed to tp.
// No longer checking exemption status
if (portalsConfig.getEnforcePortalAccess() && !event.getPlayer().hasPermission(portal.getPermission())) {
this.helper.stateFailure(player.getDisplayName(), portal.getName());
return;
}

// call event for other plugins
MVPortalEvent portalEvent = new MVPortalEvent(d, event.getPlayer(), portal);
this.plugin.getServer().getPluginManager().callEvent(portalEvent);
if (portalEvent.isCancelled()) {
return;
}
if (portalUseResult.needToPay()) {
helper.payPortalEntryFee(portal, player);
}
helper.performTeleport(event.getPlayer(), event.getTo(), ps, d, portal.getCheckDestinationSafety());
if (portal == null
|| (portalsConfig.getNetherAnimation() && !portal.isLegacyPortal())
|| !ps.doTeleportPlayer(MoveType.PLAYER_MOVE)
|| ps.showDebugInfo()) {
return;
}

DestinationInstance<?, ?> destination = portal.getDestination();
if (destination == null) {
Logging.fine("Invalid Destination!");
return;
}
player.setFallDistance(0);

Location destLocation = destination.getLocation(player).getOrNull();
if (destLocation == null) {
Logging.fine("Unable to teleport player because destination is null!");
return;
}

if (!this.worldManager.isLoadedWorld(destLocation.getWorld())) {
Logging.fine("Unable to teleport player because the destination world is not managed by Multiverse!");
return;
}
if (!portal.isFrameValid(loc)) {
player.sendMessage("This portal's frame is made of an " + ChatColor.RED + "incorrect material. You should exit it now.");
return;
}
if (ps.checkAndSendCooldownMessage()) {
return;
}

PlayerListenerHelper.PortalUseResult portalUseResult = helper.checkPlayerCanUsePortal(portal, player);
if (!portalUseResult.canUse()) {
return;
}

// If they're using Access and they don't have permission and they're NOT excempt, return, they're not allowed to tp.
// No longer checking exemption status
if (portalsConfig.getEnforcePortalAccess() && !event.getPlayer().hasPermission(portal.getPermission())) {
this.helper.stateFailure(player.getDisplayName(), portal.getName());
return;
}

// call event for other plugins
MVPortalEvent portalEvent = new MVPortalEvent(destination, event.getPlayer(), portal);
this.plugin.getServer().getPluginManager().callEvent(portalEvent);
if (portalEvent.isCancelled()) {
return;
}
if (portalUseResult.needToPay()) {
helper.payPortalEntryFee(portal, player);
}

teleporter.to(destination)
.checkSafety(portal.getCheckDestinationSafety() && destination.checkTeleportSafety())
.passengerMode(portal.getTeleportNonPlayers() ? PassengerModes.RETAIN_ALL : PassengerModes.DISMOUNT_VEHICLE)
.teleportSingle(player)
.onSuccess(() -> {
ps.playerDidTeleport(destLocation);
ps.setTeleportTime(new Date());
helper.stateSuccess(player.getDisplayName(), destination.toString());
})
.onFailure(reason -> Logging.fine(
"Failed to teleport player '%s' to destination '%s'. Reason: %s",
player.getDisplayName(), destination, reason)
);
}
}
Loading