EntityArgument.java

net.minecraft.commands.arguments.EntityArgument

信息

  • 全限定名:net.minecraft.commands.arguments.EntityArgument
  • 类型:public class
  • 包:net.minecraft.commands.arguments
  • 源码路径:src/main/java/net/minecraft/commands/arguments/EntityArgument.java
  • 起始行号:L29
  • 实现:ArgumentType
  • 职责:

    TODO

字段/常量

  • EXAMPLES

    • 类型: Collection<String>
    • 修饰符: private static final
    • 源码定位: L30
    • 说明:

      TODO

  • ERROR_NOT_SINGLE_ENTITY

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L31
    • 说明:

      TODO

  • ERROR_NOT_SINGLE_PLAYER

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L32
    • 说明:

      TODO

  • ERROR_ONLY_PLAYERS_ALLOWED

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L33
    • 说明:

      TODO

  • NO_ENTITIES_FOUND

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L36
    • 说明:

      TODO

  • NO_PLAYERS_FOUND

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L37
    • 说明:

      TODO

  • ERROR_SELECTORS_NOT_ALLOWED

    • 类型: SimpleCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L38
    • 说明:

      TODO

  • single

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L41
    • 说明:

      TODO

  • playersOnly

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L42
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.EntityArgument.Info

    • 类型: class
    • 修饰符: public static
    • 源码定位: L159
    • 说明:

      TODO

  • net.minecraft.commands.arguments.EntityArgument.Info.Template

    • 类型: class
    • 修饰符: public final
    • 源码定位: L190
    • 说明:

      TODO

构造器

protected EntityArgument(boolean single, boolean playersOnly) @ L44

  • 构造器名:EntityArgument
  • 源码定位:L44
  • 修饰符:protected

参数:

  • single: boolean
  • playersOnly: boolean

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static EntityArgument entity() @ L49

  • 方法名:entity
  • 源码定位:L49
  • 返回类型:EntityArgument
  • 修饰符:public static

参数:

说明:

TODO

public static Entity getEntity(CommandContext<CommandSourceStack> context, String name) @ L53

  • 方法名:getEntity
  • 源码定位:L53
  • 返回类型:Entity
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static EntityArgument entities() @ L57

  • 方法名:entities
  • 源码定位:L57
  • 返回类型:EntityArgument
  • 修饰符:public static

参数:

说明:

TODO

public static Collection<?extends Entity> getEntities(CommandContext<CommandSourceStack> context, String name) @ L61

  • 方法名:getEntities
  • 源码定位:L61
  • 返回类型:Collection<?extends Entity>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Collection<?extends Entity> getOptionalEntities(CommandContext<CommandSourceStack> context, String name) @ L70

  • 方法名:getOptionalEntities
  • 源码定位:L70
  • 返回类型:Collection<?extends Entity>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Collection<ServerPlayer> getOptionalPlayers(CommandContext<CommandSourceStack> context, String name) @ L74

  • 方法名:getOptionalPlayers
  • 源码定位:L74
  • 返回类型:Collection
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static EntityArgument player() @ L78

  • 方法名:player
  • 源码定位:L78
  • 返回类型:EntityArgument
  • 修饰符:public static

参数:

说明:

TODO

public static ServerPlayer getPlayer(CommandContext<CommandSourceStack> context, String name) @ L82

  • 方法名:getPlayer
  • 源码定位:L82
  • 返回类型:ServerPlayer
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static EntityArgument players() @ L86

  • 方法名:players
  • 源码定位:L86
  • 返回类型:EntityArgument
  • 修饰符:public static

参数:

说明:

TODO

public static Collection<ServerPlayer> getPlayers(CommandContext<CommandSourceStack> context, String name) @ L90

  • 方法名:getPlayers
  • 源码定位:L90
  • 返回类型:Collection
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public EntitySelector parse(StringReader reader) @ L99

  • 方法名:parse
  • 源码定位:L99
  • 返回类型:EntitySelector
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

public <S> EntitySelector parse(StringReader reader, S source) @ L103

  • 方法名:parse
  • 源码定位:L103
  • 返回类型: EntitySelector
  • 修饰符:public

参数:

  • reader: StringReader
  • source: S

说明:

TODO

private EntitySelector parse(StringReader reader, boolean allowSelectors) @ L107

  • 方法名:parse
  • 源码定位:L107
  • 返回类型:EntitySelector
  • 修饰符:private

参数:

  • reader: StringReader
  • allowSelectors: boolean

说明:

TODO

public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> contextBuilder, SuggestionsBuilder builder) @ L127

  • 方法名:listSuggestions
  • 源码定位:L127
  • 返回类型: CompletableFuture
  • 修饰符:public

参数:

  • contextBuilder: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L154

  • 方法名:getExamples
  • 源码定位:L154
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

代码

public class EntityArgument implements ArgumentType<EntitySelector> {
    private static final Collection<String> EXAMPLES = Arrays.asList("Player", "0123", "@e", "@e[type=foo]", "dd12be42-52a9-4a91-a8a1-11c01849e498");
    public static final SimpleCommandExceptionType ERROR_NOT_SINGLE_ENTITY = new SimpleCommandExceptionType(Component.translatable("argument.entity.toomany"));
    public static final SimpleCommandExceptionType ERROR_NOT_SINGLE_PLAYER = new SimpleCommandExceptionType(Component.translatable("argument.player.toomany"));
    public static final SimpleCommandExceptionType ERROR_ONLY_PLAYERS_ALLOWED = new SimpleCommandExceptionType(
        Component.translatable("argument.player.entities")
    );
    public static final SimpleCommandExceptionType NO_ENTITIES_FOUND = new SimpleCommandExceptionType(Component.translatable("argument.entity.notfound.entity"));
    public static final SimpleCommandExceptionType NO_PLAYERS_FOUND = new SimpleCommandExceptionType(Component.translatable("argument.entity.notfound.player"));
    public static final SimpleCommandExceptionType ERROR_SELECTORS_NOT_ALLOWED = new SimpleCommandExceptionType(
        Component.translatable("argument.entity.selector.not_allowed")
    );
    private final boolean single;
    private final boolean playersOnly;
 
    protected EntityArgument(boolean single, boolean playersOnly) {
        this.single = single;
        this.playersOnly = playersOnly;
    }
 
    public static EntityArgument entity() {
        return new EntityArgument(true, false);
    }
 
    public static Entity getEntity(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return context.getArgument(name, EntitySelector.class).findSingleEntity(context.getSource());
    }
 
    public static EntityArgument entities() {
        return new EntityArgument(false, false);
    }
 
    public static Collection<? extends Entity> getEntities(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        Collection<? extends Entity> result = getOptionalEntities(context, name);
        if (result.isEmpty()) {
            throw NO_ENTITIES_FOUND.create();
        } else {
            return result;
        }
    }
 
    public static Collection<? extends Entity> getOptionalEntities(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return context.getArgument(name, EntitySelector.class).findEntities(context.getSource());
    }
 
    public static Collection<ServerPlayer> getOptionalPlayers(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return context.getArgument(name, EntitySelector.class).findPlayers(context.getSource());
    }
 
    public static EntityArgument player() {
        return new EntityArgument(true, true);
    }
 
    public static ServerPlayer getPlayer(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return context.getArgument(name, EntitySelector.class).findSinglePlayer(context.getSource());
    }
 
    public static EntityArgument players() {
        return new EntityArgument(false, true);
    }
 
    public static Collection<ServerPlayer> getPlayers(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        List<ServerPlayer> players = context.getArgument(name, EntitySelector.class).findPlayers(context.getSource());
        if (players.isEmpty()) {
            throw NO_PLAYERS_FOUND.create();
        } else {
            return players;
        }
    }
 
    public EntitySelector parse(StringReader reader) throws CommandSyntaxException {
        return this.parse(reader, true);
    }
 
    public <S> EntitySelector parse(StringReader reader, S source) throws CommandSyntaxException {
        return this.parse(reader, EntitySelectorParser.allowSelectors(source));
    }
 
    private EntitySelector parse(StringReader reader, boolean allowSelectors) throws CommandSyntaxException {
        int start = 0;
        EntitySelectorParser parser = new EntitySelectorParser(reader, allowSelectors);
        EntitySelector selector = parser.parse();
        if (selector.getMaxResults() > 1 && this.single) {
            if (this.playersOnly) {
                reader.setCursor(0);
                throw ERROR_NOT_SINGLE_PLAYER.createWithContext(reader);
            } else {
                reader.setCursor(0);
                throw ERROR_NOT_SINGLE_ENTITY.createWithContext(reader);
            }
        } else if (selector.includesEntities() && this.playersOnly && !selector.isSelfSelector()) {
            reader.setCursor(0);
            throw ERROR_ONLY_PLAYERS_ALLOWED.createWithContext(reader);
        } else {
            return selector;
        }
    }
 
    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> contextBuilder, SuggestionsBuilder builder) {
        if (contextBuilder.getSource() instanceof SharedSuggestionProvider source) {
            StringReader reader = new StringReader(builder.getInput());
            reader.setCursor(builder.getStart());
            EntitySelectorParser parser = new EntitySelectorParser(reader, source.permissions().hasPermission(Permissions.COMMANDS_ENTITY_SELECTORS));
 
            try {
                parser.parse();
            } catch (CommandSyntaxException var7) {
            }
 
            return parser.fillSuggestions(
                builder,
                suggestions -> {
                    Collection<String> onlinePlayerNames = source.getOnlinePlayerNames();
                    Iterable<String> suggestedNames = (Iterable<String>)(this.playersOnly
                        ? onlinePlayerNames
                        : Iterables.concat(onlinePlayerNames, source.getSelectedEntities()));
                    SharedSuggestionProvider.suggest(suggestedNames, suggestions);
                }
            );
        } else {
            return Suggestions.empty();
        }
    }
 
    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
 
    public static class Info implements ArgumentTypeInfo<EntityArgument, EntityArgument.Info.Template> {
        private static final byte FLAG_SINGLE = 1;
        private static final byte FLAG_PLAYERS_ONLY = 2;
 
        public void serializeToNetwork(EntityArgument.Info.Template template, FriendlyByteBuf out) {
            int flags = 0;
            if (template.single) {
                flags |= 1;
            }
 
            if (template.playersOnly) {
                flags |= 2;
            }
 
            out.writeByte(flags);
        }
 
        public EntityArgument.Info.Template deserializeFromNetwork(FriendlyByteBuf in) {
            byte flags = in.readByte();
            return new EntityArgument.Info.Template((flags & 1) != 0, (flags & 2) != 0);
        }
 
        public void serializeToJson(EntityArgument.Info.Template template, JsonObject out) {
            out.addProperty("amount", template.single ? "single" : "multiple");
            out.addProperty("type", template.playersOnly ? "players" : "entities");
        }
 
        public EntityArgument.Info.Template unpack(EntityArgument argument) {
            return new EntityArgument.Info.Template(argument.single, argument.playersOnly);
        }
 
        public final class Template implements ArgumentTypeInfo.Template<EntityArgument> {
            private final boolean single;
            private final boolean playersOnly;
 
            private Template(boolean single, boolean playersOnly) {
                Objects.requireNonNull(Info.this);
                super();
                this.single = single;
                this.playersOnly = playersOnly;
            }
 
            public EntityArgument instantiate(CommandBuildContext context) {
                return new EntityArgument(this.single, this.playersOnly);
            }
 
            @Override
            public ArgumentTypeInfo<EntityArgument, ?> type() {
                return Info.this;
            }
        }
    }
}

引用的其他类