fixed some major flaws in the flag system

This commit is contained in:
Redecouverte 2011-02-26 00:16:14 +01:00
parent 97b09aab34
commit 16fb6b4f9d
7 changed files with 84 additions and 71 deletions

View File

@ -92,9 +92,9 @@ private void registerEvents() {
pm.registerEvent(Event.Type.BLOCK_BURN, blockListener, Priority.High, this);
pm.registerEvent(Event.Type.REDSTONE_CHANGE, blockListener, Priority.High, this);
pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.High, this);
pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.High, this);
pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.High, this);
// pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.High, this);
// pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.High, this);
// pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.High, this);
pm.registerEvent(Event.Type.PLAYER_ITEM, playerListener, Priority.High, this);
pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, playerListener, Priority.High, this);
@ -107,7 +107,7 @@ private void registerEvents() {
pm.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Priority.Monitor, this);
// 25 equals about 1s real time
this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TimedFlagsTimer(this), 25 * 5, 25 * 5);
// this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TimedFlagsTimer(this), 25 * 5, 25 * 5);
}
/**
@ -135,4 +135,4 @@ public GlobalRegionManager getGlobalRegionManager() {
public WorldGuardConfiguration getWgConfiguration() {
return configuration;
}
}
}

View File

@ -42,7 +42,7 @@ public class ApplicableRegionSet {
private GlobalFlags global;
private Vector pt;
private Map<String, ProtectedRegion> applicable;
private List<ProtectedRegion> applicable;
/**
* Construct the object.
@ -51,7 +51,7 @@ public class ApplicableRegionSet {
* @param regions
* @param global
*/
public ApplicableRegionSet(Vector pt, Map<String, ProtectedRegion> applicable,
public ApplicableRegionSet(Vector pt, List<ProtectedRegion> applicable,
GlobalFlags global) {
this.pt = pt;
this.applicable = applicable;
@ -65,7 +65,33 @@ public ApplicableRegionSet(Vector pt, Map<String, ProtectedRegion> applicable,
* @return
*/
public boolean canBuild(LocalPlayer player) {
return isFlagAllowed(AreaFlags.FLAG_BUILD, global.canBuild, player);
if (this.applicable.size() < 1) {
return global.canBuild;
}
ProtectedRegion affectedRegion = getAffectedRegion();
if (affectedRegion == null) {
return global.canBuild;
}
String data = getAreaFlag("states", AreaFlags.FLAG_BUILD, true, null, affectedRegion);
State state;
try {
state = data != null ? State.valueOf(data) : State.DENY;
} catch (Exception e) {
state = State.DENY;
}
if (state != State.ALLOW && !affectedRegion.isMember(player)) {
return false;
}
return true;
}
/**
@ -97,7 +123,9 @@ public boolean allowsFlag(String flag) {
}
private boolean isFlagAllowed(String flag, boolean def, LocalPlayer player) {
return getStateAreaFlag("states", flag, def, player) == State.ALLOW;
State defState = def ? State.ALLOW : State.DENY;
return getStateAreaFlag("states", flag, defState, true, player) == State.ALLOW;
}
/**
@ -145,11 +173,6 @@ private boolean isFlagAllowed(String flag, boolean def, LocalPlayer player) {
}
// Forget about regions that are not covered
if (!region.contains(pt)) {
continue;
}
// Allow DENY to override everything
if (region.getFlags().getStateFlag(flag) == State.DENY) {
return false;
@ -200,24 +223,28 @@ private boolean isFlagAllowed(String flag, boolean def, LocalPlayer player) {
*/
public String getAreaFlag(String name, String subname, Boolean inherit, LocalPlayer player) {
ProtectedRegion childRegion = getChildRegion();
if (childRegion == null) {
return getAreaFlag(name, subname, inherit, player, getAffectedRegion());
}
private String getAreaFlag(String name, String subname, Boolean inherit, LocalPlayer player, ProtectedRegion affectedRegion) {
if (affectedRegion == null) {
return null;
}
if (player != null && !childRegion.isMember(player)) {
if (player != null && !affectedRegion.isMember(player)) {
return null;
}
if (!inherit) {
return childRegion.getFlags().getFlag(name, subname);
return affectedRegion.getFlags().getFlag(name, subname);
} else {
String value;
do {
value = childRegion.getFlags().getFlag(name, subname);
childRegion = childRegion.getParent();
value = affectedRegion.getFlags().getFlag(name, subname);
affectedRegion = affectedRegion.getParent();
} while (value == null && childRegion != null);
} while (value == null && affectedRegion != null);
return value;
}
@ -228,45 +255,28 @@ public String getAreaFlag(String name, String subname, Boolean inherit, LocalPla
* Gets the region with the hightest priority that is not a parent.
*
*/
public ProtectedRegion getChildRegion() {
public ProtectedRegion getAffectedRegion() {
int appSize = applicable.size();
if (appSize < 1) {
return null;
} else if (appSize < 2) {
for (Entry<String, ProtectedRegion> entry : applicable.entrySet()) {
return entry.getValue();
}
return applicable.get(0);
}
List<String> parents = new ArrayList<String>();
Iterator<Entry<String, ProtectedRegion>> iter = applicable.entrySet().iterator();
ProtectedRegion affectedRegion = null;
Iterator<ProtectedRegion> iter = applicable.iterator();
while (iter.hasNext()) {
ProtectedRegion region = iter.next().getValue();
ProtectedRegion parent = region.getParent();
ProtectedRegion region = iter.next();
if (parent == null) {
parents.add(region.getId());
} else {
parents.add(parent.getId());
if (affectedRegion == null || affectedRegion.getPriority() < region.getPriority()) {
affectedRegion = region;
}
}
ProtectedRegion childRegion = null;
iter = applicable.entrySet().iterator();
while (iter.hasNext()) {
ProtectedRegion region = iter.next().getValue();
if (!parents.contains(region.getId())) {
if (childRegion == null || childRegion.getPriority() < region.getPriority()) {
childRegion = region;
}
}
}
return childRegion;
return affectedRegion;
}
public String getAreaFlag(String name, String subname, String defaultValue, Boolean inherit, LocalPlayer player) {
@ -360,7 +370,7 @@ public State getStateAreaFlag(String name, String subname, State defaultValue, B
public Location getLocationAreaFlag(String name, Server server, Boolean inherit, LocalPlayer player) {
ProtectedRegion childRegion = getChildRegion();
ProtectedRegion childRegion = getAffectedRegion();
if (childRegion == null) {
return null;
}

View File

@ -93,7 +93,7 @@ public void run() {
//check greeting/farewell flag
ProtectedRegion newRegion = regions.getChildRegion();
ProtectedRegion newRegion = regions.getAffectedRegion();
String newRegionName = null;
if (newRegion != null) {

View File

@ -132,7 +132,7 @@ public ProtectedRegion getRegion(String id) {
*/
public ApplicableRegionSet getApplicableRegions(Vector pt) {
/* This would only allow parents that overlap with their child
List<ProtectedRegion> appRegions = new ArrayList<ProtectedRegion>();
for (Map.Entry<String,ProtectedRegion> entry : regions.entrySet()) {
@ -140,11 +140,8 @@ public ApplicableRegionSet getApplicableRegions(Vector pt) {
appRegions.add(entry.getValue());
}
}
*/
// this allows parents not to overlap with their childs
return new ApplicableRegionSet(pt, this.regions, global);
return new ApplicableRegionSet(pt, appRegions, global);
}
/**

View File

@ -146,7 +146,6 @@ public void removeRegion(String id) {
*/
public ApplicableRegionSet getApplicableRegions(Vector pt) {
/* This would only allow parents that overlap with their child
List<ProtectedRegion> appRegions = new ArrayList<ProtectedRegion>();
int x = pt.getBlockX();
@ -157,11 +156,10 @@ public ApplicableRegionSet getApplicableRegions(Vector pt) {
appRegions.add(region);
}
}
*
*/
// this allows parents not to overlap with their childs
return new ApplicableRegionSet(pt, this.regions, global);
return new ApplicableRegionSet(pt, appRegions, global);
}
/**

View File

@ -37,18 +37,20 @@ public enum State {
ALLOW,
DENY,
};
public static final String FLAG_PASSTHROUGH = "z";
public static final String FLAG_BUILD = "b";
public static final String FLAG_PVP = "p";
public static final String FLAG_MOB_DAMAGE = "m";
public static final String FLAG_CREEPER_EXPLOSION = "c";
public static final String FLAG_TNT = "t";
public static final String FLAG_LIGHTER = "l";
public static final String FLAG_FIRE_SPREAD = "f";
public static final String FLAG_LAVA_FIRE = "F";
public static final String FLAG_CHEST_ACCESS = "C";
public static final String FLAG_WATER_FLOW = "w";
public static final String FLAG_PASSTHROUGH = "passthrough";
public static final String FLAG_BUILD = "build";
public static final String FLAG_PVP = "pvp";
public static final String FLAG_MOB_DAMAGE = "mobdamage";
public static final String FLAG_CREEPER_EXPLOSION = "creeper";
public static final String FLAG_TNT = "tnt";
public static final String FLAG_LIGHTER = "llighter";
public static final String FLAG_FIRE_SPREAD = "firespread";
public static final String FLAG_LAVA_FIRE = "lavafirespread";
public static final String FLAG_CHEST_ACCESS = "chest";
public static final String FLAG_WATER_FLOW = "waterflow";
/**
* Get the user-friendly name of a flag. If a name isn't known, then
* the flag is returned unchanged.
@ -56,6 +58,7 @@ public enum State {
* @param flag
* @return
*/
/*
public static String getFlagName(String flag) {
if (flag.equals(FLAG_PASSTHROUGH)) {
return "passthrough";
@ -83,13 +86,14 @@ public static String getFlagName(String flag) {
return flag;
}
}
*/
/**
* Gets a flag from an alias. May return null.
*
* @param name
* @return
*/
/*
public static String fromAlias(String name) {
if (name.equalsIgnoreCase("passthrough")) {
return FLAG_PASSTHROUGH;
@ -116,7 +120,8 @@ public static String fromAlias(String name) {
} else {
return null;
}
}
}*/
private Map<String, Map<String, String>> flags = new HashMap<String, Map<String, String>>();
public Map<String, String> getFlagData(String name) {

View File

@ -78,6 +78,9 @@ public String getId() {
}
/*
* Important for serialization
*/
public String getParentId() {
if (this.parent != null) {