diff --git a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java index b895773..aeb0ff7 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java +++ b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java @@ -1,9 +1,11 @@ package me.youhavetrouble.blockedit; +import me.youhavetrouble.blockedit.util.Clipboard; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.util.BoundingBox; +import org.bukkit.util.Vector; import java.util.HashMap; import java.util.UUID; @@ -12,11 +14,14 @@ public class BEPlayer { private static final HashMap playerHashMap = new HashMap<>(); private BoundingBox selection; + + private final Clipboard clipboard; private Location selectionPoint1, selectionPoint2; private final UUID playerUuid; public BEPlayer(Player player) { this.playerUuid = player.getUniqueId(); + this.clipboard = new Clipboard(player.getLocation()); } public Player getPlayer() { @@ -27,6 +32,26 @@ public class BEPlayer { return selection; } + public Clipboard getClipboard() { + return clipboard; + } + + public void setClipboardFromSelection() { + if (selection == null) throw new IllegalStateException("Selection is null"); + // add every block between selection points to clipboard + clipboard.setBaseLocation(getPlayer().getLocation()); + for (int x = (int) selection.getMinX(); x <= selection.getMaxX(); x++) { + for (int y = (int) selection.getMinY(); y <= selection.getMaxY(); y++) { + for (int z = (int) selection.getMinZ(); z <= selection.getMaxZ(); z++) { + Vector relativeLocation = new Vector(x, y, z).subtract(clipboard.getBaseLocationVector()); + Location location = new Location(selectionPoint1.getWorld(), x, y, z); + clipboard.addBlock(relativeLocation, location.getBlock().getState()); + } + } + } + + } + public void resetSelection() { this.selection = null; this.selectionPoint1 = null; diff --git a/src/main/java/me/youhavetrouble/blockedit/util/Clipboard.java b/src/main/java/me/youhavetrouble/blockedit/util/Clipboard.java index 915ea65..2c3d26f 100644 --- a/src/main/java/me/youhavetrouble/blockedit/util/Clipboard.java +++ b/src/main/java/me/youhavetrouble/blockedit/util/Clipboard.java @@ -1,5 +1,7 @@ package me.youhavetrouble.blockedit.util; +import org.bukkit.Location; +import org.bukkit.util.Vector; import org.bukkit.block.BlockState; import java.util.Collections; @@ -8,18 +10,38 @@ import java.util.Map; public class Clipboard { - private HashMap blocks = new HashMap<>(); + /** + * Map of locations relative to the center of the clipboard and their block states + */ + private final HashMap blocks = new HashMap<>(); + private Location baseLocation; + private Vector baseLocationVector; - public Clipboard() {} - - public void setBlocks(HashMap newClipboard) { - this.blocks = newClipboard; + public Clipboard(Location baseLocation) { + this.baseLocation = baseLocation; } - public Map getBlocks() { + public void addBlock(Vector relativeLocation, BlockState blockState) { + this.blocks.put(relativeLocation, blockState); + } + + public Map getBlocks() { return Collections.unmodifiableMap(this.blocks); } + public void setBaseLocation(Location baseLocation) { + this.baseLocation = baseLocation; + this.baseLocationVector = baseLocation.toVector(); + } + + public Location getBaseLocation() { + return baseLocation; + } + + public Vector getBaseLocationVector() { + return baseLocationVector; + } + public void clear() { this.blocks.clear(); } @@ -27,4 +49,16 @@ public class Clipboard { public boolean isEmpty() { return this.blocks.isEmpty(); } + + /** + * Rotates clipboard by specified degrees around the base location. + * @param angle angle in degrees + */ + public void rotate(int angle) { + for (Map.Entry entry : this.blocks.entrySet()) { + Vector relativeLocation = entry.getKey(); + relativeLocation.rotateAroundAxis(baseLocationVector, angle); + } + } + } diff --git a/src/main/java/me/youhavetrouble/blockedit/util/RelativeLocation.java b/src/main/java/me/youhavetrouble/blockedit/util/RelativeLocation.java deleted file mode 100644 index ffbbece..0000000 --- a/src/main/java/me/youhavetrouble/blockedit/util/RelativeLocation.java +++ /dev/null @@ -1,36 +0,0 @@ -package me.youhavetrouble.blockedit.util; - -public class RelativeLocation { - - private double x, y, z; - - public RelativeLocation(double x, double y, double z) { - this.x = x; - this.y = y; - this.z = z; - } - - public double getX() { - return x; - } - - public void setX(double x) { - this.x = x; - } - - public double getY() { - return y; - } - - public void setY(double y) { - this.y = y; - } - - public double getZ() { - return z; - } - - public void setZ(double z) { - this.z = z; - } -}