From 9cf9928bbd918c0b130b080d037b78aaaf5b102e Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 13 May 2019 08:23:23 -0300 Subject: [PATCH] two last commands from 1.13.2 --- .../java/carpet/commands/DistanceCommand.java | 42 ++++++ .../java/carpet/commands/InfoCommand.java | 129 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/main/java/carpet/commands/DistanceCommand.java create mode 100644 src/main/java/carpet/commands/InfoCommand.java diff --git a/src/main/java/carpet/commands/DistanceCommand.java b/src/main/java/carpet/commands/DistanceCommand.java new file mode 100644 index 00000000..947fe38a --- /dev/null +++ b/src/main/java/carpet/commands/DistanceCommand.java @@ -0,0 +1,42 @@ +package carpet.commands; + +import carpet.CarpetSettings; +import carpet.utils.DistanceCalculator; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.command.arguments.Vec3ArgumentType; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +public class DistanceCommand +{ + public static void register(CommandDispatcher dispatcher) + { + LiteralArgumentBuilder command = literal("distance"). + requires((player) -> CarpetSettings.getBool("commandDistance")). + then(literal("from"). + executes( (c) -> DistanceCalculator.setStart(c.getSource(), c.getSource().getPosition())). + then(argument("from", Vec3ArgumentType.create()). + executes( (c) -> DistanceCalculator.setStart( + c.getSource(), + Vec3ArgumentType.getVec3(c, "from"))). + then(literal("to"). + executes((c) -> DistanceCalculator.distance( + c.getSource(), + Vec3ArgumentType.getVec3(c, "from"), + c.getSource().getPosition())). + then(argument("to", Vec3ArgumentType.create()). + executes( (c) -> DistanceCalculator.distance( + c.getSource(), + Vec3ArgumentType.getVec3(c, "from"), + Vec3ArgumentType.getVec3(c, "to") + )))))). + then(literal("to"). + executes( (c) -> DistanceCalculator.setEnd(c.getSource(), c.getSource().getPosition()) ). + then(argument("to", Vec3ArgumentType.create()). + executes( (c) -> DistanceCalculator.setEnd(c.getSource(), Vec3ArgumentType.getVec3(c, "to"))))); + dispatcher.register(command); + } +} diff --git a/src/main/java/carpet/commands/InfoCommand.java b/src/main/java/carpet/commands/InfoCommand.java new file mode 100644 index 00000000..6d22fae9 --- /dev/null +++ b/src/main/java/carpet/commands/InfoCommand.java @@ -0,0 +1,129 @@ +package carpet.commands; + +import carpet.CarpetSettings; +import carpet.utils.BlockInfo; +import carpet.utils.EntityInfo; +import carpet.utils.Messenger; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.network.chat.BaseComponent; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.command.arguments.BlockPosArgumentType; +import net.minecraft.command.arguments.EntityArgumentType; +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +public class InfoCommand +{ + public static void register(CommandDispatcher dispatcher) + { + LiteralArgumentBuilder command = literal("info"). + requires((player) -> CarpetSettings.getBool("commandInfo")). + then(literal("block"). + then(argument("block position", BlockPosArgumentType.create()). + executes( (c) -> infoBlock( + c.getSource(), + BlockPosArgumentType.getBlockPos(c, "block position"), null)). + then(literal("grep"). + then(argument("regexp",greedyString()). + executes( (c) -> infoBlock( + c.getSource(), + BlockPosArgumentType.getBlockPos(c, "block position"), + getString(c, "regexp"))))))). + then(literal("entity"). + then(argument("entity selector", EntityArgumentType.entities()). + executes( (c) -> infoEntities( + c.getSource(), EntityArgumentType.getEntities(c,"entity selector"), null)). + then(literal("grep"). + then(argument("regexp",greedyString()). + executes( (c) -> infoEntities( + c.getSource(), + EntityArgumentType.getEntities(c,"entity selector"), + getString(c, "regexp"))))))); + + dispatcher.register(command); + } + + public static void printEntity(List messages, ServerCommandSource source, String grep) + { + List actual = new ArrayList<>(); + if (grep != null) + { + Pattern p = Pattern.compile(grep); + actual.add(messages.get(0)); + boolean empty = true; + for (int i = 1; i messages, ServerCommandSource source, String grep) + { + Messenger.m(source, ""); + if (grep != null) + { + Pattern p = Pattern.compile(grep); + Messenger.m(source, messages.get(0)); + for (int i = 1; i entities, String grep) + { + for (Entity e: entities) + { + List report = EntityInfo.entityInfo(e, source.getWorld()); + printEntity(report, source, grep); + } + return 1; + } + private static int infoBlock(ServerCommandSource source, BlockPos pos, String grep) + { + printBlock(BlockInfo.blockInfo(pos, source.getWorld()),source, grep); + return 1; + } + +}