Skip to content

Commit

Permalink
Upgrade Mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Dec 14, 2013
1 parent 627734a commit b69b105
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/Adjustable.java
Expand Up @@ -8,6 +8,6 @@ public interface Adjustable {
* @param mechanism the name of mechanism to change
* @param value the value to input into the mechanism
*/
public void adjust(Mechanism mechanism, Element value);
// TODO?: * @return a string result of the fetched attribute
public void adjust(Mechanism mechanism);

}
32 changes: 32 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -151,6 +151,38 @@ public String asString() {
return element;
}

public boolean isBoolean() {
return (element != null && (element.equalsIgnoreCase("true") || element.equalsIgnoreCase("false")));
}

public boolean isDouble() {
try {
if (Double.valueOf(element) != null)
return true;
} catch (Exception e) {}
return false;
}

public boolean isFloat() {
try {
if (Float.valueOf(element) != null)
return true;
} catch (Exception e) {}
return false;
}

public boolean isInt() {
try {
if (Integer.valueOf(element) != null)
return true;
} catch (Exception e) {}
return false;
}

public boolean isString() {
return (element != null && !element.isEmpty());
}

public boolean matchesType(Class<? extends dObject> dClass) {
return ObjectFetcher.checkMatch(dClass, element);
}
Expand Down
104 changes: 96 additions & 8 deletions src/main/java/net/aufdemrand/denizen/objects/Mechanism.java
@@ -1,27 +1,115 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.utilities.debugging.dB;

public class Mechanism {

boolean fulfilled;
String raw_mechanism;
String outcome = null;
private boolean fulfilled;
private String raw_mechanism;
private Element value;
private String outcome = null;

public Mechanism(String string) {
public Mechanism(Element mechanism, Element value) {
fulfilled = false;
raw_mechanism = string;
raw_mechanism = mechanism.asString();
this.value = value;
}

public void fulfill(String _outcome) {
fulfilled = true;
outcome = _outcome;
outcome = _outcome; // TODO: Return outcome somewhere?
}

public boolean fulfilled() {
return fulfilled;
}

public Element getValue() {
return value;
}

public boolean matches(String string) {
return (string.equalsIgnoreCase(raw_mechanism));
}

public boolean fulfilled() {
return fulfilled;
public boolean requireBoolean() {
return requireBoolean("Invalid boolean. Must specify TRUE or FALSE.");
}

public boolean requireDouble() {
return requireDouble("Invalid double specified.");
}

public boolean requireEnum(boolean allowInt, Enum<?>... values) {
return requireEnum("Invalid " + values[0].getDeclaringClass().getName() + "."
+ " Must specify valid name" + (allowInt ? " or number" : "") + ".", allowInt, values);
}

public boolean requireFloat() {
return requireFloat("Invalid float specified.");
}

public boolean requireInteger() {
return requireInteger("Invalid integer specified.");
}

public <T extends dObject> boolean requireObject(Class<T> type) {
return requireObject("Invalid " + type.getName() + " specified.", type);
}

public boolean requireBoolean(String error) {
fulfill("");
if (value.isBoolean())
return true;
dB.echoError(error);
return false;
}

public boolean requireDouble(String error) {
fulfill("");
if (value.isDouble())
return true;
dB.echoError(error);
return false;
}

public boolean requireEnum(String error, boolean allowInt, Enum<?>... values) {
fulfill("");
if (allowInt && value.isInt() && value.asInt() < values.length)
return true;
if (value.isString()) {
String raw_value = value.asString().toUpperCase();
for (Enum<?> check_value : values) {
if (raw_value.equals(check_value.name()))
return true;
}
}
dB.echoError(error);
return false;
}

public boolean requireFloat(String error) {
fulfill("");
if (value.isFloat())
return true;
dB.echoError(error);
return false;
}

public boolean requireInteger(String error) {
fulfill("");
if (value.isInt())
return true;
dB.echoError(error);
return false;
}

public <T extends dObject> boolean requireObject(String error, Class<T> type) {
fulfill("");
if (value.matchesType(type))
return true;
dB.echoError(error);
return false;
}

}
5 changes: 3 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dChunk.java
Expand Up @@ -264,7 +264,7 @@ public String getAttribute(Attribute attribute) {
}

@Override
public void adjust(Mechanism mechanism, Element value) {
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object dChunk
Expand All @@ -277,7 +277,6 @@ public void adjust(Mechanism mechanism, Element value) {
// -->
if (mechanism.matches("unload")) {
unload(true);
return;
}

// <--[mechanism]
Expand Down Expand Up @@ -336,6 +335,8 @@ public void adjust(Mechanism mechanism, Element value) {
return;
}

if (!mechanism.fulfilled())
dB.echoError("Invalid dChunk mechanism specified.");

}

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Expand Up @@ -652,8 +652,9 @@ public String getAttribute(Attribute attribute) {


@Override
public void adjust(Mechanism mechanism, Element value) {
public void adjust(Mechanism mechanism) {

Element value = mechanism.getValue();

// <--[mechanism]
// @object dCuboid
Expand All @@ -669,7 +670,7 @@ public void adjust(Mechanism mechanism, Element value) {
// -->
if (mechanism.matches("outset")) {
int mod = 1;
if (value != null)
if (value != null && mechanism.requireInteger("Invalid integer specified. Assuming '1'."));
mod = value.asInt();
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
Expand Down Expand Up @@ -697,7 +698,7 @@ public void adjust(Mechanism mechanism, Element value) {
// -->
if (mechanism.matches("expand")) {
int mod = 1;
if (value != null)
if (value != null && mechanism.requireInteger("Invalid integer specified. Assuming '1'."))
mod = value.asInt();
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
Expand Down Expand Up @@ -725,7 +726,7 @@ public void adjust(Mechanism mechanism, Element value) {
// -->
if (mechanism.matches("set_location")) {
int mod = 1;
if (value != null)
if (value != null && mechanism.requireInteger("Invalid integer specified. Assuming '1'."))
mod = value.asInt();
for (LocationPair pair : pairs) {
pair.low.add(-1 * mod, -1 * mod, -1 * mod);
Expand All @@ -739,7 +740,8 @@ public void adjust(Mechanism mechanism, Element value) {
return;
}


if (!mechanism.fulfilled())
dB.echoError("Invalid dCuboid mechanism specified.");

}
}

0 comments on commit b69b105

Please sign in to comment.