Skip to content

Commit 2ef321a

Browse files
authored
copy expr and bug fix, bump to beta1 (#15)
1 parent f1261fc commit 2ef321a

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 1.0-alpha2
1+
version = 1.0-beta1

src/main/java/com/sovdee/oopsk/core/Struct.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.njol.skript.lang.Expression;
44
import ch.njol.skript.lang.util.ContextlessEvent;
5+
import ch.njol.skript.registrations.Classes;
56
import com.sovdee.oopsk.events.DynamicFieldEvalEvent;
67
import org.bukkit.event.Event;
78
import org.jetbrains.annotations.NotNull;
@@ -28,14 +29,33 @@ public class Struct {
2829
* @param event The event to evaluate the default values in.
2930
* @see StructManager#createStruct(StructTemplate, Event)
3031
*/
31-
Struct(@NotNull StructTemplate template, @Nullable Event event) {
32+
public Struct(@NotNull StructTemplate template, @Nullable Event event) {
3233
this.template = template;
3334
fieldValues = new HashMap<>();
3435
for (Field<?> field : template.getFields()) {
3536
fieldValues.put(field, field.defaultValue(event));
3637
}
3738
}
3839

40+
/**
41+
* Copy constructor for creating a struct that's a 'deep' copy of another struct.
42+
* Uses {@link Classes#clone(Object)} to clone the field values.
43+
*
44+
* @param source The struct to copy from.
45+
* @see StructManager#createStruct(StructTemplate, Event)
46+
*/
47+
public Struct(Struct source) {
48+
this.template = source.template;
49+
fieldValues = new HashMap<>();
50+
for (Map.Entry<Field<?>, Object[]> entry : source.fieldValues.entrySet()) {
51+
Field<?> field = entry.getKey();
52+
Object[] value = entry.getValue();
53+
// clone the value array
54+
Object[] clonedValue = (Object[]) Classes.clone(value);
55+
fieldValues.put(field, clonedValue);
56+
}
57+
}
58+
3959
/**
4060
* Creates a new struct with the given template and event. Allows a map of initial values to be set in the struct.
4161
*

src/main/java/com/sovdee/oopsk/elements/expressions/ExprFieldAccess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ private boolean updateFieldGuesses() {
130130
@Override
131131
protected Object[] get(Event event, Struct[] source) {
132132
if (source.length == 0)
133-
return null;
133+
return new Object[0];
134134
// get actual struct and template
135135
Struct struct = source[0];
136136
StructTemplate template = struct.getTemplate();
137137
// get the field
138138
Field<?> field = template.getField(fieldName);
139139
if (field == null) {
140140
error("Field " + fieldName + " not found in struct " + template.getName());
141-
return null;
141+
return new Object[0];
142142
}
143143
var value = struct.getFieldValue(field);
144144
// check type is accurate to what we claimed
@@ -153,7 +153,7 @@ protected Object[] get(Event event, Struct[] source) {
153153
// if the field is not valid, and the value is not convertible, error
154154
error("The " + field + " of " + struct + " is not the same type it claimed to be at parse time. " +
155155
"This likely was caused by template changes. Consider reloading this script.");
156-
return null;
156+
return new Object[0];
157157
}
158158
// get the value
159159
return value;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.sovdee.oopsk.elements.expressions;
2+
3+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
4+
import com.sovdee.oopsk.core.Struct;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
public class ExprStructCopy extends SimplePropertyExpression<Struct, Struct> {
8+
9+
static {
10+
register(ExprStructCopy.class, Struct.class, "[a] struct copy", "structs");
11+
}
12+
13+
@Override
14+
public @Nullable Struct convert(Struct struct) {
15+
return new Struct(struct);
16+
}
17+
18+
@Override
19+
public Class<? extends Struct> getReturnType() {
20+
return Struct.class;
21+
}
22+
23+
@Override
24+
protected String getPropertyName() {
25+
return "copy";
26+
}
27+
}

src/test/scripts/copies.sk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct copyable:
2+
copy_num: int
3+
copy_vectors: vectors
4+
5+
test "copy structs":
6+
set {_A} to a copyable struct:
7+
copy_num: 1
8+
copy_vectors: vector(1, 2, 3) and vector(4, 5, 6)
9+
10+
assert {_A}->copy_num is 1 with "copy_num was not set correctly"
11+
assert {_A}->copy_vectors is vector(1, 2, 3) and vector(4, 5, 6) with "copy_vectors was not set correctly"
12+
13+
set {_B} to a struct copy of {_A}
14+
assert {_B}->copy_num is 1 with "copy_num was not copied correctly"
15+
assert {_B}->copy_vectors is vector(1, 2, 3) and vector(4, 5, 6) with "copy_vectors was not copied correctly"
16+
17+
set {_B}->copy_num to 2
18+
assert {_A}->copy_num is 1 with "copy_num was changed in original after copying"
19+
assert {_B}->copy_num is 2 with "copy_num was not changed in copy"
20+
21+
set x of {_A}->copy_vectors to 10
22+
assert {_A}->copy_vectors is vector(10, 2, 3) and vector(10, 5, 6) with "copy_vectors was not changed in original"
23+
assert {_B}->copy_vectors is vector(1, 2, 3) and vector(4, 5, 6) with "copy_vectors was changed in copy"
24+
25+
set {_B}->copy_vectors to vector(7, 8, 9) and vector(10, 11, 12)
26+
assert {_A}->copy_vectors is vector(10, 2, 3) and vector(10, 5, 6) with "copy_vectors was changed in original after copying"
27+
assert {_B}->copy_vectors is vector(7, 8, 9) and vector(10, 11, 12) with "copy_vectors was not changed in copy"

0 commit comments

Comments
 (0)