From 453dea774e35e2a79609c605aa95f1293f8ea65e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 1 Sep 2021 16:50:41 +0200 Subject: [PATCH] Unify WE offset calculation Signed-off-by: Lixfel --- .../fightsystem/utils/WorldeditWrapper14.java | 47 +++++-------------- .../fightsystem/utils/WorldeditWrapper8.java | 46 +++++------------- .../fightsystem/fight/FightSchematic.java | 16 +++++-- .../steamwar/fightsystem/record/Recorder.java | 32 +++++++------ .../fightsystem/utils/WorldeditWrapper.java | 5 +- 5 files changed, 59 insertions(+), 87 deletions(-) diff --git a/FightSystem_14/src/de/steamwar/fightsystem/utils/WorldeditWrapper14.java b/FightSystem_14/src/de/steamwar/fightsystem/utils/WorldeditWrapper14.java index 6b2ae67..8f1c317 100644 --- a/FightSystem_14/src/de/steamwar/fightsystem/utils/WorldeditWrapper14.java +++ b/FightSystem_14/src/de/steamwar/fightsystem/utils/WorldeditWrapper14.java @@ -39,12 +39,12 @@ import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockTypes; -import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.sql.Schematic; import org.bukkit.Bukkit; import org.bukkit.DyeColor; import org.bukkit.Location; +import org.bukkit.util.Vector; import java.io.ByteArrayOutputStream; import java.io.File; @@ -88,45 +88,24 @@ public class WorldeditWrapper14 implements WorldeditWrapper.IWorldeditWrapper { } @Override - public void pasteSchematic(Clipboard clipboard, Region region, boolean rotate) { - AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0); - BlockVector3 dimensions = clipboard.getDimensions(); - BlockVector3 v = BlockVector3.at(region.centerX(), region.getMinY(), region.centerZ()); - if(rotate){ - v = v.add(dimensions.getX()/2 + dimensions.getX()%2, 0, dimensions.getZ()/2 + dimensions.getZ()%2).subtract(1, 0, 1); - }else{ - v = v.subtract(dimensions.getX()/2, 0, dimensions.getZ()/2); + public int getWaterDepth(Clipboard clipboard) { + BlockVector3 it = clipboard.getMinimumPoint().add(0, 0, 1); + int depth = 0; + while(!clipboard.getBlock(it).getBlockType().getMaterial().isAir()){ + depth++; + it = it.add(0, 1, 0); } - v = v.subtract(aT.apply(clipboardOffset(clipboard).toVector3()).toBlockPoint()); - - if(Config.WaterDepth != 0){ - BlockVector3 it = clipboard.getMinimumPoint().add(0, 0, 1); - int depth = 0; - while(!clipboard.getBlock(it).getBlockType().getMaterial().isAir()){ - depth++; - it = it.add(0, 1, 0); - } - v = v.add(0, Config.WaterDepth - depth, 0); - } - - pasteClipboard(Bukkit.getWorlds().get(0), clipboard, aT, v, false); + return depth; } @Override - public void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT) { - BlockVector3 v = aT.apply(BlockVector3.at(xOffset, 0, 0).subtract(clipboardOffset(character)).toVector3()).toBlockPoint(); - pasteClipboard(pos.getWorld(), character, aT, v.add(BukkitAdapter.asBlockVector(pos)), true); - } - - private BlockVector3 clipboardOffset(Clipboard clipboard) { - return clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()); - } - - private void pasteClipboard(org.bukkit.World world, Clipboard clipboard, AffineTransform aT, BlockVector3 pos, boolean ignoreAir) { - EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(world), -1); + public void pasteClipboard(Clipboard clipboard, Location position, Vector offset, AffineTransform aT) { + EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(position.getWorld()), -1); ClipboardHolder ch = new ClipboardHolder(clipboard); ch.setTransform(aT); - Operations.completeBlindly(ch.createPaste(e).to(pos).ignoreAirBlocks(ignoreAir).build()); + Operations.completeBlindly(ch.createPaste(e).to(BukkitAdapter.asBlockVector(position).add( + aT.apply(BlockVector3.at(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()).add(clipboard.getOrigin()).subtract(clipboard.getMinimumPoint()).toVector3()).toBlockPoint() + )).build()); e.flushSession(); } diff --git a/FightSystem_8/src/de/steamwar/fightsystem/utils/WorldeditWrapper8.java b/FightSystem_8/src/de/steamwar/fightsystem/utils/WorldeditWrapper8.java index f3c17e3..ad3ea1e 100644 --- a/FightSystem_8/src/de/steamwar/fightsystem/utils/WorldeditWrapper8.java +++ b/FightSystem_8/src/de/steamwar/fightsystem/utils/WorldeditWrapper8.java @@ -37,7 +37,6 @@ import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; -import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.sql.Schematic; import org.bukkit.Bukkit; @@ -88,46 +87,25 @@ public class WorldeditWrapper8 implements WorldeditWrapper.IWorldeditWrapper { } @Override - public void pasteSchematic(Clipboard clipboard, Region region, boolean rotate) { - AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0); - Vector dimensions = clipboard.getDimensions(); - Vector v = new Vector(region.centerX(), region.getMinY(), region.centerZ()); - if(rotate){ - v = v.add(dimensions.getX()/2 + dimensions.getX()%2, 0, dimensions.getZ()/2 + dimensions.getZ()%2).subtract(1, 0, 1); - }else{ - v = v.subtract(dimensions.getX()/2 - dimensions.getX()%2, 0, dimensions.getZ()/2 - dimensions.getZ()%2); + public int getWaterDepth(Clipboard clipboard) { + Vector it = clipboard.getMinimumPoint().add(0, 0, 1); + int depth = 0; + while(!clipboard.getBlock(it).isAir()) { + depth++; + it = it.add(0, 1, 0); } - v = v.subtract(aT.apply(clipboardOffset(clipboard)).toBlockPoint()); - - if(Config.WaterDepth != 0){ - Vector it = clipboard.getMinimumPoint().add(0, 0, 1); - int depth = 0; - while(!clipboard.getBlock(it).isAir()) { - depth++; - it = it.add(0, 1, 0); - } - v = v.add(0, Config.WaterDepth - depth, 0); - } - - pasteClipboard(Bukkit.getWorlds().get(0), clipboard, aT, v, false); + return depth; } @Override - public void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT) { - Vector v = aT.apply(new Vector(xOffset, 0, 0).subtract(clipboardOffset(character))).toBlockPoint(); - pasteClipboard(pos.getWorld(), character, aT, v.add(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), true); - } - - private Vector clipboardOffset(Clipboard clipboard) { - return clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()); - } - - private void pasteClipboard(org.bukkit.World world, Clipboard clipboard, AffineTransform aT, Vector pos, boolean ignoreAir) { - World w = new BukkitWorld(world); + public void pasteClipboard(Clipboard clipboard, Location position, org.bukkit.util.Vector offset, AffineTransform aT) { + World w = new BukkitWorld(position.getWorld()); EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(w, -1); ClipboardHolder ch = new ClipboardHolder(clipboard, w.getWorldData()); ch.setTransform(aT); - Operations.completeBlindly(ch.createPaste(e, w.getWorldData()).to(pos).ignoreAirBlocks(ignoreAir).build()); + Operations.completeBlindly(ch.createPaste(e, w.getWorldData()).to(new Vector(position.getBlockX(), position.getBlockY(), position.getBlockZ()).add( + aT.apply(new Vector(offset.getBlockX(), offset.getBlockY(), offset.getBlockZ()).add(clipboard.getOrigin()).subtract(clipboard.getMinimumPoint())).toBlockPoint() + )).build()); e.flushQueue(); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java index 7c81d40..64cdf2e 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java @@ -35,6 +35,7 @@ import de.steamwar.sql.Schematic; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.event.HandlerList; +import org.bukkit.util.Vector; import java.io.IOException; import java.util.List; @@ -122,7 +123,16 @@ public class FightSchematic extends StateDependent { try { replaceTeamColor(clipboard); - WorldeditWrapper.impl.pasteSchematic(clipboard, region, rotate); + WorldeditWrapper.impl.pasteClipboard( + clipboard, + new Location(Bukkit.getWorlds().get(0), region.centerX(), region.getMinY(), region.centerZ()), + new Vector( + -clipboard.getDimensions().getBlockX()/2, + Config.WaterDepth != 0 ? Config.WaterDepth - WorldeditWrapper.impl.getWaterDepth(clipboard) : 0, + -clipboard.getDimensions().getBlockZ()/2 + ), + new AffineTransform().rotateY(rotate ? 180 : 0) + ); } catch (WorldEditException e) { FightSystem.broadcast("§cFehler beim Pasten der Schematic"); Bukkit.getScheduler().runTask(FightSystem.getPlugin(), FightSystem::setPreSchemState); @@ -182,9 +192,9 @@ public class FightSchematic extends StateDependent { length -= 1; AffineTransform aT = new AffineTransform().rotateY(rotate ? 180 : 0); - Location base = new Location(Bukkit.getWorlds().get(0), team.getSchemRegion().centerX(), team.getExtendRegion().getMaxY(), team.getSchemRegion().centerZ() + (rotate ? 1 : -1) * team.getSchemRegion().getSizeZ() / 2); + Location base = new Location(Bukkit.getWorlds().get(0), region.centerX(), team.getExtendRegion().getMaxY(), region.centerZ()); for(int i = 0; i < characters.length; i++){ - WorldeditWrapper.impl.pasteChar(characters[i], base, offsets[i] - length/2, aT); + WorldeditWrapper.impl.pasteClipboard(characters[i], base, new Vector(offsets[i] - length/2, 0, region.getSizeZ()/2), aT); } } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/Recorder.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/Recorder.java index 4e6525a..c98e21d 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/Recorder.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/Recorder.java @@ -38,10 +38,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.logging.Level; public interface Recorder { @@ -249,16 +246,23 @@ public interface Recorder { } default void schem(int embedId, int noEmbedId, int schemId){ - try { - if(schemId == 0) - throw new IOException(); - - write(embedId, schemId, Schematic.getSchemFromDB(schemId).schemData()); - } catch (IOException e) { - if(schemId != 0) - Bukkit.getLogger().log(Level.SEVERE, "Could not embed schematic", e); + if(schemId == 0) { write(noEmbedId, schemId); + return; } + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try{ + copy(Schematic.getSchemFromDB(schemId).schemData(), buffer); + }catch (EOFException e) { + Bukkit.getLogger().log(Level.INFO, "EOFException ignored"); + } catch (IOException e) { + Bukkit.getLogger().log(Level.SEVERE, "Could not embed schematic", e); + disable(); + return; + } + + write(embedId, schemId, buffer.toByteArray()); } default void scoreboardTitle(String title){ @@ -292,8 +296,8 @@ public interface Recorder { stream.writeDouble((Double)o); else if(o instanceof String) stream.writeUTF((String)o); - else if(o instanceof InputStream) - copy((InputStream) o, stream); + else if(o instanceof byte[]) + stream.write((byte[])o); else throw new SecurityException("Undefined write for: " + o.getClass().getName()); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/WorldeditWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/WorldeditWrapper.java index 4d1585a..0edf2e9 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/WorldeditWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/WorldeditWrapper.java @@ -26,6 +26,7 @@ import de.steamwar.fightsystem.VersionDependent; import de.steamwar.sql.Schematic; import org.bukkit.DyeColor; import org.bukkit.Location; +import org.bukkit.util.Vector; import java.io.IOException; @@ -36,8 +37,8 @@ public class WorldeditWrapper { public interface IWorldeditWrapper { void replaceTeamColor(Clipboard clipboard, DyeColor c) throws WorldEditException; - void pasteSchematic(Clipboard clipboard, Region region, boolean rotate); - void pasteChar(Clipboard character, Location pos, int xOffset, AffineTransform aT); + int getWaterDepth(Clipboard clipboard); + void pasteClipboard(Clipboard clipboard, Location position, Vector offset, AffineTransform aT); Clipboard loadChar(String charName) throws IOException; void saveSchem(Schematic schem, Region region, int minY) throws WorldEditException; }