| 1 | package net.minecraft.server.commands.data;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.Locale;
|
| 7 | import java.util.UUID;
|
| 8 | import net.minecraft.advancements.predicates.NbtPredicate;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.arguments.EntityArgument;
|
| 11 | import net.minecraft.commands.arguments.NbtPathArgument;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.NbtUtils;
|
| 14 | import net.minecraft.nbt.Tag;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.server.commands.ArgProvider;
|
| 17 | import net.minecraft.util.ProblemReporter;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.level.storage.TagValueInput;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class EntityDataAccessor implements DataAccessor {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private static final SimpleCommandExceptionType ERROR_NO_PLAYERS = new SimpleCommandExceptionType(Component.translatable("commands.data.entity.invalid"));
|
| 26 | public static final ArgProvider.Factory<DataAccessor> PROVIDER = arg -> ArgProvider.create(
|
| 27 | "entity", () -> Commands.argument(arg, EntityArgument.entity()), c -> new EntityDataAccessor(EntityArgument.getEntity(c, arg))
|
| 28 | );
|
| 29 | private final Entity entity;
|
| 30 |
|
| 31 | public EntityDataAccessor(final Entity entity) {
|
| 32 | this.entity = entity;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public void setData(final CompoundTag tag) throws CommandSyntaxException {
|
| 37 | if (this.entity instanceof Player) {
|
| 38 | throw ERROR_NO_PLAYERS.create();
|
| 39 | }
|
| 40 |
|
| 41 | UUID uuid = this.entity.getUUID();
|
| 42 |
|
| 43 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this.entity.problemPath(), LOGGER)) {
|
| 44 | this.entity.load(TagValueInput.create(reporter, this.entity.registryAccess(), tag));
|
| 45 | this.entity.setUUID(uuid);
|
| 46 | this.entity.postDataManipulated();
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public CompoundTag getData() {
|
| 52 | return NbtPredicate.getEntityTagToCompare(this.entity);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Component getModifiedSuccess() {
|
| 57 | return Component.translatable("commands.data.entity.modified", this.entity.getDisplayName());
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public Component getPrintSuccess(final Tag data) {
|
| 62 | return Component.translatable("commands.data.entity.query", this.entity.getDisplayName(), NbtUtils.toPrettyComponent(data));
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public Component getPrintSuccess(final NbtPathArgument.NbtPath path, final double scale, final int value) {
|
| 67 | return Component.translatable("commands.data.entity.get", path.asString(), this.entity.getDisplayName(), String.format(Locale.ROOT, "%.2f", scale), value);
|
| 68 | }
|
| 69 | }
|
| 70 |
|