Skip to content

Commit

Permalink
PropertyHolderObject tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 27, 2021
1 parent f9499d9 commit 653474b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
32 changes: 21 additions & 11 deletions src/main/java/com/denizenscript/denizencore/objects/Mechanism.java
Expand Up @@ -9,19 +9,19 @@ public class Mechanism {

private boolean fulfilled;
private String raw_mechanism;
private ElementTag value;
public ObjectTag value;

public TagContext context;

public boolean isProperty = false;

public Mechanism(ElementTag mechanism, ElementTag value) {
public Mechanism(ElementTag mechanism, ObjectTag value) {
fulfilled = false;
raw_mechanism = CoreUtilities.toLowerCase(mechanism.asString());
this.value = value;
}

public Mechanism(ElementTag mechanism, ElementTag value, TagContext context) {
public Mechanism(ElementTag mechanism, ObjectTag value, TagContext context) {
this(mechanism, value);
this.context = context;
}
Expand All @@ -42,15 +42,21 @@ public ElementTag getValue() {
if (value == null) {
return new ElementTag("");
}
return value;
return new ElementTag(value.toString());
}

public <T extends ObjectTag> T valueAsType(Class<T> dClass) {
return getValue().asType(dClass, context);
}

public boolean hasValue() {
return value != null && !value.asString().isEmpty();
if (value == null) {
return false;
}
if (value instanceof ElementTag && ((ElementTag) value).asString().isEmpty()) {
return false;
}
return true;
}

public boolean matches(String string) {
Expand All @@ -63,7 +69,7 @@ public boolean matches(String string) {

public String forMechanismText() {
return "For input to mechanism '" + raw_mechanism + "'"
+ (value == null ? "" : " with value '" + value.asString() + "'")
+ (value == null ? "" : " with value '" + value.toString() + "'")
+ ": ";
}

Expand Down Expand Up @@ -92,22 +98,26 @@ public <T extends ObjectTag> boolean requireObject(Class<T> type) {
}

public boolean requireBoolean(String error) {
if (hasValue() && value.isBoolean()) {
if (hasValue() && getValue().isBoolean()) {
return true;
}
echoError(error);
return false;
}

public boolean requireDouble(String error) {
if (hasValue() && value.isDouble()) {
if (hasValue() && getValue().isDouble()) {
return true;
}
echoError(error);
return false;
}

public boolean requireEnum(String error, boolean allowInt, Enum<?>... values) {
if (!hasValue()) {
return false;
}
ElementTag value = getValue();
if (hasValue() && allowInt && value.isInt() && value.asInt() < values.length) {
return true;
}
Expand All @@ -130,23 +140,23 @@ public boolean requireEnum(String error, boolean allowInt, Enum<?>... values) {
}

public boolean requireFloat(String error) {
if (hasValue() && value.isFloat()) {
if (hasValue() && getValue().isFloat()) {
return true;
}
echoError(error);
return false;
}

public boolean requireInteger(String error) {
if (hasValue() && value.isInt()) {
if (hasValue() && getValue().isInt()) {
return true;
}
echoError(error);
return false;
}

public <T extends ObjectTag> boolean requireObject(String error, Class<T> type) {
if (hasValue() && value.matchesType(type)) {
if (hasValue() && CoreUtilities.canPossiblyBeType(value, type)) {
return true;
}
if (error == null) {
Expand Down
@@ -1,5 +1,7 @@
package com.denizenscript.denizencore.objects.properties;

import com.denizenscript.denizencore.objects.Adjustable;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.MapTag;
Expand All @@ -9,6 +11,7 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.utilities.text.StringHolder;

import java.lang.invoke.*;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -239,4 +242,86 @@ public static List<Property> getProperties(ObjectTag object) {
}
return props;
}

public static <T extends Adjustable> void registerPropertyTagHandlers(ObjectTagProcessor<T> processor) {

// <--[tag]
// @attribute <PropertyHolderObject.with[<mechanism>=<value>;...]>
// @returns ObjectTag
// @group properties
// @description
// Returns a copy of the object with mechanism adjustments applied.
// -->
processor.registerTag("with", (attribute, object) -> {
if (!attribute.hasContext(1)) {
return null;
}
Adjustable instance = (Adjustable) object.duplicate();
List<String> properties = ObjectFetcher.separateProperties("[" + attribute.getContext(1) + "]");
for (int i = 1; i < properties.size(); i++) {
List<String> data = CoreUtilities.split(properties.get(i), '=', 2);
if (data.size() != 2) {
Debug.echoError("Invalid property string '" + properties.get(i) + "'!");
}
else {
instance.safeApplyProperty(new Mechanism(new ElementTag(data.get(0)), new ElementTag(data.get(1)), attribute.context));
}
}
return instance;
});

// <--[tag]
// @attribute <PropertyHolderObject.with_map[<property-map>]>
// @returns ObjectTag
// @group properties
// @description
// Returns a copy of the object with the MapTag of mechanism adjustments applied.
// -->
processor.registerTag("with_map", (attribute, object) -> {
if (!attribute.hasContext(1)) {
return null;
}
MapTag properties = attribute.contextAsType(1, MapTag.class);
Adjustable instance = (Adjustable) object.duplicate();
for (Map.Entry<StringHolder, ObjectTag> pair : properties.map.entrySet()) {
instance.safeApplyProperty(new Mechanism(new ElementTag(pair.getKey().low), pair.getValue(), attribute.context));
}
return instance;
});

// <--[tag]
// @attribute <PropertyHolderObject.supports[<property-name>]>
// @returns ElementTag(Boolean)
// @group properties
// @description
// Returns true if the property named is supported by the object.
// This does not necessarily mean it has a valid current value, just that it's supported at all.
// -->
processor.registerTag("supports", (attribute, object) -> {
if (!attribute.hasContext(1)) {
return null;
}
String propertyName = attribute.getContext(1);
ClassPropertiesInfo properties = propertiesByClass.get(object.getObjectTagClass());
if (properties == null) {
return new ElementTag(false);
}
PropertyGetter getter = properties.propertiesByMechanism.get(CoreUtilities.toLowerCase(propertyName));
if (getter == null) {
return new ElementTag(false);
}
return new ElementTag(getter.get(object) != null);
});

// <--[tag]
// @attribute <PropertyHolderObject.property_map>
// @returns MapTag
// @group properties
// @description
// Returns the object's property map.
// -->
processor.registerTag("property_map", (attribute, object) -> {
return getPropertiesMap(object);
});
}
}
Expand Up @@ -89,7 +89,7 @@ else if (!scriptEntry.hasObject("mechanism")
}
else if (arg.hasPrefix()) {
scriptEntry.addObject("mechanism", new ElementTag(arg.getPrefix().getValue()));
scriptEntry.addObject("mechanism_value", arg.asElement());
scriptEntry.addObject("mechanism_value", arg.object);
}
else {
scriptEntry.addObject("mechanism", arg.asElement());
Expand All @@ -113,7 +113,7 @@ else if (arg.hasPrefix()) {
specialAdjustables.put("system", UtilTagBase::adjustSystem);
}

public ObjectTag adjust(ObjectTag object, ElementTag mechanismName, ElementTag value, ScriptEntry entry) {
public ObjectTag adjust(ObjectTag object, ElementTag mechanismName, ObjectTag value, ScriptEntry entry) {
Mechanism mechanism = new Mechanism(mechanismName, value, entry.entryData.getTagContext());
return adjust(object, mechanism, entry);
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public ObjectTag adjust(ObjectTag object, Mechanism mechanism, ScriptEntry entry
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag mechanism = scriptEntry.getElement("mechanism");
ElementTag value = scriptEntry.getElement("mechanism_value");
ObjectTag value = scriptEntry.getObjectTag("mechanism_value");
ListTag objects = scriptEntry.getObjectTag("object");
MapTag mechanismMap = scriptEntry.getObjectTag("mechanism_map");
if (scriptEntry.dbCallShouldDebug()) {
Expand All @@ -191,7 +191,7 @@ public void execute(ScriptEntry scriptEntry) {
for (ObjectTag object : objects.objectForms) {
if (mechanismMap != null) {
for (Map.Entry<StringHolder, ObjectTag> entry : mechanismMap.map.entrySet()) {
object = adjust(object, new ElementTag(entry.getKey().str), new ElementTag(entry.getValue().toString()), scriptEntry);
object = adjust(object, new ElementTag(entry.getKey().str), entry.getValue(), scriptEntry);
}
}
else {
Expand Down

0 comments on commit 653474b

Please sign in to comment.