Search in sources :

Example 1 with SpawnData

use of net.minecraft.world.level.SpawnData in project Denizen-For-Bukkit by DenizenScript.

the class BlockHelperImpl method setSpawnerCustomRules.

@Override
public void setSpawnerCustomRules(CreatureSpawner spawner, int skyMin, int skyMax, int blockMin, int blockMax) {
    try {
        CraftCreatureSpawner bukkitSpawner = (CraftCreatureSpawner) spawner;
        SpawnerBlockEntity nmsSnapshot = (SpawnerBlockEntity) craftBlockEntityState_snapshot.get(bukkitSpawner);
        BaseSpawner nmsSpawner = nmsSnapshot.getSpawner();
        SpawnData toSpawn = nmsSpawner.nextSpawnData;
        SpawnData.CustomSpawnRules rules = skyMin == -1 ? null : new SpawnData.CustomSpawnRules(new InclusiveRange<>(skyMin, skyMax), new InclusiveRange<>(blockMin, blockMax));
        nmsSpawner.nextSpawnData = new SpawnData(toSpawn.entityToSpawn(), Optional.ofNullable(rules));
        nmsSpawner.spawnPotentials = SimpleWeightedRandomList.empty();
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
}
Also used : SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BaseSpawner(net.minecraft.world.level.BaseSpawner) SpawnData(net.minecraft.world.level.SpawnData) InclusiveRange(net.minecraft.util.InclusiveRange)

Example 2 with SpawnData

use of net.minecraft.world.level.SpawnData in project Denizen-For-Bukkit by DenizenScript.

the class BlockHelperImpl method setSpawnerSpawnedType.

@Override
public void setSpawnerSpawnedType(CreatureSpawner spawner, EntityTag entity) {
    spawner.setSpawnedType(entity.getBukkitEntityType());
    if (entity.getWaitingMechanisms() == null || entity.getWaitingMechanisms().size() == 0) {
        return;
    }
    try {
        // Wrangle a fake entity
        Entity nmsEntity = ((CraftWorld) spawner.getWorld()).createEntity(spawner.getLocation(), entity.getBukkitEntityType().getEntityClass());
        EntityTag entityTag = new EntityTag(nmsEntity.getBukkitEntity());
        entityTag.isFake = true;
        entityTag.isFakeValid = true;
        for (Mechanism mechanism : entity.getWaitingMechanisms()) {
            entityTag.safeAdjustDuplicate(mechanism);
        }
        nmsEntity.unsetRemoved();
        // Store it into the spawner
        CraftCreatureSpawner bukkitSpawner = (CraftCreatureSpawner) spawner;
        SpawnerBlockEntity nmsSnapshot = (SpawnerBlockEntity) craftBlockEntityState_snapshot.get(bukkitSpawner);
        BaseSpawner nmsSpawner = nmsSnapshot.getSpawner();
        SpawnData toSpawn = nmsSpawner.nextSpawnData;
        net.minecraft.nbt.CompoundTag tag = toSpawn.getEntityToSpawn();
        nmsEntity.saveWithoutId(tag);
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
}
Also used : SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Entity(net.minecraft.world.entity.Entity) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BaseSpawner(net.minecraft.world.level.BaseSpawner) EntityTag(com.denizenscript.denizen.objects.EntityTag) SpawnData(net.minecraft.world.level.SpawnData) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld) Mechanism(com.denizenscript.denizencore.objects.Mechanism)

Example 3 with SpawnData

use of net.minecraft.world.level.SpawnData 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)

Example 4 with SpawnData

use of net.minecraft.world.level.SpawnData 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 5 with SpawnData

use of net.minecraft.world.level.SpawnData 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)

Aggregations

SpawnData (net.minecraft.world.level.SpawnData)5 CompoundTag (net.minecraft.nbt.CompoundTag)3 BaseSpawner (net.minecraft.world.level.BaseSpawner)2 SpawnerBlockEntity (net.minecraft.world.level.block.entity.SpawnerBlockEntity)2 ResourceKey (org.spongepowered.api.ResourceKey)2 EntityArchetype (org.spongepowered.api.entity.EntityArchetype)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 Mechanism (com.denizenscript.denizencore.objects.Mechanism)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 InclusiveRange (net.minecraft.util.InclusiveRange)1 Entity (net.minecraft.world.entity.Entity)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)1 WeightedObject (org.spongepowered.api.util.weighted.WeightedObject)1 WeightedTable (org.spongepowered.api.util.weighted.WeightedTable)1 WeighedRandom_WeighedRandomItemAccessor (org.spongepowered.common.accessor.util.WeighedRandom_WeighedRandomItemAccessor)1 BaseSpawnerAccessor (org.spongepowered.common.accessor.world.level.BaseSpawnerAccessor)1