-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement WorldEdit restriction (#4)
- Loading branch information
Showing
7 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/de/t14d3/zones/integrations/FAWEIntegration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package de.t14d3.zones.integrations; | ||
|
||
import com.fastasyncworldedit.bukkit.regions.BukkitMaskManager; | ||
import com.fastasyncworldedit.core.regions.FaweMask; | ||
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||
import com.sk89q.worldedit.regions.CuboidRegion; | ||
import de.t14d3.zones.Region; | ||
import de.t14d3.zones.Zones; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.List; | ||
|
||
public class FAWEIntegration extends BukkitMaskManager { | ||
|
||
private final Zones plugin; | ||
|
||
public FAWEIntegration(final Zones plugin) { | ||
super(plugin.getName()); | ||
this.plugin = plugin; | ||
} | ||
|
||
public boolean isAllowed(Player player, Region region, MaskType type) { | ||
return region != null && | ||
(region.isAdmin(player.getUniqueId()) || | ||
type == MaskType.MEMBER && ( | ||
plugin.getPermissionManager().hasPermission(player.getUniqueId(), "PLACE", "true", region) | ||
|| plugin.getPermissionManager().hasPermission(player.getUniqueId(), "BREAK", "true", region))); | ||
} | ||
|
||
@Override | ||
public FaweMask getMask(final com.sk89q.worldedit.entity.Player wePlayer, final MaskType type, boolean isWhitelist) { | ||
final Player player = BukkitAdapter.adapt(wePlayer); | ||
final Location location = player.getLocation(); | ||
List<Region> regions = plugin.getRegionManager().getRegionsAt(location); | ||
if (!regions.isEmpty()) { | ||
boolean isAllowed = true; | ||
for (Region region : regions) { | ||
if (!isAllowed(player, region, type)) { | ||
isAllowed = false; | ||
break; | ||
} | ||
} | ||
if (isAllowed) { | ||
final Location pos1 = regions.get(0).getMin(); | ||
final Location pos2 = regions.get(0).getMax(); | ||
final Region region = regions.get(0); | ||
return new FaweMask(new CuboidRegion(BukkitAdapter.asBlockVector(pos1), BukkitAdapter.asBlockVector(pos2))) { | ||
@Override | ||
public boolean isValid(com.sk89q.worldedit.entity.Player player, MaskType type) { | ||
return isAllowed(BukkitAdapter.adapt(player), region, type); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/de/t14d3/zones/integrations/WorldEditExtent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.t14d3.zones.integrations; | ||
|
||
import com.sk89q.worldedit.WorldEditException; | ||
import com.sk89q.worldedit.extent.AbstractDelegateExtent; | ||
import com.sk89q.worldedit.extent.Extent; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldedit.regions.CuboidRegion; | ||
import com.sk89q.worldedit.world.block.BlockStateHolder; | ||
|
||
import java.util.Set; | ||
|
||
public class WorldEditExtent extends AbstractDelegateExtent { | ||
private final Set<CuboidRegion> mask; | ||
|
||
public WorldEditExtent(Set<CuboidRegion> mask, Extent extent) { | ||
super(extent); | ||
this.mask = mask; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException { | ||
if (!WorldEditSession.WorldEditUtils.maskContains(this.mask, location.x(), location.y(), location.z())) { | ||
return false; | ||
} | ||
return super.setBlock(location, block); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/de/t14d3/zones/integrations/WorldEditSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package de.t14d3.zones.integrations; | ||
|
||
import com.sk89q.worldedit.EditSession; | ||
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||
import com.sk89q.worldedit.event.extent.EditSessionEvent; | ||
import com.sk89q.worldedit.regions.CuboidRegion; | ||
import com.sk89q.worldedit.util.eventbus.Subscribe; | ||
import de.t14d3.zones.Region; | ||
import de.t14d3.zones.Zones; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class WorldEditSession { | ||
private final Zones plugin; | ||
private final WorldEditUtils utils; | ||
|
||
|
||
public WorldEditSession(Zones plugin) { | ||
this.plugin = plugin; | ||
this.utils = new WorldEditUtils(); | ||
} | ||
|
||
@Subscribe | ||
public void onEditSessionEvent(EditSessionEvent event) { | ||
if (event.getStage() == EditSession.Stage.BEFORE_REORDER) { | ||
Player player = null; | ||
if (event.getActor().isPlayer()) { | ||
player = Bukkit.getPlayer(event.getActor().getName()); | ||
} | ||
Set<CuboidRegion> mask = utils.getMask(player); | ||
event.setExtent(new WorldEditExtent(mask, event.getExtent())); | ||
} | ||
} | ||
|
||
public class WorldEditUtils { | ||
public HashSet<CuboidRegion> getMask(Player player) { | ||
HashSet<CuboidRegion> mask = new HashSet<>(); | ||
for (Region region : plugin.getRegionManager().regions().values()) { | ||
if ((plugin.getPermissionManager().hasPermission(player.getUniqueId(), "PLACE", "true", region) | ||
&& plugin.getPermissionManager().hasPermission(player.getUniqueId(), "BREAK", "true", region)) | ||
|| region.isAdmin(player.getUniqueId())) { | ||
mask.add(new CuboidRegion( | ||
BukkitAdapter.asBlockVector(region.getMin()), | ||
BukkitAdapter.asBlockVector(region.getMax().clone().subtract(1, 1, 1)) // Don't ask me why, but it works | ||
)); | ||
} | ||
} | ||
return mask; | ||
} | ||
|
||
public static boolean cuboidRegionContains(CuboidRegion region, int x, int y, int z) { | ||
return region.getMinimumPoint().x() <= x && region.getMaximumPoint().x() >= x && | ||
region.getMinimumPoint().y() <= y && region.getMaximumPoint().y() >= y && | ||
region.getMinimumPoint().z() <= z && region.getMaximumPoint().z() >= z; | ||
} | ||
|
||
public static boolean maskContains(Set<CuboidRegion> mask, int x, int y, int z) { | ||
for (CuboidRegion region : mask) { | ||
if (cuboidRegionContains(region, x, y, z)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters