|
14 | 14 | import org.byteskript.skript.lang.element.StandardElements; |
15 | 15 | import org.byteskript.skript.runtime.config.ConfigMap; |
16 | 16 |
|
| 17 | +import java.util.Map; |
| 18 | + |
17 | 19 | @Documentation( |
18 | 20 | name = "Save Config", |
19 | 21 | description = """ |
|
29 | 31 | public class SaveConfigEffect extends Effect { |
30 | 32 |
|
31 | 33 | public SaveConfigEffect() { |
32 | | - super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "save config %Object%"); |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - public void preCompile(Context context, Pattern.Match match) throws Throwable { |
37 | | - final MethodBuilder method = context.getMethod(); |
38 | | - method.writeCode(WriteInstruction.getField(System.class.getField("out"))); |
39 | | - super.preCompile(context, match); |
| 34 | + super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "save config %Object% to %Object%", "save config %Object%"); |
40 | 35 | } |
41 | 36 |
|
42 | 37 | @Override |
43 | 38 | public void compile(Context context, Pattern.Match match) throws Throwable { |
44 | 39 | final MethodBuilder method = context.getMethod(); |
45 | | - method.writeCode(WriteInstruction.cast(CommonTypes.CONFIG)); |
46 | | - method.writeCode(WriteInstruction.invokeVirtual(ConfigMap.class.getMethod("save"))); |
| 40 | + if (match.matchedPattern == 1) { |
| 41 | + method.writeCode(WriteInstruction.cast(CommonTypes.CONFIG)); |
| 42 | + method.writeCode(WriteInstruction.invokeVirtual(ConfigMap.class.getMethod("save"))); |
| 43 | + } else { |
| 44 | + method.writeCode(WriteInstruction.cast(CommonTypes.CONFIG)); // orig, new |
| 45 | + method.writeCode(WriteInstruction.duplicateDrop2()); // new, orig, new |
| 46 | + method.writeCode(WriteInstruction.swap()); // new, new, orig |
| 47 | + method.writeCode(WriteInstruction.cast(CommonTypes.CONFIG)); // new, new, orig |
| 48 | + method.writeCode(WriteInstruction.invokeVirtual(ConfigMap.class.getMethod("putAll", Map.class))); // new |
| 49 | + method.writeCode(WriteInstruction.invokeVirtual(ConfigMap.class.getMethod("save"))); |
| 50 | + } |
47 | 51 | context.setState(CompileState.CODE_BODY); |
48 | 52 | } |
49 | 53 |
|
|
0 commit comments