move getItemStackFromValue to ValueConversions
This commit is contained in:
1 parent
40bc200615
commit
d418b7d090
3 files changed
+32
-28
No files matched your search
@@ -14,6 +14,7 @@ import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.Vec3i;
|
||||
import net.minecraft.core.particles.ParticleOptions;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@@ -52,6 +53,8 @@ import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
||||
public class ValueConversions
|
||||
{
|
||||
public static Value of(BlockPos pos)
|
||||
@@ -411,6 +414,33 @@ public class ValueConversions
|
||||
);
|
||||
}
|
||||
|
||||
static public ItemStack getItemStackFromValue(Value value){
|
||||
List<Value> items;
|
||||
if(value instanceof ListValue blv && blv.length()==3){
|
||||
items = blv.getItems();
|
||||
}else if(value.isNull()){
|
||||
return ItemStack.EMPTY;
|
||||
}else{
|
||||
items= List.of(StringValue.of(value.getString()),Value.ONE,Value.NULL);
|
||||
}
|
||||
String id=items.get(0).getString();
|
||||
int count=((NumericValue)items.get(1)).getInt();
|
||||
CompoundTag nbt=null;
|
||||
if (!items.get(2).isNull()){
|
||||
if(items.get(2) instanceof NBTSerializableValue nbtv){
|
||||
nbt=nbtv.getCompoundTag();
|
||||
}
|
||||
else{
|
||||
nbt=new NBTSerializableValue(items.get(2).getString()).getCompoundTag();
|
||||
}
|
||||
}
|
||||
try{
|
||||
return NBTSerializableValue.parseItem(id,nbt).createItemStack(count,false);
|
||||
} catch (CommandSyntaxException e1) {
|
||||
throw new IllegalStateException("Unexpected exception while creating item stack", e1);
|
||||
}
|
||||
}
|
||||
|
||||
public static Value guess(ServerLevel serverWorld, Object o) {
|
||||
if (o == null)
|
||||
return Value.NULL;
|
||||
|
||||
Reference in new issue
Block a user