From e7438029b07ce40fa4446744ea24818b7b5c32d2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 7 Apr 2024 16:39:00 +0200 Subject: [PATCH] Add /rgc and /rgp and /region copy and /region paste --- .../bausystem/utils/FlatteningWrapper15.java | 20 ++- BauSystem_Main/src/BauSystem.properties | 5 + BauSystem_Main/src/BauSystem_de.properties | 5 + .../features/region/RegionCommand.java | 117 +++++++++++++++++- .../de/steamwar/bausystem/region/Point.java | 5 + .../bausystem/utils/FlatteningWrapper.java | 1 + 6 files changed, 144 insertions(+), 9 deletions(-) diff --git a/BauSystem_15/src/de/steamwar/bausystem/utils/FlatteningWrapper15.java b/BauSystem_15/src/de/steamwar/bausystem/utils/FlatteningWrapper15.java index 0a01fb42..9407f542 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/utils/FlatteningWrapper15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/utils/FlatteningWrapper15.java @@ -106,6 +106,7 @@ public class FlatteningWrapper15 implements FlatteningWrapper { private static final WorldEditPlugin WORLDEDIT_PLUGIN = Objects.requireNonNull((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit")); private static final World BUKKITWORLD = new BukkitWorld(Bukkit.getWorlds().get(0)); + @Override public void setSelection(Player p, Point minPoint, Point maxPoint) { WORLDEDIT_PLUGIN.getSession(p).setRegionSelector(BUKKITWORLD, new CuboidRegionSelector(BUKKITWORLD, toBlockVector3(minPoint), toBlockVector3(maxPoint))); @@ -190,7 +191,7 @@ public class FlatteningWrapper15 implements FlatteningWrapper { } @Override - public boolean backup(Point minPoint, Point maxPoint, File file) { + public Clipboard copy(Point minPoint, Point maxPoint, Point copyPoint) { BukkitWorld bukkitWorld = new BukkitWorld(Bukkit.getWorlds().get(0)); CuboidRegion region = new CuboidRegion(bukkitWorld, toBlockVector3(minPoint), toBlockVector3(maxPoint)); BlockArrayClipboard clipboard = new BlockArrayClipboard(region); @@ -203,12 +204,21 @@ public class FlatteningWrapper15 implements FlatteningWrapper { copy.setCopyingBiomes(false); Operations.complete(copy); + clipboard.setOrigin(toBlockVector3(copyPoint)); + return clipboard; + } catch (WorldEditException e) { + Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); + return null; + } + } - try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) { - writer.write(clipboard); - } + @Override + public boolean backup(Point minPoint, Point maxPoint, File file) { + Clipboard clipboard = copy(minPoint, maxPoint, minPoint); + try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) { + writer.write(clipboard); return true; - } catch (WorldEditException | IOException e) { + } catch (IOException e) { Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); return false; } diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 887439f7..ba087b21 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -811,6 +811,8 @@ REGION_REGION_HELP_COPYPOINT=§8/§eregion copypoint §8- §7Teleport to the reg REGION_REGION_HELP_TESTBLOCKPOINT=§8/§eregion testblockpoint §8- §7Teleport to the regions dummy point REGION_REGION_HELP_CHANGESKIN_INFO=§8/§eregion changeskin §8- §7Returns the region skin REGION_REGION_HELP_CHANGESKIN=§8/§eregion changeskin §8[§7Skin§8] §8- §8Sets the region skin +REGION_REGION_HELP_COPY=§8/§eregion copy [-e] [-s] §8- §8Copy the build area optional with extensions or selection at the copypoint +REGION_REGION_HELP_PASTE=§8/§eregion paste [-a] [-s] §8[§7Skin§8] §8- §8Pastes at the copypoint optional without air and selecting the pasted region REGION_REGION_NOTHING_UNDO=§cNothing left to undo REGION_REGION_UNDID=§7Last action undone REGION_REGION_NOTHING_REDO=§cNothing left to redo @@ -824,6 +826,9 @@ REGION_REGION_TP_COPY=§7Teleported to the copy point REGION_REGION_TP_TEST_BLOCK=§7Teleported to the tesblock REGION_REGION_TP_UNKNOWN=§cUndefined teleport point REGION_REGION_NO_REGION=§cYou are not inside any region +REGION_REGION_NO_BUILD=§cThis region has no build area +REGION_REGION_COPY_DONE=§eBuild region or selection copied +REGION_REGION_PASTE_DONE=§eBuild region or selection pasted REGION_REGION_CHANGESKIN_INFO=§7Region skin is §e{0} REGION_REGION_CHANGESKIN_INFO_CREATOR=§7Skin created by §e{0} REGION_REGION_CHANGESKIN_UNKNOWN=§cRegion skin is invalid diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 46b30880..87c033ed 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -774,6 +774,8 @@ REGION_REGION_HELP_COPYPOINT=§8/§eregion copypoint §8- §7Teleportiere dich z REGION_REGION_HELP_TESTBLOCKPOINT=§8/§eregion testblockpoint §8- §7Teleportiere dich zum Regions Testblockpunkt REGION_REGION_HELP_CHANGESKIN_INFO=§8/§eregion changeskin §8- §7Gebe den Regions Skin aus REGION_REGION_HELP_CHANGESKIN=§8/§eregion changeskin §8[§7Skin§8] §8- §8Setzte den Regions Skin +REGION_REGION_HELP_COPY=§8/§eregion copy [-e] [-s] §8- §8Kopieren des Baubereichs optional mit Erweiterungen oder Auswahl am Kopierpunkt +REGION_REGION_HELP_PASTE=§8/§eregion paste [-a] [-s] §8[§7Skin§8] §8- §8Einfügen am Kopierpunkt optional ohne Luft und Auswahl des eingefügten Bereichs REGION_REGION_NOTHING_UNDO=§cNichts zum rückgängig machen REGION_REGION_UNDID=§7Letzte Aktion rückgangig gemacht REGION_REGION_NOTHING_REDO=§cNichts zum wiederhohlen @@ -787,6 +789,9 @@ REGION_REGION_TP_COPY=§7Zum Kopierpunkt teleportiert REGION_REGION_TP_TEST_BLOCK=§7Zum Testblock teleportiert REGION_REGION_TP_UNKNOWN=§cNicht definierter Teleportierpunkt REGION_REGION_NO_REGION=§cDu bist in keiner Region +REGION_REGION_NO_BUILD=§cDiese Region hat kein Baugebiet +REGION_REGION_COPY_DONE=§eBauregion oder Selektion kopiert +REGION_REGION_PASTE_DONE=§eBauregion oder Selektion eingefügt REGION_REGION_CHANGESKIN_INFO=§7Regions Skin ist §e{0} REGION_REGION_CHANGESKIN_INFO_CREATOR=§7Skin erstellt von §e{0} REGION_REGION_CHANGESKIN_UNKNOWN=§cRegions Skin ist nicht valide diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java index efad26fd..eb30b112 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java @@ -19,31 +19,40 @@ package de.steamwar.bausystem.features.region; +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.function.operation.Operations; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.session.ClipboardHolder; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.features.util.SelectCommand; -import de.steamwar.bausystem.region.Prototype; +import de.steamwar.bausystem.region.Point; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.flagvalues.ColorMode; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; +import de.steamwar.bausystem.shared.Pair; +import de.steamwar.bausystem.utils.FlatteningWrapper; import de.steamwar.bausystem.utils.PasteBuilder; +import de.steamwar.bausystem.utils.WorldEditUtils; import de.steamwar.command.PreviousArguments; import de.steamwar.command.SWCommand; import de.steamwar.command.TypeMapper; -import de.steamwar.command.TypeValidator; import de.steamwar.linkage.Linked; import de.steamwar.linkage.LinkedInstance; import de.steamwar.sql.SchematicNode; import net.md_5.bungee.api.chat.ClickEvent; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; -import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -197,6 +206,106 @@ public class RegionCommand extends SWCommand { } } + @Register(value = "copy", description = "REGION_REGION_HELP_COPY") + public void copyCommand(@Validator Player p, @OptionalValue("") @StaticValue(value = {"", "-e", "-s"}, allowISE = true) int option) { + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return; + } + if (!region.hasType(RegionType.BUILD)) { + BauSystem.MESSAGE.send("REGION_REGION_NO_BUILD", p); + return; + } + if (region.getCopyPoint() == null) { + BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p); + return; + } + + Point minPoint = region.getMinPoint(RegionType.BUILD, RegionExtensionType.NORMAL); + Point maxPoint = region.getMaxPoint(RegionType.BUILD, RegionExtensionType.NORMAL); + switch (option) { + case 0: + break; + case 1: + minPoint = region.getMinPoint(RegionType.BUILD, RegionExtensionType.EXTENSION); + maxPoint = region.getMaxPoint(RegionType.BUILD, RegionExtensionType.EXTENSION); + break; + case 2: + Pair selection = WorldEditUtils.getSelection(p); + minPoint = Point.fromLocation(selection.getKey()); + maxPoint = Point.fromLocation(selection.getValue()); + break; + default: + break; + } + + Clipboard clipboard = FlatteningWrapper.impl.copy(minPoint, maxPoint, region.getCopyPoint()); + WorldEdit.getInstance() + .getSessionManager() + .get(BukkitAdapter.adapt(p)) + .setClipboard(new ClipboardHolder(clipboard)); + BauSystem.MESSAGE.send("REGION_REGION_COPY_DONE", p); + } + + @Register(value = "paste", description = "REGION_REGION_HELP_PASTE") + public void pasteCommand(@Validator Player p, @OptionalValue("") @StaticValue(value = {"", "-a", "-s", "-as", "-sa"}, allowISE = true) int options) { + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return; + } + if (!region.hasType(RegionType.BUILD)) { + BauSystem.MESSAGE.send("REGION_REGION_NO_BUILD", p); + return; + } + if (region.getCopyPoint() == null) { + BauSystem.MESSAGE.send("REGION_REGION_TP_UNKNOWN", p); + return; + } + + ClipboardHolder clipboardHolder = WorldEdit.getInstance() + .getSessionManager() + .get(BukkitAdapter.adapt(p)) + .getClipboard(); + + boolean selectPasted = false; + boolean ignoreAir = false; + switch (options) { + case 0: + break; + case 1: + ignoreAir = true; + break; + case 2: + selectPasted = true; + break; + case 3: + case 4: + selectPasted = true; + ignoreAir = true; + break; + default: + break; + } + + try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) { + Operations.completeBlindly(clipboardHolder.createPaste(e).ignoreAirBlocks(ignoreAir).to(toBlockVector3(region.getCopyPoint())).build()); + WorldEditUtils.addToPlayer(p, e); + + if (selectPasted) { + Clipboard clipboard = clipboardHolder.getClipboards().get(0); + BlockVector3 minPointSelection = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(toBlockVector3(region.getCopyPoint())); + BlockVector3 maxPointSelection = clipboard.getRegion().getMaximumPoint().subtract(clipboard.getOrigin()).add(toBlockVector3(region.getCopyPoint())); + FlatteningWrapper.impl.setSelection(p, Point.fromBlockVector3(minPointSelection), Point.fromBlockVector3(maxPointSelection)); + } + BauSystem.MESSAGE.send("REGION_REGION_PASTE_DONE", p); + } + } + + private BlockVector3 toBlockVector3(Point point) { + return BlockVector3.at(point.getX(), point.getY(), point.getZ()); + } + + @Mapper(value = "skinTypeMapper", local = true) private TypeMapper skinTypeMapper() { return new TypeMapper() { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Point.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Point.java index 4a659169..0fa5496e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Point.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Point.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.region; +import com.sk89q.worldedit.math.BlockVector3; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -41,6 +42,10 @@ public class Point { return new Point(location.getBlockX(), location.getBlockY(), location.getBlockZ()); } + public static Point fromBlockVector3(final BlockVector3 blockVector3) { + return new Point(blockVector3.getBlockX(), blockVector3.getBlockY(), blockVector3.getBlockZ()); + } + public Point add(int x, int y, int z) { return new Point(this.x + x, this.y + y, this.z + z); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/FlatteningWrapper.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/FlatteningWrapper.java index aef0b302..3cd96295 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/utils/FlatteningWrapper.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/FlatteningWrapper.java @@ -46,6 +46,7 @@ public interface FlatteningWrapper { Clipboard loadSchematic(File file); EditSession paste(PasteBuilder pasteBuilder); + Clipboard copy(Point minPoint, Point maxPoint, Point copyPoint); boolean backup(Point minPoint, Point maxPoint, File file); boolean inWater(World world, Vector tntPosition);