Improved the API.

This commit is contained in:
sk89q 2011-06-26 22:50:21 -07:00
parent 110838715b
commit 80e706e962
4 changed files with 107 additions and 50 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.bukkit; package com.sk89q.worldguard.bukkit;
import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
@ -736,6 +737,20 @@ public boolean canBuild(Player player, Block block) {
return getGlobalRegionManager().canBuild(player, block); return getGlobalRegionManager().canBuild(player, block);
} }
/**
* Gets the region manager for a world.
*
* @param world world to get the region manager for
* @return the region manager or null if regions are not enabled
*/
public RegionManager getRegionManager(World world) {
if (!getGlobalStateManager().get(world).useRegions) {
return null;
}
return getGlobalRegionManager().get(world);
}
/** /**
* Replace macros in the text. * Replace macros in the text.
* *

View File

@ -31,8 +31,12 @@
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
/** /**
* Represents a setFlag of regions and their rules as applied to one point or * Represents a set of regions for a particular point or area and the rules
* region. * that are represented by that set. An instance of this can be used to
* query the value of a flag or check if a player can build in the respective
* region or point. This object contains the list of applicable regions and so
* the expensive search of regions that are in the desired area has already
* been completed.
* *
* @author sk89q * @author sk89q
*/ */
@ -57,47 +61,60 @@ public ApplicableRegionSet(Collection<ProtectedRegion> applicable,
* Checks if a player can build in an area. * Checks if a player can build in an area.
* *
* @param player * @param player
* @return * @return build ability
*/ */
public boolean canBuild(LocalPlayer player) { public boolean canBuild(LocalPlayer player) {
return internalGetState(DefaultFlag.BUILD, player, null, null); return internalGetState(DefaultFlag.BUILD, player, null, null);
} }
/** /**
* Checks if a player can use items in an area. * Checks if a player can use buttons and such in an area.
* *
* @param player * @param player
* @return * @return able to use items
*/ */
public boolean canUse(LocalPlayer player) { public boolean canUse(LocalPlayer player) {
return internalGetState(DefaultFlag.USE, player, null, null); return !allows(DefaultFlag.USE, player)
&& !canBuild(player);
} }
/** /**
* Gets the state of a state flag. This cannot be used for the build flag. * Gets the state of a state flag. This cannot be used for the build flag.
* *
* @param flag * @see #allows(com.sk89q.worldguard.protection.flags.StateFlag, com.sk89q.worldguard.LocalPlayer)
* @return * @deprecated use the {@link #allows(StateFlag, LocalPlayer)} that takes a player
* @param flag flag to check
* @return whether it is allowed
* @throws IllegalArgumentException if the build flag is given
*/ */
@Deprecated
public boolean allows(StateFlag flag) { public boolean allows(StateFlag flag) {
if (flag == DefaultFlag.BUILD) {
throw new IllegalArgumentException("Can't use build flag with allows()");
}
return internalGetState(flag, null, null, null); return internalGetState(flag, null, null, null);
} }
/** /**
* Gets the state of a state flag. This cannot be used for the build flag. * Gets the state of a state flag. This cannot be used for the build flag.
* *
* @param flag * @param flag flag to check
* @return * @param player player (used by some flags)
* @return whether the state is allows for it
* @throws IllegalArgumentException if the build flag is given
*/ */
public boolean allows(StateFlag flag, LocalPlayer player) { public boolean allows(StateFlag flag, LocalPlayer player) {
if (flag == DefaultFlag.BUILD) {
throw new IllegalArgumentException("Can't use build flag with allows()");
}
return internalGetState(flag, null, flag.getGroupFlag(), player); return internalGetState(flag, null, flag.getGroupFlag(), player);
} }
/** /**
* Indicates whether a player is an owner of all regions in this set. * Indicates whether a player is an owner of all regions in this set.
* *
* @param player * @param player player
* @return * @return whether the player is an owner of all regions
*/ */
public boolean isOwnerOfAll(LocalPlayer player) { public boolean isOwnerOfAll(LocalPlayer player) {
for (ProtectedRegion region : applicable) { for (ProtectedRegion region : applicable) {
@ -113,8 +130,8 @@ public boolean isOwnerOfAll(LocalPlayer player) {
* Indicates whether a player is an owner or member of all regions in * Indicates whether a player is an owner or member of all regions in
* this set. * this set.
* *
* @param player * @param player player
* @return * @return whether the player is a member of all regions
*/ */
public boolean isMemberOfAll(LocalPlayer player) { public boolean isMemberOfAll(LocalPlayer player) {
for (ProtectedRegion region : applicable) { for (ProtectedRegion region : applicable) {
@ -269,14 +286,18 @@ private void clearParents(Set<ProtectedRegion> needsClear,
} }
/** /**
* Gets the value of a flag. Do not use this for state flags. * Gets the value of a flag. Do not use this for state flags
* (use {@link #allows(StateFlag, LocalPlayer)} for that).
* *
* @param flag * @param flag flag to check
* @param <T> * @return value of the flag
* @param <V> * @throws IllegalArgumentException if a StateFlag is given
* @return
*/ */
public <T extends Flag<V>, V> V getFlag(T flag) { public <T extends Flag<V>, V> V getFlag(T flag) {
if (flag instanceof StateFlag) {
throw new IllegalArgumentException("Cannot use StateFlag with getFlag()");
}
int lastPriority = 0; int lastPriority = 0;
boolean found = false; boolean found = false;

View File

@ -209,6 +209,18 @@ public RegionManager get(World world) {
return manager; return manager;
} }
/**
* Returns whether the player can bypass.
*
* @param player
* @param world
* @return
*/
public boolean hasBypass(LocalPlayer player, World world) {
return player.hasPermission("worldguard.region.bypass."
+ world.getName());
}
/** /**
* Returns whether the player can bypass. * Returns whether the player can bypass.
* *

View File

@ -50,9 +50,10 @@ public RegionManager(ProtectionDatabase loader) {
} }
/** /**
* Load the list of regions. * Load the list of regions. If the regions do not load properly, then
* the existing list should be used (as stored previously).
* *
* @throws IOException * @throws IOException thrown on load error
*/ */
public void load() throws IOException { public void load() throws IOException {
loader.load(this); loader.load(this);
@ -61,78 +62,86 @@ public void load() throws IOException {
/** /**
* Save the list of regions. * Save the list of regions.
* *
* @throws IOException * @throws IOException thrown on save error
*/ */
public void save() throws IOException { public void save() throws IOException {
loader.save(this); loader.save(this);
} }
/** /**
* Get a list of protected regions. * Get a map of protected regions. Use one of the region manager methods
* if possible if working with regions.
* *
* @return * @return map of regions, with keys being region IDs (lowercase)
*/ */
public abstract Map<String, ProtectedRegion> getRegions(); public abstract Map<String, ProtectedRegion> getRegions();
/** /**
* Set a list of protected regions. * Set a list of protected regions. Keys should be lowercase in the given
* map fo regions.
* *
* @param regions * @param regions map of regions
*/ */
public abstract void setRegions(Map<String, ProtectedRegion> regions); public abstract void setRegions(Map<String, ProtectedRegion> regions);
/** /**
* Adds a region. * Adds a region. If a region by the given name already exists, then
* the existing region will be replaced.
* *
* @param region * @param region region to add
*/ */
public abstract void addRegion(ProtectedRegion region); public abstract void addRegion(ProtectedRegion region);
/** /**
* Return whether a region exists by an ID. * Return whether a region exists by an ID.
* *
* @param id * @param id id of the region, can be mixed-case
* @return * @return whether the region exists
*/ */
public abstract boolean hasRegion(String id); public abstract boolean hasRegion(String id);
/** /**
* Get a region by its ID. * Get a region by its ID.
* *
* @param id * @param id id of the region, can be mixed-case
* @return * @return region or null if it doesn't exist
*/ */
public abstract ProtectedRegion getRegion(String id); public abstract ProtectedRegion getRegion(String id);
/** /**
* Removes a region, including inheriting children. * Removes a region, including inheriting children.
* *
* @param id * @param id id of the region, can be mixed-case
*/ */
public abstract void removeRegion(String id); public abstract void removeRegion(String id);
/** /**
* Get an object for a point for rules to be applied with. * Get an object for a point for rules to be applied with. Use this
* in order to query for flag data or membership data for a given
* point. If checking multiple flags for a single location,
* *
* @param pt * @param pt point
* @return * @return applicable region set
*/ */
public abstract ApplicableRegionSet getApplicableRegions(Vector pt); public abstract ApplicableRegionSet getApplicableRegions(Vector pt);
/** /**
* Get an object for a point for rules to be applied with. * Get an object for a point for rules to be applied with. This gets
* a set for the given reason.
* *
* @param region * @deprecated not yet fully supported
* @return * @param region region
* @return regino set
*/ */
@Deprecated
public abstract ApplicableRegionSet getApplicableRegions( public abstract ApplicableRegionSet getApplicableRegions(
ProtectedRegion region); ProtectedRegion region);
/** /**
* Get a list of region IDs that contain a point. * Get a list of region IDs that contain a point.
* *
* @param pt * @param pt point
* @return * @return list of region Ids
*/ */
public abstract List<String> getApplicableRegionsIDs(Vector pt); public abstract List<String> getApplicableRegionsIDs(Vector pt);
@ -140,9 +149,9 @@ public abstract ApplicableRegionSet getApplicableRegions(
* Returns true if the provided region overlaps with any other region that * Returns true if the provided region overlaps with any other region that
* is not owned by the player. * is not owned by the player.
* *
* @param region * @param region region to check
* @param player * @param player player to check against
* @return * @return whether there is an overlap
*/ */
public abstract boolean overlapsUnownedRegion(ProtectedRegion region, public abstract boolean overlapsUnownedRegion(ProtectedRegion region,
LocalPlayer player); LocalPlayer player);
@ -150,15 +159,15 @@ public abstract boolean overlapsUnownedRegion(ProtectedRegion region,
/** /**
* Get the number of regions. * Get the number of regions.
* *
* @return * @return number of regions
*/ */
public abstract int size(); public abstract int size();
/** /**
* Get the number of regions for a player. * Get the number of regions for a player.
* *
* @param player * @param player player
* @return * @return name number of regions that a player owns
*/ */
public abstract int getRegionCountOfPlayer(LocalPlayer player); public abstract int getRegionCountOfPlayer(LocalPlayer player);
} }