Search in sources :

Example 1 with BlockMobSpawner

use of net.minecraft.block.BlockMobSpawner in project NetherEx by LogicTechCorp.

the class WorldGenUtil method generateStructure.

public static void generateStructure(World world, BlockPos pos, Random rand, Template template, PlacementSettings placementSettings, ResourceLocation[] lootTables, ResourceLocation[] spawnerMobs) {
    try {
        List<Template.BlockInfo> blocks = (List<Template.BlockInfo>) FIELD_BLOCKS.get(template);
        List<Template.EntityInfo> entities = (List<Template.EntityInfo>) FIELD_ENTITIES.get(template);
        if ((!blocks.isEmpty() || !placementSettings.getIgnoreEntities() && !entities.isEmpty()) && template.getSize().getX() >= 1 && template.getSize().getY() >= 1 && template.getSize().getZ() >= 1) {
            BlockRotationProcessor processor = new BlockRotationProcessor(pos, placementSettings);
            Block block = placementSettings.getReplacedBlock();
            StructureBoundingBox boundingBox = placementSettings.getBoundingBox();
            for (Template.BlockInfo blockInfo : blocks) {
                BlockPos blockPos = Template.transformedBlockPos(placementSettings, blockInfo.pos).add(pos);
                Template.BlockInfo blockInfo1 = processor != null ? processor.processBlock(world, blockPos, blockInfo) : blockInfo;
                if (blockInfo1 != null) {
                    Block block1 = blockInfo1.blockState.getBlock();
                    if ((block == null || block != block1) && (!placementSettings.getIgnoreStructureBlock() || block1 != Blocks.STRUCTURE_BLOCK) && (boundingBox == null || boundingBox.isVecInside(blockPos))) {
                        IBlockState state = blockInfo1.blockState.withMirror(placementSettings.getMirror()).withRotation(placementSettings.getRotation());
                        if (blockInfo1.tileentityData != null) {
                            TileEntity tileEntity = world.getTileEntity(blockPos);
                            if (tileEntity != null) {
                                if (tileEntity instanceof IInventory) {
                                    ((IInventory) tileEntity).clear();
                                }
                                world.setBlockState(blockPos, Blocks.BARRIER.getDefaultState(), 4);
                            }
                        }
                        if (world.setBlockState(blockPos, state, 3) && blockInfo1.tileentityData != null) {
                            TileEntity tileEntity = world.getTileEntity(blockPos);
                            if (tileEntity != null) {
                                blockInfo1.tileentityData.setInteger("x", blockPos.getX());
                                blockInfo1.tileentityData.setInteger("y", blockPos.getY());
                                blockInfo1.tileentityData.setInteger("z", blockPos.getZ());
                                tileEntity.readFromNBT(blockInfo1.tileentityData);
                                tileEntity.mirror(placementSettings.getMirror());
                                tileEntity.rotate(placementSettings.getRotation());
                                if (state.getBlock() instanceof BlockChest) {
                                    ((TileEntityChest) tileEntity).setLootTable(lootTables[rand.nextInt(lootTables.length)], rand.nextLong());
                                } else if (state.getBlock() instanceof BlockMobSpawner) {
                                    ((TileEntityMobSpawner) tileEntity).getSpawnerBaseLogic().setEntityId(spawnerMobs[rand.nextInt(spawnerMobs.length)]);
                                } else if (state.getBlock() instanceof BlockUrnOfSorrow) {
                                    ((TileEntityUrnOfSorrow) tileEntity).setCanBreak(false);
                                }
                            }
                        }
                    }
                }
            }
            for (Template.BlockInfo blockInfo2 : blocks) {
                if (block == null || block != blockInfo2.blockState.getBlock()) {
                    BlockPos blockPos1 = Template.transformedBlockPos(placementSettings, blockInfo2.pos).add(pos);
                    if (boundingBox == null || boundingBox.isVecInside(blockPos1)) {
                        world.notifyNeighborsRespectDebug(blockPos1, blockInfo2.blockState.getBlock(), false);
                        if (blockInfo2.tileentityData != null) {
                            TileEntity tileEntity = world.getTileEntity(blockPos1);
                            if (tileEntity != null) {
                                tileEntity.markDirty();
                            }
                        }
                    }
                }
            }
            if (!placementSettings.getIgnoreEntities()) {
                addEntitiesToWorld(world, pos, placementSettings, entities, boundingBox);
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) TileEntityChest(net.minecraft.tileentity.TileEntityChest) StructureBoundingBox(net.minecraft.world.gen.structure.StructureBoundingBox) IBlockState(net.minecraft.block.state.IBlockState) BlockChest(net.minecraft.block.BlockChest) BlockUrnOfSorrow(nex.block.BlockUrnOfSorrow) BlockRotationProcessor(net.minecraft.world.gen.structure.template.BlockRotationProcessor) Template(net.minecraft.world.gen.structure.template.Template) TileEntity(net.minecraft.tileentity.TileEntity) BlockMobSpawner(net.minecraft.block.BlockMobSpawner) TileEntityUrnOfSorrow(nex.tileentity.TileEntityUrnOfSorrow) Block(net.minecraft.block.Block) NBTTagList(net.minecraft.nbt.NBTTagList) EntityList(net.minecraft.entity.EntityList) List(java.util.List) BlockPos(net.minecraft.util.math.BlockPos) TileEntityMobSpawner(net.minecraft.tileentity.TileEntityMobSpawner)

Example 2 with BlockMobSpawner

use of net.minecraft.block.BlockMobSpawner in project EnderIO by SleepyTrousers.

the class BrokenSpawnerHandler method onBreakEvent.

@SubscribeEvent
public static void onBreakEvent(BlockEvent.BreakEvent evt) {
    if (evt.getState().getBlock() instanceof BlockMobSpawner) {
        if (evt.getPlayer() != null && !evt.getPlayer().capabilities.isCreativeMode && !evt.getPlayer().world.isRemote && !evt.isCanceled()) {
            TileEntity tile = evt.getPlayer().world.getTileEntity(NullHelper.notnullF(evt.getPos(), "BlockEvent.BreakEvent.getPos()"));
            if (tile instanceof TileEntityMobSpawner) {
                if (Math.random() > BrokenSpawnerConfig.brokenSpawnerDropChance.get()) {
                    return;
                }
                ItemStack equipped = evt.getPlayer().getHeldItemMainhand();
                if (Prep.isValid(equipped) && BrokenSpawnerConfig.brokenSpawnerToolBlacklist.get().contains(equipped)) {
                    return;
                }
                TileEntityMobSpawner spawner = (TileEntityMobSpawner) tile;
                MobSpawnerBaseLogic logic = spawner.getSpawnerBaseLogic();
                ResourceLocation entityName = getEntityName(logic);
                if (entityName != null && !isBlackListed(entityName)) {
                    final CapturedMob capturedMob = CapturedMob.create(entityName);
                    if (capturedMob != null) {
                        ItemStack drop = capturedMob.toStack(ModObject.itemBrokenSpawner.getItemNN(), 0, 1);
                        dropCache.put(evt.getPos().toImmutable(), drop);
                        for (int i = (int) (Math.random() * 7); i > 0; i--) {
                            setSpawnDelay(logic);
                            logic.updateSpawner();
                        }
                    } else {
                        dropCache.put(evt.getPos().toImmutable(), Prep.getEmpty());
                    }
                }
            }
        } else {
            dropCache.put(evt.getPos().toImmutable(), Prep.getEmpty());
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) MobSpawnerBaseLogic(net.minecraft.tileentity.MobSpawnerBaseLogic) BlockMobSpawner(net.minecraft.block.BlockMobSpawner) CapturedMob(crazypants.enderio.util.CapturedMob) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntityMobSpawner(net.minecraft.tileentity.TileEntityMobSpawner) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with BlockMobSpawner

use of net.minecraft.block.BlockMobSpawner in project EnderIO by SleepyTrousers.

the class BrokenSpawnerHandler method onHarvestDropsEvent.

@SubscribeEvent
public static void onHarvestDropsEvent(BlockEvent.HarvestDropsEvent evt) {
    if (!evt.isCanceled() && evt.getState().getBlock() instanceof BlockMobSpawner) {
        if (dropCache.containsKey(evt.getPos())) {
            ItemStack stack = dropCache.get(evt.getPos());
            if (Prep.isValid(stack)) {
                evt.getDrops().add(stack);
                dropCache.put(evt.getPos().toImmutable(), Prep.getEmpty());
            }
        } else {
            // invalidated already, but we might be able to recover it.
            try {
                for (Object object : evt.getWorld().loadedTileEntityList) {
                    if (object instanceof TileEntityMobSpawner) {
                        TileEntityMobSpawner spawner = (TileEntityMobSpawner) object;
                        BlockPos p = spawner.getPos();
                        if (spawner.getWorld() == evt.getWorld() && p.equals(evt.getPos())) {
                            // Bingo!
                            MobSpawnerBaseLogic logic = spawner.getSpawnerBaseLogic();
                            ResourceLocation entityName = getEntityName(logic);
                            if (entityName != null && !isBlackListed(entityName)) {
                                final CapturedMob capturedMob = CapturedMob.create(entityName);
                                if (capturedMob != null) {
                                    evt.getDrops().add(capturedMob.toStack(ModObject.itemBrokenSpawner.getItemNN(), 0, 1));
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
            // Risky recovery failed. Happens.
            }
        }
    }
}
Also used : MobSpawnerBaseLogic(net.minecraft.tileentity.MobSpawnerBaseLogic) BlockMobSpawner(net.minecraft.block.BlockMobSpawner) CapturedMob(crazypants.enderio.util.CapturedMob) ResourceLocation(net.minecraft.util.ResourceLocation) ModObject(crazypants.enderio.base.init.ModObject) BlockPos(net.minecraft.util.math.BlockPos) TileEntityMobSpawner(net.minecraft.tileentity.TileEntityMobSpawner) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

BlockMobSpawner (net.minecraft.block.BlockMobSpawner)3 TileEntityMobSpawner (net.minecraft.tileentity.TileEntityMobSpawner)3 CapturedMob (crazypants.enderio.util.CapturedMob)2 ItemStack (net.minecraft.item.ItemStack)2 MobSpawnerBaseLogic (net.minecraft.tileentity.MobSpawnerBaseLogic)2 TileEntity (net.minecraft.tileentity.TileEntity)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 BlockPos (net.minecraft.util.math.BlockPos)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 ModObject (crazypants.enderio.base.init.ModObject)1 List (java.util.List)1 Block (net.minecraft.block.Block)1 BlockChest (net.minecraft.block.BlockChest)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityList (net.minecraft.entity.EntityList)1 IInventory (net.minecraft.inventory.IInventory)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 TileEntityChest (net.minecraft.tileentity.TileEntityChest)1 StructureBoundingBox (net.minecraft.world.gen.structure.StructureBoundingBox)1 BlockRotationProcessor (net.minecraft.world.gen.structure.template.BlockRotationProcessor)1