migrate to paper plugin system

This commit is contained in:
2023-02-26 17:37:16 +01:00
parent e10e378300
commit 35c3bf000b
13 changed files with 85 additions and 254 deletions
+2 -13
View File
@@ -73,20 +73,9 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.5.Final</version>
<version>1.19.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -1,6 +1,5 @@
package me.youhavetrouble.blockedit;
import me.youhavetrouble.blockedit.optionals.SelectionHighlight;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@@ -3,6 +3,7 @@ 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;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabExecutor;
import org.bukkit.plugin.java.JavaPlugin;
@@ -21,12 +22,12 @@ public final class BlockEdit extends JavaPlugin {
BlockEditWands.registerWand(selectionWand);
getServer().getPluginManager().registerEvents(selectionWand, this);
registerCommand("/wand", new WandCommand());
registerCommand("/set", new SetCommand());
registerCommand("/replace", new ReplaceCommand());
registerCommand("/pos1", new Pos1Command());
registerCommand("/pos2", new Pos2Command());
registerCommand("/desel", new DeselCommand());
registerCommand(new WandCommand());
registerCommand(new SetCommand());
registerCommand(new ReplaceCommand());
registerCommand(new DeselCommand());
registerCommand(new Pos1Command());
registerCommand(new Pos2Command());
}
@@ -35,11 +36,7 @@ public final class BlockEdit extends JavaPlugin {
return plugin;
}
private void registerCommand(String command, TabExecutor executor) {
PluginCommand bukkitReplaceCommand = getCommand(command);
if (bukkitReplaceCommand != null) {
bukkitReplaceCommand.setExecutor(executor);
bukkitReplaceCommand.setTabCompleter(executor);
}
private void registerCommand(Command command) {
getServer().getCommandMap().register("blockedit", command);
}
}
@@ -1,31 +1,24 @@
package me.youhavetrouble.blockedit.commands;
import me.youhavetrouble.blockedit.BEPlayer;
import me.youhavetrouble.blockedit.BlockEdit;
import me.youhavetrouble.blockedit.optionals.SelectionHighlight;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class DeselCommand extends Command {
public DeselCommand() {
super("desel");
setPermission("blockedit.command.desel");
}
public class DeselCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
BEPlayer.getByPlayer(player).resetSelection();
Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(),() -> SelectionHighlight.sendStop(player));
player.sendMessage(Component.text("You have reset your selection"));
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}
@@ -4,16 +4,17 @@ import me.youhavetrouble.blockedit.BEPlayer;
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.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class Pos1Command extends Command {
public Pos1Command() {
super("pos1");
setPermission("blockedit.command.pos");
}
public class Pos1Command implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
bePlayer.setSelectionPoint1(player.getLocation().toBlockLocation());
@@ -21,8 +22,4 @@ public class Pos1Command implements TabExecutor {
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}
@@ -4,25 +4,21 @@ import me.youhavetrouble.blockedit.BEPlayer;
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.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class Pos2Command extends Command {
public class Pos2Command implements TabExecutor {
public Pos2Command() {
super("pos2");
setPermission("blockedit.command.pos");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
bePlayer.setSelectionPoint2(player.getLocation().toBlockLocation());
player.sendMessage(Component.text("Second point set at your location"));
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}
@@ -9,21 +9,37 @@ import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
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 ReplaceCommand implements TabExecutor {
public class ReplaceCommand extends Command {
public ReplaceCommand() {
super("replace");
setPermission("blockedit.command.replace");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1 || args.length == 2) {
ArrayList<String> suggestions = new ArrayList<>();
for (Material material : Material.values()) {
if (material.isBlock())
suggestions.add(material.name().toLowerCase());
}
return StringUtil.copyPartialMatches(args[args.length-1], suggestions, new ArrayList<>());
}
return new ArrayList<>();
}
@Override
public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] args) {
if (!(commandSender instanceof Player player)) return true;
if (args.length == 0) {
player.sendMessage(Component.text("You need to provide block type"));
return true;
@@ -54,18 +70,4 @@ public class ReplaceCommand implements TabExecutor {
BlockEditAPI.runOperation(new Selection(selection, bePlayer.getSelectionWorld()), 1, new ReplaceOperation(blockData, blockDataToReplaceWith));
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1 || args.length == 2) {
ArrayList<String> suggestions = new ArrayList<>();
for (Material material : Material.values()) {
if (material.isBlock())
suggestions.add(material.name().toLowerCase());
}
return StringUtil.copyPartialMatches(args[args.length-1], suggestions, new ArrayList<>());
}
return null;
}
}
@@ -1,7 +1,6 @@
package me.youhavetrouble.blockedit.commands;
import me.youhavetrouble.blockedit.BEPlayer;
import me.youhavetrouble.blockedit.WorkSplitter;
import me.youhavetrouble.blockedit.api.BlockEditAPI;
import me.youhavetrouble.blockedit.operations.SetOperation;
import me.youhavetrouble.blockedit.util.Selection;
@@ -10,20 +9,38 @@ import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
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 SetCommand implements TabExecutor {
public class SetCommand extends Command {
public SetCommand() {
super("set");
setPermission("blockedit.command.set");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1) {
ArrayList<String> suggestions = new ArrayList<>();
for (Material material : Material.values()) {
if (material.isBlock())
suggestions.add(material.name().toLowerCase());
}
return StringUtil.copyPartialMatches(args[0], suggestions, new ArrayList<>());
}
return new ArrayList<>();
}
@Override
public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] args) {
if (!(commandSender instanceof Player player)) return true;
if (args.length == 0) {
player.sendMessage(Component.text("You need to provide block type"));
return true;
@@ -44,19 +61,4 @@ public class SetCommand implements TabExecutor {
}
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1) {
ArrayList<String> suggestions = new ArrayList<>();
for (Material material : Material.values()) {
if (material.isBlock())
suggestions.add(material.name().toLowerCase());
}
return StringUtil.copyPartialMatches(args[0], suggestions, new ArrayList<>());
}
return null;
}
}
@@ -4,21 +4,24 @@ 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 {
public class WandCommand extends Command {
public WandCommand() {
super("wand");
setPermission("blockedit.command.wand");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return true;
public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] args) {
if (!(commandSender instanceof Player player)) return true;
ItemStack wand;
if (args.length == 0) {
wand = BlockEditWands.getWand("select");
@@ -34,11 +37,11 @@ public class WandCommand implements TabExecutor {
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
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 null;
return new ArrayList<>();
}
}
@@ -1,106 +0,0 @@
package me.youhavetrouble.blockedit.optionals;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.MinecraftKey;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import me.youhavetrouble.blockedit.BlockEdit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
/*
* Highlighting selection blocks thanks to https://github.com/ArtFect/BlockHighlight
*
* MIT License
*
* Copyright (c) 2021 ArtFect
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class SelectionHighlight {
public static void highlightBlock(Player player, Location location, String color, String text, int time) {
if (location == null) return;
if (BlockEdit.getPlugin().getServer().getPluginManager().isPluginEnabled("ProtocolLib")) {
sendBlockHighlight(player, location, color, text, time);
}
}
public static void sendStop(Player pl) {
sendPayload(pl, "debug/game_test_clear", Unpooled.wrappedBuffer(new byte[0]));
}
private static void sendBlockHighlight(Player player, Location location, String hex, String text, int time) {
ByteBuf packet = Unpooled.buffer();
packet.writeLong(blockPosToLong((int)location.getX(), (int)location.getY(), (int)location.getZ()));
int color = hex2Rgb(hex, 100).getRGB();
packet.writeInt(color);
writeString(packet, text);
packet.writeInt(time);
sendPayload(player, "debug/game_test_add_marker", packet);
}
private static long blockPosToLong(int x, int y, int z) {
return ((long) x & 67108863L) << 38 | (long) y & 4095L | ((long) z & 67108863L) << 12;
}
private static void writeBytes(ByteBuf packet, int i) {
while ((i & -128) != 0) {
packet.writeByte(i & 127 | 128);
i >>>= 7;
}
packet.writeByte(i);
}
private static void writeString(ByteBuf packet, String s) {
byte[] abyte = s.getBytes(StandardCharsets.UTF_8);
writeBytes(packet, abyte.length);
packet.writeBytes(abyte);
}
private static void sendPayload(Player receiver, String channel, ByteBuf bytes) {
PacketContainer handle = new PacketContainer(PacketType.Play.Server.CUSTOM_PAYLOAD);
handle.getMinecraftKeys().write(0, new MinecraftKey(channel));
Object serializer = MinecraftReflection.getPacketDataSerializer(bytes);
handle.getModifier().withType(ByteBuf.class).write(0, serializer);
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, handle);
} catch (InvocationTargetException e) {
throw new RuntimeException("Unable to send the packet", e);
}
}
//https://stackoverflow.com/questions/4129666/how-to-convert-hex-to-rgb-using-java/4129692
private static Color hex2Rgb(String colorStr, int transparency) {
return new Color(
Integer.valueOf(colorStr.substring(1, 3), 16),
Integer.valueOf(colorStr.substring(3, 5), 16),
Integer.valueOf(colorStr.substring(5, 7), 16), transparency);
}
}
@@ -1,14 +1,10 @@
package me.youhavetrouble.blockedit.wands;
import me.youhavetrouble.blockedit.BEPlayer;
import me.youhavetrouble.blockedit.BlockEdit;
import me.youhavetrouble.blockedit.api.BlockEditWand;
import me.youhavetrouble.blockedit.api.BlockEditWands;
import me.youhavetrouble.blockedit.optionals.SelectionHighlight;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -52,42 +48,15 @@ public class SelectionWand implements Listener, BlockEditWand {
if (action.equals(Action.LEFT_CLICK_BLOCK)) {
event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint1(block.getLocation());
highlightPoints(player);
player.sendMessage(Component.text("First point set"));
return;
}
if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint2(block.getLocation());
highlightPoints(player);
player.sendMessage(Component.text("Second point set"));
return;
}
}
private void highlightPoints(Player player) {
Bukkit.getScheduler().runTaskAsynchronously(BlockEdit.getPlugin(), () -> {
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
SelectionHighlight.sendStop(player);
Location selection1 = bePlayer.getSelectionPoint1();
Location selection2 = bePlayer.getSelectionPoint2();
if (selection1 != null && selection1.equals(selection2)) {
SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint1(), "#ffffff", "Selection Points", 10000);
return;
}
if (selection1 != null && !selection1.equals(selection2)) {
SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint1(), "#ffffff", "Selection Point 1", 10000);
}
if (selection2 != null && !selection2.equals(selection1)) {
SelectionHighlight.highlightBlock(player, bePlayer.getSelectionPoint2(), "#ffffff", "Selection Point 2", 10000);
}
});
}
}
@@ -1,7 +1,7 @@
name: BlockEdit
version: ${project.version}
main: me.youhavetrouble.blockedit.BlockEdit
api-version: 1.17
api-version: 1.19
authors: [ YouHaveTrouble ]
description: Modern WorldEdit alternative
website: youhavetrouble.me
-10
View File
@@ -1,10 +0,0 @@
import me.youhavetrouble.blockedit.WorkSplitter;
import me.youhavetrouble.blockedit.util.ChunkWork;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class WorkSplitterTest {
}