Skip to content

Instantly share code, notes, and snippets.

@Choonster
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Choonster/8ae9cd7264f9de6de18e to your computer and use it in GitHub Desktop.
Save Choonster/8ae9cd7264f9de6de18e to your computer and use it in GitHub Desktop.
Minecraft Forge 1.7.10-10.13.2.1236 - An example of an event handler that has a chance of catching Stone on fire when it's left clicked with an Iron Pickaxe
package com.choonster.testmod2.event;
import com.choonster.testmod2.Logger;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import java.util.Random;
public class BlockEventHandler {
private Random random = new Random();
@SubscribeEvent
public void onBlockLeftClicked(PlayerInteractEvent event) {
if (event.action != PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) return;
ItemStack heldItem = event.entityPlayer.getHeldItem();
Block block = event.world.getBlock(event.x, event.y, event.z);
// If the block clicked was Stone, the player was holding an Iron Pickaxe and a random integer from 0 (inclusive) to 2 (exclusive) is 0 (50% chance)
if (block == Blocks.stone && heldItem != null && heldItem.getItem() == Items.iron_pickaxe && random.nextInt(2) == 0) {
ForgeDirection direction = ForgeDirection.getOrientation(event.face); // Convert the numeric face to a ForgeDirection
int fireX = event.x + direction.offsetX, fireY = event.y + direction.offsetY, fireZ = event.z + direction.offsetZ; // Offset the block's coordinates according to the direction
if (event.world.isAirBlock(fireX, fireY, fireZ)) { // If the block at the new coordinates is Air
event.world.setBlock(fireX, fireY, fireZ, Blocks.fire); // Replace it with Fire
event.useBlock = Event.Result.DENY; // Prevent the Fire from being extinguished (also prevents Block#onBlockClicked from being called)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment