Search in sources :

Example 11 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class BookItemStackData method get.

// @formatter:on
private static <V> List<V> get(final ItemStack holder, final Function<String, V> predicate) {
    final CompoundTag tag = holder.getTag();
    if (tag == null || !tag.contains(Constants.Item.Book.ITEM_BOOK_PAGES)) {
        return null;
    }
    final ListTag list = tag.getList(Constants.Item.Book.ITEM_BOOK_PAGES, Constants.NBT.TAG_STRING);
    return list.stream().map(Tag::getAsString).map(predicate).collect(Collectors.toList());
}
Also used : Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) IntTag(net.minecraft.nbt.IntTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 12 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class FireworkUtil method getFireworkEffects.

public static Optional<List<FireworkEffect>> getFireworkEffects(final Object object) {
    final ItemStack item = FireworkUtil.getItem(object);
    if (item.isEmpty()) {
        return Optional.empty();
    }
    final List<FireworkEffect> effects;
    if (item.getItem() == Items.FIREWORK_ROCKET) {
        @Nullable final CompoundTag fireworks = item.getTagElement(Constants.Item.Fireworks.FIREWORKS);
        if (fireworks == null || !fireworks.contains(Constants.Item.Fireworks.EXPLOSIONS)) {
            return Optional.empty();
        }
        final ListTag effectCompounds = fireworks.getList(Constants.Item.Fireworks.EXPLOSIONS, Constants.NBT.TAG_COMPOUND);
        effects = NBTStreams.toCompounds(effectCompounds).map(FireworkUtil::fromCompound).collect(Collectors.toList());
    } else {
        @Nullable final FireworkEffect effect = FireworkUtil.getStarEffect(item);
        if (effect == null) {
            return Optional.empty();
        }
        effects = ImmutableList.of(effect);
    }
    return Optional.of(effects);
}
Also used : SpongeItemStack(org.spongepowered.common.item.SpongeItemStack) ItemStack(net.minecraft.world.item.ItemStack) FireworkEffect(org.spongepowered.api.item.FireworkEffect) ListTag(net.minecraft.nbt.ListTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 13 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class SpongeUserData method loadInventory.

private UserInventory loadInventory() {
    if (this.inventory == null) {
        this.inventory = new SpongeUserInventory(this);
        final ListTag listNBT = this.compound.getList(Constants.Entity.Player.INVENTORY, 10);
        this.inventory.readList(listNBT);
        this.inventory.currentItem = this.compound.getInt(Constants.Entity.Player.SELECTED_ITEM_SLOT);
    }
    return (UserInventory) this.inventory;
}
Also used : ListTag(net.minecraft.nbt.ListTag) UserInventory(org.spongepowered.api.item.inventory.entity.UserInventory)

Example 14 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class SpongeEntityArchetype method apply.

@Override
public Optional<org.spongepowered.api.entity.Entity> apply(final ServerLocation location) {
    if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
        return Optional.empty();
    }
    final org.spongepowered.api.world.server.ServerWorld spongeWorld = location.world();
    final ServerLevel level = (ServerLevel) spongeWorld;
    final ResourceLocation key = net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) this.type);
    if (key == null) {
        return Optional.empty();
    }
    final CompoundTag compound = this.compound.copy();
    compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
    final ListTag pos = new ListTag();
    pos.add(DoubleTag.valueOf(location.x()));
    pos.add(DoubleTag.valueOf(location.y()));
    pos.add(DoubleTag.valueOf(location.z()));
    compound.put(Constants.Entity.ENTITY_POSITION, pos);
    compound.remove(Constants.Entity.ENTITY_UUID);
    final boolean requiresInitialSpawn;
    if (compound.contains(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN)) {
        requiresInitialSpawn = compound.getBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
        compound.remove(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
    } else {
        requiresInitialSpawn = true;
    }
    @Nullable final Entity entity = net.minecraft.world.entity.EntityType.loadEntityRecursive(compound, level, e -> {
        e.moveTo(location.x(), location.y(), location.z());
        if (requiresInitialSpawn && e instanceof Mob) {
            ((Mob) e).finalizeSpawn(level, level.getCurrentDifficultyAt(e.blockPosition()), MobSpawnType.COMMAND, null, compound);
        }
        return e;
    });
    if (entity == null) {
        return Optional.empty();
    }
    if (level.tryAddFreshEntityWithPassengers(entity)) {
        return Optional.of((org.spongepowered.api.entity.Entity) entity);
    }
    return Optional.empty();
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) Entity(net.minecraft.world.entity.Entity) Mob(net.minecraft.world.entity.Mob) ListTag(net.minecraft.nbt.ListTag) ResourceLocation(net.minecraft.resources.ResourceLocation) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 15 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class ItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.APPLICABLE_POTION_EFFECTS).get(h -> {
        if (h.isEdible()) {
            final List<Pair<MobEffectInstance, Float>> itemEffects = h.getItem().getFoodProperties().getEffects();
            final WeightedTable<PotionEffect> effects = new WeightedTable<>();
            final ChanceTable<PotionEffect> chance = new ChanceTable<>();
            for (final Pair<MobEffectInstance, Float> effect : itemEffects) {
                chance.add((PotionEffect) effect.getFirst(), effect.getSecond());
            }
            effects.add(new NestedTableEntry<>(1, chance));
            return effects;
        }
        return null;
    }).create(Keys.BURN_TIME).get(h -> {
        final Integer burnTime = AbstractFurnaceBlockEntity.getFuel().get(h.getItem());
        if (burnTime != null && burnTime > 0) {
            return burnTime;
        }
        return null;
    }).create(Keys.CAN_HARVEST).get(h -> {
        final Item item = h.getItem();
        if (item instanceof DiggerItemAccessor && !(item instanceof PickaxeItem)) {
            final Set<Block> blocks = ((DiggerItemAccessor) item).accessor$blocks();
            return ImmutableSet.copyOf((Set<BlockType>) (Object) blocks);
        }
        final Set<BlockType> blockTypes = Registry.BLOCK.stream().filter(b -> item.isCorrectToolForDrops(b.defaultBlockState())).map(BlockType.class::cast).collect(ImmutableSet.toImmutableSet());
        return blockTypes.isEmpty() ? null : blockTypes;
    }).create(Keys.CONTAINER_ITEM).get(h -> (ItemType) h.getItem().getCraftingRemainingItem()).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.CUSTOM_MODEL_DATA).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.CUSTOM_MODEL_DATA, Constants.NBT.TAG_INT)) {
            return null;
        }
        return tag.getInt(Constants.Item.CUSTOM_MODEL_DATA);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        tag.putInt(Constants.Item.CUSTOM_MODEL_DATA, v);
    }).delete(h -> {
        final CompoundTag tag = h.getTag();
        if (tag != null) {
            tag.remove(Constants.Item.CUSTOM_MODEL_DATA);
        }
    }).create(Keys.CUSTOM_NAME).get(h -> {
        if (h.hasCustomHoverName()) {
            return SpongeAdventure.asAdventure(h.getHoverName());
        }
        if (h.getItem() == Items.WRITTEN_BOOK) {
            // When no custom name is set on a written book fallback to its title
            // The custom name has a higher priority than the title so no setter is needed.
            final CompoundTag tag = h.getTag();
            if (tag != null) {
                final String title = tag.getString(Constants.Item.Book.ITEM_BOOK_TITLE);
                return LegacyComponentSerializer.legacySection().deserialize(title);
            }
        }
        return null;
    }).set((h, v) -> h.setHoverName(SpongeAdventure.asVanilla(v))).delete(ItemStack::resetHoverName).create(Keys.IS_UNBREAKABLE).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.ITEM_UNBREAKABLE, Constants.NBT.TAG_BYTE)) {
            return false;
        }
        return tag.getBoolean(Constants.Item.ITEM_UNBREAKABLE);
    }).set(ItemStackData::setIsUnbrekable).delete(h -> ItemStackData.setIsUnbrekable(h, false)).create(Keys.LORE).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.ITEM_DISPLAY)) {
            return null;
        }
        final CompoundTag displayCompound = tag.getCompound(Constants.Item.ITEM_DISPLAY);
        final ListTag list = displayCompound.getList(Constants.Item.ITEM_LORE, Constants.NBT.TAG_STRING);
        return list.isEmpty() ? null : SpongeAdventure.json(list.stream().collect(NBTCollectors.toStringList()));
    }).set((h, v) -> {
        if (v.isEmpty()) {
            ItemStackData.deleteLore(h);
            return;
        }
        final ListTag list = SpongeAdventure.listTagJson(v);
        h.getOrCreateTagElement(Constants.Item.ITEM_DISPLAY).put(Constants.Item.ITEM_LORE, list);
    }).delete(ItemStackData::deleteLore).create(Keys.MAX_DURABILITY).get(h -> h.getItem().canBeDepleted() ? h.getItem().getMaxDamage() : null).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_DURABILITY).get(stack -> stack.getMaxDamage() - stack.getDamageValue()).set((stack, durability) -> stack.setDamageValue(stack.getMaxDamage() - durability)).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_RARITY).get(stack -> (ItemRarity) (Object) stack.getRarity()).create(Keys.REPLENISHED_FOOD).get(h -> {
        if (h.getItem().isEdible()) {
            final FoodProperties food = h.getItem().getFoodProperties();
            return food == null ? null : food.getNutrition();
        }
        return null;
    }).supports(h -> h.getItem().isEdible()).create(Keys.REPLENISHED_SATURATION).get(h -> {
        if (h.getItem().isEdible()) {
            final FoodProperties food = h.getItem().getFoodProperties();
            if (food != null) {
                // Translate's Minecraft's weird internal value to the actual saturation value
                return food.getSaturationModifier() * food.getNutrition() * 2.0;
            }
        }
        return null;
    }).supports(h -> h.getItem().isEdible());
}
Also used : Items(net.minecraft.world.item.Items) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) Constants(org.spongepowered.common.util.Constants) StringTag(net.minecraft.nbt.StringTag) Item(net.minecraft.world.item.Item) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PickaxeItem(net.minecraft.world.item.PickaxeItem) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) Registry(net.minecraft.core.Registry) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) FoodProperties(net.minecraft.world.food.FoodProperties) ImmutableSet(com.google.common.collect.ImmutableSet) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) Set(java.util.Set) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) SpongeCommon(org.spongepowered.common.SpongeCommon) Pair(com.mojang.datafixers.util.Pair) AbstractFurnaceBlockEntity(net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ItemRarity(org.spongepowered.api.item.ItemRarity) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ItemType(org.spongepowered.api.item.ItemType) ListTag(net.minecraft.nbt.ListTag) PickaxeItem(net.minecraft.world.item.PickaxeItem) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemType(org.spongepowered.api.item.ItemType) Item(net.minecraft.world.item.Item) PickaxeItem(net.minecraft.world.item.PickaxeItem) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Pair(com.mojang.datafixers.util.Pair) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) FoodProperties(net.minecraft.world.food.FoodProperties) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ListTag(net.minecraft.nbt.ListTag) BlockType(org.spongepowered.api.block.BlockType) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

ListTag (net.minecraft.nbt.ListTag)52 CompoundTag (net.minecraft.nbt.CompoundTag)38 ItemStack (net.minecraft.world.item.ItemStack)10 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 List (java.util.List)7 IntArrayTag (net.minecraft.nbt.IntArrayTag)7 ResourceLocation (net.minecraft.resources.ResourceLocation)7 TagString (de.keyle.knbt.TagString)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 StringTag (net.minecraft.nbt.StringTag)5 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntTag (net.minecraft.nbt.IntTag)4 Tag (net.minecraft.nbt.Tag)4 ByteTag (net.minecraft.nbt.ByteTag)3 DoubleTag (net.minecraft.nbt.DoubleTag)3 FloatTag (net.minecraft.nbt.FloatTag)3 LongTag (net.minecraft.nbt.LongTag)3 ShortTag (net.minecraft.nbt.ShortTag)3