Skip to content

Commit 4e90a51

Browse files
committed
EffChange - if change is not allowed, check Type changers
1 parent fffc343 commit 4e90a51

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/main/java/io/github/syst3ms/skriptparser/effects/EffChange.java

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import io.github.syst3ms.skriptparser.log.ErrorType;
88
import io.github.syst3ms.skriptparser.parsing.ParseContext;
99
import io.github.syst3ms.skriptparser.registration.PatternInfos;
10+
import io.github.syst3ms.skriptparser.types.Type;
1011
import io.github.syst3ms.skriptparser.types.TypeManager;
1112
import io.github.syst3ms.skriptparser.types.changers.ChangeMode;
13+
import io.github.syst3ms.skriptparser.types.changers.Changer;
1214
import org.jetbrains.annotations.Nullable;
1315

1416
/**
@@ -55,6 +57,8 @@ public class EffChange extends Effect {
5557

5658
private Expression<?> changed;
5759
@Nullable
60+
private Changer<?> changer;
61+
@Nullable
5862
private Expression<?> changeWith;
5963
private ChangeMode mode;
6064

@@ -79,12 +83,36 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex
7983
if (changeWith == null) {
8084
assert mode == ChangeMode.DELETE || mode == ChangeMode.RESET;
8185
boolean present = changed.acceptsChange(mode).isPresent();
86+
if (!present) {
87+
Class<?> returnType = this.changed.getReturnType();
88+
Type<?> type = TypeManager.getByClass(returnType).orElse(null);
89+
if (type != null) {
90+
Changer<?> changer = type.getDefaultChanger().orElse(null);
91+
if (changer != null) {
92+
present = changer.acceptsChange(mode) != null;
93+
if (present) {
94+
this.changer = changer;
95+
}
96+
}
97+
}
98+
}
8299
if (!present) {
83100
logger.error("Cannot " + mode.name().toLowerCase() + " " + changedString, ErrorType.SEMANTIC_ERROR);
84101
}
85102
return present;
86103
} else {
87-
if (changed.acceptsChange(mode).isEmpty()) {
104+
boolean present = this.changed.acceptsChange(mode).isPresent();
105+
if (!present) {
106+
Type<?> changedType = TypeManager.getByClass(this.changed.getReturnType()).orElseThrow();
107+
Changer<?> changer = changedType.getDefaultChanger().orElse(null);
108+
if (changer != null) {
109+
present = changer.acceptsChange(mode) != null;
110+
if (present) {
111+
this.changer = changer;
112+
}
113+
}
114+
}
115+
if (!present) {
88116
switch (mode) {
89117
case SET:
90118
logger.error(changedString + " cannot be set to anything", ErrorType.SEMANTIC_ERROR);
@@ -100,10 +128,22 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex
100128
throw new IllegalStateException();
101129
}
102130
return false;
103-
} else if (!changed.acceptsChange(mode, changeWith)) {
104-
var type = TypeManager.getByClassExact(changeWith.getReturnType());
105-
assert type.isPresent();
106-
String changeTypeName = type.get().withIndefiniteArticle(!changeWith.isSingle());
131+
} else if (!this.changed.acceptsChange(mode, this.changeWith)) {
132+
Type<?> changeWithType = TypeManager.getByClassExact(this.changeWith.getReturnType()).orElseThrow();
133+
134+
Type<?> changedType = TypeManager.getByClass(this.changed.getReturnType()).orElseThrow();
135+
Changer<?> changer = changedType.getDefaultChanger().orElse(null);
136+
if (changer != null) {
137+
for (Class<?> aClass : changer.acceptsChange(mode)) {
138+
if (changeWithType.getTypeClass().isAssignableFrom(aClass)) {
139+
this.changer = changer;
140+
return true;
141+
}
142+
}
143+
144+
}
145+
146+
String changeTypeName = changeWithType.withIndefiniteArticle(!this.changeWith.isSingle());
107147
switch (mode) {
108148
case SET:
109149
logger.error(changedString + " cannot be set to " + changeTypeName, ErrorType.SEMANTIC_ERROR);
@@ -128,15 +168,29 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex
128168
@Override
129169
public void execute(TriggerContext ctx) {
130170
if (changeWith == null) {
131-
changed.change(ctx, mode, new Object[0]);
171+
if (this.changer != null) {
172+
applyChanger(this.changer, new Object[0], ctx);
173+
} else {
174+
this.changed.change(ctx, mode, new Object[0]);
175+
}
132176
} else {
133177
var values = changeWith.getValues(ctx);
134178
if (values == null || values.length == 0)
135179
return;
136-
changed.change(ctx, mode, values);
180+
if (this.changer != null) {
181+
applyChanger(this.changer, values, ctx);
182+
} else {
183+
this.changed.change(ctx, mode, values);
184+
}
137185
}
138186
}
139187

188+
@SuppressWarnings("unchecked")
189+
private <T> void applyChanger(Changer<T> changer, Object[] change, TriggerContext ctx) {
190+
T[] array = (T[]) this.changed.getArray(ctx);
191+
changer.change(array, change, this.mode);
192+
}
193+
140194
@Override
141195
public String toString(TriggerContext ctx, boolean debug) {
142196
String changedString = changed.toString(ctx, debug);
@@ -150,4 +204,5 @@ public String toString(TriggerContext ctx, boolean debug) {
150204
default -> throw new IllegalStateException();
151205
};
152206
}
207+
153208
}

0 commit comments

Comments
 (0)