cleanup, refactors, hopefully fix for all boundingbox issues

This commit is contained in:
2023-04-30 02:05:08 +02:00
parent 7229da78e2
commit c86519d7a3
14 changed files with 83 additions and 45 deletions
@@ -1,10 +1,10 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.util.Clipboard;
import me.youhavetrouble.blockedit.util.Selection;
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;
@@ -13,7 +13,7 @@ import java.util.UUID;
public class BEPlayer {
private static final HashMap<UUID, BEPlayer> playerHashMap = new HashMap<>();
private BoundingBox selection;
private Selection selection;
private final Clipboard clipboard;
private Location selectionPoint1, selectionPoint2;
@@ -28,7 +28,7 @@ public class BEPlayer {
return Bukkit.getPlayer(playerUuid);
}
public BoundingBox getSelection() {
public Selection getSelection() {
return selection;
}
@@ -73,7 +73,7 @@ public class BEPlayer {
return;
}
selection = BoundingBox.of(selectionPoint1.toBlockLocation(), selectionPoint2.toBlockLocation());
selection = new Selection(selectionPoint1.toBlockLocation(), selectionPoint2.toBlockLocation(), selectionPoint1.getWorld().getUID());
}
public void setSelectionPoint1(Location selectionPoint1) {
@@ -1,6 +1,5 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import me.youhavetrouble.blockedit.commands.*;
import me.youhavetrouble.blockedit.wands.SelectionWand;
import org.bukkit.command.Command;
@@ -11,6 +10,7 @@ public final class BlockEdit extends JavaPlugin {
private static BlockEdit plugin;
private static SchematicHandler schematicHandler;
private static WandsHandler wandsHandler;
@Override
public void onEnable() {
@@ -18,10 +18,6 @@ public final class BlockEdit extends JavaPlugin {
getServer().getPluginManager().registerEvents(new JoinLeaveListener(), this);
SelectionWand selectionWand = new SelectionWand();
BlockEditWands.registerWand(selectionWand);
getServer().getPluginManager().registerEvents(selectionWand, this);
registerCommand(new WandCommand());
registerCommand(new SetCommand());
registerCommand(new ReplaceCommand());
@@ -33,6 +29,11 @@ public final class BlockEdit extends JavaPlugin {
registerCommand(new RotateCommand());
schematicHandler = new SchematicHandler(this);
wandsHandler = new WandsHandler(this);
SelectionWand selectionWand = new SelectionWand();
wandsHandler.registerWand(selectionWand);
getServer().getPluginManager().registerEvents(selectionWand, this);
}
@@ -46,6 +47,10 @@ public final class BlockEdit extends JavaPlugin {
return schematicHandler;
}
public static WandsHandler getWandsHandler() {
return wandsHandler;
}
private void registerCommand(Command command) {
getServer().getCommandMap().register("blockedit", command);
}
@@ -1,7 +1,7 @@
package me.youhavetrouble.blockedit.api;
package me.youhavetrouble.blockedit;
import com.google.common.collect.ImmutableSet;
import me.youhavetrouble.blockedit.BlockEdit;
import me.youhavetrouble.blockedit.api.BlockEditWand;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
@@ -11,34 +11,41 @@ import org.bukkit.persistence.PersistentDataType;
import java.util.Collection;
import java.util.HashMap;
public class BlockEditWands {
public class WandsHandler {
private static final NamespacedKey wandKey = new NamespacedKey(BlockEdit.getPlugin(), "wand");
private final NamespacedKey wandKey;
private static final HashMap<String, BlockEditWand> wands = new HashMap<>();
public static NamespacedKey getWandKey() {
return wandKey;
protected WandsHandler(BlockEdit plugin) {
wandKey = new NamespacedKey(plugin, "wand");
}
/**
* Gets wand id from ItemStack
* @param itemStack ItemStack to check
* @return WandId if a wand, null otherwise
* @return WandId if Itemstack is a wand, null otherwise
*/
public static String isWand(ItemStack itemStack) {
public String getWandId(ItemStack itemStack) {
if (itemStack == null) return null;
if (itemStack.getItemMeta() == null) return null;
if (!itemStack.getItemMeta().getPersistentDataContainer().has(BlockEditWands.getWandKey(), PersistentDataType.STRING)) return null;
return itemStack.getItemMeta().getPersistentDataContainer().get(BlockEditWands.getWandKey(), PersistentDataType.STRING);
if (!itemStack.getItemMeta().getPersistentDataContainer().has(wandKey, PersistentDataType.STRING)) return null;
return itemStack.getItemMeta().getPersistentDataContainer().get(wandKey, PersistentDataType.STRING);
}
/**
* @return Immutable set of registered wand IDs
*/
public static Collection<String> getWandIds() {
public Collection<String> getWandIds() {
return ImmutableSet.copyOf(wands.keySet());
}
public static ItemStack getWand(String wandId) {
/**
* Gets wand ItemStack from wand id
* @param wandId Wand id
* @return Wand ItemStack if wand with given ID exists, null otherwise
*/
public ItemStack getWand(String wandId) {
BlockEditWand wand = wands.get(wandId);
if (wand == null) return null;
ItemStack itemStack = new ItemStack(Material.WOODEN_AXE);
@@ -47,7 +54,7 @@ public class BlockEditWands {
meta.displayName(wand.getName());
if (wand.getCustomModelData() != 0)
meta.setCustomModelData(wand.getCustomModelData());
meta.getPersistentDataContainer().set(BlockEditWands.getWandKey(), PersistentDataType.STRING, wandId);
meta.getPersistentDataContainer().set(wandKey, PersistentDataType.STRING, wandId);
itemStack.setItemMeta(meta);
return itemStack;
}
@@ -57,7 +64,7 @@ public class BlockEditWands {
* PSA: Wand IDs will get converted to lowercase.
* @return true if registered successfully, false if not
*/
public static boolean registerWand(BlockEditWand wand) {
public boolean registerWand(BlockEditWand wand) {
if (wands.containsKey(wand.getId().toLowerCase())) {
BlockEdit.getPlugin().getLogger().warning("Tried to register wand with id \""+wand.getId()+"\", but wand with that id already exists!");
return false;
@@ -2,28 +2,43 @@ package me.youhavetrouble.blockedit.api;
import me.youhavetrouble.blockedit.BlockEdit;
import me.youhavetrouble.blockedit.SchematicHandler;
import me.youhavetrouble.blockedit.WandsHandler;
import me.youhavetrouble.blockedit.util.ChunkWork;
import me.youhavetrouble.blockedit.util.Selection;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
public class BlockEditAPI {
/**
* Runs an operation on the given selection with the given amount of chunks per tick
* @param selection The area that will be operated on
* @param chunksPerTick Amount of chunks per tick to modify
* @param operation Operation to execute
*/
public static void runOperation(Selection selection, int chunksPerTick, BlockEditOperation operation) {
public static void runOperation(
@NotNull Selection selection,
int chunksPerTick,
@NotNull BlockEditOperation operation
) {
HashSet<ChunkWork> work = WorkSplitter.getOperatedOnChunks(selection);
WorkSplitter.runOperation(work, selection, chunksPerTick, operation);
}
/**
* Gets the wands handler object that can be used to work with wands
* @return Wands handler
*/
public static WandsHandler getWandsHandler() {
return BlockEdit.getWandsHandler();
}
/**
* Gets schematic handler object that can be used to work with schematics
* @return Schematic handler
*/
public SchematicHandler getSchematicHandler() {
public static SchematicHandler getSchematicHandler() {
return BlockEdit.getSchematicHandler();
}
}
@@ -1,7 +1,12 @@
package me.youhavetrouble.blockedit.api;
import me.youhavetrouble.blockedit.util.Selection;
import org.bukkit.block.Block;
/**
* This interface is used to define a BlockEdit operation.<br>
* Implement this interface and pass it to {@link BlockEditAPI#runOperation(Selection, int, BlockEditOperation)} to run it.
*/
public interface BlockEditOperation {
/**
@@ -6,7 +6,7 @@ import net.kyori.adventure.text.Component;
public interface BlockEditWand {
/**
* A unique id to identify the wand. Also used in //wand command.
* A unique id to identify the wand. Also used in /wand command.
*/
String getId();
@@ -31,6 +31,7 @@ public class WorkSplitter {
protected static void runOperation(HashSet<ChunkWork> chunkWorks, Selection selection, int chunksPerTick, BlockEditOperation operation) {
if (selection == null) return;
Selection sel = new Selection(selection.clone().expand(0.1), selection.getWorldUuid());
List<ChunkWork> chunkWork = new ArrayList<>(chunkWorks);
AtomicInteger element = new AtomicInteger(chunkWork.size()-1);
Bukkit.getScheduler().runTaskTimer(BlockEdit.getPlugin(), (task) -> {
@@ -39,7 +40,7 @@ public class WorkSplitter {
return;
}
for (int i = 0; i< chunksPerTick; i++) {
processChunkWork(chunkWork.get(element.getAndDecrement()), selection, operation);
processChunkWork(chunkWork.get(element.getAndDecrement()), sel, operation);
}
}, 0, 1);
}
@@ -39,7 +39,6 @@ public class PasteCommand extends Command {
});
Selection selection = Selection.fromClipboard(absoluteBlocks.keySet(), player.getWorld());
selection.expand(1);
BlockEditAPI.runOperation(selection, 1, new PasteOperation(absoluteBlocks));
player.sendMessage(Component.text("Pasting clipboard..."));
@@ -10,7 +10,6 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
@@ -62,12 +61,12 @@ public class ReplaceCommand extends Command {
BlockData blockData = material.createBlockData();
BlockData blockDataToReplaceWith = materialToReplace.createBlockData();
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BoundingBox selection = bePlayer.getSelection();
Selection selection = bePlayer.getSelection();
if (selection == null) {
player.sendMessage(Component.text("You need to select 2 points to do this"));
return true;
}
BlockEditAPI.runOperation(new Selection(selection, bePlayer.getSelectionWorld()), 1, new ReplaceOperation(blockData, blockDataToReplaceWith));
BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(blockData, blockDataToReplaceWith));
return true;
}
}
@@ -1,6 +1,7 @@
package me.youhavetrouble.blockedit.commands;
import me.youhavetrouble.blockedit.BEPlayer;
import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -17,12 +18,12 @@ public class RotateCommand extends Command {
public boolean execute(@NotNull CommandSender sender, @NotNull String s, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("You need to be a player to use this command");
sender.sendMessage(Component.text("You need to be a player to use this command"));
return true;
}
if (args.length == 0) {
player.sendMessage("You need to provide an angle");
player.sendMessage(Component.text("You need to provide an angle"));
return true;
}
@@ -31,12 +32,12 @@ public class RotateCommand extends Command {
try {
angle = Double.parseDouble(args[0]);
} catch (NumberFormatException e) {
player.sendMessage("Angle must be a number");
player.sendMessage(Component.text("Angle must be a number"));
return true;
}
if (angle > 360 || angle < -360) {
player.sendMessage("Angle must be between -360 and 360");
player.sendMessage(Component.text("Angle must be between -360 and 360"));
return true;
}
@@ -44,7 +45,7 @@ public class RotateCommand extends Command {
bePlayer.getClipboard().rotate(angle);
player.sendMessage("Rotated clipboard by " + angle + " degrees");
player.sendMessage(Component.text("Rotated clipboard by " + angle + " degrees"));
return false;
}
@@ -10,7 +10,6 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
@@ -52,12 +51,12 @@ public class SetCommand extends Command {
}
BlockData blockData = material.createBlockData();
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BoundingBox selection = bePlayer.getSelection();
Selection selection = bePlayer.getSelection();
if (selection == null) {
player.sendMessage(Component.text("You need to select 2 points to do this"));
return true;
}
BlockEditAPI.runOperation(new Selection(selection, bePlayer.getSelectionWorld()), 1, new SetOperation(blockData));
BlockEditAPI.runOperation(selection, 1, new SetOperation(blockData));
}
return true;
}
@@ -1,6 +1,6 @@
package me.youhavetrouble.blockedit.commands;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import me.youhavetrouble.blockedit.api.BlockEditAPI;
import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -24,10 +24,10 @@ public class WandCommand extends Command {
if (!(commandSender instanceof Player player)) return true;
ItemStack wand;
if (args.length == 0) {
wand = BlockEditWands.getWand("select");
wand = BlockEditAPI.getWandsHandler().getWand("select");
if (wand == null) return true;
} else {
wand = BlockEditWands.getWand(args[0]);
wand = BlockEditAPI.getWandsHandler().getWand(args[0]);
if (wand == null) {
player.sendMessage(Component.text("Could not find wand with id "+args[0]));
return true;
@@ -40,7 +40,7 @@ public class WandCommand extends Command {
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1)
return StringUtil.copyPartialMatches(args[0], BlockEditWands.getWandIds(), new ArrayList<>());
return StringUtil.copyPartialMatches(args[0], BlockEditAPI.getWandsHandler().getWandIds(), new ArrayList<>());
return new ArrayList<>();
}
@@ -5,6 +5,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
import java.util.UUID;
@@ -46,6 +47,12 @@ public class Selection extends BoundingBox {
this.worldUuid = worldUuid;
}
@NotNull
@Override
public Selection clone() {
return new Selection(this, 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);
@@ -1,8 +1,8 @@
package me.youhavetrouble.blockedit.wands;
import me.youhavetrouble.blockedit.BEPlayer;
import me.youhavetrouble.blockedit.api.BlockEditAPI;
import me.youhavetrouble.blockedit.api.BlockEditWand;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.block.Block;
@@ -38,7 +38,7 @@ public class SelectionWand implements Listener, BlockEditWand {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerSelectBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
String wandId = BlockEditWands.isWand(event.getItem());
String wandId = BlockEditAPI.getWandsHandler().getWandId(event.getItem());
if (wandId == null) return;
if (!wandId.equals(getId())) return;
if (!player.hasPermission(getPermission())) return;