From e26f6b5e8d05c899b544afdc06fd21f91596b0a2 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 26 Jun 2011 12:32:01 -0700 Subject: [PATCH] Added 'entry' flag to deny entry. --- .../bukkit/WorldGuardPlayerListener.java | 38 +++++++-- .../protection/ApplicableRegionSet.java | 43 +++++++--- .../protection/flags/DefaultFlag.java | 14 ++-- .../protection/flags/RegionGroupFlag.java | 84 +++++++++++++++++-- .../protection/flags/StateFlag.java | 9 ++ 5 files changed, 157 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java index b19d87d6..62d31066 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java @@ -23,6 +23,8 @@ import java.util.Set; 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.Location; import org.bukkit.Material; @@ -236,10 +238,34 @@ public void onPlayerMove(PlayerMoveEvent event) { || event.getFrom().getBlockY() != event.getTo().getBlockY() || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { PlayerFlagState state = plugin.getFlagStateManager().getState(player); + LocalPlayer localPlayer = plugin.wrapPlayer(player); RegionManager mgr = plugin.getGlobalRegionManager().get(world); Vector pt = toVector(event.getTo()); 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 farewell = set.getFlag(DefaultFlag.FAREWELL_MESSAGE); Boolean notifyEnter = set.getFlag(DefaultFlag.NOTIFY_ENTER); @@ -821,15 +847,9 @@ public void onPlayerRespawn(PlayerRespawnEvent event) { if (group != null) { LocalPlayer localPlayer = plugin.wrapPlayer(player); - - if (group == RegionGroup.OWNERS) { - if (set.isOwnerOfAll(localPlayer)) { - event.setRespawnLocation(spawnLoc); - } - } else if (group == RegionGroup.MEMBERS) { - if (set.isMemberOfAll(localPlayer)) { - event.setRespawnLocation(spawnLoc); - } + + if (RegionGroupFlag.isMember(set, group, localPlayer)) { + event.setRespawnLocation(spawnLoc); } } else { event.setRespawnLocation(spawnLoc); diff --git a/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java b/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java index 1ab961c5..d7d62200 100644 --- a/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java +++ b/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java @@ -60,17 +60,27 @@ public ApplicableRegionSet(Collection applicable, * @return */ 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 * @return */ public boolean canUse(LocalPlayer player) { - return internalGetState(DefaultFlag.USE, player); + return internalGetState(DefaultFlag.USE, player, 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) { + return internalGetState(flag, null, null, null); } /** @@ -79,8 +89,8 @@ public boolean canUse(LocalPlayer player) { * @param flag * @return */ - public boolean allows(StateFlag flag) { - return internalGetState(flag, null); + 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. * - * @param def - * default state if there are no regions defined - * @param player - * null to not check owners and members + * @param flag flag to check + * @param player null to not check owners and members + * @param groupFlag group flag to check + * @param groupPlayer player to use for the group flag check * @return */ - private boolean internalGetState(StateFlag flag, LocalPlayer player) { + private boolean internalGetState(StateFlag flag, LocalPlayer player, + RegionGroupFlag groupFlag, + LocalPlayer groupPlayer) { boolean found = false; boolean allowed = false; // Used for ALLOW override boolean def = flag.getDefault(); @@ -186,6 +198,17 @@ private boolean internalGetState(StateFlag flag, LocalPlayer player) { && region.getFlag(DefaultFlag.PASSTHROUGH) == State.ALLOW) { 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); diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java index 19c39081..9112b104 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java @@ -21,8 +21,6 @@ import org.bukkit.entity.CreatureType; -import java.util.Set; - /** * * @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 SNOW_FALL = new StateFlag("snow-fall", 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 FAREWELL_MESSAGE = new StringFlag("farewell"); 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_AMOUNT = new IntegerFlag("heal-amount"); 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 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 DoubleFlag PRICE = new DoubleFlag("price"); public static final SetFlag BLOCKED_CMDS = new SetFlag("blocked-cmds", new CommandStringFlag(null)); @@ -71,8 +71,12 @@ public final class DefaultFlag { USE, PLACE_VEHICLE, GREET_MESSAGE, FAREWELL_MESSAGE, NOTIFY_ENTER, NOTIFY_LEAVE, DENY_SPAWN, HEAL_DELAY, HEAL_AMOUNT, TELE_LOC, 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() { } diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java index 0617e178..d0f7b4d5 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java @@ -18,6 +18,9 @@ */ 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 com.sk89q.worldguard.bukkit.WorldGuardPlugin; @@ -26,18 +29,28 @@ * @author sk89q */ public class RegionGroupFlag extends Flag { - + public static enum RegionGroup { MEMBERS, - OWNERS - } - - public RegionGroupFlag(String name, char legacyCode) { - super(name, legacyCode); + OWNERS, + NON_MEMBERS, + NON_OWNERS } - public RegionGroupFlag(String name) { + private RegionGroup def; + + public RegionGroupFlag(String name, char legacyCode, RegionGroup def) { + super(name, legacyCode); + this.def = def; + } + + public RegionGroupFlag(String name, RegionGroup def) { super(name); + this.def = def; + } + + public RegionGroup getDefault() { + return def; } @Override @@ -49,6 +62,10 @@ public RegionGroup parseInput(WorldGuardPlugin plugin, CommandSender sender, return RegionGroup.MEMBERS; } else if (input.equalsIgnoreCase("owners") || input.equalsIgnoreCase("owner")) { 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")) { return null; } else { @@ -63,6 +80,10 @@ public RegionGroup unmarshal(Object o) { return RegionGroup.MEMBERS; } else if (str.equalsIgnoreCase("owners")) { return RegionGroup.OWNERS; + } else if (str.equalsIgnoreCase("nonmembers")) { + return RegionGroup.NON_MEMBERS; + } else if (str.equalsIgnoreCase("nonowners")) { + return RegionGroup.NON_OWNERS; } else { return null; } @@ -74,9 +95,58 @@ public Object marshal(RegionGroup o) { return "members"; } else if (o == RegionGroup.OWNERS) { return "owners"; + } else if (o == RegionGroup.NON_MEMBERS) { + return "nonmembers"; + } else if (o == RegionGroup.NON_OWNERS) { + return "nonowners"; } else { 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; + } } diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/StateFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/StateFlag.java index 0b427366..b763317f 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/StateFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/StateFlag.java @@ -33,6 +33,7 @@ public enum State { } private boolean def; + private RegionGroupFlag groupFlag; public StateFlag(String name, char legacyCode, boolean def) { super(name, legacyCode); @@ -48,6 +49,14 @@ public boolean getDefault() { return def; } + public RegionGroupFlag getGroupFlag() { + return groupFlag; + } + + public void setGroupFlag(RegionGroupFlag groupFlag) { + this.groupFlag = groupFlag; + } + @Override public State parseInput(WorldGuardPlugin plugin, CommandSender sender, String input) throws InvalidFlagFormat {