Search in sources :

Example 91 with CompoundTag

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

the class MobSpawnerData method getNextEntity.

// @formatter:on
private static WeightedSerializableObject<EntityArchetype> getNextEntity(final BaseSpawnerAccessor logic) {
    final int weight = ((WeighedRandom_WeighedRandomItemAccessor) logic.accessor$nextSpawnData()).accessor$weight();
    final String resourceLocation = logic.accessor$nextSpawnData().getTag().getString(Constants.Entity.ENTITY_TYPE_ID);
    final EntityType<?> type = Registry.ENTITY_TYPE.getOptional(new ResourceLocation(resourceLocation)).map(EntityType.class::cast).orElse(EntityTypes.PIG.get());
    final CompoundTag data = logic.accessor$nextSpawnData().getTag();
    final EntityArchetype archetype = SpongeEntityArchetypeBuilder.pooled().type(type).entityData(NBTTranslator.INSTANCE.translateFrom(data)).build();
    return new WeightedSerializableObject<>(archetype, weight);
}
Also used : EntityArchetype(org.spongepowered.api.entity.EntityArchetype) WeighedRandom_WeighedRandomItemAccessor(org.spongepowered.common.accessor.util.WeighedRandom_WeighedRandomItemAccessor) ResourceLocation(net.minecraft.resources.ResourceLocation) CompoundTag(net.minecraft.nbt.CompoundTag) WeightedSerializableObject(org.spongepowered.api.util.weighted.WeightedSerializableObject)

Example 92 with CompoundTag

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

the class MobSpawnerData method setNextEntity.

private static void setNextEntity(final BaseSpawner logic, final WeightedSerializableObject<EntityArchetype> value) {
    final CompoundTag compound = NBTTranslator.INSTANCE.translate(value.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<?>) value.get().type());
        compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
    }
    logic.setNextSpawnData(new SpawnData((int) value.weight(), compound));
}
Also used : SpawnData(net.minecraft.world.level.SpawnData) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(org.spongepowered.api.ResourceKey)

Example 93 with CompoundTag

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

the class MobSpawnerData method getEntities.

private static WeightedTable<EntityArchetype> getEntities(final BaseSpawner logic) {
    final WeightedTable<EntityArchetype> possibleEntities = new WeightedTable<>();
    for (final SpawnData weightedEntity : ((BaseSpawnerAccessor) logic).accessor$spawnPotentials()) {
        final CompoundTag nbt = weightedEntity.getTag();
        final String resourceLocation = nbt.getString(Constants.Entity.ENTITY_TYPE_ID);
        final EntityType<?> type = Registry.ENTITY_TYPE.getOptional(new ResourceLocation(resourceLocation)).map(EntityType.class::cast).orElse(EntityTypes.PIG.get());
        final EntityArchetype archetype = SpongeEntityArchetypeBuilder.pooled().type(type).entityData(NBTTranslator.INSTANCE.translateFrom(nbt)).build();
        possibleEntities.add(new WeightedSerializableObject<>(archetype, ((WeighedRandom_WeighedRandomItemAccessor) weightedEntity).accessor$weight()));
    }
    return possibleEntities;
}
Also used : WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) ResourceLocation(net.minecraft.resources.ResourceLocation) WeighedRandom_WeighedRandomItemAccessor(org.spongepowered.common.accessor.util.WeighedRandom_WeighedRandomItemAccessor) BaseSpawnerAccessor(org.spongepowered.common.accessor.world.level.BaseSpawnerAccessor) SpawnData(net.minecraft.world.level.SpawnData) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 94 with CompoundTag

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

the class NBTTranslator method containerToCompound.

private static void containerToCompound(final DataView container, final CompoundTag compound) {
    // We don't need to get deep values since all nested DataViews will be found
    // from the instance of checks.
    checkNotNull(container);
    checkNotNull(compound);
    for (Map.Entry<DataQuery, Object> entry : container.values(false).entrySet()) {
        Object value = entry.getValue();
        String key = entry.getKey().asString('.');
        if (value instanceof DataView) {
            CompoundTag inner = new CompoundTag();
            NBTTranslator.containerToCompound(container.getView(entry.getKey()).get(), inner);
            compound.put(key, inner);
        } else if (value instanceof Boolean) {
            compound.put(key + NBTTranslator.BOOLEAN_IDENTIFIER, ByteTag.valueOf((Boolean) value));
        } else {
            compound.put(key, NBTTranslator.getBaseFromObject(value));
        }
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataQuery(org.spongepowered.api.data.persistence.DataQuery) Map(java.util.Map) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 95 with CompoundTag

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

the class NBTTranslator method addTo.

@Override
public DataView addTo(CompoundTag compound, DataView container) {
    for (String key : compound.getAllKeys()) {
        Tag base = compound.get(key);
        byte type = base.getId();
        // gotta love recursion
        NBTTranslator.setInternal(base, type, container, key);
    }
    return container;
}
Also used : LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag)

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