Improve portalForm checking.

* Remove use of deprecated EntityCreatePortalEvent
* Move to use PortalCreateEvent for all nether portalForm checking.
This commit is contained in:
benwoo1110 2021-02-08 18:56:32 +08:00
parent d33176add5
commit e2ea938d1b
2 changed files with 48 additions and 25 deletions

View File

@ -383,7 +383,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
pm.registerEvents(this.entityListener, this); pm.registerEvents(this.entityListener, this);
pm.registerEvents(this.weatherListener, this); pm.registerEvents(this.weatherListener, this);
pm.registerEvents(this.portalListener, 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(this.worldListener, this);
pm.registerEvents(new MVMapListener(this), this); pm.registerEvents(new MVMapListener(this), this);
} }

View File

@ -12,6 +12,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.PortalType; import org.bukkit.PortalType;
import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
@ -33,31 +34,48 @@ public class MVPortalListener implements Listener {
} }
/** /**
* This is called when an entity creates a portal. * This is called when a portal is formed.
* * @param event The event where a portal was create or formed due to a world link
* @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
*/ */
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void portalForm(PortalCreateEvent event) { 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()); MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getWorld());
if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.NETHER)) { if (world == null) {
Logging.fine("Cancelling creation of nether portal because portalForm disallows."); 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); event.setCancelled(true);
} }
} }
@ -72,15 +90,21 @@ public class MVPortalListener implements Listener {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return; return;
} }
if (event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) {
return; return;
} }
if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) { if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) {
return; return;
} }
MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld()); MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld());
if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.ENDER)) { if (world == null) {
Logging.fine("Cancelling creation of ender portal because portalForm disallows."); 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); event.setCancelled(true);
} }
} }