code cleanup for getItemsStackFromValue and npp keywords

This commit is contained in:
gnembon committed 2022-08-28 20:01:40 +02:00
1 parent 44bce20d75
commit bb12e3ddd1
4 files changed
+46 -26

No files matched your search

@@ -53,7 +53,7 @@
</Keywords> </Keywords>
<Keywords name="Keywords2"> <Keywords name="Keywords2">
block pos pos_offset solid air liquid flammable block pos pos_offset solid air liquid flammable
transparent opacity blocks_daylight emitted_light light block_light sky_light see_sky brightness transparent opacity blocks_daylight emitted_light light block_light sky_light effective_light see_sky brightness
hardness blast_resistance top loaded suffocates power ticks_randomly update block_tick random_tick in_slime_chunk hardness blast_resistance top loaded suffocates power ticks_randomly update block_tick random_tick in_slime_chunk
set without_updates set without_updates
blocks_movement block_sound material map_colour block_state block_list block_tags block_data poi set_poi blocks_movement block_sound material map_colour block_state block_list block_tags block_data poi set_poi
@@ -77,7 +77,7 @@
player spawn entity_id entity_list entity_area entity_selector query modify entity_event entity_load_handler entity_types player spawn entity_id entity_list entity_area entity_selector query modify entity_event entity_load_handler entity_types
item_list item_tags stack_limit item_category recipe_data crafting_remaining_item item_list item_tags stack_limit item_category recipe_data crafting_remaining_item item_display_name
inventory_size inventory_has_items inventory_get inventory_set inventory_find inventory_remove drop_item inventory_size inventory_has_items inventory_get inventory_set inventory_find inventory_remove drop_item
@@ -258,10 +258,7 @@ public class Auxiliary {
)); ));
}); });
expression.addContextFunction("item_display_name", -1, (c, t, lv)->{ expression.addUnaryFunction("item_display_name", v -> new FormattedTextValue(ValueConversions.getItemStackFromValue(v, false).getHoverName()));
return new FormattedTextValue(ValueConversions.getItemStackFromValue(lv.get(0)).getHoverName());
});
expression.addContextFunction("particle_box", -1, (c, t, lv) -> expression.addContextFunction("particle_box", -1, (c, t, lv) ->
{ {
@@ -1701,7 +1701,7 @@ public class EntityValue extends Value
}); });
put("item", (e, v) -> { put("item", (e, v) -> {
ItemStack item=ValueConversions.getItemStackFromValue(v); ItemStack item=ValueConversions.getItemStackFromValue(v, true);
if(e instanceof ItemEntity itementity) if(e instanceof ItemEntity itementity)
itementity.setItem(item); itementity.setItem(item);
if(e instanceof ItemFrame itemframe) if(e instanceof ItemFrame itemframe)
@@ -8,6 +8,7 @@ import carpet.utils.BlockInfo;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos; import net.minecraft.core.GlobalPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
@@ -414,30 +415,52 @@ public class ValueConversions
); );
} }
static public ItemStack getItemStackFromValue(Value value){ public static ItemStack getItemStackFromValue(Value value, boolean withCount)
List<Value> items; {
if(value instanceof ListValue blv && blv.length()==3){ if (value.isNull())
items = blv.getItems(); {
}else if(value.isNull()){
return ItemStack.EMPTY; return ItemStack.EMPTY;
}else{
items= List.of(StringValue.of(value.getString()),Value.ONE,Value.NULL);
} }
String id=items.get(0).getString(); final String name;
int count=((NumericValue)items.get(1)).getInt(); int count = 1;
CompoundTag nbt=null; CompoundTag nbtTag = null;
if (!items.get(2).isNull()){ if (value instanceof ListValue list)
if(items.get(2) instanceof NBTSerializableValue nbtv){ {
nbt=nbtv.getCompoundTag(); if (list.length() != 3)
{
throw new ThrowStatement("item definition from list of size "+list.length(), Throwables.UNKNOWN_ITEM);
} }
else{ final List<Value> items = list.getItems();
nbt=new NBTSerializableValue(items.get(2).getString()).getCompoundTag(); name = items.get(0).getString();
if (withCount)
{
count = NumericValue.asNumber(items.get(1)).getInt();
}
Value nbtValue = items.get(2);
if (!nbtValue.isNull())
{
nbtTag = ((NBTSerializableValue) NBTSerializableValue.fromValue(nbtValue)).getCompoundTag();
} }
} }
try{ else
return NBTSerializableValue.parseItem(id,nbt).createItemStack(count,false); {
} catch (CommandSyntaxException e1) { name = value.getString();
throw new IllegalStateException("Unexpected exception while creating item stack", e1); }
ItemInput itemInput = NBTSerializableValue.parseItem(name, nbtTag);
try
{
return itemInput.createItemStack(count,false);
}
catch (CommandSyntaxException cse)
{
if (!withCount)
{
throw new IllegalStateException("Unexpected exception while creating item stack of " + name + ". All items should be able to stack to one", cse);
}
else
{
throw new ThrowStatement(count + " stack size of " + name, Throwables.UNKNOWN_ITEM);
}
} }
} }