178 lines
7.4 KiB
Java
178 lines
7.4 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.brigadier.StringReader
|
|
* com.mojang.brigadier.exceptions.CommandSyntaxException
|
|
* com.mojang.datafixers.DataFixUtils
|
|
* com.mojang.datafixers.kinds.App
|
|
* com.mojang.datafixers.kinds.Applicative
|
|
* com.mojang.logging.LogUtils
|
|
* com.mojang.serialization.Codec
|
|
* com.mojang.serialization.DynamicOps
|
|
* com.mojang.serialization.MapCodec
|
|
* com.mojang.serialization.codecs.RecordCodecBuilder
|
|
* org.jspecify.annotations.Nullable
|
|
* org.slf4j.Logger
|
|
*/
|
|
package net.minecraft.network.chat.contents;
|
|
|
|
import com.mojang.brigadier.StringReader;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import com.mojang.datafixers.DataFixUtils;
|
|
import com.mojang.datafixers.kinds.App;
|
|
import com.mojang.datafixers.kinds.Applicative;
|
|
import com.mojang.logging.LogUtils;
|
|
import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.DynamicOps;
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.arguments.NbtPathArgument;
|
|
import net.minecraft.nbt.NbtOps;
|
|
import net.minecraft.nbt.StringTag;
|
|
import net.minecraft.nbt.Tag;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.chat.ComponentContents;
|
|
import net.minecraft.network.chat.ComponentSerialization;
|
|
import net.minecraft.network.chat.ComponentUtils;
|
|
import net.minecraft.network.chat.MutableComponent;
|
|
import net.minecraft.network.chat.contents.data.DataSource;
|
|
import net.minecraft.network.chat.contents.data.DataSources;
|
|
import net.minecraft.resources.RegistryOps;
|
|
import net.minecraft.world.entity.Entity;
|
|
import org.jspecify.annotations.Nullable;
|
|
import org.slf4j.Logger;
|
|
|
|
public class NbtContents
|
|
implements ComponentContents {
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
public static final MapCodec<NbtContents> MAP_CODEC = RecordCodecBuilder.mapCodec(i -> i.group((App)Codec.STRING.fieldOf("nbt").forGetter(NbtContents::getNbtPath), (App)Codec.BOOL.lenientOptionalFieldOf("interpret", (Object)false).forGetter(NbtContents::isInterpreting), (App)ComponentSerialization.CODEC.lenientOptionalFieldOf("separator").forGetter(NbtContents::getSeparator), (App)DataSources.CODEC.forGetter(NbtContents::getDataSource)).apply((Applicative)i, NbtContents::new));
|
|
private final boolean interpreting;
|
|
private final Optional<Component> separator;
|
|
private final String nbtPathPattern;
|
|
private final DataSource dataSource;
|
|
protected final @Nullable NbtPathArgument.NbtPath compiledNbtPath;
|
|
|
|
public NbtContents(String nbtPath, boolean interpreting, Optional<Component> separator, DataSource dataSource) {
|
|
this(nbtPath, NbtContents.compileNbtPath(nbtPath), interpreting, separator, dataSource);
|
|
}
|
|
|
|
private NbtContents(String nbtPathPattern, @Nullable NbtPathArgument.NbtPath compiledNbtPath, boolean interpreting, Optional<Component> separator, DataSource dataSource) {
|
|
this.nbtPathPattern = nbtPathPattern;
|
|
this.compiledNbtPath = compiledNbtPath;
|
|
this.interpreting = interpreting;
|
|
this.separator = separator;
|
|
this.dataSource = dataSource;
|
|
}
|
|
|
|
private static @Nullable NbtPathArgument.NbtPath compileNbtPath(String path) {
|
|
try {
|
|
return new NbtPathArgument().parse(new StringReader(path));
|
|
}
|
|
catch (CommandSyntaxException ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public String getNbtPath() {
|
|
return this.nbtPathPattern;
|
|
}
|
|
|
|
public boolean isInterpreting() {
|
|
return this.interpreting;
|
|
}
|
|
|
|
public Optional<Component> getSeparator() {
|
|
return this.separator;
|
|
}
|
|
|
|
public DataSource getDataSource() {
|
|
return this.dataSource;
|
|
}
|
|
|
|
/*
|
|
* Enabled force condition propagation
|
|
* Lifted jumps to return sites
|
|
*/
|
|
public boolean equals(Object o) {
|
|
if (this == o) {
|
|
return true;
|
|
}
|
|
if (!(o instanceof NbtContents)) return false;
|
|
NbtContents that = (NbtContents)o;
|
|
if (!this.dataSource.equals(that.dataSource)) return false;
|
|
if (!this.separator.equals(that.separator)) return false;
|
|
if (this.interpreting != that.interpreting) return false;
|
|
if (!this.nbtPathPattern.equals(that.nbtPathPattern)) return false;
|
|
return true;
|
|
}
|
|
|
|
public int hashCode() {
|
|
int result = this.interpreting ? 1 : 0;
|
|
result = 31 * result + this.separator.hashCode();
|
|
result = 31 * result + this.nbtPathPattern.hashCode();
|
|
result = 31 * result + this.dataSource.hashCode();
|
|
return result;
|
|
}
|
|
|
|
public String toString() {
|
|
return "nbt{" + String.valueOf(this.dataSource) + ", interpreting=" + this.interpreting + ", separator=" + String.valueOf(this.separator) + "}";
|
|
}
|
|
|
|
@Override
|
|
public MutableComponent resolve(@Nullable CommandSourceStack source, @Nullable Entity entity, int recursionDepth) throws CommandSyntaxException {
|
|
if (source == null || this.compiledNbtPath == null) {
|
|
return Component.empty();
|
|
}
|
|
Stream<String> elements = this.dataSource.getData(source).flatMap(t -> {
|
|
try {
|
|
return this.compiledNbtPath.get((Tag)t).stream();
|
|
}
|
|
catch (CommandSyntaxException ignored) {
|
|
return Stream.empty();
|
|
}
|
|
});
|
|
if (this.interpreting) {
|
|
RegistryOps<Tag> registryOps = source.registryAccess().createSerializationContext(NbtOps.INSTANCE);
|
|
Component resolvedSeparator2 = (Component)DataFixUtils.orElse(ComponentUtils.updateForEntity(source, this.separator, entity, recursionDepth), (Object)ComponentUtils.DEFAULT_NO_STYLE_SEPARATOR);
|
|
return elements.flatMap(tag -> {
|
|
try {
|
|
Component component = (Component)ComponentSerialization.CODEC.parse((DynamicOps)registryOps, tag).getOrThrow();
|
|
return Stream.of(ComponentUtils.updateForEntity(source, component, entity, recursionDepth));
|
|
}
|
|
catch (Exception e) {
|
|
LOGGER.warn("Failed to parse component: {}", tag, (Object)e);
|
|
return Stream.of(new MutableComponent[0]);
|
|
}
|
|
}).reduce((left, right) -> left.append(resolvedSeparator2).append((Component)right)).orElseGet(Component::empty);
|
|
}
|
|
Stream<String> stringElements = elements.map(NbtContents::asString);
|
|
return ComponentUtils.updateForEntity(source, this.separator, entity, recursionDepth).map(resolvedSeparator -> stringElements.map(Component::literal).reduce((left, right) -> left.append((Component)resolvedSeparator).append((Component)right)).orElseGet(Component::empty)).orElseGet(() -> Component.literal(stringElements.collect(Collectors.joining(", "))));
|
|
}
|
|
|
|
/*
|
|
* Enabled force condition propagation
|
|
* Lifted jumps to return sites
|
|
*/
|
|
private static String asString(Tag tag) {
|
|
if (!(tag instanceof StringTag)) return tag.toString();
|
|
StringTag stringTag = (StringTag)tag;
|
|
try {
|
|
String string = stringTag.value();
|
|
return string;
|
|
}
|
|
catch (Throwable throwable) {
|
|
throw new MatchException(throwable.toString(), throwable);
|
|
}
|
|
}
|
|
|
|
public MapCodec<NbtContents> codec() {
|
|
return MAP_CODEC;
|
|
}
|
|
}
|
|
|