diff --git a/src/main/java/ch/njol/skript/effects/EffCopy.java b/src/main/java/ch/njol/skript/effects/EffCopy.java new file mode 100644 index 00000000000..37cb27e820b --- /dev/null +++ b/src/main/java/ch/njol/skript/effects/EffCopy.java @@ -0,0 +1,104 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.*; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.Variable; +import ch.njol.skript.registrations.Classes; +import ch.njol.skript.variables.Variables; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import java.util.Map; + +@Name("Copy") +@Description("Copies objects into a variable. When copying a list over to another list, the source list and its sublists are also copied over.") +@Examples("") +@Since("INSERT VERSION") +@Keywords({"copy", "clone"}) +public class EffCopy extends Effect { + + static { + Skript.registerEffect(EffCopy.class, "copy %~objects% [in]to %~objects%"); + } + + private Expression source; + private Variable destination; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (!(exprs[1] instanceof Variable)) { + Skript.error("You can only copy objects into variables"); + return false; + } + source = exprs[0]; + destination = (Variable) exprs[1]; + if (!source.isSingle() && destination.isSingle()) { + Skript.error("Cannot copy multiple objects into a single variable"); + return false; + } + return true; + } + + @Override + @SuppressWarnings("unchecked") + protected void execute(Event event) { + if (!(source instanceof Variable) || source.isSingle()) { + ChangeMode mode = ChangeMode.SET; + Object[] clone = (Object[]) Classes.clone(source.getArray(event)); + if (clone.length == 0) + mode = ChangeMode.DELETE; + destination.change(event, clone, mode); + return; + } + destination.change(event, null, ChangeMode.DELETE); + Map source = (Map) ((Variable) this.source).getRaw(event); + if (source == null) + return; + String target = destination.getName().getSingle(event); + copy(event, source, target.substring(0, target.length() - 3), destination.isLocal()); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "copy " + source.toString(event, debug) + " into " + destination.toString(event, debug); + } + + @SuppressWarnings("unchecked") + private static void copy(Event event, Map source, String targetName, boolean local) { + for (Map.Entry entry : source.entrySet()) { + if (entry.getKey() == null) + continue; + String node = targetName + Variable.SEPARATOR + entry.getKey(); + Object value = entry.getValue(); + if (value instanceof Map) { + copy(event, (Map) value, node, local); + return; + } + Variables.setVariable(node, value, event, local); + } + } + +}