Skip to content

Commit

Permalink
create file/dir fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tim740 committed Jul 25, 2016
1 parent 68dba81 commit c82332e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: An addon for skript that adds conversions, utilities, files and muc
author: tim740
website: https://tim740.github.io/
main: uk.tim740.skUtilities.skUtilities
version: 0.6.3
version: 0.6.4
depend: [Skript]
3 changes: 2 additions & 1 deletion src/uk/tim740/skUtilities/RegFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static void regF() {
Skript.registerExpression(SExprEditLine.class,String.class,ExpressionType.PROPERTY,"line %number% in file %string%", "file %string%'s line %number%");

Skript.registerEffect(EffRunApp.class, "run (script|program|app[lication]|file) at %string%");
Skript.registerEffect(EffCreateDeleteFile.class, "(0¦crea|1¦dele)te (script|program|app[lication]|[zip ]file) %string%");
Skript.registerEffect(EffCreateFile.class, "create (0¦(script|program|app[lication]|[zip ]file)|1¦dir[ectory]) %string%");
Skript.registerEffect(EffDeleteFile.class, "delete (script|program|app[lication]|[zip ]file) %string%");
Skript.registerEffect(EffFileRenameMove.class, "(0¦rename|1¦move|2¦copy) file %string% to %-string%");
Skript.registerEffect(EffFileDownload.class, "download file from %string% to file %-string%");
Skript.registerEffect(EffZipFiles.class, "zip file[s] %strings% to zip[ file] %-string%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.event.Event;
import uk.tim740.skUtilities.Utils;
import uk.tim740.skUtilities.files.event.EvtFileCreation;
import uk.tim740.skUtilities.files.event.EvtFileDeletion;
import uk.tim740.skUtilities.skUtilities;

import javax.annotation.Nullable;
Expand All @@ -20,32 +19,28 @@
/**
* Created by tim740 on 16/03/2016
*/
public class EffCreateDeleteFile extends Effect {
public class EffCreateFile extends Effect {
private Expression<String> path;
private int ty;

@Override
protected void execute(Event arg0) {
Path pth = Paths.get(Utils.getDefaultPath(path.getSingle(arg0)));
if (ty == 0) {
try {
EvtFileCreation efc = new EvtFileCreation(pth.toFile());
Bukkit.getServer().getPluginManager().callEvent(efc);
if (!efc.isCancelled()) {
EvtFileCreation efc = new EvtFileCreation(pth.toFile());
Bukkit.getServer().getPluginManager().callEvent(efc);
if (!efc.isCancelled()) {
if (ty == 0) {
try {
Files.createFile(pth);
} catch (IOException e) {
skUtilities.prSysE("File: '" + pth + "' already exists!", getClass().getSimpleName(), e);
}
} catch (IOException e) {
skUtilities.prSysE("File: '" + pth + "' already exists!", getClass().getSimpleName(), e);
}
} else {
try {
EvtFileDeletion efd = new EvtFileDeletion(pth.toFile());
Bukkit.getServer().getPluginManager().callEvent(efd);
if (!efd.isCancelled()) {
Files.delete(pth);
} else {
try {
Files.createDirectory(pth);
} catch (IOException e) {
skUtilities.prSysE("Directory: '" + pth + "' already exists!", getClass().getSimpleName(), e);
}
} catch (IOException e) {
skUtilities.prSysE("File: '" + pth + "' doesn't exist!", getClass().getSimpleName(), e);
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/uk/tim740/skUtilities/files/EffDeleteFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package uk.tim740.skUtilities.files;

import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import uk.tim740.skUtilities.Utils;
import uk.tim740.skUtilities.files.event.EvtFileDeletion;
import uk.tim740.skUtilities.skUtilities;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Created by tim740 on 16/03/2016
*/
public class EffDeleteFile extends Effect {
private Expression<String> path;

@Override
protected void execute(Event arg0) {
Path pth = Paths.get(Utils.getDefaultPath(path.getSingle(arg0)));
try {
EvtFileDeletion efd = new EvtFileDeletion(pth.toFile());
Bukkit.getServer().getPluginManager().callEvent(efd);
if (!efd.isCancelled()) {
Files.delete(pth);
}
} catch (IOException e) {
skUtilities.prSysE("File: '" + pth + "' doesn't exist!", getClass().getSimpleName(), e);
}
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] arg0, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) {
path = (Expression<String>) arg0[0];
return true;
}
@Override
public String toString(@Nullable Event arg0, boolean arg1) {
return getClass().getName();
}
}

0 comments on commit c82332e

Please sign in to comment.