| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.function.BiFunction;
|
| 16 | import net.minecraft.commands.CommandSourceStack;
|
| 17 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import net.minecraft.world.entity.Entity;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | public class EntityAnchorArgument implements ArgumentType<EntityAnchorArgument.Anchor> {
|
| 25 | private static final Collection<String> EXAMPLES = Arrays.asList("eyes", "feet");
|
| 26 | private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
|
| 27 | name -> Component.translatableEscape("argument.anchor.invalid", name)
|
| 28 | );
|
| 29 |
|
| 30 | public static EntityAnchorArgument.Anchor getAnchor(final CommandContext<CommandSourceStack> context, final String name) {
|
| 31 | return context.getArgument(name, EntityAnchorArgument.Anchor.class);
|
| 32 | }
|
| 33 |
|
| 34 | public static EntityAnchorArgument anchor() {
|
| 35 | return new EntityAnchorArgument();
|
| 36 | }
|
| 37 |
|
| 38 | public EntityAnchorArgument.Anchor parse(final StringReader reader) throws CommandSyntaxException {
|
| 39 | int start = reader.getCursor();
|
| 40 | String name = reader.readUnquotedString();
|
| 41 | EntityAnchorArgument.Anchor anchor = EntityAnchorArgument.Anchor.getByName(name);
|
| 42 | if (anchor == null) {
|
| 43 | reader.setCursor(start);
|
| 44 | throw ERROR_INVALID.createWithContext(reader, name);
|
| 45 | } else {
|
| 46 | return anchor;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 52 | return SharedSuggestionProvider.suggest(EntityAnchorArgument.Anchor.BY_NAME.keySet(), builder);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Collection<String> getExamples() {
|
| 57 | return EXAMPLES;
|
| 58 | }
|
| 59 |
|
| 60 | public enum Anchor {
|
| 61 | FEET("feet", (p, e) -> p),
|
| 62 | EYES("eyes", (p, e) -> new Vec3(p.x, p.y + e.getEyeHeight(), p.z));
|
| 63 |
|
| 64 | private static final Map<String, EntityAnchorArgument.Anchor> BY_NAME = Util.make(Maps.newHashMap(), map -> {
|
| 65 | for (EntityAnchorArgument.Anchor anchor : values()) {
|
| 66 | map.put(anchor.name, anchor);
|
| 67 | }
|
| 68 | });
|
| 69 | private final String name;
|
| 70 | private final BiFunction<Vec3, Entity, Vec3> transform;
|
| 71 |
|
| 72 | Anchor(final String name, final BiFunction<Vec3, Entity, Vec3> transform) {
|
| 73 | this.name = name;
|
| 74 | this.transform = transform;
|
| 75 | }
|
| 76 |
|
| 77 | public static EntityAnchorArgument.@Nullable Anchor getByName(final String name) {
|
| 78 | return BY_NAME.get(name);
|
| 79 | }
|
| 80 |
|
| 81 | public Vec3 apply(final Entity entity) {
|
| 82 | return this.transform.apply(entity.position(), entity);
|
| 83 | }
|
| 84 |
|
| 85 | public Vec3 apply(final CommandSourceStack source) {
|
| 86 | Entity entity = source.getEntity();
|
| 87 | return entity == null ? source.getPosition() : this.transform.apply(source.getPosition(), entity);
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|