location flags are now supported in FlagInfo class

This commit is contained in:
Redecouverte 2011-02-25 18:42:27 +01:00
parent 40238a9d25
commit dcd379bce7
5 changed files with 141 additions and 125 deletions

View File

@ -80,68 +80,7 @@ public boolean handle(CommandSender sender, String senderName, String command, S
FlagInfo nfo = FlagInfo.getFlagInfo(nameStr, subnameStr);
if (nfo == null) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Unknown flag, or not supported in console mode.");
return true;
}
Player player = (Player) sender;
if (nameStr.equals("spawn")) {
if (valueStr.equals("set")) {
Location l = player.getLocation();
if (region.contains(BukkitUtil.toVector(l))) {
AreaFlags flags = region.getFlags();
flags.setFlag("spawn", "x", l.getX());
flags.setFlag("spawn", "y", l.getY());
flags.setFlag("spawn", "z", l.getZ());
flags.setFlag("spawn", "yaw", l.getYaw());
flags.setFlag("spawn", "pitch", l.getPitch());
flags.setFlag("spawn", "world", l.getWorld().getName());
sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag spawn set to current location");
} else {
player.sendMessage(ChatColor.RED + "You must set the teleport location inside the region it belongs to.");
}
} else if (valueStr.equals("delete")) {
AreaFlags flags = region.getFlags();
flags.setFlag("spawn", "x", (String) null);
flags.setFlag("spawn", "y", (String) null);
flags.setFlag("spawn", "z", (String) null);
flags.setFlag("spawn", "yaw", (String) null);
flags.setFlag("spawn", "pitch", (String) null);
flags.setFlag("spawn", "world", (String) null);
sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag spawn removed.");
} else {
player.sendMessage(ChatColor.RED + "Usage: /region flag <regionid> spawn <set|delete>");
}
} else if (nameStr.equals("teleport")) {
if (valueStr.equals("set")) {
Location l = player.getLocation();
if (region.contains(BukkitUtil.toVector(l))) {
AreaFlags flags = region.getFlags();
flags.setFlag("teleport", "x", l.getX());
flags.setFlag("teleport", "y", l.getY());
flags.setFlag("teleport", "z", l.getZ());
flags.setFlag("teleport", "world", l.getWorld().getName());
player.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag teleport set to current location");
} else {
player.sendMessage(ChatColor.RED + "You must set the teleport location inside the region it belongs to.");
}
} else if (valueStr.equals("delete")) {
AreaFlags flags = region.getFlags();
flags.setFlag("teleport", "x", (String) null);
flags.setFlag("teleport", "y", (String) null);
flags.setFlag("teleport", "z", (String) null);
flags.setFlag("teleport", "world", (String) null);
player.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag teleport removed.");
} else {
player.sendMessage(ChatColor.RED + "Usage: /region flag <regionid> teleport <set|delete>");
}
} else {
player.sendMessage(ChatColor.RED + "Unknown flag specified.");
}
return true;
sender.sendMessage(ChatColor.RED + "Unknown flag specified.");
}
boolean validValue = false;
@ -201,6 +140,40 @@ public boolean handle(CommandSender sender, String senderName, String command, S
}
break;
}
case LOCATION: {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Flag not supported in console mode.");
return true;
}
Player player = (Player) sender;
Location l = player.getLocation();
if (valueStr.equals("set")) {
if (region.contains(BukkitUtil.toVector(l))) {
region.getFlags().setLocationFlag("spawn", l);
validValue = true;
sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag " + nameStr + " set to current location");
} else {
player.sendMessage(ChatColor.RED + "You must set the " + nameStr + " location inside the region it belongs to.");
}
} else if (valueStr.equals("delete")) {
region.getFlags().setLocationFlag("spawn", null);
validValue = true;
sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated. Flag " + nameStr + " removed.");
}
if (validValue) {
mgr.save();
return true;
}
break;
}
default: {
validValue = false;
break;

View File

@ -6,13 +6,11 @@
import com.sk89q.worldguard.bukkit.WorldGuardConfiguration;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.sk89q.worldguard.bukkit.commands.CommandHandler.CommandHandlingException;
import com.sk89q.worldguard.protection.regionmanager.RegionManager;
import com.sk89q.worldguard.protection.regions.AreaFlags;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
/**
@ -41,22 +39,14 @@ public boolean handle(CommandSender sender, String senderName, String command, S
RegionManager mgr = cfg.getWorldGuardPlugin().getGlobalRegionManager().getRegionManager(player.getWorld().getName());
ProtectedRegion region = mgr.getRegion(id);
if (region != null) {
AreaFlags flags = region.getFlags();
Double x, y, z;
World world;
Location location = null;
if (spawn) {
x = flags.getDoubleFlag("spawn", "x");
y = flags.getDoubleFlag("spawn", "y");
z = flags.getDoubleFlag("spawn", "z");
world = cfg.getWorldGuardPlugin().getServer().getWorld(flags.getFlag("teleport", "world"));
location = region.getFlags().getLocationFlag(cfg.getWorldGuardPlugin().getServer(), "spawn");
} else {
x = flags.getDoubleFlag("teleport", "x");
y = flags.getDoubleFlag("teleport", "y");
z = flags.getDoubleFlag("teleport", "z");
world = cfg.getWorldGuardPlugin().getServer().getWorld(flags.getFlag("teleport", "world"));
location = region.getFlags().getLocationFlag(cfg.getWorldGuardPlugin().getServer(), "teleport");
}
if (x != null && y != null && z != null && world != null) {
Location location = new Location(world, x, y, z);
if (location != null) {
player.teleportTo(location);
return true;
} else {

View File

@ -29,7 +29,7 @@
public class FlagInfo {
public static enum FlagValueType { STRING, BOOLEAN, INT, FLOAT, DOUBLE, STATE };
public static enum FlagValueType { STRING, BOOLEAN, INT, FLOAT, DOUBLE, STATE, LOCATION };
public String name;
public String subName;
@ -58,6 +58,8 @@ public static enum FlagValueType { STRING, BOOLEAN, INT, FLOAT, DOUBLE, STATE };
flagList.add(new FlagInfo("waterflow", null, FlagValueType.STATE, "states", "waterflow"));
flagList.add(new FlagInfo("iconomy", "buyable", FlagValueType.BOOLEAN, "iconomy", "buyable"));
flagList.add(new FlagInfo("iconomy", "price", FlagValueType.INT, "iconomy", "price"));
flagList.add(new FlagInfo("spawn", "", FlagValueType.LOCATION, "spawn", ""));
flagList.add(new FlagInfo("teleport", "", FlagValueType.LOCATION, "teleport", ""));
}
public static FlagInfo getFlagInfo(String name, String subName) {

View File

@ -29,6 +29,8 @@
import com.sk89q.worldguard.protection.regions.AreaFlags.State;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Server;
/**
* Represents a set of regions and their rules as applied to one point.
@ -328,6 +330,40 @@ public Double getDoubleAreaFlag(String name, String subname, double defaultValue
return data != null ? Double.valueOf(data) : defaultValue;
}
public Location getLocationFlag(String name, Server server, Boolean inherit, LocalPlayer player) {
ProtectedRegion childRegion = getChildRegion();
if(childRegion == null)
{
return null;
}
if (player != null && !childRegion.isMember(player)) {
return null;
}
if(!inherit)
{
return childRegion.getFlags().getLocationFlag(server, name);
}
else
{
Location value;
do
{
value = childRegion.getFlags().getLocationFlag(server, name);
childRegion = childRegion.getParent();
} while(value == null && childRegion != null);
return value;
}
}
/**
* Clear a region's parents for isFlagAllowed().
*

View File

@ -15,13 +15,14 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldguard.protection.regions;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.Server;
/**
* Holds the flags for a region.
@ -29,12 +30,13 @@
* @author sk89q
*/
public class AreaFlags {
public enum State {
NONE,
ALLOW,
DENY,
};
public static final String FLAG_PASSTHROUGH = "z";
public static final String FLAG_BUILD = "b";
public static final String FLAG_PVP = "p";
@ -46,7 +48,7 @@ public enum State {
public static final String FLAG_LAVA_FIRE = "F";
public static final String FLAG_CHEST_ACCESS = "C";
public static final String FLAG_WATER_FLOW = "w";
/**
* Get the user-friendly name of a flag. If a name isn't known, then
* the flag is returned unchanged.
@ -81,7 +83,7 @@ public static String getFlagName(String flag) {
return flag;
}
}
/**
* Gets a flag from an alias. May return null.
*
@ -115,15 +117,11 @@ public static String fromAlias(String name) {
return null;
}
}
private Map<String, Map<String, String>> flags = new HashMap<String, Map<String, String>>();
private Map<String, String> getFlagData(String name)
{
private Map<String, String> getFlagData(String name) {
Map<String, String> ret = flags.get(name);
if(ret == null)
{
if (ret == null) {
ret = new HashMap<String, String>();
flags.put(name, ret);
}
@ -139,8 +137,7 @@ private Map<String, String> getFlagData(String name)
*/
public State get(String flag) {
String value = getFlagData("states").get(flag);
if(value == null)
{
if (value == null) {
return State.NONE;
}
State state = State.valueOf(value);
@ -163,26 +160,25 @@ public void set(String flag, State state) {
getFlagData("states").put(flag, state.toString());
}
}
public Set<Map.Entry<String, State>> entrySet() {
Map<String, State> ret = new HashMap<String, State>();
for(Map.Entry<String, String> entry : getFlagData("states").entrySet())
{
for (Map.Entry<String, String> entry : getFlagData("states").entrySet()) {
ret.put(entry.getKey(), State.valueOf(entry.getValue()));
}
return ret.entrySet();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof AreaFlags)) {
return false;
}
AreaFlags other = (AreaFlags)obj;
AreaFlags other = (AreaFlags) obj;
return other.flags.equals(this.flags);
}
@ -193,9 +189,7 @@ public int hashCode() {
return hash;
}
public void setFlag(String name, String subname, String value)
{
public void setFlag(String name, String subname, String value) {
if (value == null) {
getFlagData(name).remove(subname);
} else {
@ -203,8 +197,7 @@ public void setFlag(String name, String subname, String value)
}
}
public void setFlag(String name, String subname, Boolean value)
{
public void setFlag(String name, String subname, Boolean value) {
if (value == null) {
getFlagData(name).remove(subname);
} else {
@ -212,8 +205,7 @@ public void setFlag(String name, String subname, Boolean value)
}
}
public void setFlag(String name, String subname, Integer value)
{
public void setFlag(String name, String subname, Integer value) {
if (value == null) {
getFlagData(name).remove(subname);
} else {
@ -221,8 +213,7 @@ public void setFlag(String name, String subname, Integer value)
}
}
public void setFlag(String name, String subname, Float value)
{
public void setFlag(String name, String subname, Float value) {
if (value == null) {
getFlagData(name).remove(subname);
} else {
@ -230,8 +221,7 @@ public void setFlag(String name, String subname, Float value)
}
}
public void setFlag(String name, String subname, Double value)
{
public void setFlag(String name, String subname, Double value) {
if (value == null) {
getFlagData(name).remove(subname);
} else {
@ -239,49 +229,41 @@ public void setFlag(String name, String subname, Double value)
}
}
public String getFlag(String name, String subname)
{
public String getFlag(String name, String subname) {
return getFlagData(name).get(subname);
}
public String getFlag(String name, String subname, String defaultValue)
{
public String getFlag(String name, String subname, String defaultValue) {
String data = getFlagData(name).get(subname);
return data != null ? data : defaultValue;
}
public Boolean getBooleanFlag(String name, String subname)
{
public Boolean getBooleanFlag(String name, String subname) {
String data = getFlagData(name).get(subname);
return data != null ? Boolean.valueOf(data) : null;
}
public Boolean getBooleanFlag(String name, String subname, boolean defaultValue)
{
public Boolean getBooleanFlag(String name, String subname, boolean defaultValue) {
String data = getFlagData(name).get(subname);
return data != null ? Boolean.valueOf(data) : defaultValue;
}
public Integer getIntFlag(String name, String subname)
{
public Integer getIntFlag(String name, String subname) {
String data = getFlagData(name).get(subname);
return data != null ? Integer.valueOf(data) : null;
}
public Integer getIntFlag(String name, String subname, int defaultValue)
{
public Integer getIntFlag(String name, String subname, int defaultValue) {
String data = getFlagData(name).get(subname);
return data != null ? Integer.valueOf(data) : defaultValue;
}
public Float getFloatFlag(String name, String subname)
{
public Float getFloatFlag(String name, String subname) {
String data = getFlagData(name).get(subname);
return data != null ? Float.valueOf(data) : null;
}
public Float getFloatFlag(String name, String subname, float defaultValue)
{
public Float getFloatFlag(String name, String subname, float defaultValue) {
String data = getFlagData(name).get(subname);
return data != null ? Float.valueOf(data) : defaultValue;
}
@ -296,4 +278,37 @@ public Double getDoubleFlag(String name, String subname, double defaultValue) {
return data != null ? Double.valueOf(data) : defaultValue;
}
public Location getLocationFlag(Server server, String name) {
try {
Double x = Double.valueOf(getFlagData(name).get("x"));
Double y = Double.valueOf(getFlagData(name).get("y"));
Double z = Double.valueOf(getFlagData(name).get("z"));
Float yaw = Float.valueOf(getFlagData(name).get("yaw"));
Float pitch = Float.valueOf(getFlagData(name).get("pitch"));
String worldName = getFlagData(name).get("world");
Location l = new Location(server.getWorld(worldName), x, y, z, yaw, pitch);
return l;
} catch (Exception e) {
return null;
}
}
public void setLocationFlag(String name, Location l) {
if (l != null) {
getFlagData(name).put("x", new Double(l.getX()).toString());
getFlagData(name).put("y", new Double(l.getY()).toString());
getFlagData(name).put("z", new Double(l.getZ()).toString());
getFlagData(name).put("yaw", new Float(l.getYaw()).toString());
getFlagData(name).put("pitch", new Float(l.getPitch()).toString());
getFlagData(name).put("world", l.getWorld().getName());
} else {
getFlagData(name).put("x", null);
getFlagData(name).put("y", null);
getFlagData(name).put("z", null);
getFlagData(name).put("yaw", null);
getFlagData(name).put("pitch", null);
getFlagData(name).put("world", null);
}
}
}