Spaces:
Build error
Build error
File size: 3,831 Bytes
d46f4a3 |
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 86 87 88 89 90 91 92 |
package net.minecraft.commands.arguments;
import com.google.common.collect.Maps;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
import net.minecraft.Util;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
public class EntityAnchorArgument implements ArgumentType<EntityAnchorArgument.Anchor> {
private static final Collection<String> EXAMPLES = Arrays.asList("eyes", "feet");
private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
p_308348_ -> Component.translatableEscape("argument.anchor.invalid", p_308348_)
);
public static EntityAnchorArgument.Anchor getAnchor(CommandContext<CommandSourceStack> p_90354_, String p_90355_) {
return p_90354_.getArgument(p_90355_, EntityAnchorArgument.Anchor.class);
}
public static EntityAnchorArgument anchor() {
return new EntityAnchorArgument();
}
public EntityAnchorArgument.Anchor parse(StringReader p_90352_) throws CommandSyntaxException {
int i = p_90352_.getCursor();
String s = p_90352_.readUnquotedString();
EntityAnchorArgument.Anchor entityanchorargument$anchor = EntityAnchorArgument.Anchor.getByName(s);
if (entityanchorargument$anchor == null) {
p_90352_.setCursor(i);
throw ERROR_INVALID.createWithContext(p_90352_, s);
} else {
return entityanchorargument$anchor;
}
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> p_90360_, SuggestionsBuilder p_90361_) {
return SharedSuggestionProvider.suggest(EntityAnchorArgument.Anchor.BY_NAME.keySet(), p_90361_);
}
@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
public static enum Anchor {
FEET("feet", (p_90389_, p_90390_) -> p_90389_),
EYES("eyes", (p_90382_, p_90383_) -> new Vec3(p_90382_.x, p_90382_.y + (double)p_90383_.getEyeHeight(), p_90382_.z));
static final Map<String, EntityAnchorArgument.Anchor> BY_NAME = Util.make(Maps.newHashMap(), p_90387_ -> {
for (EntityAnchorArgument.Anchor entityanchorargument$anchor : values()) {
p_90387_.put(entityanchorargument$anchor.name, entityanchorargument$anchor);
}
});
private final String name;
private final BiFunction<Vec3, Entity, Vec3> transform;
private Anchor(final String p_90374_, final BiFunction<Vec3, Entity, Vec3> p_90375_) {
this.name = p_90374_;
this.transform = p_90375_;
}
@Nullable
public static EntityAnchorArgument.Anchor getByName(String p_90385_) {
return BY_NAME.get(p_90385_);
}
public Vec3 apply(Entity p_90378_) {
return this.transform.apply(p_90378_.position(), p_90378_);
}
public Vec3 apply(CommandSourceStack p_90380_) {
Entity entity = p_90380_.getEntity();
return entity == null ? p_90380_.getPosition() : this.transform.apply(p_90380_.getPosition(), entity);
}
}
} |