mirror of
https://github.com/YouHaveTrouble/BlockEdit.git
synced 2026-06-29 13:36:19 +00:00
working copy and paste functionalities
This commit is contained in:
@@ -39,7 +39,8 @@ public class BEPlayer {
|
||||
public void setClipboardFromSelection() {
|
||||
if (selection == null) throw new IllegalStateException("Selection is null");
|
||||
// add every block between selection points to clipboard
|
||||
clipboard.setBaseLocation(getPlayer().getLocation());
|
||||
clipboard.clear();
|
||||
clipboard.setBaseLocation(getPlayer().getLocation().toBlockLocation());
|
||||
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++) {
|
||||
|
||||
@@ -28,6 +28,8 @@ public final class BlockEdit extends JavaPlugin {
|
||||
registerCommand(new DeselCommand());
|
||||
registerCommand(new Pos1Command());
|
||||
registerCommand(new Pos2Command());
|
||||
registerCommand(new CopyCommand());
|
||||
registerCommand(new PasteCommand());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package me.youhavetrouble.blockedit.commands;
|
||||
|
||||
import me.youhavetrouble.blockedit.BEPlayer;
|
||||
import me.youhavetrouble.blockedit.api.BlockEditAPI;
|
||||
import me.youhavetrouble.blockedit.operations.PasteOperation;
|
||||
import me.youhavetrouble.blockedit.util.Selection;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PasteCommand extends Command {
|
||||
|
||||
public PasteCommand() {
|
||||
super("paste");
|
||||
setPermission("blockedit.command.paste");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(@NotNull CommandSender sender, @NotNull String s, @NotNull String[] args) {
|
||||
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage(Component.text("You need to be a player to do this"));
|
||||
return true;
|
||||
}
|
||||
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
|
||||
Vector playerLocationVector = player.getLocation().toBlockLocation().toVector();
|
||||
|
||||
HashMap<Vector, BlockState> absoluteBlocks = new HashMap<>(bePlayer.getClipboard().getBlocks().size());
|
||||
|
||||
bePlayer.getClipboard().getBlocks().forEach((vector, blockState) -> {
|
||||
Vector absolutePosition = vector.clone().add(playerLocationVector);
|
||||
absoluteBlocks.put(absolutePosition, blockState);
|
||||
});
|
||||
|
||||
Selection selection = Selection.fromClipboard(absoluteBlocks.keySet(), player.getWorld());
|
||||
BlockEditAPI.runOperation(selection, 1, new PasteOperation(absoluteBlocks));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package me.youhavetrouble.blockedit.operations;
|
||||
|
||||
import me.youhavetrouble.blockedit.api.BlockEditOperation;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public record PasteOperation(Map<Vector, BlockState> blockStateMap) implements BlockEditOperation {
|
||||
|
||||
@Override
|
||||
public void transformBlock(Block block) {
|
||||
if (!blockStateMap.containsKey(block.getLocation().toVector())) return;
|
||||
block.setBlockData(blockStateMap.get(block.getLocation().toVector()).getBlockData());
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class Clipboard {
|
||||
}
|
||||
|
||||
public void setBaseLocation(Location baseLocation) {
|
||||
this.baseLocation = baseLocation;
|
||||
this.baseLocation = baseLocation.toBlockLocation();
|
||||
this.baseLocationVector = baseLocation.toVector();
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class Clipboard {
|
||||
* Rotates clipboard by specified degrees around the base location.
|
||||
* @param angle angle in degrees
|
||||
*/
|
||||
public void rotate(int angle) {
|
||||
public void rotate(double angle) {
|
||||
for (Map.Entry<Vector, BlockState> entry : this.blocks.entrySet()) {
|
||||
Vector relativeLocation = entry.getKey();
|
||||
relativeLocation.rotateAroundAxis(baseLocationVector, angle);
|
||||
|
||||
@@ -4,7 +4,9 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Selection extends BoundingBox {
|
||||
@@ -43,4 +45,23 @@ public class Selection extends BoundingBox {
|
||||
public void setWorldUuid(UUID worldUuid) {
|
||||
this.worldUuid = worldUuid;
|
||||
}
|
||||
|
||||
public static Selection fromClipboard(Set<Vector> locations, World world) {
|
||||
Vector closestVector = new Vector(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
|
||||
Vector farthestVector = new Vector(Double.MIN_VALUE,Double.MIN_VALUE,Double.MIN_VALUE);
|
||||
|
||||
for (Vector vector : locations) {
|
||||
if (vector.getX() + vector.getY() + vector.getZ() < closestVector.getX() + closestVector.getY() + closestVector.getZ()) {
|
||||
closestVector = vector;
|
||||
}
|
||||
if (vector.getX() + vector.getY() + vector.getZ() > farthestVector.getX() + farthestVector.getY() + farthestVector.getZ()) {
|
||||
farthestVector = vector;
|
||||
}
|
||||
}
|
||||
|
||||
Location minLocation = closestVector.toLocation(world);
|
||||
Location maxLocation = farthestVector.toLocation(world);
|
||||
|
||||
return new Selection(minLocation, maxLocation, world.getUID());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user