add chunks per tick arguments and refactor commands a bit

This commit is contained in:
2024-12-04 22:20:46 +01:00
parent ec9742690f
commit 56e212b8e7
2 changed files with 51 additions and 65 deletions
@@ -2,6 +2,7 @@ package me.youhavetrouble.blockedit;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.CommandSourceStack;
@@ -101,10 +102,7 @@ public class BlockEditCommands {
return builder.buildFuture(); return builder.buildFuture();
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
String wandId = ctx.getArgument("wand_id", String.class); String wandId = ctx.getArgument("wand_id", String.class);
ItemStack wand = BlockEditAPI.getWandsHandler().getWand(wandId); ItemStack wand = BlockEditAPI.getWandsHandler().getWand(wandId);
if (wand == null) { if (wand == null) {
@@ -116,10 +114,7 @@ public class BlockEditCommands {
}) })
) )
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
ItemStack wand = BlockEditAPI.getWandsHandler().getWand("select"); ItemStack wand = BlockEditAPI.getWandsHandler().getWand("select");
player.getInventory().addItem(wand); player.getInventory().addItem(wand);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
@@ -134,10 +129,7 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.copy"); return player.hasPermission("blockedit.command.copy");
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
try { try {
bePlayer.setClipboardFromSelection(); bePlayer.setClipboardFromSelection();
@@ -157,10 +149,7 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.desel"); return player.hasPermission("blockedit.command.desel");
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer.getByPlayer(player).resetSelection(); BEPlayer.getByPlayer(player).resetSelection();
player.sendMessage(Component.text("You have reset your selection")); player.sendMessage(Component.text("You have reset your selection"));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
@@ -175,10 +164,7 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.pos"); return player.hasPermission("blockedit.command.pos");
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer.getByPlayer(player).setSelectionPoint1(player.getLocation().toBlockLocation()); BEPlayer.getByPlayer(player).setSelectionPoint1(player.getLocation().toBlockLocation());
player.sendMessage(Component.text("First point set at your location")); player.sendMessage(Component.text("First point set at your location"));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
@@ -193,10 +179,7 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.pos"); return player.hasPermission("blockedit.command.pos");
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer.getByPlayer(player).setSelectionPoint2(player.getLocation().toBlockLocation()); BEPlayer.getByPlayer(player).setSelectionPoint2(player.getLocation().toBlockLocation());
player.sendMessage(Component.text("Second point set at your location")); player.sendMessage(Component.text("Second point set at your location"));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
@@ -211,10 +194,7 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.paste"); return player.hasPermission("blockedit.command.paste");
}) })
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
Vector playerLocationVector = player.getLocation().toBlockLocation().toVector(); Vector playerLocationVector = player.getLocation().toBlockLocation().toVector();
@@ -243,10 +223,7 @@ public class BlockEditCommands {
.then( .then(
Commands.argument("angle", DoubleArgumentType.doubleArg(-360, 360)) Commands.argument("angle", DoubleArgumentType.doubleArg(-360, 360))
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
double angle = ctx.getArgument("angle", Double.class); double angle = ctx.getArgument("angle", Double.class);
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
bePlayer.getClipboard().rotate(angle); bePlayer.getClipboard().rotate(angle);
@@ -264,25 +241,29 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.set"); return player.hasPermission("blockedit.command.set");
}) })
.then( .then(
// TODO replace with better way to provide block data, possibly multiple arguments
Commands.argument("block", ArgumentTypes.blockState()) Commands.argument("block", ArgumentTypes.blockState())
.executes(ctx -> { .executes(ctx -> {
if (!(ctx.getSource().getSender() instanceof Player player)) { Player player = (Player) ctx.getSource().getSender();
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED));
return Command.SINGLE_SUCCESS;
}
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BlockState blockState = ctx.getArgument("block", BlockState.class); BlockState blockState = ctx.getArgument("block", BlockState.class);
Selection selection = bePlayer.getSelection(); Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, 1, new SetOperation(blockState)); BlockEditAPI.runOperation(selection, 1, new SetOperation(blockState));
player.sendMessage(Component.text("Setting blocks...")); player.sendMessage(Component.text("Setting blocks..."));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
}) })
) )
.then(
Commands.argument("chunks_per_tick", IntegerArgumentType.integer(1))
.executes(ctx -> {
Player player = (Player) ctx.getSource().getSender();
BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BlockState blockState = ctx.getArgument("block", BlockState.class);
int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class);
Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, chunksPerTick, new SetOperation(blockState));
player.sendMessage(Component.text("Setting blocks..."));
return Command.SINGLE_SUCCESS;
}))
.build(); .build();
} }
@@ -293,28 +274,34 @@ public class BlockEditCommands {
return player.hasPermission("blockedit.command.replace"); return player.hasPermission("blockedit.command.replace");
}) })
.then( .then(
// TODO replace with better way to provide block data, possibly multiple arguments
Commands.argument("to_replace", ArgumentTypes.blockState()) Commands.argument("to_replace", ArgumentTypes.blockState())
.then(Commands.argument("replace_with", ArgumentTypes.blockState()) )
.executes(ctx -> { .then(
if (!(ctx.getSource().getSender() instanceof Player player)) { Commands.argument("replace_with", ArgumentTypes.blockState())
ctx.getSource().getSender().sendMessage(Component.text("Only players can use this command", NamedTextColor.RED)); .executes(ctx -> {
return Command.SINGLE_SUCCESS; Player player = (Player) ctx.getSource().getSender();
} BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BlockState toReplace = ctx.getArgument("to_replace", BlockState.class);
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class);
BlockState toReplace = ctx.getArgument("to_replace", BlockState.class); Selection selection = bePlayer.getSelection();
BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class); BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(toReplace, replaceWith));
player.sendMessage(Component.text("Replacing blocks..."));
Selection selection = bePlayer.getSelection(); return Command.SINGLE_SUCCESS;
})
BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(toReplace.getBlockData(), replaceWith)); )
player.sendMessage(Component.text("Replacing blocks...")); .then(
Commands.argument("chunks_per_tick", IntegerArgumentType.integer(1))
return Command.SINGLE_SUCCESS; .executes(ctx -> {
}) Player player = (Player) ctx.getSource().getSender();
) BEPlayer bePlayer = BEPlayer.getByPlayer(player);
BlockState toReplace = ctx.getArgument("to_replace", BlockState.class);
BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class);
int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class);
Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, chunksPerTick, new ReplaceOperation(toReplace, replaceWith));
player.sendMessage(Component.text("Replacing blocks..."));
return Command.SINGLE_SUCCESS;
})
) )
.build(); .build();
} }
@@ -3,14 +3,13 @@ package me.youhavetrouble.blockedit.operations;
import me.youhavetrouble.blockedit.api.BlockEditOperation; import me.youhavetrouble.blockedit.api.BlockEditOperation;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
public record ReplaceOperation(BlockData blockToReplace, BlockState blockToSet) implements BlockEditOperation { public record ReplaceOperation(BlockState blockToReplace, BlockState blockToSet) implements BlockEditOperation {
@Override @Override
public void transformBlock(Block block) { public void transformBlock(Block block) {
if (!block.getBlockData().matches(blockToReplace)) return; if (!block.getBlockData().matches(blockToReplace.getBlockData())) return;
BlockState newState = blockToSet.copy(block.getLocation()); BlockState newState = blockToSet.copy(block.getLocation());
newState.update(true, false); newState.update(true, false);
} }