Skip to content

Commit

Permalink
smarter showErrors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 22, 2022
1 parent a6fc02b commit 2335e14
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
Expand Up @@ -25,7 +25,7 @@ public class WebGetCommand extends AbstractCommand implements Holdable {

public WebGetCommand() {
setName("webget");
setSyntax("webget [<url>] (post:<data>) (headers:<map>) (timeout:<duration>/{10s}) (savefile:<path>) (hide_failure)");
setSyntax("webget [<url>] (data:<data>) (method:<method>) (headers:<map>) (timeout:<duration>/{10s}) (savefile:<path>) (hide_failure)");
setRequiredArguments(1, 7);
isProcedural = false;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/denizenscript/denizencore/tags/Attribute.java
Expand Up @@ -167,14 +167,22 @@ public Attribute(Attribute ref, ScriptEntry scriptEntry, TagContext context) {
this(ref, scriptEntry, context, 0);
}

private void setContext(TagContext context) {
if (context == null) {
context = CoreUtilities.basicContext;
}
this.context = context.clone();
this.context.showErrors = () -> !hasAlternative();
}

public Attribute(Attribute ref, ScriptEntry scriptEntry, TagContext context, int skippable) {
origin = ref.origin;
this.scriptEntry = scriptEntry;
this.context = context;
setContext(context);
attributes = ref.attributes;
contexts = new ObjectTag[attributes.length];
setHadAlternative(ref.hadAlternative);
if (context == null || context.debug) {
if (this.context.debug) {
filled = new int[attributes.length];
for (int i = 0; i < skippable; i++) {
filled[i] = 3;
Expand All @@ -186,10 +194,10 @@ public Attribute(Attribute ref, ScriptEntry scriptEntry, TagContext context, int
public Attribute(String attributes, ScriptEntry scriptEntry, TagContext context) throws TagProcessingException {
origin = attributes;
this.scriptEntry = scriptEntry;
this.context = context;
setContext(context);
this.attributes = separate_attributes(attributes);
contexts = new ObjectTag[this.attributes.length];
if (context == null || context.debug) {
if (this.context.debug) {
filled = new int[this.attributes.length];
}
}
Expand Down
Expand Up @@ -11,8 +11,6 @@

public class ReplaceableTagEvent {

private final TagContext context;

private boolean wasReplaced = false;

private String value_tagged = null;
Expand All @@ -26,9 +24,6 @@ public ObjectTag getReplacedObj() {
return replaced_obj;
}

////////////
// Constructors

public static class ReferenceData {

public Attribute attribs = null;
Expand All @@ -55,14 +50,10 @@ public static class ReferenceData {
public static HashMap<String, ReferenceData> refs = new HashMap<>();

public ReplaceableTagEvent(ReferenceData ref, String tag, TagContext context) {
// Reference context
this.context = context;

// If tag is not replaced, return the tag
// TODO: Possibly make this return "null" ... might break some
// scripts using tags incorrectly, but makes more sense overall
this.replaced_obj = new ElementTag(tag);

if (ref != null) {
mainRef = ref;
core_attributes = new Attribute(ref.attribs, context.entry, context, ref.skippable);
Expand All @@ -76,37 +67,25 @@ public ReplaceableTagEvent(String tag, TagContext context) throws TagProcessingE
return;
}
String otag = tag;

mainRef = new ReferenceData();

// Get alternative text
int alternativeLoc = locateAlternative(tag);

if (alternativeLoc >= 0) {
// get rid of the || at the alternative's start and any trailing spaces
mainRef.alternative = tag.substring(alternativeLoc + 2).trim();
// remove found alternative from tag
tag = tag.substring(0, alternativeLoc);
}

// Get value (if present)
int valueLoc = locateValue(tag);

if (valueLoc > 0) {
mainRef.value = tag.substring(valueLoc + 1);
tag = tag.substring(0, valueLoc);
}

// Alternatives are stripped, value is stripped, let's remember the raw tag for the attributer.
raw_tag = tag.trim();

// Use Attributes system to get type/subtype/etc. etc. for 'static/legacy' tags.
core_attributes = new Attribute(raw_tag, context.entry, context);
core_attributes.setHadAlternative(hasAlternative());

mainRef.attribs = new Attribute(core_attributes, null, null, 0);
mainRef.rawTag = raw_tag;

String startValue = getName();
mainRef.tagBase = TagManager.baseTags.get(startValue);
if (mainRef.tagBase == null) {
Expand Down Expand Up @@ -195,7 +174,7 @@ public String getName() {
@Deprecated
public String getValue() {
if (value_tagged == null) {
value_tagged = TagManager.tag(mainRef.value, context);
value_tagged = TagManager.tag(mainRef.value, core_attributes.context);
}
return value_tagged;
}
Expand All @@ -215,10 +194,10 @@ public ObjectTag getAlternative() {
}
core_attributes.fulfilled = index;
alternateBase = Attribute.fallbackTags.get(core_attributes.getAttributeWithoutParam(1));
return TagManager.readSingleTagObjectNoDebug(context, this);
return TagManager.readSingleTagObjectNoDebug(core_attributes.context, this);
}
if (mainRef.alternative != null) {
return TagManager.tagObject(mainRef.alternative, context);
return TagManager.tagObject(mainRef.alternative, core_attributes.context);
}
return null;
}
Expand All @@ -230,14 +209,8 @@ public boolean hasAlternative() {
return core_attributes.hasAlternative();
}

// Other internal mechanics

public TagContext getContext() {
return context;
}

public ScriptTag getScript() {
return context.script;
return core_attributes.context.script;
}

public boolean replaced() {
Expand All @@ -250,16 +223,9 @@ public void setReplacedObject(ObjectTag obj) {
}

public ScriptEntry getScriptEntry() {
return context.entry;
return core_attributes.context.entry;
}

/**
* Gets an Attribute object for easy parsing/reading
* of the different tag attributes.
*
* @return attributes
*/

public Attribute getAttributes() {
return core_attributes;
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/denizenscript/denizencore/tags/TagContext.java
Expand Up @@ -10,8 +10,18 @@
import com.denizenscript.denizencore.utilities.debugging.Debuggable;

public abstract class TagContext implements Debuggable, Cloneable {

@FunctionalInterface
public interface ShowErrorsMethod {
boolean showErrors();
}

private static boolean defaultShowErrors() {
return true;
}

public boolean debug;
public boolean showErrors;
public ShowErrorsMethod showErrors = TagContext::defaultShowErrors;
public ScriptEntry entry;
public ScriptTag script;
public DefinitionProvider definitionProvider;
Expand All @@ -29,7 +39,7 @@ public TagContext clone() {
}

public boolean showErrors() {
return debug || showErrors;
return debug || (showErrors != null && showErrors.showErrors());
}

@Override
Expand Down Expand Up @@ -69,4 +79,3 @@ public TagContext(boolean debug, ScriptEntry entry, ScriptTag script, Definition

public abstract ScriptEntryData getScriptEntryData();
}

Expand Up @@ -73,7 +73,7 @@ public void procedureTag(ReplaceableTagEvent event) {
definitions = attribute.contextAsType(2, ListTag.class);
attribute.fulfill(1);
}
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, event.getContext().getScriptEntryData(), null, (q) -> {
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, attribute.context.getScriptEntryData(), null, (q) -> {
q.procedural = true;
}, new DurationTag(0), null, definitions, script.getContainer());
if (queue == null) {
Expand Down
Expand Up @@ -29,8 +29,17 @@

public class CoreUtilities {

/**
* No debug, no errors.
*/
public static TagContext noDebugContext;
/**
* Debug and errors shown.
*/
public static TagContext basicContext;
/**
* No debug, yes errors.
*/
public static TagContext errorButNoDebugContext;

public static DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.US);
Expand Down

0 comments on commit 2335e14

Please sign in to comment.