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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/carpet/script/api/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
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.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;

import carpet.script.value.BooleanValue;
import carpet.script.value.EntityValue;
import carpet.script.value.FormattedTextValue;
Expand All @@ -19,14 +27,6 @@
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;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/carpet/script/api/WorldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import carpet.script.value.Value;
import carpet.script.value.ValueConversions;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.Util;
Expand Down Expand Up @@ -67,7 +66,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand All @@ -81,9 +79,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.game.ClientboundExplodePacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public NBTSerializableValue(Supplier<Tag> tagSupplier)
public static Value fromStack(ItemStack stack, RegistryAccess regs)
{
NBTSerializableValue value = new NBTSerializableValue();
value.nbtSupplier = () -> stack.saveOptional(regs);
value.nbtSupplier = () -> (Tag) stack.saveOptional(regs);
return value;
}

Expand Down
33 changes: 32 additions & 1 deletion src/main/java/carpet/script/value/ValueConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.Vec3i;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
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.nbt.Tag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -57,6 +60,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -101,7 +105,8 @@ public static Value of(ItemStack stack, RegistryAccess regs)
return stack == null || stack.isEmpty() ? Value.NULL : ListValue.of(
of(regs.lookupOrThrow(Registries.ITEM).getKey(stack.getItem())),
new NumericValue(stack.getCount()),
NBTSerializableValue.fromStack(stack, regs)
getComponentsFromItemStack(stack, regs)
// NBTSerializableValue.fromStack(stack, regs)
);
}

Expand Down Expand Up @@ -491,6 +496,7 @@ public static ItemStack getItemStackFromValue(Value value, boolean withCount, Re
if (!nbtValue.isNull())
{
nbtTag = ((NBTSerializableValue) NBTSerializableValue.fromValue(nbtValue)).getCompoundTag();
System.out.println("Test: " + nbtTag);
}
}
else
Expand All @@ -499,9 +505,34 @@ public static ItemStack getItemStackFromValue(Value value, boolean withCount, Re
}
ItemStack itemInput = NBTSerializableValue.parseItem(name, nbtTag, regs);
itemInput.setCount(count);
System.out.println("itemInput: " + itemInput);
return itemInput;
}

public static Value getComponentsFromItemStack(ItemStack stack, RegistryAccess regs) {
var lst = stack.getComponents().keySet().stream().filter(Predicate.not(DataComponentType::isTransient))
.collect(Collectors.toMap(
ck -> ValueConversions.of(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(ck)),
(DataComponentType ck) -> {
var cvalue = stack.get(ck);
return switch (cvalue) {
case Number n -> NumericValue.of(n);
case Boolean b -> BooleanValue.of(b);
default -> {
Tag res = (Tag) ck.codec()
.encodeStart(
regs.createSerializationContext(NbtOps.INSTANCE),
cvalue)
.result().orElse(null);
yield NBTSerializableValue.of(res);
}
};
}));

MapValue mapValue = MapValue.wrap(Map.of(StringValue.of("components"), MapValue.wrap(lst), StringValue.of("id"), StringValue.of(stack.getItem().toString()), StringValue.of("count"), NumericValue.of(stack.getCount())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you pack all of those k-v pairs to a NBT tag again, why you left the switch there?
you can let Number&Boolean use the default case too.

and you no longer need to use a NBTSerializableValue to warp the res tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed that to your implementation, removing the part that took the item from the main hand in case of no values passed to the function. I also removed unedited files. It should be fine now, I guess

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then you please edit your Overview, Reason and Request for Review......

return new NBTSerializableValue(mapValue.toTag(true, regs));
}

public static Value guess(ServerLevel serverWorld, Object o)
{
if (o == null)
Expand Down
Loading