2018-08-12 14:03:07 +00:00
|
|
|
package com.boydti.fawe.util;
|
|
|
|
|
|
|
|
import com.boydti.fawe.config.Settings;
|
|
|
|
import com.boydti.fawe.object.RegionWrapper;
|
|
|
|
import com.boydti.fawe.object.exception.FaweException;
|
|
|
|
import com.boydti.fawe.object.extent.NullExtent;
|
|
|
|
import com.boydti.fawe.regions.FaweMask;
|
|
|
|
import com.boydti.fawe.regions.FaweMaskManager;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-09-14 03:05:16 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2019-01-09 07:13:44 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.regions.Region;
|
2019-06-28 14:13:30 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2020-01-02 21:30:44 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.lang.reflect.Field;
|
2019-09-14 03:05:16 +00:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
public class WEManager {
|
|
|
|
|
|
|
|
public final static WEManager IMP = new WEManager();
|
|
|
|
|
|
|
|
public final ArrayDeque<FaweMaskManager> managers = new ArrayDeque<>();
|
|
|
|
|
2020-02-18 19:27:05 +00:00
|
|
|
public void cancelEditSafe(AbstractDelegateExtent parent, FaweException reason) throws FaweException {
|
2018-08-12 14:03:07 +00:00
|
|
|
try {
|
|
|
|
final Field field = AbstractDelegateExtent.class.getDeclaredField("extent");
|
|
|
|
field.setAccessible(true);
|
2020-02-18 19:27:05 +00:00
|
|
|
Extent currentExtent = parent.getExtent();
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!(currentExtent instanceof NullExtent)) {
|
|
|
|
field.set(parent, new NullExtent((Extent) field.get(parent), reason));
|
|
|
|
}
|
2019-07-25 18:44:10 +00:00
|
|
|
} catch (Exception e) {
|
2019-06-27 00:14:00 +00:00
|
|
|
e.printStackTrace();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-07-09 07:18:51 +00:00
|
|
|
throw reason;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 19:27:05 +00:00
|
|
|
public void cancelEdit(AbstractDelegateExtent parent, FaweException reason) throws WorldEditException {
|
2018-08-12 14:03:07 +00:00
|
|
|
cancelEditSafe(parent, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
2019-09-14 03:05:16 +00:00
|
|
|
public Region[] getMask(Player player) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return getMask(player, FaweMaskManager.MaskType.getDefaultMaskType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isIn(int x, int y, int z, Region region) {
|
2019-07-25 18:44:10 +00:00
|
|
|
return region.contains(x, y, z);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a player's mask
|
|
|
|
*
|
|
|
|
* @param player
|
|
|
|
* @return
|
|
|
|
*/
|
2019-09-14 03:05:16 +00:00
|
|
|
public Region[] getMask(Player player, FaweMaskManager.MaskType type) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!Settings.IMP.REGION_RESTRICTIONS || player.hasPermission("fawe.bypass") || player.hasPermission("fawe.bypass.regions")) {
|
|
|
|
return new Region[]{RegionWrapper.GLOBAL()};
|
|
|
|
}
|
2019-06-28 14:13:30 +00:00
|
|
|
Location loc = player.getLocation();
|
2019-06-06 01:17:34 +00:00
|
|
|
String world = player.getWorld().getName();
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!world.equals(player.getMeta("lastMaskWorld"))) {
|
|
|
|
player.deleteMeta("lastMaskWorld");
|
|
|
|
player.deleteMeta("lastMask");
|
|
|
|
}
|
|
|
|
player.setMeta("lastMaskWorld", world);
|
|
|
|
Set<FaweMask> masks = player.getMeta("lastMask");
|
|
|
|
Set<Region> backupRegions = new HashSet<>();
|
|
|
|
Set<Region> regions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
|
|
if (masks == null) {
|
|
|
|
masks = new HashSet<>();
|
|
|
|
} else {
|
|
|
|
synchronized (masks) {
|
|
|
|
boolean removed = false;
|
|
|
|
if (!masks.isEmpty()) {
|
2019-08-06 15:29:09 +00:00
|
|
|
Iterator<FaweMask> iterator = masks.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
FaweMask mask = iterator.next();
|
2018-08-12 14:03:07 +00:00
|
|
|
if (mask.isValid(player, type)) {
|
|
|
|
Region region = mask.getRegion();
|
2019-06-28 14:13:30 +00:00
|
|
|
if (region.contains(loc.toBlockPoint())) {
|
2018-08-12 14:03:07 +00:00
|
|
|
regions.add(region);
|
|
|
|
} else {
|
|
|
|
removed = true;
|
|
|
|
backupRegions.add(region);
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-02 21:30:44 +00:00
|
|
|
player.printDebug(TextComponent.of("Invalid Mask"));
|
2018-08-12 14:03:07 +00:00
|
|
|
removed = true;
|
2019-08-06 15:29:09 +00:00
|
|
|
iterator.remove();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 13:45:41 +00:00
|
|
|
if (!removed) return regions.toArray(new Region[0]);
|
2018-09-04 19:10:15 +00:00
|
|
|
masks.clear();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Set<FaweMask> tmpMasks = new HashSet<>();
|
2019-07-25 18:44:10 +00:00
|
|
|
for (FaweMaskManager manager : managers) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (player.hasPermission("fawe." + manager.getKey())) {
|
|
|
|
try {
|
2019-06-12 13:45:41 +00:00
|
|
|
if (manager.isExclusive() && !masks.isEmpty()) continue;
|
2018-08-12 14:03:07 +00:00
|
|
|
final FaweMask mask = manager.getMask(player, FaweMaskManager.MaskType.getDefaultMaskType());
|
|
|
|
if (mask != null) {
|
|
|
|
regions.add(mask.getRegion());
|
|
|
|
masks.add(mask);
|
2019-06-12 13:45:41 +00:00
|
|
|
if (manager.isExclusive()) break;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-01-02 21:30:44 +00:00
|
|
|
} else {
|
|
|
|
player.printError(TextComponent.of("Missing permission " + "fawe." + manager.getKey()));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 01:17:34 +00:00
|
|
|
regions.addAll(backupRegions);
|
2018-08-12 14:03:07 +00:00
|
|
|
if (!masks.isEmpty()) {
|
|
|
|
player.setMeta("lastMask", masks);
|
|
|
|
} else {
|
|
|
|
player.deleteMeta("lastMask");
|
|
|
|
}
|
2019-03-27 16:17:05 +00:00
|
|
|
return regions.toArray(new Region[0]);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-25 18:44:10 +00:00
|
|
|
public boolean intersects(Region region1, Region region2) {
|
2019-07-22 20:42:40 +00:00
|
|
|
BlockVector3 rg1P1 = region1.getMinimumPoint();
|
|
|
|
BlockVector3 rg1P2 = region1.getMaximumPoint();
|
|
|
|
BlockVector3 rg2P1 = region2.getMinimumPoint();
|
|
|
|
BlockVector3 rg2P2 = region2.getMaximumPoint();
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-07-25 18:44:10 +00:00
|
|
|
return rg1P1.getBlockX() <= rg2P2.getBlockX() && rg1P2.getBlockX() >= rg2P1.getBlockX() &&
|
|
|
|
rg1P1.getBlockZ() <= rg2P2.getBlockZ() &&
|
|
|
|
rg1P2.getBlockZ() >= rg2P1.getBlockZ();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 18:44:10 +00:00
|
|
|
public boolean regionContains(Region selection, HashSet<Region> mask) {
|
|
|
|
for (Region region : mask) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (this.intersects(region, selection)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|