Skip to content

Commit 16df847

Browse files
authored
Merge pull request #698 from Multiverse/refactor/listeners
Refactor listeners for code readability and fix access modifiers
2 parents ccc7e5b + 63cee5c commit 16df847

File tree

7 files changed

+387
-355
lines changed

7 files changed

+387
-355
lines changed

src/main/java/org/mvplugins/multiverse/portals/listeners/MVPBlockListener.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,51 @@
99

1010
import org.bukkit.Material;
1111
import org.bukkit.event.EventHandler;
12+
import org.bukkit.event.EventPriority;
13+
import org.bukkit.event.block.BlockFromToEvent;
1214
import org.bukkit.event.block.BlockPhysicsEvent;
1315

1416
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
1517
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
1618
import org.jvnet.hk2.annotations.Service;
19+
import org.mvplugins.multiverse.portals.config.PortalsConfig;
1720
import org.mvplugins.multiverse.portals.utils.PortalManager;
1821

1922
@Service
20-
public class MVPBlockListener implements PortalsListener {
23+
final class MVPBlockListener implements PortalsListener {
2124
private final PortalManager portalManager;
25+
private final PortalsConfig portalsConfig;
2226

2327
@Inject
24-
MVPBlockListener(@NotNull PortalManager portalManager) {
28+
MVPBlockListener(@NotNull PortalManager portalManager, @NotNull PortalsConfig portalsConfig) {
2529
this.portalManager = portalManager;
30+
this.portalsConfig = portalsConfig;
2631
}
2732

28-
@EventHandler
29-
public void blockPhysics(BlockPhysicsEvent event) {
30-
if (event.isCancelled()) {
31-
return;
32-
}
33+
@EventHandler(ignoreCancelled = true)
34+
void blockPhysics(BlockPhysicsEvent event) {
3335
if (event.getChangedType() == Material.NETHER_PORTAL || event.getBlock().getType() == Material.NETHER_PORTAL) {
3436
if (portalManager.isPortal(event.getBlock().getLocation())) {
3537
event.setCancelled(true);
3638
}
3739
}
3840
}
41+
42+
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
43+
void blockFromTo(BlockFromToEvent event) {
44+
// The to block should never be null, but apparently it is sometimes...
45+
if (event.getBlock() == null || event.getToBlock() == null) {
46+
return;
47+
}
48+
49+
// If lava/something else is trying to flow in...
50+
if (portalManager.isPortal(event.getToBlock().getLocation())) {
51+
event.setCancelled(true);
52+
return;
53+
}
54+
// If something is trying to flow out, stop that too, unless bucketFilling has been disabled
55+
if (portalManager.isPortal(event.getBlock().getLocation()) && portalsConfig.getBucketFilling()) {
56+
event.setCancelled(true);
57+
}
58+
}
3959
}

src/main/java/org/mvplugins/multiverse/portals/listeners/MVPCoreListener.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@
2222
import org.bukkit.event.EventHandler;
2323

2424
import org.mvplugins.multiverse.portals.MultiversePortals;
25+
import org.mvplugins.multiverse.portals.config.PortalsConfig;
2526
import org.mvplugins.multiverse.portals.utils.PortalManager;
2627

2728
@Service
28-
public class MVPCoreListener implements PortalsListener {
29+
final class MVPCoreListener implements PortalsListener {
2930
private final MultiversePortals plugin;
31+
private final PortalsConfig config;
3032
private final PortalManager portalManager;
3133

3234
@Inject
33-
MVPCoreListener(@NotNull MultiversePortals plugin, @NotNull PortalManager portalManager) {
35+
MVPCoreListener(@NotNull MultiversePortals plugin, @NotNull PortalManager portalManager, @NotNull PortalsConfig config) {
3436
this.plugin = plugin;
3537
this.portalManager = portalManager;
38+
this.config = config;
3639
}
3740

3841
/**
@@ -73,26 +76,27 @@ public void debugModeChange(MVDebugModeEvent event) {
7376
public void portalTouchEvent(MVPlayerTouchedPortalEvent event) {
7477
Logging.finer("Found The TouchedPortal event.");
7578
Location l = event.getBlockTouched();
76-
if (event.canUseThisPortal() && (this.portalManager.isPortal(l))) {
77-
if (this.plugin.getPortalSession(event.getPlayer()).isDebugModeOn()) {
78-
event.setCancelled(true);
79+
if (!event.canUseThisPortal() || (!this.portalManager.isPortal(l))) {
80+
return;
81+
}
82+
if (this.plugin.getPortalSession(event.getPlayer()).isDebugModeOn()) {
83+
event.setCancelled(true);
84+
return;
85+
}
86+
// This is a valid portal, and they can use it so far...
87+
MVPortal p = this.portalManager.getPortal(event.getPlayer(), l);
88+
if (p == null) {
89+
// The player can't see this portal, and can't use it.
90+
Logging.finer(String.format("'%s' was DENIED access to this portal event.", event.getPlayer().getName()));
91+
event.setCanUseThisPortal(false);
92+
} else if (p.getDestination() == null) {
93+
if (config.getPortalsDefaultToNether()) {
94+
Logging.finer("Allowing MVPortal to act as nether portal.");
7995
return;
8096
}
81-
// This is a valid portal, and they can use it so far...
82-
MVPortal p = this.portalManager.getPortal(event.getPlayer(), l);
83-
if (p == null) {
84-
// The player can't see this portal, and can't use it.
85-
Logging.finer(String.format("'%s' was DENIED access to this portal event.", event.getPlayer().getName()));
86-
event.setCanUseThisPortal(false);
87-
} else if (p.getDestination() == null) {
88-
if (this.plugin.getMainConfig().getBoolean("portalsdefaulttonether", false)) {
89-
Logging.finer("Allowing MVPortal to act as nether portal.");
90-
return;
91-
}
92-
// They can see it, is it val
93-
event.getPlayer().sendMessage("This Multiverse Portal does not have a valid destination!");
94-
event.setCanUseThisPortal(false);
95-
}
97+
// They can see it, is it val
98+
event.getPlayer().sendMessage("This Multiverse Portal does not have a valid destination!");
99+
event.setCanUseThisPortal(false);
96100
}
97101
}
98102
}

0 commit comments

Comments
 (0)