Added 'entry' flag to deny entry.

This commit is contained in:
sk89q 2011-06-26 12:32:01 -07:00
parent 767c49f9fa
commit e26f6b5e8d
5 changed files with 157 additions and 31 deletions

View File

@ -23,6 +23,8 @@
import java.util.Set; import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -236,10 +238,34 @@ public void onPlayerMove(PlayerMoveEvent event) {
|| event.getFrom().getBlockY() != event.getTo().getBlockY() || event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
PlayerFlagState state = plugin.getFlagStateManager().getState(player); PlayerFlagState state = plugin.getFlagStateManager().getState(player);
LocalPlayer localPlayer = plugin.wrapPlayer(player);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
Vector pt = toVector(event.getTo()); Vector pt = toVector(event.getTo());
ApplicableRegionSet set = mgr.getApplicableRegions(pt); ApplicableRegionSet set = mgr.getApplicableRegions(pt);
boolean entryAllowed = set.allows(DefaultFlag.ENTRY, localPlayer);
if (!entryAllowed && !plugin.getGlobalRegionManager().hasBypass(player, world)) {
player.sendMessage(ChatColor.DARK_RED + "You are not permitted to enter this area.");
// Make sure that we don't get the player stuck
/*if (event.getFrom().getBlockX() == event.getTo().getBlockX()
&& event.getFrom().getBlockZ() == event.getTo().getBlockZ()
&& event.getFrom().getBlockY() > event.getTo().getBlockY()
&& BlockType.canPassThrough(event.getFrom().getBlock().getRelative(0, -1, 0).getTypeId())) {
event.setTo(world.getSpawnLocation());
player.sendMessage(ChatColor.GRAY + "Because you fell vertically into a forbidden area, you have been sent to spawn so as to not get stuck.");
return;
}*/
Location newLoc = event.getFrom();
newLoc.setX(newLoc.getBlockX() + 0.5);
newLoc.setY(newLoc.getBlockY());
newLoc.setZ(newLoc.getBlockZ() + 0.5);
event.setTo(newLoc);
return;
}
String greeting = set.getFlag(DefaultFlag.GREET_MESSAGE); String greeting = set.getFlag(DefaultFlag.GREET_MESSAGE);
String farewell = set.getFlag(DefaultFlag.FAREWELL_MESSAGE); String farewell = set.getFlag(DefaultFlag.FAREWELL_MESSAGE);
Boolean notifyEnter = set.getFlag(DefaultFlag.NOTIFY_ENTER); Boolean notifyEnter = set.getFlag(DefaultFlag.NOTIFY_ENTER);
@ -822,14 +848,8 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
if (group != null) { if (group != null) {
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = plugin.wrapPlayer(player);
if (group == RegionGroup.OWNERS) { if (RegionGroupFlag.isMember(set, group, localPlayer)) {
if (set.isOwnerOfAll(localPlayer)) { event.setRespawnLocation(spawnLoc);
event.setRespawnLocation(spawnLoc);
}
} else if (group == RegionGroup.MEMBERS) {
if (set.isMemberOfAll(localPlayer)) {
event.setRespawnLocation(spawnLoc);
}
} }
} else { } else {
event.setRespawnLocation(spawnLoc); event.setRespawnLocation(spawnLoc);

View File

@ -60,17 +60,17 @@ public ApplicableRegionSet(Collection<ProtectedRegion> applicable,
* @return * @return
*/ */
public boolean canBuild(LocalPlayer player) { public boolean canBuild(LocalPlayer player) {
return internalGetState(DefaultFlag.BUILD, player); return internalGetState(DefaultFlag.BUILD, player, null, null);
} }
/** /**
* Checks if a player can build in an area. * Checks if a player can use items in an area.
* *
* @param player * @param player
* @return * @return
*/ */
public boolean canUse(LocalPlayer player) { public boolean canUse(LocalPlayer player) {
return internalGetState(DefaultFlag.USE, player); return internalGetState(DefaultFlag.USE, player, null, null);
} }
/** /**
@ -80,7 +80,17 @@ public boolean canUse(LocalPlayer player) {
* @return * @return
*/ */
public boolean allows(StateFlag flag) { public boolean allows(StateFlag flag) {
return internalGetState(flag, null); return internalGetState(flag, null, null, null);
}
/**
* Gets the state of a state flag. This cannot be used for the build flag.
*
* @param flag
* @return
*/
public boolean allows(StateFlag flag, LocalPlayer player) {
return internalGetState(flag, null, flag.getGroupFlag(), player);
} }
/** /**
@ -119,13 +129,15 @@ public boolean isMemberOfAll(LocalPlayer player) {
/** /**
* Checks to see if a flag is permitted. * Checks to see if a flag is permitted.
* *
* @param def * @param flag flag to check
* default state if there are no regions defined * @param player null to not check owners and members
* @param player * @param groupFlag group flag to check
* null to not check owners and members * @param groupPlayer player to use for the group flag check
* @return * @return
*/ */
private boolean internalGetState(StateFlag flag, LocalPlayer player) { private boolean internalGetState(StateFlag flag, LocalPlayer player,
RegionGroupFlag groupFlag,
LocalPlayer groupPlayer) {
boolean found = false; boolean found = false;
boolean allowed = false; // Used for ALLOW override boolean allowed = false; // Used for ALLOW override
boolean def = flag.getDefault(); boolean def = flag.getDefault();
@ -187,6 +199,17 @@ private boolean internalGetState(StateFlag flag, LocalPlayer player) {
continue; continue;
} }
// Check group permissions
if (groupPlayer != null && groupFlag != null) {
RegionGroupFlag.RegionGroup group = region.getFlag(groupFlag);
if (group == null) {
group = groupFlag.getDefault();
}
if (!RegionGroupFlag.isMember(region, group, groupPlayer)) {
continue;
}
}
State v = region.getFlag(flag); State v = region.getFlag(flag);
// Allow DENY to override everything // Allow DENY to override everything

View File

@ -21,8 +21,6 @@
import org.bukkit.entity.CreatureType; import org.bukkit.entity.CreatureType;
import java.util.Set;
/** /**
* *
* @author sk89q * @author sk89q
@ -49,6 +47,8 @@ public final class DefaultFlag {
public static final StateFlag PLACE_VEHICLE = new StateFlag("vehicle-place", false); public static final StateFlag PLACE_VEHICLE = new StateFlag("vehicle-place", false);
public static final StateFlag SNOW_FALL = new StateFlag("snow-fall", true); public static final StateFlag SNOW_FALL = new StateFlag("snow-fall", true);
public static final StateFlag LEAF_DECAY = new StateFlag("leaf-decay", true); public static final StateFlag LEAF_DECAY = new StateFlag("leaf-decay", true);
public static final StateFlag ENTRY = new StateFlag("entry", true);
public static final RegionGroupFlag ENTRY_PERM = new RegionGroupFlag("entry-group", RegionGroupFlag.RegionGroup.NON_MEMBERS);
public static final StringFlag GREET_MESSAGE = new StringFlag("greeting"); public static final StringFlag GREET_MESSAGE = new StringFlag("greeting");
public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell"); public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell");
public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter"); public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter");
@ -57,9 +57,9 @@ public final class DefaultFlag {
public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay"); public static final IntegerFlag HEAL_DELAY = new IntegerFlag("heal-delay");
public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount"); public static final IntegerFlag HEAL_AMOUNT = new IntegerFlag("heal-amount");
public static final VectorFlag TELE_LOC = new VectorFlag("teleport"); public static final VectorFlag TELE_LOC = new VectorFlag("teleport");
public static final RegionGroupFlag TELE_PERM = new RegionGroupFlag("teleport-group"); public static final RegionGroupFlag TELE_PERM = new RegionGroupFlag("teleport-group", RegionGroupFlag.RegionGroup.MEMBERS);
public static final VectorFlag SPAWN_LOC = new VectorFlag("spawn"); public static final VectorFlag SPAWN_LOC = new VectorFlag("spawn");
public static final RegionGroupFlag SPAWN_PERM = new RegionGroupFlag("spawn-group"); public static final RegionGroupFlag SPAWN_PERM = new RegionGroupFlag("spawn-group", RegionGroupFlag.RegionGroup.MEMBERS);
public static final BooleanFlag BUYABLE = new BooleanFlag("buyable"); public static final BooleanFlag BUYABLE = new BooleanFlag("buyable");
public static final DoubleFlag PRICE = new DoubleFlag("price"); public static final DoubleFlag PRICE = new DoubleFlag("price");
public static final SetFlag<String> BLOCKED_CMDS = new SetFlag<String>("blocked-cmds", new CommandStringFlag(null)); public static final SetFlag<String> BLOCKED_CMDS = new SetFlag<String>("blocked-cmds", new CommandStringFlag(null));
@ -71,9 +71,13 @@ public final class DefaultFlag {
USE, PLACE_VEHICLE, GREET_MESSAGE, FAREWELL_MESSAGE, NOTIFY_ENTER, USE, PLACE_VEHICLE, GREET_MESSAGE, FAREWELL_MESSAGE, NOTIFY_ENTER,
NOTIFY_LEAVE, DENY_SPAWN, HEAL_DELAY, HEAL_AMOUNT, TELE_LOC, NOTIFY_LEAVE, DENY_SPAWN, HEAL_DELAY, HEAL_AMOUNT, TELE_LOC,
TELE_PERM, SPAWN_LOC, SPAWN_PERM, BUYABLE, PRICE, SNOW_FALL, LEAF_DECAY, TELE_PERM, SPAWN_LOC, SPAWN_PERM, BUYABLE, PRICE, SNOW_FALL, LEAF_DECAY,
GHAST_FIREBALL, BLOCKED_CMDS, ALLOWED_CMDS GHAST_FIREBALL, BLOCKED_CMDS, ALLOWED_CMDS, ENTRY, ENTRY_PERM
}; };
static {
ENTRY.setGroupFlag(ENTRY_PERM);
}
private DefaultFlag() { private DefaultFlag() {
} }

View File

@ -18,6 +18,9 @@
*/ */
package com.sk89q.worldguard.protection.flags; package com.sk89q.worldguard.protection.flags;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -29,15 +32,25 @@ public class RegionGroupFlag extends Flag<RegionGroupFlag.RegionGroup> {
public static enum RegionGroup { public static enum RegionGroup {
MEMBERS, MEMBERS,
OWNERS OWNERS,
NON_MEMBERS,
NON_OWNERS
} }
public RegionGroupFlag(String name, char legacyCode) { private RegionGroup def;
public RegionGroupFlag(String name, char legacyCode, RegionGroup def) {
super(name, legacyCode); super(name, legacyCode);
this.def = def;
} }
public RegionGroupFlag(String name) { public RegionGroupFlag(String name, RegionGroup def) {
super(name); super(name);
this.def = def;
}
public RegionGroup getDefault() {
return def;
} }
@Override @Override
@ -49,6 +62,10 @@ public RegionGroup parseInput(WorldGuardPlugin plugin, CommandSender sender,
return RegionGroup.MEMBERS; return RegionGroup.MEMBERS;
} else if (input.equalsIgnoreCase("owners") || input.equalsIgnoreCase("owner")) { } else if (input.equalsIgnoreCase("owners") || input.equalsIgnoreCase("owner")) {
return RegionGroup.OWNERS; return RegionGroup.OWNERS;
} else if (input.equalsIgnoreCase("nonowners") || input.equalsIgnoreCase("nonowner")) {
return RegionGroup.NON_OWNERS;
} else if (input.equalsIgnoreCase("nonmembers") || input.equalsIgnoreCase("nonmember")) {
return RegionGroup.NON_MEMBERS;
} else if (input.equalsIgnoreCase("everyone") || input.equalsIgnoreCase("anyone")) { } else if (input.equalsIgnoreCase("everyone") || input.equalsIgnoreCase("anyone")) {
return null; return null;
} else { } else {
@ -63,6 +80,10 @@ public RegionGroup unmarshal(Object o) {
return RegionGroup.MEMBERS; return RegionGroup.MEMBERS;
} else if (str.equalsIgnoreCase("owners")) { } else if (str.equalsIgnoreCase("owners")) {
return RegionGroup.OWNERS; return RegionGroup.OWNERS;
} else if (str.equalsIgnoreCase("nonmembers")) {
return RegionGroup.NON_MEMBERS;
} else if (str.equalsIgnoreCase("nonowners")) {
return RegionGroup.NON_OWNERS;
} else { } else {
return null; return null;
} }
@ -74,9 +95,58 @@ public Object marshal(RegionGroup o) {
return "members"; return "members";
} else if (o == RegionGroup.OWNERS) { } else if (o == RegionGroup.OWNERS) {
return "owners"; return "owners";
} else if (o == RegionGroup.NON_MEMBERS) {
return "nonmembers";
} else if (o == RegionGroup.NON_OWNERS) {
return "nonowners";
} else { } else {
return null; return null;
} }
} }
public static boolean isMember(ProtectedRegion region, RegionGroup group, LocalPlayer player) {
if (group == RegionGroupFlag.RegionGroup.OWNERS) {
if (region.isOwner(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.MEMBERS) {
if (region.isMember(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.NON_OWNERS) {
if (!region.isOwner(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.NON_MEMBERS) {
if (!region.isMember(player)) {
return true;
}
}
return false;
}
public static boolean isMember(ApplicableRegionSet set,
RegionGroup group, LocalPlayer player) {
if (group == RegionGroupFlag.RegionGroup.OWNERS) {
if (set.isOwnerOfAll(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.MEMBERS) {
if (set.isMemberOfAll(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.NON_OWNERS) {
if (!set.isOwnerOfAll(player)) {
return true;
}
} else if (group == RegionGroupFlag.RegionGroup.NON_MEMBERS) {
if (!set.isMemberOfAll(player)) {
return true;
}
}
return false;
}
} }

View File

@ -33,6 +33,7 @@ public enum State {
} }
private boolean def; private boolean def;
private RegionGroupFlag groupFlag;
public StateFlag(String name, char legacyCode, boolean def) { public StateFlag(String name, char legacyCode, boolean def) {
super(name, legacyCode); super(name, legacyCode);
@ -48,6 +49,14 @@ public boolean getDefault() {
return def; return def;
} }
public RegionGroupFlag getGroupFlag() {
return groupFlag;
}
public void setGroupFlag(RegionGroupFlag groupFlag) {
this.groupFlag = groupFlag;
}
@Override @Override
public State parseInput(WorldGuardPlugin plugin, CommandSender sender, public State parseInput(WorldGuardPlugin plugin, CommandSender sender,
String input) throws InvalidFlagFormat { String input) throws InvalidFlagFormat {