Hi all! This tutorial teaches you how to check if the mouse is clicked. Sadly, this only works in singleplayer. This should work for all Minecraft versions, and requires Forge.
For this tutorial to work, you must have a mod properly set up, and a fair amount of Java knowledge.
Step 1 - Register an event handler (if you haven't already):
In your main class, register a event handler in the init method like so:
An error will pop up saying that the class EventHandling doesn't exist. Make a new class called EventHandling. That's it for step 1!
Step 2 - Check for mouse clicks
To check for mouse clicks, first you need to make a method with the annotation
@SubscribeEvent
Then, as the parameter,
LivingUpdateEvent
Inside the method, insert this:
if (event.entityLiving instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) event.entityLiving;
if (Mouse.isButtonDown(1)) {
if (player.getHeldItem.getItem.equals(Items.brick) && player.getHeldItem.getItem != null) {
player.worldObj.spawnEntityInWorld(new EntitySnowball(player.worldObj, player));
}
}
}
Now I'll walk you through what's happening in the code above. First, we check for the player, and make a instance variable of the player. Next, we check if the Mouse's right mouse button is pressed. Use 0 if you want to check for the left mouse button. After that, we get the player's held item, and if it's a brick, the we spawn in a snowball. You can customize this however you want. You could even make a troll apple and when they try to eat it, it spawns a Ender Dragon! The limit is your imagination!
If this tutorial helped you please hit that green like button
How to Check if the Player Clicks With The Mouse
Hi all! This tutorial teaches you how to check if the mouse is clicked. Sadly, this only works in singleplayer. This should work for all Minecraft versions, and requires Forge.
For this tutorial to work, you must have a mod properly set up, and a fair amount of Java knowledge.
Step 1 - Register an event handler (if you haven't already):
In your main class, register a event handler in the init method like so:
An error will pop up saying that the class EventHandling doesn't exist. Make a new class called EventHandling. That's it for step 1!
Step 2 - Check for mouse clicks
EntityPlayer player = (EntityPlayer) event.entityLiving;
if (Mouse.isButtonDown(1)) {
if (player.getHeldItem.getItem.equals(Items.brick) && player.getHeldItem.getItem != null) {
player.worldObj.spawnEntityInWorld(new EntitySnowball(player.worldObj, player));
}
}
}
If this tutorial helped you please hit that green like button
Thanks for reading!
Oops! Yeah I just fixed that problem in the code