Search in sources :

Example 6 with MobSpawnerTileEntity

use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChaosAwakens by ChaosAwakens.

the class SpawnEggItemMixin method useOn.

@Inject(method = "useOn(Lnet/minecraft/item/ItemUseContext;)Lnet/minecraft/util/ActionResultType;", at = @At("HEAD"), cancellable = true)
public void useOn(ItemUseContext itemUseContext, CallbackInfoReturnable<ActionResultType> cir) {
    World world = itemUseContext.getLevel();
    if (!(world instanceof ServerWorld)) {
        cir.setReturnValue(ActionResultType.SUCCESS);
    } else {
        ItemStack itemstack = itemUseContext.getItemInHand();
        BlockPos blockpos = itemUseContext.getClickedPos();
        Direction direction = itemUseContext.getClickedFace();
        BlockState blockstate = world.getBlockState(blockpos);
        PlayerEntity player = itemUseContext.getPlayer();
        if (blockstate.is(Blocks.SPAWNER)) {
            if ((CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 0) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 1 && player.isCreative()) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && player.isCreative() && itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens")) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && !itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens"))) {
                TileEntity tileentity = world.getBlockEntity(blockpos);
                if (tileentity instanceof MobSpawnerTileEntity) {
                    AbstractSpawner abstractspawner = ((MobSpawnerTileEntity) tileentity).getSpawner();
                    EntityType<?> entitytype1 = this.getType(itemstack.getTag());
                    abstractspawner.setEntityId(entitytype1);
                    tileentity.setChanged();
                    world.sendBlockUpdated(blockpos, blockstate, blockstate, 3);
                    itemstack.shrink(1);
                    cir.setReturnValue(ActionResultType.CONSUME);
                }
            }
        }
        BlockPos blockpos1;
        if (blockstate.getCollisionShape(world, blockpos).isEmpty()) {
            blockpos1 = blockpos;
        } else {
            blockpos1 = blockpos.relative(direction);
        }
        EntityType<?> entitytype = this.getType(itemstack.getTag());
        if (!blockstate.is(Blocks.SPAWNER)) {
            if (entitytype.spawn((ServerWorld) world, itemstack, itemUseContext.getPlayer(), blockpos1, SpawnReason.SPAWN_EGG, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP) != null) {
                itemstack.shrink(1);
            }
        }
        cir.setReturnValue(ActionResultType.CONSUME);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) BlockPos(net.minecraft.util.math.BlockPos) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with MobSpawnerTileEntity

use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.

the class PreparableSpawnerInfo method vanillaSpawnerReadFromNBT.

private void vanillaSpawnerReadFromNBT(World world, DungeonPlacement placement, BlockPos pos, MobSpawnerTileEntity tileEntity) {
    AbstractSpawner spawnerBaseLogic = tileEntity.getSpawnerBaseLogic();
    CompoundNBT compound = new CompoundNBT();
    compound.setShort("Delay", (short) 20);
    if (this.tileEntityData.hasKey("MinSpawnDelay", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("MinSpawnDelay", this.tileEntityData.getShort("MinSpawnDelay"));
        compound.setShort("MaxSpawnDelay", this.tileEntityData.getShort("MaxSpawnDelay"));
        compound.setShort("SpawnCount", this.tileEntityData.getShort("SpawnCount"));
    }
    if (this.tileEntityData.hasKey("MaxNearbyEntities", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("MaxNearbyEntities", this.tileEntityData.getShort("MaxNearbyEntities"));
        compound.setShort("RequiredPlayerRange", this.tileEntityData.getShort("RequiredPlayerRange"));
    }
    if (this.tileEntityData.hasKey("SpawnRange", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("SpawnRange", this.tileEntityData.getShort("SpawnRange"));
    }
    ListNBT nbttaglist = new ListNBT();
    ListNBT items = this.tileEntityData.getCompoundTag("inventory").getTagList("Items", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < items.tagCount(); i++) {
        CompoundNBT itemTag = items.getCompoundTagAt(i);
        CompoundNBT entityTag = itemTag.getCompoundTag("tag").getCompoundTag("EntityIn");
        Entity entity = this.createEntityFromTag(world, placement, pos, entityTag);
        if (entity != null) {
            CompoundNBT newEntityTag = new CompoundNBT();
            entity.writeToNBTAtomically(newEntityTag);
            newEntityTag.removeTag("UUIDLeast");
            newEntityTag.removeTag("UUIDMost");
            newEntityTag.removeTag("Pos");
            if (nbttaglist.isEmpty()) {
                compound.setTag("SpawnData", newEntityTag);
            }
            nbttaglist.appendTag(new WeightedSpawnerEntity(itemTag.getByte("Count"), newEntityTag).toCompoundTag());
        }
    }
    compound.setTag("SpawnPotentials", nbttaglist);
    spawnerBaseLogic.readFromNBT(compound);
}
Also used : WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity) MobEntity(net.minecraft.entity.MobEntity) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) ListNBT(net.minecraft.nbt.ListNBT) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) CompoundNBT(net.minecraft.nbt.CompoundNBT) WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity)

Example 8 with MobSpawnerTileEntity

use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.

the class PreparableSpawnerInfo method prepare.

@Override
protected GeneratablePosInfo prepare(World world, DungeonPlacement placement, BlockPos pos) {
    BlockState state;
    TileEntity tileEntity;
    BlockPos p = pos.toImmutable();
    if (this.tileEntityData.getBoolean("vanillaSpawner")) {
        state = Blocks.MOB_SPAWNER.getDefaultState();
        tileEntity = state.getBlock().createTileEntity(world, state);
        if (tileEntity instanceof MobSpawnerTileEntity) {
            this.vanillaSpawnerReadFromNBT(world, placement, p, (MobSpawnerTileEntity) tileEntity);
        }
    } else {
        state = CQRBlocks.SPAWNER.getDefaultState();
        tileEntity = state.getBlock().createTileEntity(world, state);
        if (tileEntity instanceof TileEntitySpawner) {
            this.cqrSpawnerReadFromNBT(world, placement, p, (TileEntitySpawner) tileEntity);
        }
    }
    return new GeneratableBlockInfo(p, state, tileEntity);
}
Also used : MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) TileEntitySpawner(team.cqr.cqrepoured.tileentity.TileEntitySpawner) GeneratableBlockInfo(team.cqr.cqrepoured.world.structure.generation.generation.generatable.GeneratableBlockInfo)

Example 9 with MobSpawnerTileEntity

use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.

the class SpawnerFactory method createSimpleMultiUseSpawner.

/**
 * Places a vanilla spawner in the provided world at the provided position using the provided ResourceLocation for the
 * entity that it should spawn.
 */
public static void createSimpleMultiUseSpawner(World world, BlockPos pos, ResourceLocation entityResLoc) {
    world.setBlockAndUpdate(pos, Blocks.SPAWNER.defaultBlockState());
    MobSpawnerTileEntity spawner = (MobSpawnerTileEntity) world.getBlockEntity(pos);
    spawner.getSpawner().setEntityId(ForgeRegistries.ENTITIES.getValue(entityResLoc));
    spawner.setChanged();
    // Correct method?
    spawner.requestModelDataUpdate();
    spawner.tick();
}
Also used : MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity)

Example 10 with MobSpawnerTileEntity

use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.

the class SpawnerFactory method convertVanillaSpawnerToCQSpawner.

/**
 * Converts the vanilla spawner at the provided World/BlockPos to a CQR spawner
 */
public static void convertVanillaSpawnerToCQSpawner(World world, BlockPos pos) {
    TileEntity tile = world.getBlockEntity(pos);
    if (tile != null && tile instanceof MobSpawnerTileEntity) {
        MobSpawnerTileEntity spawnerMultiUseTile = (MobSpawnerTileEntity) tile;
        // TODO: Create mixin to retrieve this
        List<WeightedSpawnerEntity> spawnerEntries = ((AccessorAbstractSpawner) spawnerMultiUseTile.getSpawner()).getSpawnPotentials();
        if (!spawnerEntries.isEmpty()) {
            Iterator<WeightedSpawnerEntity> iterator = spawnerEntries.iterator();
            // Entity[] entities = new Entity[9];
            CompoundNBT[] entityCompound = new CompoundNBT[9];
            int entriesRead = 0;
            while (entriesRead < 9 && iterator.hasNext()) {
                /*
					 * Entity entity = createEntityFromNBTWithoutSpawningIt(iterator.next().getNbt(), world); entities[entriesRead] =
					 * entity;
					 */
                entityCompound[entriesRead] = iterator.next().getTag();
                entriesRead++;
            }
            // placeSpawner(entities, false, null, world, pos);
            placeSpawner(entityCompound, false, null, world, pos);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) CompoundNBT(net.minecraft.nbt.CompoundNBT) WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) AccessorAbstractSpawner(team.cqr.cqrepoured.mixin.AccessorAbstractSpawner)

Aggregations

MobSpawnerTileEntity (net.minecraft.tileentity.MobSpawnerTileEntity)11 TileEntity (net.minecraft.tileentity.TileEntity)7 BlockState (net.minecraft.block.BlockState)5 BlockPos (net.minecraft.util.math.BlockPos)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)3 Direction (net.minecraft.util.Direction)3 AbstractSpawner (net.minecraft.world.spawner.AbstractSpawner)3 ItemStack (net.minecraft.item.ItemStack)2 ListNBT (net.minecraft.nbt.ListNBT)2 WeightedSpawnerEntity (net.minecraft.util.WeightedSpawnerEntity)2 World (net.minecraft.world.World)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 TileEntitySpawner (team.cqr.cqrepoured.tileentity.TileEntitySpawner)2 Block (net.minecraft.block.Block)1 FlowingFluidBlock (net.minecraft.block.FlowingFluidBlock)1 Material (net.minecraft.block.material.Material)1 Entity (net.minecraft.entity.Entity)1 MobEntity (net.minecraft.entity.MobEntity)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 INBT (net.minecraft.nbt.INBT)1