Skip to content

Commit

Permalink
Adds Tweak for blocking nether portal formation and teleport in World…
Browse files Browse the repository at this point in the history
…Guard regions.

Also adds a lisr to the config of excluded regions.
  • Loading branch information
ShakeforProtein committed Sep 1, 2021
1 parent 914181f commit ab90228
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ShakeforProtein</groupId>
<artifactId>TreeboTweaksSP</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
<packaging>jar</packaging>

<name>TreeboTweaksSP</name>
Expand Down Expand Up @@ -121,11 +121,13 @@
<groupId>us.dynmap</groupId>
<artifactId>DynmapCoreAPI</artifactId>
<version>3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>us.dynmap</groupId>
<artifactId>dynmap-api</artifactId>
<version>3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.ShakeforProtein</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.shakeforprotein.treeboroots.TreeboRoots;
import me.shakeforprotein.treebotweakssp.Tweaks.ASTHandler.Listeners.AstHandler;
import me.shakeforprotein.treebotweakssp.Tweaks.AutoBalancePvP.AutoBalancePvP;
import me.shakeforprotein.treebotweakssp.Tweaks.BlockPortalsInWorldGuardRegions.BlockPortalsInWorldGuardRegions;
import me.shakeforprotein.treebotweakssp.Tweaks.BlockPvPNearOwnClaims.BlockPvPNearClaims;
import me.shakeforprotein.treebotweakssp.Tweaks.BlockWithersNearClaims.BlockWither;
import me.shakeforprotein.treebotweakssp.Tweaks.DoubleDoors.DoubleDoors;
Expand Down Expand Up @@ -192,6 +193,9 @@ public void run() {
if(getConfig().getBoolean("Tweaks.MaintainFlightBetweenWorlds")){
Bukkit.getPluginManager().registerEvents(new MaintainFlightBetweenWorlds(this), this);
}
if(getConfig().getBoolean("Tweaks.BlockPortalsInWorldGuardRegions")){
Bukkit.getPluginManager().registerEvents(new BlockPortalsInWorldGuardRegions(this), this);
}
}

public void runCommandSynchronouslyLater(CommandSender sender, String command, long delay) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package me.shakeforprotein.treebotweakssp.Tweaks.BlockPortalsInWorldGuardRegions;

import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import jdk.nashorn.internal.codegen.ClassEmitter;
import me.shakeforprotein.treebotweakssp.TreeboTweaksSP;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.EntityPortalEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.inventory.ItemStack;

public class BlockPortalsInWorldGuardRegions implements Listener {

private TreeboTweaksSP pl;
private WorldGuard worldGuard;
private boolean wgEnabled = false;

public BlockPortalsInWorldGuardRegions(TreeboTweaksSP main) {
this.pl = main;
if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) {
Bukkit.getScheduler().runTaskLater(pl, () -> {
if (Bukkit.getPluginManager().getPlugin("WorldGuard").isEnabled()) {
wgEnabled = true;
worldGuard = WorldGuard.getInstance();
}
}, 5L);
}
}

@EventHandler
public void onPortalCreate(PortalCreateEvent event) {
if (!event.isCancelled()) {
for (BlockState blockState : event.getBlocks()) {
if (isInWgRegion(blockState.getLocation())) {
if (event.getEntity() instanceof Player) {
event.getEntity().sendMessage(pl.badge + "This portal was blocked as it would breach a protected region.");
}
event.setCancelled(true);
return;
}
}
}
}

@EventHandler
public void onPlayerUsePortal(PlayerPortalEvent event) {
if(!event.isCancelled()) {
if (event.getTo() != null && isInWgRegion(event.getTo())) {
event.getPlayer().sendMessage(pl.badge + "This portal has been blocked as the other end would form in a protected region.");
event.setCanCreatePortal(false);
event.setCancelled(true);
for(BlockFace face : BlockFace.values()){
if(event.getFrom().getBlock().getRelative(face).getType() == Material.NETHER_PORTAL){
event.getFrom().getBlock().getRelative(face).breakNaturally();
return;
}
}
}
}
}

@EventHandler
public void entityUsePortal(EntityPortalEvent event){
if(!event.isCancelled()){
if (event.getTo() != null && isInWgRegion(event.getTo())) {
event.setCancelled(true);
for(BlockFace face : BlockFace.values()){
if(event.getFrom().getBlock().getRelative(face).getType() == Material.NETHER_PORTAL){
event.getFrom().getBlock().getRelative(face).breakNaturally();
return;
}
}
}
}
}

private boolean isInWgRegion(Location loc) {
RegionContainer container = worldGuard.getPlatform().getRegionContainer();
RegionManager regions = container.get(BukkitAdapter.adapt(loc.getWorld()));


for(String allowedRegion : pl.getConfig().getStringList("AllowedPortalRegions")) {
if (regions != null && regions.size() > 0 && regions.hasRegion(allowedRegion) && allowedRegion.toLowerCase() != "__global__") {
if (regions.getRegion(allowedRegion).contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
return false;
}
}
}

for (String regionKey : regions.getRegions().keySet()) {
if (regionKey.toLowerCase() != "__global__") {
if (regions.getRegion(regionKey) != null && regions.getRegion(regionKey).contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
return true;
}
}
}

return false;
}
}
7 changes: 6 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ Tweaks:
Description_MarkHomesOnDynMap: Loads homes data from TreeboTeleport and makes markers available to DynMap
MaintainFlightBetweenWorlds: true
Description_MaintainFlightBetweenWorlds: Ensures flight status is toggled on during world change if it was toggled on prior to world change.
BlockPortalsInWorldGuardRegions: true
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.

DynmapToInGameMapDetails:
World: 'Survival'
StartFromX: -16
StartFromY: -16
Dimensions: 4096
Dimensions: 4096

AllowedPortalRegions:
- Anything-In-This-List-Will-Allow-Portals-Even-If-Overlapping-A-Region-They-Would-Be-Denied-In

0 comments on commit ab90228

Please sign in to comment.