Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Value#of Method for Improved Item Data Retrieval (Components compatibility) #1996

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
50 changes: 42 additions & 8 deletions src/main/java/carpet/script/api/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,38 @@
import carpet.script.exception.Throwables;
import carpet.script.external.Vanilla;
import carpet.script.utils.InputValidator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import carpet.script.value.BooleanValue;
import carpet.script.value.EntityValue;
import carpet.script.value.FormattedTextValue;
import carpet.script.value.FunctionValue;
import carpet.script.value.ListValue;
import carpet.script.value.MapValue;
import carpet.script.value.NBTSerializableValue;
import carpet.script.value.NumericValue;
import carpet.script.value.ScreenValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import carpet.script.value.ValueConversions;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;

import net.minecraft.core.HolderSet;
Gnottero marked this conversation as resolved.
Show resolved Hide resolved
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -95,6 +103,32 @@ public static void apply(Expression expression)
return tagSet.isEmpty() ? Value.NULL : BooleanValue.of(tagSet.get().stream().anyMatch(h -> h.value() == item));
});

expression.addContextFunction("item_components", 1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext) c;
ItemStack stack = ValueConversions.getItemStackFromValue(lv.getFirst(), true, cc.registryAccess());

Stream<DataComponentType<Object>> stream = (Stream<DataComponentType<Object>>)(Object)stack.getComponents().keySet().stream();
Map<Value, Value> lst = stream.filter(Predicate.not(DataComponentType::isTransient))
.collect(Collectors.toMap(
ck -> ValueConversions.of(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(ck)),
ck -> {
var cvalue = stack.get(ck);
return switch (cvalue) {
case Number n -> NumericValue.of(n);
case Boolean b -> BooleanValue.of(b);
case Component ft -> FormattedTextValue.of(ft);
default -> NBTSerializableValue.of(ck.codec()
.encodeStart(
cc.registryAccess().createSerializationContext(NbtOps.INSTANCE),
cvalue)
.result().orElse(null));
};

}));

return MapValue.wrap(lst);
});

expression.addContextFunction("recipe_data", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c;
Expand Down
Loading