forked from gnembon/fabric-carpet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfoCommand.java
85 lines (77 loc) · 3.31 KB
/
InfoCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package carpet.commands;
import carpet.CarpetSettings;
import carpet.utils.BlockInfo;
import carpet.utils.CommandHelper;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
public class InfoCommand
{
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> command = literal("info").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandInfo)).
then(literal("block").
then(argument("block position", BlockPosArgument.blockPos()).
executes( (c) -> infoBlock(
c.getSource(),
BlockPosArgument.getSpawnablePos(c, "block position"), null)).
then(literal("grep").
then(argument("regexp",greedyString()).
executes( (c) -> infoBlock(
c.getSource(),
BlockPosArgument.getSpawnablePos(c, "block position"),
getString(c, "regexp")))))));
dispatcher.register(command);
}
public static void printBlock(List<Component> messages, CommandSourceStack source, String grep)
{
Messenger.m(source, "");
if (grep != null)
{
Pattern p = Pattern.compile(grep);
Messenger.m(source, messages.get(0));
messages.forEach(line -> {
Matcher m = p.matcher(line.getString());
if (m.find())
{
Messenger.m(source, line);
}
});
}
else
{
Messenger.send(source, messages);
}
}
private static int infoBlock(CommandSourceStack source, BlockPos pos, String grep)
{
if (!source.hasPermission(Commands.LEVEL_GAMEMASTERS)) {
//check id pos is loaded
if (!source.getLevel().hasChunkAt(pos)) {
Messenger.m(source, "r Chunk is not loaded");
return 0;
}
// verify it is in world bounds
if (!source.getLevel().isInWorldBounds(pos)) {
Messenger.m(source, "r Position is outside of world bounds");
return 0;
}
}
printBlock(BlockInfo.blockInfo(pos, source.getLevel()),source, grep);
return 1;
}
}