Search in sources :

Example 36 with CompoundTag

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

the class SpongeEntitySnapshot method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(this.compound == null ? new CompoundTag() : this.compound);
    final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(Queries.CONTENT_VERSION, this.contentVersion()).set(Queries.WORLD_KEY, this.worldKey.formatted()).createView(Constants.Sponge.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, this.position.x()).set(Queries.POSITION_Y, this.position.y()).set(Queries.POSITION_Z, this.position.z()).container().createView(Constants.Entity.ROTATION).set(Queries.POSITION_X, this.rotation.x()).set(Queries.POSITION_Y, this.rotation.y()).set(Queries.POSITION_Z, this.rotation.z()).container().createView(Constants.Entity.SCALE).set(Queries.POSITION_X, this.scale.x()).set(Queries.POSITION_Y, this.scale.y()).set(Queries.POSITION_Z, this.scale.z()).container().set(Constants.Entity.TYPE, net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) this.entityType)).set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
    if (this.uniqueId != null) {
        container.set(Constants.Entity.UUID, this.uniqueId.toString());
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 37 with CompoundTag

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

the class SpongeEntityArchetypeBuilder method from.

@SuppressWarnings({ "rawtypes", "EqualsBetweenInconvertibleTypes" })
@Override
public EntityArchetype.Builder from(final Entity entity) {
    final EntityType<@NonNull ?> entityType = Objects.requireNonNull(Objects.requireNonNull(entity, "Cannot build an EntityArchetype for a null entity!").type(), "Entity is returning a null EntityType!");
    if (!((net.minecraft.world.entity.EntityType) entityType).canSerialize() && entityType != HumanEntity.TYPE) {
        throw new IllegalArgumentException("Attempting to archetype a non-serializable entity: " + entity);
    }
    this.entityType = entityType;
    final CompoundTag compound = new CompoundTag();
    net.minecraft.world.entity.Entity mcEntity = (net.minecraft.world.entity.Entity) entity;
    if (entityType == HumanEntity.TYPE) {
        mcEntity.saveWithoutId(compound);
    } else {
        mcEntity.saveAsPassenger(compound);
    }
    this.position = new Vector3d(mcEntity.getX(), mcEntity.getY(), mcEntity.getZ());
    SpongeEntityArchetypeBuilder.stripCompound(compound);
    compound.putBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN, true);
    this.position = entity.position();
    this.compound = compound;
    return this;
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) Entity(org.spongepowered.api.entity.Entity) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) Vector3d(org.spongepowered.math.vector.Vector3d) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 38 with CompoundTag

use of net.minecraft.nbt.CompoundTag 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)

Example 39 with CompoundTag

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

the class MapInfoItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
        if (itemStack.getTag() == null) {
            return null;
        }
        return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
    }).set((itemStack, mapInfo) -> {
        @Nullable CompoundTag nbt = itemStack.getTag();
        if (nbt == null) {
            nbt = new CompoundTag();
        }
        nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
        itemStack.setTag(nbt);
    });
}
Also used : Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) MapInfo(org.spongepowered.api.map.MapInfo) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) MapItem(net.minecraft.world.item.MapItem) Sponge(org.spongepowered.api.Sponge) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) Level(net.minecraft.world.level.Level) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Level(net.minecraft.world.level.Level) MapItem(net.minecraft.world.item.MapItem) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 40 with CompoundTag

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

the class SpongeEntitySnapshotBuilder method from.

@Override
public SpongeEntitySnapshotBuilder from(final Entity entity) {
    this.reset();
    this.entityReference = new WeakReference<>(entity);
    this.worldKey = entity.serverLocation().worldKey();
    this.position = entity.transform().position();
    this.rotation = entity.transform().rotation();
    this.scale = entity.transform().scale();
    this.entityType = entity.type();
    this.uniqueId = entity.uniqueId();
    this.manipulator = ((SpongeDataHolderBridge) entity).bridge$getManipulator().copy();
    this.compound = new CompoundTag();
    ((net.minecraft.world.entity.Entity) entity).saveWithoutId(this.compound);
    return this;
}
Also used : Entity(org.spongepowered.api.entity.Entity) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)476 ListTag (net.minecraft.nbt.ListTag)145 ItemStack (net.minecraft.world.item.ItemStack)63 Tag (net.minecraft.nbt.Tag)53 ArrayList (java.util.ArrayList)43 StringTag (net.minecraft.nbt.StringTag)39 ItemStack (org.bukkit.inventory.ItemStack)37 IntTag (net.minecraft.nbt.IntTag)36 IntArrayTag (net.minecraft.nbt.IntArrayTag)34 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)32 HashMap (java.util.HashMap)25 ByteTag (net.minecraft.nbt.ByteTag)25 DoubleTag (net.minecraft.nbt.DoubleTag)25 FloatTag (net.minecraft.nbt.FloatTag)25 LongTag (net.minecraft.nbt.LongTag)25 ShortTag (net.minecraft.nbt.ShortTag)25 Map (java.util.Map)24 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)24 LongArrayTag (net.minecraft.nbt.LongArrayTag)23 ResourceLocation (net.minecraft.resources.ResourceLocation)21