22
33import ch .njol .skript .lang .Expression ;
44import ch .njol .skript .lang .util .ContextlessEvent ;
5+ import ch .njol .skript .registrations .Classes ;
56import com .sovdee .oopsk .events .DynamicFieldEvalEvent ;
67import org .bukkit .event .Event ;
78import 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 *
0 commit comments