Skip to content

Commit ab90228

Browse files
Adds Tweak for blocking nether portal formation and teleport in WorldGuard regions.
Also adds a lisr to the config of excluded regions.
1 parent 914181f commit ab90228

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.ShakeforProtein</groupId>
88
<artifactId>TreeboTweaksSP</artifactId>
9-
<version>0.0.8</version>
9+
<version>0.0.9</version>
1010
<packaging>jar</packaging>
1111

1212
<name>TreeboTweaksSP</name>
@@ -121,11 +121,13 @@
121121
<groupId>us.dynmap</groupId>
122122
<artifactId>DynmapCoreAPI</artifactId>
123123
<version>3.0-SNAPSHOT</version>
124+
<scope>provided</scope>
124125
</dependency>
125126
<dependency>
126127
<groupId>us.dynmap</groupId>
127128
<artifactId>dynmap-api</artifactId>
128129
<version>3.0-SNAPSHOT</version>
130+
<scope>provided</scope>
129131
</dependency>
130132
<dependency>
131133
<groupId>me.ShakeforProtein</groupId>

src/main/java/me/shakeforprotein/treebotweakssp/TreeboTweaksSP.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import me.shakeforprotein.treeboroots.TreeboRoots;
1010
import me.shakeforprotein.treebotweakssp.Tweaks.ASTHandler.Listeners.AstHandler;
1111
import me.shakeforprotein.treebotweakssp.Tweaks.AutoBalancePvP.AutoBalancePvP;
12+
import me.shakeforprotein.treebotweakssp.Tweaks.BlockPortalsInWorldGuardRegions.BlockPortalsInWorldGuardRegions;
1213
import me.shakeforprotein.treebotweakssp.Tweaks.BlockPvPNearOwnClaims.BlockPvPNearClaims;
1314
import me.shakeforprotein.treebotweakssp.Tweaks.BlockWithersNearClaims.BlockWither;
1415
import me.shakeforprotein.treebotweakssp.Tweaks.DoubleDoors.DoubleDoors;
@@ -192,6 +193,9 @@ public void run() {
192193
if(getConfig().getBoolean("Tweaks.MaintainFlightBetweenWorlds")){
193194
Bukkit.getPluginManager().registerEvents(new MaintainFlightBetweenWorlds(this), this);
194195
}
196+
if(getConfig().getBoolean("Tweaks.BlockPortalsInWorldGuardRegions")){
197+
Bukkit.getPluginManager().registerEvents(new BlockPortalsInWorldGuardRegions(this), this);
198+
}
195199
}
196200

197201
public void runCommandSynchronouslyLater(CommandSender sender, String command, long delay) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package me.shakeforprotein.treebotweakssp.Tweaks.BlockPortalsInWorldGuardRegions;
2+
3+
import com.sk89q.worldedit.BlockVector;
4+
import com.sk89q.worldedit.bukkit.BukkitAdapter;
5+
import com.sk89q.worldguard.WorldGuard;
6+
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
7+
import com.sk89q.worldguard.protection.flags.Flags;
8+
import com.sk89q.worldguard.protection.managers.RegionManager;
9+
import com.sk89q.worldguard.protection.regions.RegionContainer;
10+
import jdk.nashorn.internal.codegen.ClassEmitter;
11+
import me.shakeforprotein.treebotweakssp.TreeboTweaksSP;
12+
import org.bukkit.Bukkit;
13+
import org.bukkit.Location;
14+
import org.bukkit.Material;
15+
import org.bukkit.block.Block;
16+
import org.bukkit.block.BlockFace;
17+
import org.bukkit.block.BlockState;
18+
import org.bukkit.entity.Player;
19+
import org.bukkit.event.EventHandler;
20+
import org.bukkit.event.Listener;
21+
import org.bukkit.event.entity.EntityPortalEnterEvent;
22+
import org.bukkit.event.entity.EntityPortalEvent;
23+
import org.bukkit.event.player.PlayerPortalEvent;
24+
import org.bukkit.event.world.PortalCreateEvent;
25+
import org.bukkit.inventory.ItemStack;
26+
27+
public class BlockPortalsInWorldGuardRegions implements Listener {
28+
29+
private TreeboTweaksSP pl;
30+
private WorldGuard worldGuard;
31+
private boolean wgEnabled = false;
32+
33+
public BlockPortalsInWorldGuardRegions(TreeboTweaksSP main) {
34+
this.pl = main;
35+
if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) {
36+
Bukkit.getScheduler().runTaskLater(pl, () -> {
37+
if (Bukkit.getPluginManager().getPlugin("WorldGuard").isEnabled()) {
38+
wgEnabled = true;
39+
worldGuard = WorldGuard.getInstance();
40+
}
41+
}, 5L);
42+
}
43+
}
44+
45+
@EventHandler
46+
public void onPortalCreate(PortalCreateEvent event) {
47+
if (!event.isCancelled()) {
48+
for (BlockState blockState : event.getBlocks()) {
49+
if (isInWgRegion(blockState.getLocation())) {
50+
if (event.getEntity() instanceof Player) {
51+
event.getEntity().sendMessage(pl.badge + "This portal was blocked as it would breach a protected region.");
52+
}
53+
event.setCancelled(true);
54+
return;
55+
}
56+
}
57+
}
58+
}
59+
60+
@EventHandler
61+
public void onPlayerUsePortal(PlayerPortalEvent event) {
62+
if(!event.isCancelled()) {
63+
if (event.getTo() != null && isInWgRegion(event.getTo())) {
64+
event.getPlayer().sendMessage(pl.badge + "This portal has been blocked as the other end would form in a protected region.");
65+
event.setCanCreatePortal(false);
66+
event.setCancelled(true);
67+
for(BlockFace face : BlockFace.values()){
68+
if(event.getFrom().getBlock().getRelative(face).getType() == Material.NETHER_PORTAL){
69+
event.getFrom().getBlock().getRelative(face).breakNaturally();
70+
return;
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
@EventHandler
78+
public void entityUsePortal(EntityPortalEvent event){
79+
if(!event.isCancelled()){
80+
if (event.getTo() != null && isInWgRegion(event.getTo())) {
81+
event.setCancelled(true);
82+
for(BlockFace face : BlockFace.values()){
83+
if(event.getFrom().getBlock().getRelative(face).getType() == Material.NETHER_PORTAL){
84+
event.getFrom().getBlock().getRelative(face).breakNaturally();
85+
return;
86+
}
87+
}
88+
}
89+
}
90+
}
91+
92+
private boolean isInWgRegion(Location loc) {
93+
RegionContainer container = worldGuard.getPlatform().getRegionContainer();
94+
RegionManager regions = container.get(BukkitAdapter.adapt(loc.getWorld()));
95+
96+
97+
for(String allowedRegion : pl.getConfig().getStringList("AllowedPortalRegions")) {
98+
if (regions != null && regions.size() > 0 && regions.hasRegion(allowedRegion) && allowedRegion.toLowerCase() != "__global__") {
99+
if (regions.getRegion(allowedRegion).contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
100+
return false;
101+
}
102+
}
103+
}
104+
105+
for (String regionKey : regions.getRegions().keySet()) {
106+
if (regionKey.toLowerCase() != "__global__") {
107+
if (regions.getRegion(regionKey) != null && regions.getRegion(regionKey).contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
108+
return true;
109+
}
110+
}
111+
}
112+
113+
return false;
114+
}
115+
}

src/main/resources/config.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ Tweaks:
3939
Description_MarkHomesOnDynMap: Loads homes data from TreeboTeleport and makes markers available to DynMap
4040
MaintainFlightBetweenWorlds: true
4141
Description_MaintainFlightBetweenWorlds: Ensures flight status is toggled on during world change if it was toggled on prior to world change.
42+
BlockPortalsInWorldGuardRegions: true
43+
Description_BlockPortalsInWorldGuardRegions: Block portals from being created in a world guard protected region, both manually and as a result of using a portal in another dimension.
4244

4345
DynmapToInGameMapDetails:
4446
World: 'Survival'
4547
StartFromX: -16
4648
StartFromY: -16
47-
Dimensions: 4096
49+
Dimensions: 4096
50+
51+
AllowedPortalRegions:
52+
- Anything-In-This-List-Will-Allow-Portals-Even-If-Overlapping-A-Region-They-Would-Be-Denied-In

0 commit comments

Comments
 (0)