Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reinstate filecopy command, better and smarter than ever
- Loading branch information
1 parent
b9229c4
commit 6b72cd8
Showing
4 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/net/aufdemrand/denizen/scripts/commands/core/FileCopyCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters