From 5cc6ab21452f07b92df691be09955d18d37eb91a Mon Sep 17 00:00:00 2001 From: Chaos Date: Fri, 11 Feb 2022 21:58:44 +0100 Subject: [PATCH] refactor(standalone): Move WorldEdit Directory to StandaloneProvider feat(standalone): Add Creating Nodes --- .../de/steamwar/core/WorldEditWrapper.java | 5 +-- .../de/steamwar/sql/StandaloneProvider.java | 36 +++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java b/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java index 58d5ee1..7cfba3b 100644 --- a/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java @@ -18,12 +18,9 @@ public class WorldEditWrapper { InputStream getPlayerClipboard(Player player, boolean schemFormat); void setPlayerClipboard(Player player, InputStream is, boolean schemFormat); Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException; - default File getWorldEditSchematicFolder() { - return WorldEditWrapper.getWorldEditPlugin().getWorldEdit().getWorkingDirectoryFile(WorldEditWrapper.getWorldEditPlugin().getWorldEdit().getConfiguration().saveDir); - } } - static WorldEditPlugin getWorldEditPlugin() { + public static WorldEditPlugin getWorldEditPlugin() { return (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 97f810a..c9c6259 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -203,7 +203,7 @@ public class StandaloneProvider implements Provider { } private int nodeId = 1; - private final File schematicDir = WorldEditWrapper.impl.getWorldEditSchematicFolder(); + private final File schematicDir = WorldEditWrapper.getWorldEditPlugin().getWorldEdit().getWorkingDirectoryFile(WorldEditWrapper.getWorldEditPlugin().getWorldEdit().getConfiguration().saveDir); private final Map nodeById = new HashMap<>(); private final Map> nodesByParent = new HashMap<>(); private final Map nodesToPath = new HashMap<>(); @@ -215,7 +215,7 @@ public class StandaloneProvider implements Provider { SchematicNode node = new SchematicNode( nodeId++, 0, - file.getName().substring(file.getName().lastIndexOf(".")), + file.isDirectory()?file.getName():file.getName().substring(file.getName().lastIndexOf(".")), null, "", "normal", @@ -237,7 +237,35 @@ public class StandaloneProvider implements Provider { @Override public void createSchematicNode(int owner, String name, Integer parent, String type, String item) { + boolean isDir = type == null; + Path p = null; + try { + if(isDir) { + p = Files.createDirectory(new File(nodesToPath.get(parent == null?-1:parent).toFile(), name).toPath()); + } else { + p = Files.createFile(new File(nodesToPath.get(parent == null?-1:parent).toFile(), name + ".schem").toPath()); + } + } catch (IOException e) { + throw new SecurityException(e); + } + File file = p.toFile(); + int id = nodeId++; + nodesToPath.put(id, p); + SchematicNode node = new SchematicNode( + nodeId++, + 0, + file.isDirectory()?file.getName():file.getName().substring(file.getName().lastIndexOf(".")), + null, + "", + "normal", + file.isDirectory(), + 0, + Timestamp.from(Instant.now()), + file.getName().endsWith(".schem") + ); + nodeById.put(id, node); + nodesByParent.get(parent == null?-1:parent).add(node); } @Override @@ -352,6 +380,10 @@ public class StandaloneProvider implements Provider { while((nReadBytes = blob.read(bucket, 0, bucket.length)) !=-1){ stream.write(bucket, 0, nReadBytes); } + + if(newFormat != node.getSchemFormat()) { + nodesToPath.get(node.getId()).toFile().renameTo(new File(nodesToPath.get(node.getId()).toFile().getParentFile(), node.getName() + "." + (newFormat?".schem":"schematic"))); + } } catch (IOException e) { throw new SecurityException(e); }