From 1c316e52a95b91674d6baeef010236367a3394c4 Mon Sep 17 00:00:00 2001 From: dfsek Date: Wed, 23 Dec 2020 23:02:58 -0700 Subject: [PATCH] Exporting --- .../script/functions/StructureFunction.java | 1 - .../src/test/java/structure/ParserTest.java | 4 +- .../test/java/structure/TokenizerTest.java | 2 +- .../command/structure/ExportCommand.java | 72 ++++++++++++++++++- 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/StructureFunction.java b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/StructureFunction.java index a464e21fc..f55b604d3 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/StructureFunction.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/StructureFunction.java @@ -47,7 +47,6 @@ public class StructureFunction implements Function { @Override public Void apply(Location location, Rotation rotation, int recursions) { - System.out.println("executing structure function"); Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue()); diff --git a/common/src/test/java/structure/ParserTest.java b/common/src/test/java/structure/ParserTest.java index d59a7bd8e..667d9a1d5 100644 --- a/common/src/test/java/structure/ParserTest.java +++ b/common/src/test/java/structure/ParserTest.java @@ -51,9 +51,9 @@ public class ParserTest { long t = System.nanoTime() - l; System.out.println("Took " + (double) t / 1000000); - block.apply(null, Rotation.NONE, recursions); + block.apply(null, Rotation.NONE, 0); - block.apply(null, Rotation.NONE, recursions); + block.apply(null, Rotation.NONE, 0); } private static class Test1 implements Function { diff --git a/common/src/test/java/structure/TokenizerTest.java b/common/src/test/java/structure/TokenizerTest.java index fe7a01ae4..73e0fd6e2 100644 --- a/common/src/test/java/structure/TokenizerTest.java +++ b/common/src/test/java/structure/TokenizerTest.java @@ -11,7 +11,7 @@ import java.io.IOException; public class TokenizerTest { @Test public void tokens() throws IOException, TokenizerException { - Tokenizer tokenizer = new Tokenizer(IOUtils.toString(getClass().getResourceAsStream("/target/server/plugins/Terra/test.tesf"))); + Tokenizer tokenizer = new Tokenizer(IOUtils.toString(getClass().getResourceAsStream("/test.tesf"))); // Actual run long l = System.nanoTime(); diff --git a/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/ExportCommand.java b/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/ExportCommand.java index d405787dc..6dd51cc08 100644 --- a/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/ExportCommand.java +++ b/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/ExportCommand.java @@ -2,12 +2,21 @@ package com.dfsek.terra.bukkit.command.command.structure; import com.dfsek.terra.bukkit.command.PlayerCommand; import com.dfsek.terra.bukkit.structure.WorldEditUtil; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -19,10 +28,71 @@ public class ExportCommand extends PlayerCommand { @Override public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { Location[] l = WorldEditUtil.getSelectionLocations(sender); - /*if(l == null) return true; + if(l == null) return true; Location l1 = l[0]; Location l2 = l[1]; + + StringBuilder scriptBuilder = new StringBuilder("id \"" + args[0] + "\";\nnum y = 0;\n"); + + int centerX = 0; + int centerY = 0; + int centerZ = 0; + + for(int x = l1.getBlockX(); x <= l2.getBlockX(); x++) { + for(int y = l1.getBlockY(); y <= l2.getBlockY(); y++) { + for(int z = l1.getBlockZ(); z <= l2.getBlockZ(); z++) { + Block block = new Location(l1.getWorld(), x, y, z).getBlock(); + BlockState state = block.getState(); + if(state instanceof Sign) { + Sign sign = (Sign) state; + if(sign.getLine(0).equals("[TERRA]") && sign.getLine(1).equals("[CENTER]")) { + centerX = x - l1.getBlockX(); + centerY = y - l1.getBlockY(); + centerZ = z - l1.getBlockZ(); + } + } + } + } + } + + for(int x = l1.getBlockX(); x <= l2.getBlockX(); x++) { + for(int y = l1.getBlockY(); y <= l2.getBlockY(); y++) { + for(int z = l1.getBlockZ(); z <= l2.getBlockZ(); z++) { + Block block = new Location(l1.getWorld(), x, y, z).getBlock(); + if(block.getType().equals(Material.STRUCTURE_VOID)) continue; + scriptBuilder.append("block(").append(x - l1.getBlockX() - centerX).append(", y + ").append(y - l1.getBlockY() - centerY).append(", ").append(z - l1.getBlockZ() - centerZ).append(", ") + .append("\""); + BlockState state = block.getState(); + if(state instanceof Sign) { + Sign sign = (Sign) state; + if(sign.getLine(0).equals("[TERRA]")) { + scriptBuilder.append(Bukkit.createBlockData(sign.getLine(2) + sign.getLine(3)).getAsString(false)); + } + } else { + scriptBuilder.append(block.getBlockData().getAsString(false)); + } + scriptBuilder.append("\");\n"); + } + } + } + + File file = new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tesf"); + try { + file.getParentFile().mkdirs(); + file.createNewFile(); + } catch(IOException e) { + e.printStackTrace(); + } + try(BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + writer.write(scriptBuilder.toString()); + } catch(IOException e) { + e.printStackTrace(); + } + + + + /* Structure structure; try { structure = new Structure(l1, l2, args[0]);