Search in sources :

Example 86 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ConfigItem method load.

@Override
public void load(MaterialHolder material, String data) {
    ResourceLocation key = new ResourceLocation(material.getId());
    Item item = Registry.ITEM.get(key);
    // TODO AIR now?
    if (item == null) {
        Block block = Registry.BLOCK.get(key);
        item = block.asItem();
    }
    if (item == null) {
        return;
    }
    net.minecraft.world.item.ItemStack is = new net.minecraft.world.item.ItemStack(item, 1);
    if (data != null) {
        CompoundTag tag = null;
        String nbtString = data.trim();
        if (nbtString.startsWith("{") && nbtString.endsWith("}")) {
            try {
                tag = TagParser.parseTag(nbtString);
            } catch (Exception e) {
                MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
                MyPetApi.getLogger().warning(item.getDescriptionId() + " " + nbtString);
            }
            if (tag != null) {
                is.setTag(tag);
            }
        }
    }
    this.item = CraftItemStack.asCraftMirror(is);
}
Also used : Item(net.minecraft.world.item.Item) ResourceLocation(net.minecraft.resources.ResourceLocation) Block(net.minecraft.world.level.block.Block) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 87 with CompoundTag

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

the class SpongeBlockEntityArchetypeBuilder method blockEntity.

@Override
public BlockEntityArchetype.Builder blockEntity(final BlockEntity blockEntity) {
    if (!(Objects.requireNonNull(blockEntity, "BlockEntity cannot be null!") instanceof net.minecraft.world.level.block.entity.BlockEntity)) {
        throw new IllegalArgumentException("BlockEntity is not compatible with this implementation!");
    }
    final CompoundTag compound = new CompoundTag();
    ((net.minecraft.world.level.block.entity.BlockEntity) blockEntity).save(compound);
    compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_X);
    compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Y);
    compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Z);
    compound.remove(Constants.Item.BLOCK_ENTITY_ID);
    this.data = NBTTranslator.INSTANCE.translate(compound);
    this.blockState = blockEntity.block();
    this.type = blockEntity.type();
    return this;
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag) BlockEntity(org.spongepowered.api.block.entity.BlockEntity)

Example 88 with CompoundTag

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

the class NbtLegacyHoverEventSerializer method serializeShowItem.

@Override
@NonNull
public Component serializeShowItem(final HoverEvent.@NonNull ShowItem input) throws IOException {
    final CompoundTag tag = new CompoundTag();
    tag.putString(NbtLegacyHoverEventSerializer.ITEM_TYPE, input.item().asString());
    tag.putByte(NbtLegacyHoverEventSerializer.ITEM_COUNT, (byte) input.count());
    if (input.nbt() != null) {
        try {
            tag.put(NbtLegacyHoverEventSerializer.ITEM_TAG, input.nbt().get(NbtLegacyHoverEventSerializer.SNBT_CODEC));
        } catch (final CommandSyntaxException ex) {
            throw new IOException(ex);
        }
    }
    return Component.text(NbtLegacyHoverEventSerializer.SNBT_CODEC.encode(tag));
}
Also used : IOException(java.io.IOException) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 89 with CompoundTag

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

the class NbtLegacyHoverEventSerializer method deserializeShowItem.

@Override
public HoverEvent.ShowItem deserializeShowItem(final Component input) throws IOException {
    final String rawContent = PlainTextComponentSerializer.plainText().serialize(input);
    try {
        final CompoundTag contents = NbtLegacyHoverEventSerializer.SNBT_CODEC.decode(rawContent);
        final CompoundTag tag = contents.getCompound(NbtLegacyHoverEventSerializer.ITEM_TAG);
        return HoverEvent.ShowItem.of(Key.key(contents.getString(NbtLegacyHoverEventSerializer.ITEM_TYPE)), contents.contains(NbtLegacyHoverEventSerializer.ITEM_COUNT) ? contents.getByte(NbtLegacyHoverEventSerializer.ITEM_COUNT) : 1, tag.isEmpty() ? null : BinaryTagHolder.encode(tag, NbtLegacyHoverEventSerializer.SNBT_CODEC));
    } catch (final CommandSyntaxException ex) {
        throw new IOException(ex);
    }
}
Also used : IOException(java.io.IOException) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 90 with CompoundTag

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

the class MobSpawnerData method setEntities.

private static void setEntities(final BaseSpawnerAccessor logic, final WeightedTable<EntityArchetype> table) {
    logic.accessor$spawnPotentials().clear();
    for (final TableEntry<EntityArchetype> entry : table) {
        if (!(entry instanceof WeightedObject)) {
            continue;
        }
        final WeightedObject<EntityArchetype> object = (WeightedObject<EntityArchetype>) entry;
        final CompoundTag compound = NBTTranslator.INSTANCE.translate(object.get().entityData());
        if (!compound.contains(Constants.Entity.ENTITY_TYPE_ID)) {
            final ResourceKey key = (ResourceKey) (Object) net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) object.get().type());
            compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
        }
        logic.accessor$spawnPotentials().add(new SpawnData((int) entry.weight(), compound));
    }
}
Also used : WeightedObject(org.spongepowered.api.util.weighted.WeightedObject) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) SpawnData(net.minecraft.world.level.SpawnData) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(org.spongepowered.api.ResourceKey)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)146 ListTag (net.minecraft.nbt.ListTag)41 ItemStack (net.minecraft.world.item.ItemStack)22 TagCompound (de.keyle.knbt.TagCompound)12 ResourceLocation (net.minecraft.resources.ResourceLocation)12 Nullable (org.checkerframework.checker.nullness.qual.Nullable)10 Keys (org.spongepowered.api.data.Keys)9 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)9 Constants (org.spongepowered.common.util.Constants)9 TagList (de.keyle.knbt.TagList)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 IntArrayTag (net.minecraft.nbt.IntArrayTag)8 Tag (net.minecraft.nbt.Tag)8 ResourceKey (org.spongepowered.api.ResourceKey)8 DataContainer (org.spongepowered.api.data.persistence.DataContainer)8 DataView (org.spongepowered.api.data.persistence.DataView)8 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)7 TagString (de.keyle.knbt.TagString)6 Block (net.minecraft.world.level.block.Block)6