Find-Replace for Command Blocks

- Added a new utility command for map-making purposes
This commit is contained in:
simibubi 2021-01-27 14:26:14 +01:00
parent d35bdab51a
commit 433a508107
2 changed files with 71 additions and 0 deletions

View file

@ -13,6 +13,7 @@ public class AllCommands {
.then(ToggleDebugCommand.register()) .then(ToggleDebugCommand.register())
.then(OverlayConfigCommand.register()) .then(OverlayConfigCommand.register())
.then(FixLightingCommand.register()) .then(FixLightingCommand.register())
.then(ReplaceInCommandBlocksCommand.register())
//dev-util //dev-util
//Comment out for release //Comment out for release

View file

@ -0,0 +1,70 @@
package com.simibubi.create.foundation.command;
import org.apache.commons.lang3.mutable.MutableInt;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.block.BlockState;
import net.minecraft.block.CommandBlockBlock;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.arguments.BlockPosArgument;
import net.minecraft.tileentity.CommandBlockLogic;
import net.minecraft.tileentity.CommandBlockTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.server.ServerWorld;
public class ReplaceInCommandBlocksCommand {
public static ArgumentBuilder<CommandSource, ?> register() {
return Commands.literal("replaceInCommandBlocks")
.requires(cs -> cs.hasPermissionLevel(0))
.then(Commands.argument("begin", BlockPosArgument.blockPos())
.then(Commands.argument("end", BlockPosArgument.blockPos())
.then(Commands.argument("toReplace", StringArgumentType.string())
.then(Commands.argument("replaceWith", StringArgumentType.string())
.executes(ctx -> {
doReplace(ctx.getSource(), BlockPosArgument.getLoadedBlockPos(ctx, "begin"),
BlockPosArgument.getLoadedBlockPos(ctx, "end"),
StringArgumentType.getString(ctx, "toReplace"),
StringArgumentType.getString(ctx, "replaceWith"));
return 1;
})))));
}
private static void doReplace(CommandSource source, BlockPos from, BlockPos to, String toReplace,
String replaceWith) {
ServerWorld world = source.getWorld();
MutableInt blocks = new MutableInt(0);
BlockPos.getAllInBox(from, to)
.forEach(pos -> {
BlockState blockState = world.getBlockState(pos);
if (!(blockState.getBlock() instanceof CommandBlockBlock))
return;
TileEntity tileEntity = world.getTileEntity(pos);
if (!(tileEntity instanceof CommandBlockTileEntity))
return;
CommandBlockTileEntity cb = (CommandBlockTileEntity) tileEntity;
CommandBlockLogic commandBlockLogic = cb.getCommandBlockLogic();
String command = commandBlockLogic.getCommand();
if (command.indexOf(toReplace) != -1)
blocks.increment();
commandBlockLogic.setCommand(command.replaceAll(toReplace, replaceWith));
cb.markDirty();
world.notifyBlockUpdate(pos, blockState, blockState, 2);
});
int intValue = blocks.intValue();
if (intValue == 0) {
source.sendFeedback(new StringTextComponent("Couldn't find \"" + toReplace + "\" anywhere."), true);
return;
}
source.sendFeedback(
new StringTextComponent("Replaced occurrences in " + intValue + " blocks."),
true);
}
}