How do I take boolean arguments in custom command? #2121
Answered
by
Daomephsta
SirEnder125
asked this question in
Mod Dev Support
-
Hey! So I've recently been adding commands to minecraft with fabric. I want to add a boolean argument to my command though. 🤔 import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.BlockStateArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.PositionImpl;
import net.minecraft.world.World;
public class CreateSphereCommand {
public static final boolean FULL_SPHERE = true;
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
dispatcher.register(CommandManager.literal("createsphere")
.then(CommandManager.argument("pos", BlockPosArgumentType.blockPos())
.then(CommandManager.argument("block", BlockStateArgumentType.blockState())
.then(CommandManager.argument("radius", DoubleArgumentType.doubleArg())
.executes((context) -> run(context,
BlockPosArgumentType.getBlockPos(context, "pos"),
BlockStateArgumentType.getBlockState(context, "block").getBlockState(),
DoubleArgumentType.getDouble(context, "radius")))))));
}
public static int run(CommandContext<ServerCommandSource> context, BlockPos centerPos, BlockState state, double radius) {
if (radius > 100) return 0;
BlockPos startPos = centerPos.add(-100, -100, -100);
Block block = state.getBlock();
World world = context.getSource().getWorld();
for (int i = startPos.getX(); i < startPos.getX() + 200; i++)
{
for (int j = startPos.getY(); j < startPos.getY() + 200; j++)
{
for (int k = startPos.getZ(); k < startPos.getZ() + 200; k++)
{
BlockPos pos = new BlockPos(i, j, k);
// Make a sphere of the block by changing each block within a certain distance of the center block to gold.
if (pos.isWithinDistance(new PositionImpl(centerPos.getX(), centerPos.getY(), centerPos.getZ()), radius)) {
Block oldBlock = world.getBlockState(pos).getBlock();
if (FULL_SPHERE) world.setBlockState(pos, block.getDefaultState());
// Do not change water or air into the block.
else if (oldBlock != Blocks.AIR && oldBlock != Blocks.WATER) {
world.setBlockState(pos, block.getDefaultState());
}
}
}
}
}
return 1;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
Daomephsta
Apr 11, 2022
Replies: 1 comment 1 reply
-
It's called |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
SirEnder125
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's called
com.mojang.brigadier.arguments.BoolArgumentType
rather thanBooleanArgumentType