Initial commit

This commit is contained in:
YouHaveTrouble
2021-07-22 03:34:32 +02:00
commit 9dbcc6d7d0
14 changed files with 662 additions and 0 deletions
@@ -0,0 +1,66 @@
package me.youhavetrouble.blockedit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
import java.util.HashMap;
import java.util.UUID;
public class BEPlayer {
private static final HashMap<UUID, BEPlayer> playerHashMap = new HashMap<>();
private BoundingBox selection;
private Location selectionPoint1, selectionPoint2;
public BoundingBox getSelection() {
return selection;
}
private void updateSelection() {
if (selectionPoint1 == null || selectionPoint2 == null) return;
selection = BoundingBox.of(selectionPoint1, selectionPoint2);
}
public void setSelectionPoint1(Location selectionPoint1) {
if (this.selectionPoint1 != null && this.selectionPoint1.equals(selectionPoint1)) return;
this.selectionPoint1 = selectionPoint1;
updateSelection();
}
public void setSelectionPoint2(Location selectionPoint2) {
if (this.selectionPoint2 != null && this.selectionPoint2.equals(selectionPoint2)) return;
this.selectionPoint2 = selectionPoint2;
updateSelection();
}
/**
* @return Clone of selectionPoint1
*/
public Location getSelectionPoint1() {
return selectionPoint1.clone();
}
/**
* @return Clone of selectionPoint2
*/
public Location getSelectionPoint2() {
return selectionPoint2.clone();
}
protected static void addPlayer(Player player) {
playerHashMap.put(player.getUniqueId(), new BEPlayer());
}
protected static void removePlayer(Player player) {
playerHashMap.remove(player.getUniqueId());
}
public static BEPlayer getByPlayer(Player player) {
return getByUuid(player.getUniqueId());
}
protected static BEPlayer getByUuid(UUID uuid) {
return playerHashMap.get(uuid);
}
}
@@ -0,0 +1,34 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import me.youhavetrouble.blockedit.commands.WandCommand;
import me.youhavetrouble.blockedit.wands.SelectionWand;
import org.bukkit.plugin.java.JavaPlugin;
public final class BlockEdit extends JavaPlugin {
private static BlockEdit plugin;
@Override
public void onEnable() {
plugin = this;
getServer().getPluginManager().registerEvents(new JoinLeaveListener(), this);
SelectionWand selectionWand = new SelectionWand();
BlockEditWands.registerWand(selectionWand);
getServer().getPluginManager().registerEvents(selectionWand, this);
getCommand("test").setExecutor(new TestCommand());
WandCommand wandCommand = new WandCommand();
getCommand("/wand").setExecutor(wandCommand);
getCommand("/wand").setTabCompleter(wandCommand);
}
public static BlockEdit getPlugin() {
return plugin;
}
}
@@ -0,0 +1,20 @@
package me.youhavetrouble.blockedit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class JoinLeaveListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
BEPlayer.addPlayer(event.getPlayer());
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
BEPlayer.removePlayer(event.getPlayer());
}
}
@@ -0,0 +1,41 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.util.ChunkWork;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BoundingBox;
import org.jetbrains.annotations.NotNull;
public class TestCommand implements CommandExecutor {
BukkitTask task;
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof Player player) {
Location location = player.getLocation();
ChunkWork work = WorkSplitter.locationToChunkWork(location.getX(), location.getZ(), location.getWorld());
if (task != null) {
task.cancel();
}
task = Bukkit.getScheduler().runTaskTimerAsynchronously(BlockEdit.getPlugin(),() -> {
BoundingBox box = work.getWorkspace(null);
for (int y = 0; y< 255; y++) {
location.getWorld().spawnParticle(Particle.END_ROD, box.getMaxX(), y, box.getMaxZ(), 0, 0.01, 0.01, 0.01);
location.getWorld().spawnParticle(Particle.END_ROD, box.getMinX(), y, box.getMinZ(), 0, 0.01, 0.01, 0.01);
}
}, 0, 4);
}
return true;
}
}
@@ -0,0 +1,30 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.util.ChunkWork;
import org.bukkit.World;
import org.bukkit.util.BoundingBox;
import java.util.HashSet;
import java.util.Set;
public class WorkSplitter {
public static Set<ChunkWork> getOperatedOnChunks(BoundingBox boundingBox, World world) {
HashSet<ChunkWork> chunks = new HashSet<>();
for (double x = boundingBox.getMinX(); x<= boundingBox.getMaxX(); x+=16) {
for (double z = boundingBox.getMinZ(); z <= boundingBox.getMaxZ(); z+=16) {
ChunkWork chunkWork = locationToChunkWork(x,z, world);
if (chunks.contains(chunkWork)) continue;
chunks.add(chunkWork);
}
}
return chunks;
}
public static ChunkWork locationToChunkWork(double x, double z, World world) {
int chunkX = (int) x >> 4;
int chunkZ = (int) z >> 4;
return new ChunkWork(chunkX, chunkZ, world);
}
}
@@ -0,0 +1,28 @@
package me.youhavetrouble.blockedit.api;
import net.kyori.adventure.text.Component;
public interface BlockEditWand {
/**
* A unique id to identify the wand. Also used in //wand command.
*/
String getId();
/**
* Name of the wand that will be used as wand item name.
*/
Component getName();
/**
* Custom model data for the wand item. Set to 0 to not give the wand custom model data.
*/
int getCustomModelData();
/**
* Permission for the wand usage
*/
String getPermission();
}
@@ -0,0 +1,70 @@
package me.youhavetrouble.blockedit.api;
import com.google.common.collect.ImmutableSet;
import me.youhavetrouble.blockedit.BlockEdit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import java.util.Collection;
import java.util.HashMap;
public class BlockEditWands {
private static final NamespacedKey wandKey = new NamespacedKey(BlockEdit.getPlugin(), "wand");
private static final HashMap<String, BlockEditWand> wands = new HashMap<>();
public static NamespacedKey getWandKey() {
return wandKey;
}
/**
* @param itemStack ItemStack to check
* @return WandId if a wand, null otherwise
*/
public static String isWand(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);
}
/**
* @return Immutable set of registered wand IDs
*/
public static Collection<String> getWandIds() {
return ImmutableSet.copyOf(wands.keySet());
}
public static ItemStack getWand(String wandId) {
BlockEditWand wand = wands.get(wandId);
if (wand == null) return null;
ItemStack itemStack = new ItemStack(Material.WOODEN_AXE);
ItemMeta meta = itemStack.getItemMeta();
if (wand.getName() != null)
meta.displayName(wand.getName());
if (wand.getCustomModelData() != 0)
meta.setCustomModelData(wand.getCustomModelData());
meta.getPersistentDataContainer().set(BlockEditWands.getWandKey(), PersistentDataType.STRING, wandId);
itemStack.setItemMeta(meta);
return itemStack;
}
/**
* PSA: Wand IDs will get converted to lowercase.
* @return true if registered successfully, false if not
*/
public static 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;
}
wands.put(wand.getId().toLowerCase(), wand);
return true;
}
}
@@ -0,0 +1,44 @@
package me.youhavetrouble.blockedit.commands;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class WandCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
ItemStack wand;
if (args.length == 0) {
wand = BlockEditWands.getWand("select");
if (wand == null) return true;
} else {
wand = BlockEditWands.getWand(args[0]);
if (wand == null) {
player.sendMessage(Component.text("Could not find wand with id "+args[0]));
return true;
}
}
player.getInventory().addItem(wand);
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1)
return StringUtil.copyPartialMatches(args[0], BlockEditWands.getWandIds(), new ArrayList<>());
return null;
}
}
@@ -0,0 +1,38 @@
package me.youhavetrouble.blockedit.util;
import org.bukkit.World;
import org.bukkit.util.BoundingBox;
public class ChunkWork {
private final int x, z, minHeight, maxHeight;
public ChunkWork(double x, double z, World world) {
this.x = (int) x;
this.z = (int) z;
if (world == null) {
this.maxHeight = 256;
this.minHeight = 0;
} else {
this.minHeight = world.getMinHeight();
this.maxHeight = world.getMaxHeight();
}
}
public BoundingBox getWorkspace(BoundingBox selection) {
// TODO make it return shared space of getChunkBox and selection to cull some of the blocks from iterations
return getChunkBox();
}
private BoundingBox getChunkBox() {
return new BoundingBox(x*16, minHeight, z*16, (x+1)*16, maxHeight, (z+1)*16);
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
}
@@ -0,0 +1,61 @@
package me.youhavetrouble.blockedit.wands;
import me.youhavetrouble.blockedit.BEPlayer;
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;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
public class SelectionWand implements Listener, BlockEditWand {
@Override
public String getId() {
return "select";
}
@Override
public Component getName() {
return Component.text("Selection Wand").decoration(TextDecoration.ITALIC, false);
}
@Override
public int getCustomModelData() {
return 0;
}
@Override
public String getPermission() {
return "blockedit.selectwand";
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerSelectBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
String wandId = BlockEditWands.isWand(event.getItem());
if (wandId == null) return;
if (!wandId.equals(getId())) return;
if (!player.hasPermission(getPermission())) return;
Block block = event.getClickedBlock();
if (block == null) return;
Action action = event.getAction();
if (action.equals(Action.LEFT_CLICK_BLOCK)) {
event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint1(block.getLocation());
player.sendMessage(Component.text("First point set"));
return;
}
if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint2(block.getLocation());
player.sendMessage(Component.text("Second point set"));
return;
}
}
}
+13
View File
@@ -0,0 +1,13 @@
name: BlockEdit
version: ${project.version}
main: me.youhavetrouble.blockedit.BlockEdit
api-version: 1.17
authors: [ YouHaveTrouble ]
description: Modern WorldEdit alternative
website: youhavetrouble.me
commands:
test:
description: test
/wand:
permission: blockedit.wand
description: gives you a blockedit wand