Java Code Examples for net.minecraftforge.event.world.BlockEvent#BreakEvent

The following examples show how to use net.minecraftforge.event.world.BlockEvent#BreakEvent . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArenaManager.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SubscribeEvent
	public void blockBreak(BlockEvent.BreakEvent event) {
		for (Arena arena : arenas) {
			if (!arena.getPlayers().contains(event.getPlayer().getUniqueID()) || arena.getWorld() != event.getWorld())
				continue;

			if (!event.getWorld().isRemote && arena.getBoss() instanceof EntityZachriel)
				((EntityZachriel) arena.getBoss()).nemezDrive.trackBlock(event.getPos(), event.getState());


//			if (!event.getWorld().isRemote)
//				for (ZachTimeManager timeManager : zachHourGlasses) {
//					if (timeManager.getEntityZachriel().getEntityId() == arena.getBossID()) {
//						timeManager.trackBlock(event.getState(), event.getPos());
//					}
//				}
		}
	}
 
Example 2
Source File: BlockUtils.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tries breaking a block safely and fires an event for it.
 *
 * @return Whether the block was successfully broken
 */
public static boolean breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nullable IBlockState oldState, @Nonnull EntityPlayerMP player) {
	if (!world.isBlockLoaded(pos)) return false;

	if (!(player instanceof FakePlayer) && !hasEditPermission(pos, player)) return false;

	if (oldState == null) oldState = world.getBlockState(pos);

	BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, oldState, player);
	MinecraftForge.EVENT_BUS.post(event);

	if (event.isCanceled()) return false;

	TileEntity tile = world.getTileEntity(pos);
	Block block = oldState.getBlock();
	if (block.removedByPlayer(oldState, world, pos, player, true)) {
		block.onPlayerDestroy(world, pos, oldState);
		block.harvestBlock(world, player, pos, oldState, tile, player.getHeldItemMainhand());
		world.notifyBlockUpdate(pos, oldState, Blocks.AIR.getDefaultState(), 3);
	} else return false;

	return true;
}
 
Example 3
Source File: EventHandlerWorld.java    From Gadomancy with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SubscribeEvent
public void onBreak(BlockEvent.BreakEvent event) {
    if (!event.world.isRemote) {
        if (event.block == RegisteredBlocks.blockNodeManipulator) {
            TileEntity te = event.world.getTileEntity(event.x, event.y, event.z);
            if (te != null && te instanceof TileNodeManipulator) {
                if (((TileNodeManipulator) te).isInMultiblock())
                    ((TileNodeManipulator) te).breakMultiblock();
            }
        }
        if (event.block == RegisteredBlocks.blockStoneMachine && event.blockMetadata == 5) {
            TileAIShutdown.removeTrackedEntities(event.world, event.x, event.y, event.z);
        }
        if (event.world.provider.dimensionId == ModConfig.dimOuterId) {
            if(event.block == ConfigBlocks.blockEldritchNothing) {
                if(event.getPlayer().capabilities.isCreativeMode && MiscUtils.isANotApprovedOrMisunderstoodPersonFromMoreDoor(event.getPlayer())) return;
                event.setCanceled(true);
                event.getPlayer().addChatMessage(new ChatComponentText(EnumChatFormatting.ITALIC + "" + EnumChatFormatting.GRAY + StatCollector.translateToLocal("gadomancy.eldritch.nobreakPortalNothing")));
            }
        }
    }
}
 
Example 4
Source File: BlockManipulator.java    From OpenModsLib with MIT License 6 votes vote down vote up
public boolean remove() {
	if (!world.isBlockLoaded(blockPos)) return false;

	if (spawnProtection) {
		if (!world.isBlockModifiable(player, blockPos)) return false;
	}

	if (eventCheck) {
		final IBlockState blockState = world.getBlockState(blockPos);
		BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, blockPos, blockState, player);
		event.setExpToDrop(0);

		MinecraftForge.EVENT_BUS.post(event);

		if (event.isCanceled()) return false;
	}

	if (silentTeRemove) world.removeTileEntity(blockPos);

	return world.setBlockToAir(blockPos);
}
 
Example 5
Source File: ModGenuinePeoplePersonalities.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void onBlockDestroyed(BlockEvent.BreakEvent event) {
    EventData data = new EventData(event);
    if (data.getPlayer() == null || data.getPlayer().world.isRemote) return;
    if (!testCooldown(data.getPlayer())) return;
    if (generateComplaint(StringType.BREAK, data.getPlayer(), data.getBlock(), data)) {
        resetCooldown(data.getPlayer());
    }
}
 
Example 6
Source File: NearbySmeltCommandsImplementation.java    From malmo with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onBlockDestroy(BlockEvent.BreakEvent event) {
    if (!event.isCanceled() && event.getState().getBlock() instanceof BlockFurnace)
        for (int i = furnaces.size() - 1; i >= 0; i--)
            if (furnaces.get(i).equals(event.getPos()))
                furnaces.remove(i);
}
 
Example 7
Source File: NearbyCraftCommandsImplementation.java    From malmo with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onBlockDestroy(BlockEvent.BreakEvent event) {
    if (!event.isCanceled() && event.getState().getBlock() instanceof BlockWorkbench)
        for (int i = craftingTables.size() - 1; i >= 0; i--)
            if (craftingTables.get(i).equals(event.getPos()))
                craftingTables.remove(i);
}
 
Example 8
Source File: Ticker.java    From MyTown2 with The Unlicense 5 votes vote down vote up
@SubscribeEvent
public void onPlayerBreaksBlock(BlockEvent.BreakEvent ev) {
    if (VisualsHandler.instance.isBlockMarked(ev.x, ev.y, ev.z, ev.world.provider.dimensionId, (EntityPlayerMP) ev.getPlayer())) {
        // Cancel event if it's a border that has been broken
        ev.setCanceled(true);
    }
}
 
Example 9
Source File: ChiselLeftClick.java    From Chisel with GNU General Public License v2.0 5 votes vote down vote up
@SubscribeEvent
public void onBlockBreak(BlockEvent.BreakEvent event)
{
    if(!Configurations.enableChiseling) return;
    EntityPlayer player = event.getPlayer();
    ItemStack stack = player.getHeldItem();
    if(stack == null || stack.getItem() != Chisel.chisel) return;

    event.setCanceled(true);
}
 
Example 10
Source File: FakePlayerItemInWorldManager.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Attempts to harvest a block at the given coordinate
 */
@Override
public boolean tryHarvestBlock(int x, int y, int z){
    BlockEvent.BreakEvent event = ForgeHooks.onBlockBreakEvent(theWorld, getGameType(), thisPlayerMP, x, y, z);
    if(event.isCanceled()) {
        return false;
    } else {
        ItemStack stack = thisPlayerMP.getCurrentEquippedItem();
        if(stack != null && stack.getItem().onBlockStartBreak(stack, x, y, z, thisPlayerMP)) {
            return false;
        }
        Block block = theWorld.getBlock(x, y, z);
        int l = theWorld.getBlockMetadata(x, y, z);
        theWorld.playAuxSFXAtEntity(thisPlayerMP, 2001, x, y, z, Block.getIdFromBlock(block) + (theWorld.getBlockMetadata(x, y, z) << 12));
        boolean flag = false;

        ItemStack itemstack = thisPlayerMP.getCurrentEquippedItem();

        if(itemstack != null) {
            itemstack.func_150999_a(theWorld, block, x, y, z, thisPlayerMP);

            if(itemstack.stackSize == 0) {
                thisPlayerMP.destroyCurrentEquippedItem();
            }
        }

        if(removeBlock(x, y, z)) {
            block.harvestBlock(theWorld, thisPlayerMP, x, y, z, l);
            flag = true;
        }

        // Drop experience
        if(!isCreative() && flag && event != null) {
            block.dropXpOnBlockBreak(theWorld, x, y, z, event.getExpToDrop());
        }
        drone.addAir(null, -PneumaticValues.DRONE_USAGE_DIG);
        return true;
    }
}
 
Example 11
Source File: BreakBlockAction.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
public List<EntityItem> usePlayer(OpenModsFakePlayer fakePlayer) {
	if (!worldObj.isBlockModifiable(fakePlayer, blockPos)) return Lists.newArrayList();

	// this mirrors PlayerInteractionManager.tryHarvestBlock
	final IBlockState state = worldObj.getBlockState(blockPos);

	fakePlayer.inventory.currentItem = 0;
	selectTool(state, fakePlayer);

	final CaptureContext dropsCapturer = DropCapture.instance.start(blockPos);

	final List<EntityItem> drops;
	try {
		BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(worldObj, blockPos, state, fakePlayer);
		if (MinecraftForge.EVENT_BUS.post(event)) return Lists.newArrayList();

		final TileEntity te = worldObj.getTileEntity(blockPos); // OHHHHH YEEEEAAAH

		boolean canHarvest = state.getBlock().canHarvestBlock(worldObj, blockPos, fakePlayer);
		boolean isRemoved = removeBlock(fakePlayer, blockPos, state, canHarvest);
		if (isRemoved && canHarvest) {
			state.getBlock().harvestBlock(worldObj, fakePlayer, blockPos, state, te, stackToUse);
			worldObj.playEvent(fakePlayer, 2001, blockPos, Block.getStateId(state));
		}

	} finally {
		drops = dropsCapturer.stop();
	}

	return drops;
}
 
Example 12
Source File: ForgeMod.java    From BlueMap with MIT License 4 votes vote down vote up
@SubscribeEvent
public void onBlockBreak(BlockEvent.BreakEvent evt) {
	onBlockChange(evt);
}
 
Example 13
Source File: MixinServerPlayerInteractionManager.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Inject(method = "tryBreakBlock", at = @At("HEAD"), cancellable = true)
private void hookBreakBlock(BlockPos pos, CallbackInfoReturnable<Boolean> callback) {
	boolean preCancelEvent = false;

	ItemStack itemstack = player.getMainHandStack();

	if (!itemstack.isEmpty() && !itemstack.getItem().canMine(world.getBlockState(pos), world, pos, player)) {
		preCancelEvent = true;
	}

	// method_21701 => canMine
	// Isn't the function really canNotMine?

	if (player.method_21701(world, pos, gameMode)) {
		preCancelEvent = true;
	}

	// Tell client the block is gone immediately then process events
	if (world.getBlockEntity(pos) == null) {
		player.networkHandler.sendPacket(new BlockUpdateS2CPacket(EmptyBlockView.INSTANCE, pos));
	}

	// Post the block break event
	BlockState state = world.getBlockState(pos);
	BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, player);
	event.setCanceled(preCancelEvent);
	MinecraftForge.EVENT_BUS.post(event);

	// Handle if the event is canceled
	if (event.isCanceled()) {
		// Let the client know the block still exists
		player.networkHandler.sendPacket(new BlockUpdateS2CPacket(world, pos));

		// Update any block entity data for this block
		BlockEntity entity = world.getBlockEntity(pos);

		if (entity != null) {
			BlockEntityUpdateS2CPacket packet = entity.toUpdatePacket();

			if (packet != null) {
				player.networkHandler.sendPacket(packet);
			}
		}

		callback.setReturnValue(false);
	} else if (event.getExpToDrop() != 0) {
		// TODO: Drop experience
		throw new UnsupportedOperationException("Cannot drop exp from a BreakEvent yet");
	}
}
 
Example 14
Source File: Events.java    From XRay-Mod with GNU General Public License v3.0 4 votes vote down vote up
@SubscribeEvent
public static void pickupItem( BlockEvent.BreakEvent event ) {
	RenderEnqueue.checkBlock( event.getPos(), event.getState(), false);
}
 
Example 15
Source File: ProtectionHandlers.java    From MyTown2 with The Unlicense 4 votes vote down vote up
@SuppressWarnings("unchecked")
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onPlayerBreaksBlock(BlockEvent.BreakEvent ev) {
    if(ev.world.isRemote || ev.isCanceled()) {
        return;
    }

    if(ev.getPlayer() instanceof FakePlayer) {
        if(!ProtectionManager.getFlagValueAtLocation(FlagType.FAKERS, ev.world.provider.dimensionId, ev.x, ev.y, ev.z)) {
            ev.setCanceled(true);
        }
    } else {
        Resident res = MyTownUniverse.instance.getOrMakeResident(ev.getPlayer());

        if (!MyTownUniverse.instance.blocks.contains(ev.world.provider.dimensionId, ev.x >> 4, ev.z >> 4)) {
            int range = Config.instance.placeProtectionRange.get();
            Volume breakBox = new Volume(ev.x-range, ev.y-range, ev.z-range, ev.x+range, ev.y+range, ev.z+range);

            if(!ProtectionManager.checkBlockBreak(ev.block)) {
                if(!ProtectionManager.hasPermission(res, FlagType.MODIFY, ev.world.provider.dimensionId, breakBox)) {
                    ev.setCanceled(true);
                    return;
                }
            }

        } else {
            if (!ProtectionManager.checkBlockBreak(ev.block)) {
                if(!ProtectionManager.hasPermission(res, FlagType.MODIFY, ev.world.provider.dimensionId, ev.x, ev.y, ev.z)) {
                    ev.setCanceled(true);
                    return;
                }
            }
        }

        if(ev.getPlayer().getHeldItem() != null) {
            ProtectionManager.checkBreakWithItem(ev.getPlayer().getHeldItem(), res, new BlockPos(ev.x, ev.y, ev.z, ev.world.provider.dimensionId), ev);
        }
    }

    if (!ev.isCanceled() && ev.block instanceof ITileEntityProvider) {
        TileEntity te = ((ITileEntityProvider) ev.block).createNewTileEntity(ev.world, ev.blockMetadata);
        if(te != null && ProtectionManager.isOwnable(te.getClass())) {
            te = ev.world.getTileEntity(ev.x, ev.y, ev.z);
            ownedTileEntities.remove(te);
            MyTown.instance.LOG.info("Removed te {}", te.toString());
        }
    }
}