Skip to content

Commit aa8ed4c

Browse files
committed
Merge branch 'master' of github.com:sk89q/commandhelper
2 parents ac82531 + 8909ae4 commit aa8ed4c

File tree

5 files changed

+79
-193
lines changed

5 files changed

+79
-193
lines changed

src/main/java/com/laytonsmith/core/Globals.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,6 @@ private Globals(){}
2020
public static Map<String, IVariable> global_ivar = new HashMap<String, IVariable>();
2121
public static Map<String, Construct> global_construct = new HashMap<String, Construct>();
2222

23-
/**
24-
* Sets a global variable. The ivar works as both the key and the value.
25-
* @param ivar
26-
* @deprecated Use {@link #SetGlobal(java.lang.String, com.laytonsmith.core.constructs.Construct)}
27-
* instead. This method will be removed in future versions.
28-
*/
29-
public static synchronized void SetGlobal(IVariable ivar){
30-
Map<String, IVariable> vars = global_ivar;//(HashMap<String, IVariable>)env.get("global_ivar");
31-
vars.put(ivar.getName(), ivar);
32-
}
33-
34-
/**
35-
* Gets a global variable. The ivar returned works as both the key and the value.
36-
* @param var
37-
* @return
38-
* @deprecated Use {@link #GetGlobalConstruct(java.lang.String)} instead. This method will
39-
* be removed in future versions.
40-
*/
41-
public static synchronized IVariable GetGlobalIVar(IVariable var){
42-
Map<String, IVariable> vars = global_ivar;//(HashMap<String, IVariable>)env.get("global_ivar");
43-
if(vars.containsKey(var.getName())){
44-
return vars.get(var.getName());
45-
} else {
46-
IVariable v = new IVariable(var.getDefinedType(), var.getName(), new CString("", Target.UNKNOWN), Target.UNKNOWN);
47-
vars.put(v.getName(), v);
48-
return v;
49-
}
50-
}
51-
5223
/**
5324
* Sets a variable in the global registry.
5425
* @param name The value name

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public MCItemStack item(Construct i, Target t) {
280280
if (item.get("type", t).val().contains(":")) {
281281
//We're using the combo addressing method
282282
String[] split = item.get("type", t).val().split(":");
283+
item = item.deepClone(t);
283284
item.set("type", split[0]);
284285
item.set("data", split[1]);
285286
}

src/main/java/com/laytonsmith/core/functions/DataHandling.java

Lines changed: 64 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,7 +2703,7 @@ public ExampleScript[] examples() throws ConfigCompileException {
27032703

27042704
@api
27052705
@seealso({_export.class})
2706-
public static class _import extends AbstractFunction implements Optimizable {
2706+
public static class _import extends AbstractFunction {
27072707

27082708
@Override
27092709
public String getName() {
@@ -2712,38 +2712,28 @@ public String getName() {
27122712

27132713
@Override
27142714
public Integer[] numArgs() {
2715-
return new Integer[]{Integer.MAX_VALUE};
2715+
return new Integer[]{1,2};
27162716
}
27172717

27182718
@Override
27192719
public String docs() {
2720-
return "mixed {ivar | key} This function imports a value from the global value"
2721-
+ " register. In the first mode, it looks for an ivariable with the specified"
2722-
+ " name, and stores the value in the variable, and returns void. The first"
2723-
+ " mode is deprecated, and should not be used. In the"
2724-
+ " second mode, it looks for a value stored with the specified key, and"
2725-
+ " returns that value. Items can be stored with the export function. If"
2726-
+ " the specified ivar doesn't exist, the ivar will be assigned an empty"
2727-
+ " string, and if the specified string key doesn't exist, null is returned."
2728-
+ " See the documentation on [[CommandHelper/import-export|imports/exports]]"
2729-
+ " for more information. import() is threadsafe.";
2720+
return "mixed {key, [default]} This function imports a value from the global value register. It looks for a"
2721+
+ " value stored with the specified key (using the export function), and returns that value."
2722+
+ " If specified key doesn't exist, it will return either null or the default value if specified."
2723+
+ " An array may be used as a key. It is converted into a string with the array values separated by"
2724+
+ " dots. import() is threadsafe.";
27302725
}
27312726

27322727
@Override
27332728
public ExceptionType[] thrown() {
2734-
return new ExceptionType[]{ExceptionType.IllegalArgumentException};
2729+
return new ExceptionType[]{ExceptionType.IllegalArgumentException, ExceptionType.IndexOverflowException};
27352730
}
27362731

27372732
@Override
27382733
public boolean isRestricted() {
27392734
return true;
27402735
}
27412736

2742-
@Override
2743-
public boolean preResolveVariables() {
2744-
return true;
2745-
}
2746-
27472737
@Override
27482738
public CHVersion since() {
27492739
return CHVersion.V3_3_0;
@@ -2756,57 +2746,31 @@ public Boolean runAsync() {
27562746

27572747
@Override
27582748
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
2759-
// if (args[0] instanceof IVariable) {
2760-
// //Mode 1
2761-
// IVariable var = (IVariable) args[0];
2762-
// environment.getEnv(GlobalEnv.class).GetVarList().set(Globals.GetGlobalIVar(var));
2763-
// return CVoid.VOID;
2764-
// } else {
2765-
//Mode 2
2766-
String key;
2767-
if(args.length == 1) {
2768-
if(!(args[0] instanceof CString)) {
2769-
throw new ConfigRuntimeException(this.getName() + " with 1 argument expects the argument to be a string.",
2770-
ExceptionType.IllegalArgumentException, t);
2771-
}
2772-
key = args[0].val();
2773-
} else {
2774-
// Handle the deprecated syntax.
2775-
key = GetNamespace(args, null, getName(), t);
2776-
}
2777-
return Globals.GetGlobalConstruct(key);
2778-
// }
2749+
String key;
2750+
if(args[0] instanceof CString){
2751+
key = args[0].val();
2752+
} else if(args[0] instanceof CArray){
2753+
key = GetNamespace((CArray) args[0], t);
2754+
} else {
2755+
throw new ConfigRuntimeException("Argument 1 in " + this.getName() + " must be a string or array.",
2756+
ExceptionType.IllegalArgumentException, t);
2757+
}
2758+
Construct c = Globals.GetGlobalConstruct(key);
2759+
if(args.length == 2 && c instanceof CNull){
2760+
c = args[1];
2761+
}
2762+
return c;
27792763
}
27802764

27812765
@Override
27822766
public ExampleScript[] examples() throws ConfigCompileException {
27832767
return new _export().examples();
27842768
}
2785-
2786-
@Override
2787-
public Set<OptimizationOption> optimizationOptions() {
2788-
return EnumSet.of(OptimizationOption.OPTIMIZE_DYNAMIC);
2789-
}
2790-
2791-
@Override
2792-
public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions) throws ConfigCompileException, ConfigRuntimeException {
2793-
if (children.size() > 2) {
2794-
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Automatic creation of namespaces is deprecated, and WILL be removed in the future."
2795-
+ " Use import('my.namespace') instead of import('my', 'namespace')", t);
2796-
}
2797-
// if (children.get(0).getData() instanceof IVariable) {
2798-
// CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "import(@ivar) usage is deprecated. Please use the @ivar = import('custom.name') format,"
2799-
// + " as this feature WILL be removed in the future.", t);
2800-
// }
2801-
//Just a compiler warning
2802-
return null;
2803-
}
2804-
28052769
}
28062770

28072771
@api
28082772
@seealso({_import.class})
2809-
public static class _export extends AbstractFunction implements Optimizable {
2773+
public static class _export extends AbstractFunction {
28102774

28112775
@Override
28122776
public String getName() {
@@ -2815,37 +2779,30 @@ public String getName() {
28152779

28162780
@Override
28172781
public Integer[] numArgs() {
2818-
return new Integer[]{Integer.MAX_VALUE};
2782+
return new Integer[]{2};
28192783
}
28202784

28212785
@Override
28222786
public String docs() {
2823-
return "void {ivar | key, value} Stores a value in the global storage register."
2824-
+ " When using the first mode, the ivariable is stored so it can be imported"
2825-
+ " later, and when using the second mode, an arbitrary value is stored with"
2826-
+ " the give key, and can be retreived using the secode mode of import. The first mode will"
2827-
+ " be deprecated in future versions, so should be avoided. If"
2828-
+ " the value is already stored, it is overwritten. See {{function|import}} and"
2829-
+ " [[CommandHelper/import-export|importing/exporting]]. The reference to the value"
2830-
+ " is stored, not a copy of the value, so in the case of arrays, manipulating the"
2831-
+ " contents of the array will manipulate the stored value. export() is threadsafe.";
2787+
return "void {key, value} Stores a value in the global storage register."
2788+
+ " An arbitrary value is stored with the given key, and can be retreived using import."
2789+
+ " If the value is already stored, it is overwritten. See {{function|import}}."
2790+
+ " The reference to the value is stored, not a copy of the value, so in the case of"
2791+
+ " arrays, manipulating the contents of the array will manipulate the stored value. An array may"
2792+
+ " be used as a key. It is converted into a string with the array values separated by dots."
2793+
+ " export() is threadsafe.";
28322794
}
28332795

28342796
@Override
28352797
public ExceptionType[] thrown() {
2836-
return new ExceptionType[]{ExceptionType.InsufficientArgumentsException, ExceptionType.IllegalArgumentException};
2798+
return new ExceptionType[]{ExceptionType.IllegalArgumentException, ExceptionType.IndexOverflowException};
28372799
}
28382800

28392801
@Override
28402802
public boolean isRestricted() {
28412803
return true;
28422804
}
28432805

2844-
@Override
2845-
public boolean preResolveVariables() {
2846-
return true;
2847-
}
2848-
28492806
@Override
28502807
public CHVersion since() {
28512808
return CHVersion.V3_3_0;
@@ -2858,75 +2815,41 @@ public Boolean runAsync() {
28582815

28592816
@Override
28602817
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
2861-
// if (args.length == 1) {
2862-
// if (args[0] instanceof IVariable) {
2863-
// IVariable cur = (IVariable) args[0];
2864-
// Globals.SetGlobal(environment.getEnv(GlobalEnv.class).GetVarList().get(cur.getName(), cur.getTarget()));
2865-
// } else {
2866-
// throw new ConfigRuntimeException("Expecting a IVariable when only one parameter is specified", ExceptionType.InsufficientArgumentsException, t);
2867-
// }
2868-
// } else {
2869-
String key;
2870-
if(args.length == 2) {
2871-
if(!(args[0] instanceof CString)) {
2872-
throw new ConfigRuntimeException(this.getName() + " with 2 arguments expects the first argument to be a string.",
2873-
ExceptionType.IllegalArgumentException, t);
2874-
}
2875-
key = args[0].val();
2876-
} else {
2877-
// Handle the deprecated syntax.
2878-
key = GetNamespace(args, args.length - 1, getName(), t);
2879-
}
2880-
Construct c = args[args.length - 1];
2881-
// //We want to store the value contained, not the ivar itself
2882-
// while (c instanceof IVariable) {
2883-
// c = environment.getEnv(GlobalEnv.class).GetVarList().get(((IVariable) c).getName(), t).ival();
2884-
// }
2885-
Globals.SetGlobal(key, c);
2886-
// }
2818+
String key;
2819+
if(args[0] instanceof CString){
2820+
key = args[0].val();
2821+
} else if(args[0] instanceof CArray){
2822+
key = GetNamespace((CArray) args[0], t);
2823+
} else {
2824+
throw new ConfigRuntimeException("Argument 1 in " + this.getName() + " must be a string or array.",
2825+
ExceptionType.IllegalArgumentException, t);
2826+
}
2827+
Construct c = args[1];
2828+
Globals.SetGlobal(key, c);
28872829
return CVoid.VOID;
28882830
}
28892831

28902832
@Override
28912833
public ExampleScript[] examples() throws ConfigCompileException {
28922834
return new ExampleScript[]{
2893-
// new ExampleScript("Deprecated usage", "@var = 2\n"
2894-
// + "export(@var)\n"
2895-
// + "@var = 0\n"
2896-
// + "# In other code, perhaps inside a proc, or another execution unit\n"
2897-
// + "import(@var)\n"
2898-
// + "msg(@var)"),
2899-
new ExampleScript("Basic usage", "@var = 2\n"
2900-
+ "export('custom.name', @var)\n"
2901-
+ "@var2 = import('custom.name')\n"
2902-
+ "msg(@var2)"),
2903-
new ExampleScript("Storage of references", "@array = array(1, 2, 3)\n"
2904-
+ "export('array', @array)\n"
2905-
+ "@array[0] = 4\n"
2906-
+ "@array2 = import('array')\n"
2907-
+ "msg(@array2)")
2835+
new ExampleScript("Basic usage", "@var = 2;\n"
2836+
+ "export('custom.name', @var);\n"
2837+
+ "@var2 = import('custom.name');\n"
2838+
+ "msg(@var2);"),
2839+
new ExampleScript("Storage of references", "@array = array(1, 2, 3);\n"
2840+
+ "export('array', @array);\n"
2841+
+ "@array[0] = 4;\n"
2842+
+ "@array2 = import('array');\n"
2843+
+ "msg(@array2);"),
2844+
new ExampleScript("Array key usage", "@key = array(custom, name);\n"
2845+
+ "export(@key, 'value');\n"
2846+
+ "@value = import(@key);\n"
2847+
+ "msg(@value);"),
2848+
new ExampleScript("Default value usage", "export('custom.name', null);\n"
2849+
+ "@value = import('custom.name', 'default value');\n"
2850+
+ "msg(@value);")
29082851
};
29092852
}
2910-
2911-
@Override
2912-
public Set<Optimizable.OptimizationOption> optimizationOptions() {
2913-
return EnumSet.of(Optimizable.OptimizationOption.OPTIMIZE_DYNAMIC);
2914-
}
2915-
2916-
@Override
2917-
public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions) throws ConfigCompileException, ConfigRuntimeException {
2918-
if (children.size() > 2) {
2919-
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Automatic creation of namespaces is deprecated, and WILL be removed in the future."
2920-
+ " Use export('my.namespace', @var) instead of export('my', 'namespace', @var)", t);
2921-
}
2922-
// if (children.get(0).getData() instanceof IVariable) {
2923-
// CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "export(@ivar) usage is deprecated. Please use the export('custom.name', @ivar) format,"
2924-
// + " as this feature WILL be removed in the future.", t);
2925-
// }
2926-
//Just a compiler warning
2927-
return null;
2928-
}
2929-
29302853
}
29312854

29322855
@api(environments = CommandHelperEnvironment.class)
@@ -3652,29 +3575,20 @@ public ExampleScript[] examples() throws ConfigCompileException {
36523575
}
36533576

36543577
/**
3655-
* Generates the namespace for this value, given an array of constructs. If
3656-
* the entire list of arguments isn't supposed to be part of the namespace,
3657-
* the value to be excluded may be specified.
3578+
* Generates the namespace for this value, given an array.
36583579
*
3659-
* @param args
3660-
* @param exclude
3580+
* @param array
36613581
* @return
36623582
*/
3663-
private static String GetNamespace(Construct[] args, Integer exclude, String name, Target t) {
3664-
if (exclude != null && args.length < 2 || exclude == null && args.length < 1) {
3665-
throw new ConfigRuntimeException(name + " was not provided with enough arguments. Check the documentation, and try again.", ExceptionType.InsufficientArgumentsException, t);
3666-
}
3583+
private static String GetNamespace(CArray array, Target t) {
36673584
boolean first = true;
36683585
StringBuilder b = new StringBuilder();
3669-
for (int i = 0; i < args.length; i++) {
3670-
if (exclude != null && exclude == i) {
3671-
continue;
3672-
}
3586+
for (int i = 0; i < array.size(); i++) {
36733587
if (!first) {
36743588
b.append(".");
36753589
}
36763590
first = false;
3677-
b.append(args[i].val());
3591+
b.append(array.get(i, t).val());
36783592
}
36793593
return b.toString();
36803594
}

src/main/java/com/laytonsmith/core/functions/DataTransformations.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.Reader;
2020
import java.io.StringReader;
2121
import java.io.StringWriter;
22+
import java.util.Collection;
2223
import java.util.Map;
2324
import java.util.Properties;
2425
import javax.xml.xpath.XPathExpressionException;
@@ -228,11 +229,10 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
228229
} catch(ScannerException | ParserException ex){
229230
cause = ex;
230231
}
231-
if(!(ret instanceof Map)){
232+
if(!(ret instanceof Map) && !(ret instanceof Collection)){
232233
throw new Exceptions.FormatException("Improperly formatted YML", t, cause);
233234
}
234-
Map<String, Object> map = (Map<String, Object>) ret;
235-
return Construct.GetConstruct(map);
235+
return Construct.GetConstruct(ret);
236236
}
237237

238238
@Override

0 commit comments

Comments
 (0)