TagCommand.java
net.minecraft.server.commands.TagCommand
信息
- 全限定名:net.minecraft.server.commands.TagCommand
- 类型:public class
- 包:net.minecraft.server.commands
- 源码路径:src/main/java/net/minecraft/server/commands/TagCommand.java
- 起始行号:L18
- 职责:
TODO
字段/常量
-
ERROR_ADD_FAILED- 类型:
SimpleCommandExceptionType - 修饰符:
private static final - 源码定位:
L19 - 说明:
TODO
- 类型:
-
ERROR_REMOVE_FAILED- 类型:
SimpleCommandExceptionType - 修饰符:
private static final - 源码定位:
L20 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L22
- 方法名:register
- 源码定位:L22
- 返回类型:void
- 修饰符:public static
参数:
- dispatcher: CommandDispatcher
说明:
TODO
private static Collection<String> getTags(Collection<?extends Entity> entities) @ L50
- 方法名:getTags
- 源码定位:L50
- 返回类型:Collection
- 修饰符:private static
参数:
- entities: Collection<?extends Entity>
说明:
TODO
private static int addTag(CommandSourceStack source, Collection<?extends Entity> targets, String name) @ L60
- 方法名:addTag
- 源码定位:L60
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- targets: Collection<?extends Entity>
- name: String
说明:
TODO
private static int removeTag(CommandSourceStack source, Collection<?extends Entity> targets, String name) @ L82
- 方法名:removeTag
- 源码定位:L82
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- targets: Collection<?extends Entity>
- name: String
说明:
TODO
private static int listTags(CommandSourceStack source, Collection<?extends Entity> targets) @ L104
- 方法名:listTags
- 源码定位:L104
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- targets: Collection<?extends Entity>
说明:
TODO
代码
public class TagCommand {
private static final SimpleCommandExceptionType ERROR_ADD_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.tag.add.failed"));
private static final SimpleCommandExceptionType ERROR_REMOVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.tag.remove.failed"));
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("tag")
.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
.then(
Commands.argument("targets", EntityArgument.entities())
.then(
Commands.literal("add")
.then(
Commands.argument("name", StringArgumentType.word())
.executes(c -> addTag(c.getSource(), EntityArgument.getEntities(c, "targets"), StringArgumentType.getString(c, "name")))
)
)
.then(
Commands.literal("remove")
.then(
Commands.argument("name", StringArgumentType.word())
.suggests((c, p) -> SharedSuggestionProvider.suggest(getTags(EntityArgument.getEntities(c, "targets")), p))
.executes(
c -> removeTag(c.getSource(), EntityArgument.getEntities(c, "targets"), StringArgumentType.getString(c, "name"))
)
)
)
.then(Commands.literal("list").executes(c -> listTags(c.getSource(), EntityArgument.getEntities(c, "targets"))))
)
);
}
private static Collection<String> getTags(Collection<? extends Entity> entities) {
Set<String> result = Sets.newHashSet();
for (Entity entity : entities) {
result.addAll(entity.entityTags());
}
return result;
}
private static int addTag(CommandSourceStack source, Collection<? extends Entity> targets, String name) throws CommandSyntaxException {
int count = 0;
for (Entity entity : targets) {
if (entity.addTag(name)) {
count++;
}
}
if (count == 0) {
throw ERROR_ADD_FAILED.create();
} else {
if (targets.size() == 1) {
source.sendSuccess(() -> Component.translatable("commands.tag.add.success.single", name, targets.iterator().next().getDisplayName()), true);
} else {
source.sendSuccess(() -> Component.translatable("commands.tag.add.success.multiple", name, targets.size()), true);
}
return count;
}
}
private static int removeTag(CommandSourceStack source, Collection<? extends Entity> targets, String name) throws CommandSyntaxException {
int count = 0;
for (Entity entity : targets) {
if (entity.removeTag(name)) {
count++;
}
}
if (count == 0) {
throw ERROR_REMOVE_FAILED.create();
} else {
if (targets.size() == 1) {
source.sendSuccess(() -> Component.translatable("commands.tag.remove.success.single", name, targets.iterator().next().getDisplayName()), true);
} else {
source.sendSuccess(() -> Component.translatable("commands.tag.remove.success.multiple", name, targets.size()), true);
}
return count;
}
}
private static int listTags(CommandSourceStack source, Collection<? extends Entity> targets) {
Set<String> tags = Sets.newHashSet();
for (Entity entity : targets) {
tags.addAll(entity.entityTags());
}
if (targets.size() == 1) {
Entity entity = targets.iterator().next();
if (tags.isEmpty()) {
source.sendSuccess(() -> Component.translatable("commands.tag.list.single.empty", entity.getDisplayName()), false);
} else {
source.sendSuccess(
() -> Component.translatable("commands.tag.list.single.success", entity.getDisplayName(), tags.size(), ComponentUtils.formatList(tags)),
false
);
}
} else if (tags.isEmpty()) {
source.sendSuccess(() -> Component.translatable("commands.tag.list.multiple.empty", targets.size()), false);
} else {
source.sendSuccess(
() -> Component.translatable("commands.tag.list.multiple.success", targets.size(), tags.size(), ComponentUtils.formatList(tags)), false
);
}
return tags.size();
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.hasPermission(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
SharedSuggestionProvider.suggest()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
EntityArgument.entities(), EntityArgument.getEntities()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.translatable()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
ComponentUtils.formatList()
- 引用位置:
-
- 引用位置:
参数
- 引用位置: