Search in sources :

Example 1 with BreakEvent

use of net.minecraftforge.event.world.BlockEvent.BreakEvent in project BuildCraft by BuildCraft.

the class BlockUtil method breakBlock.

public static boolean breakBlock(WorldServer world, BlockPos pos, NonNullList<ItemStack> drops, BlockPos ownerPos, GameProfile owner) {
    FakePlayer fakePlayer = BuildCraftAPI.fakePlayerProvider.getFakePlayer(world, owner, ownerPos);
    BreakEvent breakEvent = new BreakEvent(world, pos, world.getBlockState(pos), fakePlayer);
    MinecraftForge.EVENT_BUS.post(breakEvent);
    if (breakEvent.isCanceled()) {
        return false;
    }
    if (!world.isAirBlock(pos) && !world.isRemote && world.getGameRules().getBoolean("doTileDrops")) {
        drops.addAll(getItemStackFromBlock(world, pos, owner));
    }
    world.setBlockToAir(pos);
    return true;
}
Also used : BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 2 with BreakEvent

use of net.minecraftforge.event.world.BlockEvent.BreakEvent in project BuildCraft by BuildCraft.

the class BlockUtil method harvestBlock.

public static boolean harvestBlock(WorldServer world, BlockPos pos, @Nonnull ItemStack tool, GameProfile owner) {
    FakePlayer fakePlayer = getFakePlayerWithTool(world, tool, owner);
    BreakEvent breakEvent = new BreakEvent(world, pos, world.getBlockState(pos), fakePlayer);
    MinecraftForge.EVENT_BUS.post(breakEvent);
    if (breakEvent.isCanceled()) {
        return false;
    }
    IBlockState state = world.getBlockState(pos);
    if (!state.getBlock().canHarvestBlock(world, pos, fakePlayer)) {
        return false;
    }
    state.getBlock().onBlockHarvested(world, pos, state, fakePlayer);
    state.getBlock().harvestBlock(world, fakePlayer, pos, state, world.getTileEntity(pos), tool);
    world.setBlockToAir(pos);
    return true;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 3 with BreakEvent

use of net.minecraftforge.event.world.BlockEvent.BreakEvent in project BuildCraft by BuildCraft.

the class BlockUtil method destroyBlock.

public static boolean destroyBlock(WorldServer world, BlockPos pos, @Nonnull ItemStack tool, GameProfile owner) {
    FakePlayer fakePlayer = getFakePlayerWithTool(world, tool, owner);
    BreakEvent breakEvent = new BreakEvent(world, pos, world.getBlockState(pos), fakePlayer);
    MinecraftForge.EVENT_BUS.post(breakEvent);
    if (breakEvent.isCanceled()) {
        return false;
    }
    world.destroyBlock(pos, true);
    return true;
}
Also used : BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Example 4 with BreakEvent

use of net.minecraftforge.event.world.BlockEvent.BreakEvent in project ArsMagica2 by Mithion.

the class Appropriation method applyEffectBlock.

@Override
public boolean applyEffectBlock(ItemStack stack, World world, int blockx, int blocky, int blockz, int blockFace, double impactX, double impactY, double impactZ, EntityLivingBase caster) {
    if (!(caster instanceof EntityPlayer))
        return false;
    ItemStack originalSpellStack = getOriginalSpellStack((EntityPlayer) caster);
    if (originalSpellStack == null) {
        return false;
    }
    if (originalSpellStack.stackTagCompound == null) {
        return false;
    }
    Block block = world.getBlock(blockx, blocky, blockz);
    if (block == null) {
        return false;
    }
    for (String s : AMCore.config.getAppropriationBlockBlacklist()) {
        if (block.getUnlocalizedName() == s) {
            return false;
        }
    }
    if (!world.isRemote) {
        if (originalSpellStack.stackTagCompound.hasKey(storageKey)) {
            if (world.getBlock(blockx, blocky, blockz) == Blocks.air)
                blockFace = -1;
            if (blockFace != -1) {
                switch(blockFace) {
                    case 0:
                        blocky--;
                        break;
                    case 1:
                        blocky++;
                        break;
                    case 2:
                        blockz--;
                        break;
                    case 3:
                        blockz++;
                        break;
                    case 4:
                        blockx--;
                        break;
                    case 5:
                        blockx++;
                        break;
                }
            }
            if (world.isAirBlock(blockx, blocky, blockz) || !world.getBlock(blockx, blocky, blockz).getMaterial().isSolid()) {
                // save current spell
                NBTTagCompound nbt = null;
                if (stack.getTagCompound() != null) {
                    nbt = (NBTTagCompound) stack.getTagCompound().copy();
                }
                EntityPlayerMP casterPlayer = (EntityPlayerMP) DummyEntityPlayer.fromEntityLiving(caster);
                world.captureBlockSnapshots = true;
                restore((EntityPlayer) caster, world, originalSpellStack, blockx, blocky, blockz, impactX, impactY, impactZ);
                world.captureBlockSnapshots = false;
                // save new spell data
                NBTTagCompound newNBT = null;
                if (stack.getTagCompound() != null) {
                    newNBT = (NBTTagCompound) stack.getTagCompound().copy();
                }
                net.minecraftforge.event.world.BlockEvent.PlaceEvent placeEvent = null;
                List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = (List<net.minecraftforge.common.util.BlockSnapshot>) world.capturedBlockSnapshots.clone();
                world.capturedBlockSnapshots.clear();
                // restore original item data for event
                if (nbt != null) {
                    stack.setTagCompound(nbt);
                }
                if (blockSnapshots.size() > 1) {
                    placeEvent = ForgeEventFactory.onPlayerMultiBlockPlace(casterPlayer, blockSnapshots, ForgeDirection.UNKNOWN);
                } else if (blockSnapshots.size() == 1) {
                    placeEvent = ForgeEventFactory.onPlayerBlockPlace(casterPlayer, blockSnapshots.get(0), ForgeDirection.UNKNOWN);
                }
                if (placeEvent != null && (placeEvent.isCanceled())) {
                    // revert back all captured blocks
                    for (net.minecraftforge.common.util.BlockSnapshot blocksnapshot : blockSnapshots) {
                        world.restoringBlockSnapshots = true;
                        blocksnapshot.restore(true, false);
                        world.restoringBlockSnapshots = false;
                    }
                    return false;
                } else {
                    // Change the stack to its new content
                    if (nbt != null) {
                        stack.setTagCompound(newNBT);
                    }
                    for (net.minecraftforge.common.util.BlockSnapshot blocksnapshot : blockSnapshots) {
                        int blockX = blocksnapshot.x;
                        int blockY = blocksnapshot.y;
                        int blockZ = blocksnapshot.z;
                        int metadata = world.getBlockMetadata(blockX, blockY, blockZ);
                        int updateFlag = blocksnapshot.flag;
                        Block oldBlock = blocksnapshot.replacedBlock;
                        Block newBlock = world.getBlock(blockX, blockY, blockZ);
                        if (newBlock != null && !(newBlock.hasTileEntity(metadata))) {
                            // Containers get placed automatically
                            newBlock.onBlockAdded(world, blockX, blockY, blockZ);
                        }
                        world.markAndNotifyBlock(blockX, blockY, blockZ, null, oldBlock, newBlock, updateFlag);
                    }
                }
                world.capturedBlockSnapshots.clear();
            // restore((EntityPlayer)caster, world, originalSpellStack, blockx, blocky, blockz, impactX, impactY, impactZ);
            }
        } else {
            if (block == null || block.getBlockHardness(world, blockx, blocky, blockz) == -1.0f) {
                return false;
            }
            NBTTagCompound data = new NBTTagCompound();
            data.setString(storageType, "block");
            // data.setString("blockName", block.getUnlocalizedName().replace("tile.", ""));
            data.setInteger("blockID", Block.getIdFromBlock(block));
            int meta = world.getBlockMetadata(blockx, blocky, blockz);
            data.setInteger("meta", meta);
            EntityPlayerMP casterPlayer = (EntityPlayerMP) DummyEntityPlayer.fromEntityLiving(caster);
            if (!ForgeEventFactory.doPlayerHarvestCheck(casterPlayer, block, true)) {
                return false;
            }
            BreakEvent event = ForgeHooks.onBlockBreakEvent(world, casterPlayer.theItemInWorldManager.getGameType(), casterPlayer, blockx, blocky, blockz);
            if (event.isCanceled()) {
                return false;
            }
            TileEntity te = world.getTileEntity(blockx, blocky, blockz);
            if (te != null) {
                NBTTagCompound teData = new NBTTagCompound();
                te.writeToNBT(teData);
                data.setTag("tileEntity", teData);
                // remove tile entity first to prevent content dropping which is already saved in the NBT
                try {
                    world.removeTileEntity(blockx, blocky, blockz);
                } catch (Throwable exception) {
                    exception.printStackTrace();
                }
            }
            originalSpellStack.stackTagCompound.setTag(storageKey, data);
            setOriginalSpellStackData((EntityPlayer) caster, originalSpellStack);
            world.setBlockToAir(blockx, blocky, blockz);
        }
    }
    return true;
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ParticleOrbitPoint(am2.particles.ParticleOrbitPoint) BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent) TileEntity(net.minecraft.tileentity.TileEntity) DummyEntityPlayer(am2.utility.DummyEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Block(net.minecraft.block.Block) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) StatList(net.minecraft.stats.StatList) List(java.util.List) ItemStack(net.minecraft.item.ItemStack)

Example 5 with BreakEvent

use of net.minecraftforge.event.world.BlockEvent.BreakEvent in project ArsMagica2 by Mithion.

the class Dig method applyEffectBlock.

@Override
public boolean applyEffectBlock(ItemStack stack, World world, int blockx, int blocky, int blockz, int blockFace, double impactX, double impactY, double impactZ, EntityLivingBase caster) {
    Block block = world.getBlock(blockx, blocky, blockz);
    if (block == Blocks.air) {
        return false;
    }
    TileEntity te = world.getTileEntity(blockx, blocky, blockz);
    if (te != null) {
        if (!AMCore.config.getDigBreaksTileEntities())
            return false;
        if (te instanceof IKeystoneLockable && !KeystoneUtilities.instance.canPlayerAccess((IKeystoneLockable) te, DummyEntityPlayer.fromEntityLiving(caster), KeystoneAccessType.BREAK))
            return false;
    }
    if (disallowedBlocks.contains(block))
        return false;
    if (block.getBlockHardness(world, blockx, blocky, blockz) == -1)
        return false;
    int meta = world.getBlockMetadata(blockx, blocky, blockz);
    int harvestLevel = block.getHarvestLevel(meta);
    int miningLevel = 2 + SpellUtils.instance.countModifiers(SpellModifiers.MINING_POWER, stack, 0);
    if (harvestLevel > miningLevel)
        return false;
    EntityPlayer casterPlayer = DummyEntityPlayer.fromEntityLiving(caster);
    if (ForgeEventFactory.doPlayerHarvestCheck(casterPlayer, block, true)) {
        float xMana = block.getBlockHardness(world, blockx, blocky, blockz) * hardnessManaFactor;
        float xBurnout = ArsMagicaApi.instance.getBurnoutFromMana(xMana);
        if (!world.isRemote) {
            BreakEvent event = ForgeHooks.onBlockBreakEvent(world, ((EntityPlayerMP) casterPlayer).theItemInWorldManager.getGameType(), (EntityPlayerMP) casterPlayer, blockx, blocky, blockz);
            if (event.isCanceled()) {
                return false;
            }
            block.onBlockHarvested(world, blockx, blocky, blockz, meta, casterPlayer);
            boolean flag = block.removedByPlayer(world, casterPlayer, blockx, blocky, blockz, true);
            if (flag) {
                block.onBlockDestroyedByPlayer(world, blockx, blocky, blockz, meta);
                block.harvestBlock(world, casterPlayer, blockx, blocky, blockz, meta);
            }
        }
        ExtendedProperties.For(caster).deductMana(xMana);
        ExtendedProperties.For(caster).addBurnout(xBurnout);
        return true;
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IKeystoneLockable(am2.api.blocks.IKeystoneLockable) Block(net.minecraft.block.Block) DummyEntityPlayer(am2.utility.DummyEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BreakEvent(net.minecraftforge.event.world.BlockEvent.BreakEvent)

Aggregations

BreakEvent (net.minecraftforge.event.world.BlockEvent.BreakEvent)5 FakePlayer (net.minecraftforge.common.util.FakePlayer)3 DummyEntityPlayer (am2.utility.DummyEntityPlayer)2 Block (net.minecraft.block.Block)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 TileEntity (net.minecraft.tileentity.TileEntity)2 IKeystoneLockable (am2.api.blocks.IKeystoneLockable)1 ParticleOrbitPoint (am2.particles.ParticleOrbitPoint)1 List (java.util.List)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 StatList (net.minecraft.stats.StatList)1