Skip to content

Commit

Permalink
Reinstate filecopy command, better and smarter than ever
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 25, 2016
1 parent b9229c4 commit 6b72cd8
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 7 deletions.
5 changes: 0 additions & 5 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -1609,11 +1609,6 @@ public Thread getMainThread() {
public boolean allowedToWebget() {
return Settings.allowWebget();
}

@Override
public boolean allowedToFilecopy() {
return Settings.allowFilecopy();
}
}


Expand Up @@ -1505,6 +1505,32 @@ public void registerCoreMembers() {
"FLY", "fly (cancel) [<entity>|...] (controller:<player>) (origin:<location>) (destinations:<location>|...) (speed:<#.#>) (rotationthreshold:<#.#>)", 1);


// <--[command]
// @Name FileCopy
// @Syntax filecopy [origin:<origin>] [destination:<destination>] (overwrite)
// @Required 2
// @Stable stable
// @Short Copies a file from one location to another.
// @Author mcmonkey
// @Group core
//
// @Description
// TODO: Document Command Details
// The starting directory is server/plugins/Denizen.
//
// @Tags
// <entry[saveName].success> returns whether the copy succeeded (if not, either an error or occurred, or there is an existing file in the destination.)
//
// @Usage
// Use to copy a custom YAML data file to a backup folder, overwriting any old backup of it that exists.
// - filecopy o:data/custom.yml d:data/backup/ overwrite save:copy
// - narrate "Copy success<&co> <entry[copy].success>"
//
// -->
registerCoreMember(FileCopyCommand.class,
"filecopy", "filecopy [origin:<origin>] [destination:<destination>] (overwrite)", 2);


// <--[command]
// @Name Follow
// @Syntax follow (followers:<entity>|...) (stop) (lead:<#.#>) (max:<#.#>) (speed:<#.#>) (target:<entity>) (allow_wander)
Expand Down
@@ -0,0 +1,105 @@
package net.aufdemrand.denizen.scripts.commands.core;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class FileCopyCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {


for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("origin")
&& arg.matchesPrefix("origin", "o")) {
scriptEntry.addObject("origin", arg.asElement());
}

else if (!scriptEntry.hasObject("destination")
&& arg.matchesPrefix("destination", "d")) {
scriptEntry.addObject("destination", arg.asElement());
}

else if (!scriptEntry.hasObject("overwrite")
&& arg.matches("overwrite")) {
scriptEntry.addObject("overwrite", new Element("true"));
}

else {
arg.reportUnhandled();
}
}

if (!scriptEntry.hasObject("origin")) {
throw new InvalidArgumentsException("Must have a valid origin!");
}

if (!scriptEntry.hasObject("destination")) {
throw new InvalidArgumentsException("Must have a valid destination!");
}

scriptEntry.defaultObject("overwrite", new Element("false"));
}

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element origin = scriptEntry.getElement("origin");
Element destination = scriptEntry.getElement("destination");
Element overwrite = scriptEntry.getElement("overwrite");

dB.report(scriptEntry, getName(), origin.debug() + destination.debug() + overwrite.debug());

if (!Settings.allowFilecopy()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy disabled by server administrator.");
scriptEntry.addObject("success", new Element("false"));
return;
}

File o = new File(DenizenAPI.getCurrentInstance().getDataFolder(), origin.asString());
File d = new File(DenizenAPI.getCurrentInstance().getDataFolder(), destination.asString());
boolean ow = overwrite.asBoolean();
boolean dexists = d.exists();
boolean disdir = d.isDirectory();

if (!o.exists()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy failed, origin does not exist!");
scriptEntry.addObject("success", new Element("false"));
return;
}
if (!Utilities.isSafeFile(d)) {
dB.echoError(scriptEntry.getResidingQueue(), "Can't copy files to there!");
scriptEntry.addObject("success", new Element("false"));
return;
}

if (dexists && !disdir && !ow) {
dB.echoDebug(scriptEntry, "File copy ignored, destination file already exists!");
scriptEntry.addObject("success", new Element("false"));
return;
}
try {
if (dexists && !disdir) {
d.delete();
}
FileUtils.copyFile(o, d);
scriptEntry.addObject("success", new Element("true"));
}
catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
scriptEntry.addObject("success", new Element("false"));
return;
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -6,7 +6,7 @@
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.blocks.SafeBlock;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
Expand All @@ -33,7 +33,10 @@ public class Utilities {

public static boolean isSafeFile(File f) {
try {
String lown = CoreUtilities.toLowerCase(f.getCanonicalPath());
String lown = CoreUtilities.toLowerCase(f.getCanonicalPath()).replace('\\', '/');
if (dB.verbose) {
dB.log("Checking file : " + lown);
}
if (lown.contains("denizen/config.yml")) {
return false;
}
Expand Down

0 comments on commit 6b72cd8

Please sign in to comment.