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.BLOCK_BURN, blockListener, Priority.High, this);
pm.registerEvent(Event.Type.REDSTONE_CHANGE, 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_DAMAGED, entityListener, Priority.High, this);
pm.registerEvent(Event.Type.ENTITY_EXPLODE, 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.CREATURE_SPAWN, entityListener, Priority.High, this);
pm.registerEvent(Event.Type.PLAYER_ITEM, playerListener, Priority.High, this); pm.registerEvent(Event.Type.PLAYER_ITEM, playerListener, Priority.High, this);
pm.registerEvent(Event.Type.PLAYER_DROP_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); pm.registerEvent(Event.Type.PLUGIN_ENABLE, serverListener, Priority.Monitor, this);
// 25 equals about 1s real time // 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);
} }
/** /**

View File

@ -42,7 +42,7 @@ public class ApplicableRegionSet {
private GlobalFlags global; private GlobalFlags global;
private Vector pt; private Vector pt;
private Map<String, ProtectedRegion> applicable; private List<ProtectedRegion> applicable;
/** /**
* Construct the object. * Construct the object.
@ -51,7 +51,7 @@ public class ApplicableRegionSet {
* @param regions * @param regions
* @param global * @param global
*/ */
public ApplicableRegionSet(Vector pt, Map<String, ProtectedRegion> applicable, public ApplicableRegionSet(Vector pt, List<ProtectedRegion> applicable,
GlobalFlags global) { GlobalFlags global) {
this.pt = pt; this.pt = pt;
this.applicable = applicable; this.applicable = applicable;
@ -65,7 +65,33 @@ public ApplicableRegionSet(Vector pt, Map<String, ProtectedRegion> applicable,
* @return * @return
*/ */
public boolean canBuild(LocalPlayer player) { 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) { 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 // Allow DENY to override everything
if (region.getFlags().getStateFlag(flag) == State.DENY) { if (region.getFlags().getStateFlag(flag) == State.DENY) {
return false; 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) { public String getAreaFlag(String name, String subname, Boolean inherit, LocalPlayer player) {
ProtectedRegion childRegion = getChildRegion(); return getAreaFlag(name, subname, inherit, player, getAffectedRegion());
if (childRegion == null) { }
private String getAreaFlag(String name, String subname, Boolean inherit, LocalPlayer player, ProtectedRegion affectedRegion) {
if (affectedRegion == null) {
return null; return null;
} }
if (player != null && !childRegion.isMember(player)) { if (player != null && !affectedRegion.isMember(player)) {
return null; return null;
} }
if (!inherit) { if (!inherit) {
return childRegion.getFlags().getFlag(name, subname); return affectedRegion.getFlags().getFlag(name, subname);
} else { } else {
String value; String value;
do { do {
value = childRegion.getFlags().getFlag(name, subname); value = affectedRegion.getFlags().getFlag(name, subname);
childRegion = childRegion.getParent(); affectedRegion = affectedRegion.getParent();
} while (value == null && childRegion != null); } while (value == null && affectedRegion != null);
return value; 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. * Gets the region with the hightest priority that is not a parent.
* *
*/ */
public ProtectedRegion getChildRegion() { public ProtectedRegion getAffectedRegion() {
int appSize = applicable.size(); int appSize = applicable.size();
if (appSize < 1) { if (appSize < 1) {
return null; return null;
} else if (appSize < 2) { } else if (appSize < 2) {
for (Entry<String, ProtectedRegion> entry : applicable.entrySet()) { return applicable.get(0);
return entry.getValue();
}
} }
List<String> parents = new ArrayList<String>(); ProtectedRegion affectedRegion = null;
Iterator<Entry<String, ProtectedRegion>> iter = applicable.entrySet().iterator(); Iterator<ProtectedRegion> iter = applicable.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
ProtectedRegion region = iter.next().getValue(); ProtectedRegion region = iter.next();
ProtectedRegion parent = region.getParent();
if (parent == null) { if (affectedRegion == null || affectedRegion.getPriority() < region.getPriority()) {
parents.add(region.getId()); affectedRegion = region;
} else {
parents.add(parent.getId());
} }
} }
ProtectedRegion childRegion = null; return affectedRegion;
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;
} }
public String getAreaFlag(String name, String subname, String defaultValue, Boolean inherit, LocalPlayer player) { 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) { public Location getLocationAreaFlag(String name, Server server, Boolean inherit, LocalPlayer player) {
ProtectedRegion childRegion = getChildRegion(); ProtectedRegion childRegion = getAffectedRegion();
if (childRegion == null) { if (childRegion == null) {
return null; return null;
} }

View File

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

View File

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

View File

@ -146,7 +146,6 @@ public void removeRegion(String id) {
*/ */
public ApplicableRegionSet getApplicableRegions(Vector pt) { public ApplicableRegionSet getApplicableRegions(Vector pt) {
/* This would only allow parents that overlap with their child
List<ProtectedRegion> appRegions = new ArrayList<ProtectedRegion>(); List<ProtectedRegion> appRegions = new ArrayList<ProtectedRegion>();
int x = pt.getBlockX(); int x = pt.getBlockX();
@ -157,11 +156,10 @@ public ApplicableRegionSet getApplicableRegions(Vector pt) {
appRegions.add(region); appRegions.add(region);
} }
} }
*
*/
// this allows parents not to overlap with their childs // 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,17 +37,19 @@ public enum State {
ALLOW, ALLOW,
DENY, DENY,
}; };
public static final String FLAG_PASSTHROUGH = "z";
public static final String FLAG_BUILD = "b"; public static final String FLAG_PASSTHROUGH = "passthrough";
public static final String FLAG_PVP = "p"; public static final String FLAG_BUILD = "build";
public static final String FLAG_MOB_DAMAGE = "m"; public static final String FLAG_PVP = "pvp";
public static final String FLAG_CREEPER_EXPLOSION = "c"; public static final String FLAG_MOB_DAMAGE = "mobdamage";
public static final String FLAG_TNT = "t"; public static final String FLAG_CREEPER_EXPLOSION = "creeper";
public static final String FLAG_LIGHTER = "l"; public static final String FLAG_TNT = "tnt";
public static final String FLAG_FIRE_SPREAD = "f"; public static final String FLAG_LIGHTER = "llighter";
public static final String FLAG_LAVA_FIRE = "F"; public static final String FLAG_FIRE_SPREAD = "firespread";
public static final String FLAG_CHEST_ACCESS = "C"; public static final String FLAG_LAVA_FIRE = "lavafirespread";
public static final String FLAG_WATER_FLOW = "w"; 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 * Get the user-friendly name of a flag. If a name isn't known, then
@ -56,6 +58,7 @@ public enum State {
* @param flag * @param flag
* @return * @return
*/ */
/*
public static String getFlagName(String flag) { public static String getFlagName(String flag) {
if (flag.equals(FLAG_PASSTHROUGH)) { if (flag.equals(FLAG_PASSTHROUGH)) {
return "passthrough"; return "passthrough";
@ -83,13 +86,14 @@ public static String getFlagName(String flag) {
return flag; return flag;
} }
} }
*/
/** /**
* Gets a flag from an alias. May return null. * Gets a flag from an alias. May return null.
* *
* @param name * @param name
* @return * @return
*/ */
/*
public static String fromAlias(String name) { public static String fromAlias(String name) {
if (name.equalsIgnoreCase("passthrough")) { if (name.equalsIgnoreCase("passthrough")) {
return FLAG_PASSTHROUGH; return FLAG_PASSTHROUGH;
@ -116,7 +120,8 @@ public static String fromAlias(String name) {
} else { } else {
return null; return null;
} }
} }*/
private Map<String, Map<String, String>> flags = new HashMap<String, Map<String, String>>(); private Map<String, Map<String, String>> flags = new HashMap<String, Map<String, String>>();
public Map<String, String> getFlagData(String name) { public Map<String, String> getFlagData(String name) {

View File

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