From 9d34e5d75c72030bc56da02758c5bad84e810434 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 2 Feb 2023 15:13:00 +0800 Subject: [PATCH] fix: Replace deprecated EntityCreatePortalEvent with PortalCreateEvent Use PortalCreateEvent which allows better checking of nether portal creation. --- .../MultiverseCore/MultiverseCore.java | 1 - .../listeners/MVPortalListener.java | 76 ++++++++++++------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index aa952101..a66f95de 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -391,7 +391,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { pm.registerEvents(this.entityListener, this); pm.registerEvents(this.weatherListener, this); pm.registerEvents(this.portalListener, this); - Logging.info(ChatColor.GREEN + "We are aware of the warning about the deprecated event. There is no alternative that allows us to do what we need to do and performance impact is negligible. It is safe to ignore."); pm.registerEvents(this.worldListener, this); pm.registerEvents(new MVMapListener(this), this); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java index c348ac3d..dee9f7b5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java @@ -12,15 +12,13 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.Material; import org.bukkit.PortalType; +import org.bukkit.block.BlockState; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.entity.EntityCreatePortalEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.world.PortalCreateEvent; -import java.util.logging.Level; - /** * A custom listener for portal related events. */ @@ -33,31 +31,48 @@ public class MVPortalListener implements Listener { } /** - * This is called when an entity creates a portal. - * - * @param event The event where an entity created a portal. - */ - @EventHandler - public void entityPortalCreate(EntityCreatePortalEvent event) { - if (event.isCancelled() || event.getBlocks().size() == 0) { - return; - } - MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getEntity().getWorld()); - // We have to do it like this due to a bug in 1.1-R3 - if (world != null && !world.getAllowedPortals().isPortalAllowed(event.getPortalType())) { - event.setCancelled(true); - } - } - - /** - * This is called when a portal is created as the result of another world being linked. - * @param event The event where a portal was formed due to a world link + * This is called when a portal is formed. + * @param event The event where a portal was created or formed due to a world link */ @EventHandler(ignoreCancelled = true) public void portalForm(PortalCreateEvent event) { + Logging.fine("Attempting to create portal at '%s' with reason: %s", event.getWorld().getName(), event.getReason()); + MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getWorld()); - if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.NETHER)) { - Logging.fine("Cancelling creation of nether portal because portalForm disallows."); + if (world == null) { + Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PortalCreateEvent.", event.getWorld().getName()); + return; + } + + PortalType targetType; + switch (event.getReason()) { + case FIRE: + // Ensure portal by flint and steel actually creates nether + boolean isNether = false; + for (BlockState block : event.getBlocks()) { + if (block.getType() == Material.NETHER_PORTAL) { + isNether = true; + break; + } + } + if (!isNether) { + return; + } + targetType = PortalType.NETHER; + break; + case NETHER_PAIR: + targetType = PortalType.NETHER; + break; + case END_PLATFORM: + targetType = PortalType.ENDER; + break; + default: + Logging.fine("Portal created is not NETHER or ENDER type. Ignoring..."); + return; + } + + if (!world.getAllowedPortals().isPortalAllowed(targetType)) { + Logging.fine("Cancelling creation of %s portal because portalForm disallows.", targetType); event.setCancelled(true); } } @@ -72,15 +87,22 @@ public class MVPortalListener implements Listener { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { return; } - if (event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { + if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { return; } if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) { return; } + MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld()); - if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.ENDER)) { - Logging.fine("Cancelling creation of ender portal because portalForm disallows."); + if (world == null) { + Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PlayerInteractEvent.", + event.getPlayer().getWorld().getName()); + return; + } + + if (!world.getAllowedPortals().isPortalAllowed(PortalType.ENDER)) { + Logging.fine("Cancelling creation of ENDER portal because portalForm disallows."); event.setCancelled(true); } }