Refactor and fix checkstyles for some listeners

This commit is contained in:
Ben Woo 2023-09-25 14:09:09 +08:00
parent af7d5a8cb0
commit 454bcd36e5
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
4 changed files with 62 additions and 53 deletions

View File

@ -31,10 +31,11 @@ public class MVChatListener implements InjectableListener {
/**
* This handles a {@link AsyncPlayerChatEvent}.
*
* @param event The {@link AsyncPlayerChatEvent}.
*/
@EventHandler
public void playerChat(AsyncPlayerChatEvent event) {
public void asyncPlayerChat(AsyncPlayerChatEvent event) {
if (event.isCancelled()) {
return;
}

View File

@ -42,6 +42,7 @@ public class MVEntityListener implements InjectableListener {
/**
* This method is called when an entity's food level goes higher or lower.
*
* @param event The Event that was fired.
*/
@EventHandler
@ -52,6 +53,7 @@ public class MVEntityListener implements InjectableListener {
if (!(event.getEntity() instanceof Player player)) {
return;
}
worldManager.getLoadedWorld(player.getWorld())
.peek(world -> {
if (!world.getHunger() && event.getFoodLevel() < player.getFoodLevel()) {
@ -62,6 +64,7 @@ public class MVEntityListener implements InjectableListener {
/**
* This method is called when an entity's health goes up or down.
*
* @param event The Event that was fired.
*/
@EventHandler
@ -69,6 +72,7 @@ public class MVEntityListener implements InjectableListener {
if (event.isCancelled() || event.getRegainReason() != RegainReason.REGEN) {
return;
}
worldManager.getLoadedWorld(event.getEntity().getWorld())
.peek(world -> {
if (!world.getAutoHeal()) {
@ -79,6 +83,7 @@ public class MVEntityListener implements InjectableListener {
/**
* Handle Animal/Monster Spawn settings, seems like a more concrete method than using CraftBukkit.
*
* @param event The event.
*/
@EventHandler

View File

@ -22,9 +22,10 @@ import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.config.MVCoreConfig;
import org.mvplugins.multiverse.core.inject.InjectableListener;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
import static org.bukkit.PortalType.CUSTOM;
/**
* A custom listener for portal related events.
*/
@ -42,49 +43,51 @@ public class MVPortalListener implements InjectableListener {
/**
* 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());
public void portalCreate(PortalCreateEvent event) {
Logging.fine("Attempting to create portal at '%s' with reason: %s",
event.getWorld().getName(), event.getReason());
LoadedMultiverseWorld world = this.worldManager.getLoadedWorld(event.getWorld()).getOrNull();
if (world == null) {
Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PortalCreateEvent.", event.getWorld().getName());
this.worldManager.getLoadedWorld(event.getWorld()).peek(world -> {
PortalType targetType = getPortalType(event);
if (targetType == PortalType.CUSTOM) {
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.getPortalForm().isPortalAllowed(targetType)) {
Logging.fine("Cancelling creation of %s portal because portalForm disallows.", targetType);
event.setCancelled(true);
}
}).onEmpty(() -> {
Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PortalCreateEvent.",
event.getWorld().getName());
});
}
private PortalType getPortalType(PortalCreateEvent event) {
return switch (event.getReason()) {
case FIRE -> {
// Ensure portal by flint and steel actually creates nether
for (BlockState block : event.getBlocks()) {
if (block.getType() == Material.NETHER_PORTAL) {
yield PortalType.NETHER;
}
}
yield CUSTOM;
}
case NETHER_PAIR -> {
yield PortalType.NETHER;
}
case END_PLATFORM -> {
yield PortalType.ENDER;
}
default -> {
Logging.fine("Portal created is not NETHER or ENDER type. Ignoring...");
yield CUSTOM;
}
};
}
/**
@ -93,28 +96,28 @@ public class MVPortalListener implements InjectableListener {
* @param event The player interact event.
*/
@EventHandler(ignoreCancelled = true)
public void portalForm(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) {
return;
}
if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) {
return;
}
LoadedMultiverseWorld world = this.worldManager.getLoadedWorld(event.getPlayer().getWorld()).getOrNull();
if (world == null) {
Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PlayerInteractEvent.",
event.getPlayer().getWorld().getName());
public void playerInteract(PlayerInteractEvent event) {
if (isCreateEndPortalInteraction(event)) {
return;
}
this.worldManager.getLoadedWorld(event.getPlayer().getWorld()).peek(world -> {
if (!world.getPortalForm().isPortalAllowed(PortalType.ENDER)) {
Logging.fine("Cancelling creation of ENDER portal because portalForm disallows.");
event.setCancelled(true);
}
}).onEmpty(() -> {
Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PlayerInteractEvent.",
event.getPlayer().getWorld().getName());
});
}
private boolean isCreateEndPortalInteraction(PlayerInteractEvent event) {
return event.getAction() != Action.RIGHT_CLICK_BLOCK
|| event.getClickedBlock() == null
|| event.getClickedBlock().getType() != Material.END_PORTAL_FRAME
|| event.getItem() == null
|| event.getItem().getType() != Material.ENDER_EYE;
}
/**
@ -127,7 +130,7 @@ public class MVPortalListener implements InjectableListener {
if (event.isCancelled() || event.getTo() == null) {
return;
}
if (!config.isUsingCustomPortalSearch()) {
if (config.isUsingCustomPortalSearch()) {
event.setSearchRadius(config.getCustomPortalSearchRadius());
}
}

View File

@ -40,7 +40,7 @@ public class MVWorldListener implements InjectableListener {
* @param event The Event that was fired.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void unloadWorld(WorldUnloadEvent event) {
public void worldUnload(WorldUnloadEvent event) {
if (event.isCancelled()) {
return;
}
@ -58,7 +58,7 @@ public class MVWorldListener implements InjectableListener {
* @param event The Event that was fired.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void loadWorld(WorldLoadEvent event) {
public void worldLoad(WorldLoadEvent event) {
worldManager.getUnloadedWorld(event.getWorld().getName())
.peek(world -> {
Logging.fine("Loading world: " + world.getName());