I've been at it since monday and I've gotten this far:
public class MyEventHandler
{
@SubscribeEvent
public void debark(HarvestDropsEvent event)
{
Right click on birch wood with an axe and replace it with a debarked birch log. Also drop item birch bark on the ground.
}
Well, you don't check for right-clicks there, but you shouldn't have to. The event is called right before the block gets chopped by the axe. The event has a number of fields with data about the event you can use.
In this case, you should use the event.getState() method to get an IBlockState. From that need to check the block type, so you can use event.getState().getBock() and test if it is instanceof BlockLog and whether it has property BlockOldLog.VARIANT of type BlockPlanks.EnumType.BIRCH. if you need the player to have a special axe, you can check for it by using event.getHarvester() which gives the player, and check the hand to see if it has the axe in it.
If that is true, then you need to place a de-barked block at the same position. i think you can just set the state to but if you'd rather place the block through a placement method you can get the world from event.getWorld() and get the block position with event.getPos().
To change what gets dropped, you can use the event.getDrops() method to get a list of ItemStack and you can use regular Java approach to clear that list and add your bark item (as an item stack).
My problem seems to be much bigger than I thought. One problem I keep encountering is that the tutorials tell you how to create an event handler assuming that you know and have done X amount of things. It's really problematic for people who know absolutely nothing about coding like me.
Something I'm beginning to suspect lately:
Do I need to import something for my methods to work?! It keeps saying my method package is not there and that left me thinking. Package? You mean like an import?
Wow I went to the forge forums thinking that was my best bet but I saw replies saying they no longer support 1.7.10 which is ok I guess. I've spent 2-4 hours a day researching this stuff for almost 3 weeks with no luck. On monday it will be my 3rd week and I still can't make an event handler. Something is wrong and not even coping and pasting a practice event handler seems to work. I am missing packages it says, but the internet is void of this information.
I cant find a list of events, a list of imports, or any visual help on google, mcforums, mcreator coding forums, forgeforums, planetminecraft, or youtube. Even if someone helps me create this event I'm still doomed. There is no info I can read from to learn how to use new events.
All I ask if anyone could help me is to show me where I can go to read about this stuff and learn. I can't put 2+2 together because I can't find anywhere to learn it from. My very last hope is reddit. I hope to hear from anyone soon. Thanks
This is it!!! Thank you so much. I've finally found the answer to my problem at just a glance, and will read it all to learn it all. This is simple the most complete guide I've seen. Once again thank you so much! I'll favorite all of this.
I'm exhausted... This is my wip Handler. This has my mod completely at a stall. If I can't do this I'm gonna have to change my mod idea to something more fitting of my abilities.
I know I have some imports I dont need, but I have it there because I will be doing some other stuff elsewhere and I don't wanna forget the names for now. Ok now I'm trying to make a test event handler to make sand drop sticks when broken. I have not finished, and was trying to correct the many errors I had (mostly fixed importing stuff), however this last one error got me on the ropes and I have no way to fix it.
Gradle says:
myVanillaEdits.java:35: error: cannot find symbol if(event.state.getBlock() == Blocks.sand) ^ symbol: variable state location: variable event of type HarvestDropsEvent 1 error
Am I mistakenly using code from another MC version? This makes no sense. I could never find a 'state' variable that extends from HarvestDropsEvent. I just coded it like that because it's what I saw other people do.
Yes. In your code "state" is a direct field reference. In newer versions of Forge, it was changed to a getter function getState(). Using a getter function is considered better program practice.
Note that you should have been able to figure this out by letting Eclipse help you. In Eclipse it should have said there was an error there -- I'm not even sure how you ran the code as it will usually tell you there are problems if you try to run it. In Eclipse if you hover over the error (in this case the word "state" should have had a red underline) it will give you suggestions on how to fix the problem.
Also, the Minecraft and the Forge code change frequently. So if you have code that was supposed to work but some method or field isn't recognized, you can look at the Type Hierarchy of the class and it will show all the methods and fields available. Usually it is fairly obvious what the new method is based on the name. For example, I think if you looked at the BlockEvent class' Type Hierarchy you probably could have guessed that the getState() method might be related to your problem.
Lastly, you can look at the code for BlockEvent directly and look for the state field. If you did, you'd see that it is (now) private meaning you couldn't access it directly and you should know then that you need to look for a getter method for it.
Anyway, thought you might want some tips since this is a common thing you'll face in modding.
The Meaning of Life, the Universe, and Everything.
Location:
Colorado
Join Date:
3/27/2016
Posts:
208
Location:
Colorado
Minecraft:
ProfessorOro
Discord:
gargatheoro
PMC:
GargatheOro
Member Details
Reply with the classes you have:
Main class
Client Proxy
Common Proxy
Event Handler (Obviously)
Those are the core classes. You can add items in main but I prefer to make a separate class for it. I can't really code that well either, so I can offer little help but I am good at setting up mods. Also, don't add imports before you use them. Simply press ctrl + shift + O to auto import everything.
You might want something like:
onItemRightClick
if (handheld item is axe) {
if (hovered over block is birch wood) {
(do what jabelar said to replace the block)
(do what he said to drop the item)
(if you want, you can make the axe lose one point of durability here)
}
}
do a right click event, not a harvest event or else it will fire when you pick up an item.
Ok let me explain why I am having so much trouble. Firstly I am not using eclipse to build my code. Why? Because I can't set up my workspace because of this:
I am using the mcreator code viewing tab which allows you to write code but not help you build code like in eclipse. I refrained from saying that for obvious reasons. I've read all the hate all around the forum when someone mentions mcreator. The problem is atm it's the only tool I have for learning code until I figure out how to set up eclipse.
I DO wanna use eclipse but its broken in my computer and I don't know how to fix it.
Thanks for the reply Arisa. I will add that to my notes and give it a shot.
The Meaning of Life, the Universe, and Everything.
Location:
Colorado
Join Date:
3/27/2016
Posts:
208
Location:
Colorado
Minecraft:
ProfessorOro
Discord:
gargatheoro
PMC:
GargatheOro
Member Details
I will help you with Eclipse:
Create a new folder. Title it what you like. I assume this is your Minecraft Modding folder. That forge package needs to be extracted into the folder. Once it is extracted, run the command in cmd. If it opens powershell, then hit windows + R, and in the text box that pops up, type cmd. Then, you are going to type what is in the quotes, but not the quotes: "CD C:\Users\Jonathan\Downloads\Minecraft Modding".
I think your issue is you are trying to run the command in the forge zip folder when you actually need to extract the contents and run it in the folder you extracted the zip into.
Thanks so far so good. I was trying to do this command I saw online from inside the forge folder I extracted ''gradlew setupDecompWorkspace'' Do I even do that?
My problem now is when I try to open the ''Minecraft'' folder inside eclipse i get this error:
The project description file (.project) for 'Minecraft' is missing. This file contains important information about the project. The project will not function properly until this file is restored.
Oh wow I think I solved it. Forge doesnt use java 9 I found out. Downloaded and installed JDK for java 8 and its starting to work now. Thanks for the tips!
Well I got the help of eclipse now, but I'm still pretty clueless on how to define 'when breaking a block' I keep getting errors. It's still the same one. I tried state, getState, getState(), drop, Drops, but nothing seems to hint me a clue.
EDIT: The initial method I wanted to do is way too complicated for me to understand right now, so I'm just trying to get a feel of it by having sand drop a stick when broken by the player.
EDIT: The initial method I wanted to use is way too complicated for me right now, so I'm just trying to make sand drop 1 stick when broken by the player. Hopefully I get a better understanding on how to do this with such a simple event handler.
But there are no 'getters'. All of the options eclipse gives me end up in error. They don't relate to HarvestDropsEvent even when that's exactly what eclipse is suggesting me to do, even looking at the hierarchy.
Nothing pops up that would lead me to this logic: if(Me, breakBlock(block=sand) then(sand, drops(Item.Stick)).
Thanks for the help guys. I'm just gonna go play a bunch of mods till I find a mod that does exactly what I need to do and hope it's open source so I can take a look at it, and then try to assemble something myself. I'm not ready to assemble code of my own yet. I don't understand.
Ok 8 months later and I am finally getting somewhere. I finally was able to make my first event handlers work on this weekend. I now have a few event handlers that work almost as intended. I'm so hyped up. I understand minecraft code a lot more than earlier this year through trial and error. That being said I'm still no pro and greatly lack on my ability to use eclipse to help me fix my phrases.
I have this code and I'm trying for it to work with any pickaxe, but I dont know how to do it. I only know how to specify for a specific pickaxe such as Items.iron_pickaxe. Could anyone teach me how to make picks, axes, shovels into any_pickaxe for my code?:
@SubscribeEvent
public void onBlockRightClicked(PlayerInteractEvent event) {
if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) return;
if (block == Blocks.grass && heldItem != null && heldItem.getItem() == Items.iron_pickaxe) {
int X = event.x, Y = event.y, Z = event.z;
if (event.world.getBlock(X, Y, Z) == Blocks.grass) {
event.world.setBlock(X, Y, Z, Blocks.stone);
}
}
}
This is test code. When I figure out how to do this I will be able to use this in my more to bring 1.13 stripped log functionality to 1.7.10. I need your help! Thanks
Ok 8 months later and I am finally getting somewhere. I finally was able to make my first event handlers work on this weekend. I now have a few event handlers that work almost as intended. I'm so hyped up. I understand minecraft code a lot more than earlier this year through trial and error. That being said I'm still no pro and greatly lack on my ability to use eclipse to help me fix my phrases.
I have this code and I'm trying for it to work with any pickaxe, but I dont know how to do it. I only know how to specify for a specific pickaxe such as Items.iron_pickaxe. Could anyone teach me how to make picks, axes, shovels into any_pickaxe for my code?:
@SubscribeEvent
public void onBlockRightClicked(PlayerInteractEvent event) {
if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) return;
if (block == Blocks.grass && heldItem != null && heldItem.getItem() == Items.iron_pickaxe) {
int X = event.x, Y = event.y, Z = event.z;
if (event.world.getBlock(X, Y, Z) == Blocks.grass) {
event.world.setBlock(X, Y, Z, Blocks.stone);
}
}
}
This is test code. When I figure out how to do this I will be able to use this in my more to bring 1.13 stripped log functionality to 1.7.10. I need your help! Thanks
Unfortunately you're going to have to hard-code the check for certain tools in.
Meaning that tools/pickaxes that mods add into the game will needed to be supported by the code and not automatically, assuming the tools that mods have don't use a easy system to tell what-is-what (like an interface you can check if the item's class is an instance of.)
I have some code I've written up for you. It explains some of the basics, but if you have questions about something, let me know and I'll try to answer it to the best of my ability.
I've been at it since monday and I've gotten this far:
public class MyEventHandler
{
@SubscribeEvent
public void debark(HarvestDropsEvent event)
{
Right click on birch wood with an axe and replace it with a debarked birch log. Also drop item birch bark on the ground.
}
}
How do I turn that last part to code?
Well, you don't check for right-clicks there, but you shouldn't have to. The event is called right before the block gets chopped by the axe. The event has a number of fields with data about the event you can use.
In this case, you should use the event.getState() method to get an IBlockState. From that need to check the block type, so you can use event.getState().getBock() and test if it is instanceof BlockLog and whether it has property BlockOldLog.VARIANT of type BlockPlanks.EnumType.BIRCH. if you need the player to have a special axe, you can check for it by using event.getHarvester() which gives the player, and check the hand to see if it has the axe in it.
If that is true, then you need to place a de-barked block at the same position. i think you can just set the state to but if you'd rather place the block through a placement method you can get the world from event.getWorld() and get the block position with event.getPos().
To change what gets dropped, you can use the event.getDrops() method to get a list of ItemStack and you can use regular Java approach to clear that list and add your bark item (as an item stack).
Thanks for the reply.
My problem seems to be much bigger than I thought. One problem I keep encountering is that the tutorials tell you how to create an event handler assuming that you know and have done X amount of things. It's really problematic for people who know absolutely nothing about coding like me.
Something I'm beginning to suspect lately:
Do I need to import something for my methods to work?! It keeps saying my method package is not there and that left me thinking. Package? You mean like an import?
Wow I went to the forge forums thinking that was my best bet but I saw replies saying they no longer support 1.7.10 which is ok I guess. I've spent 2-4 hours a day researching this stuff for almost 3 weeks with no luck. On monday it will be my 3rd week and I still can't make an event handler. Something is wrong and not even coping and pasting a practice event handler seems to work. I am missing packages it says, but the internet is void of this information.
I cant find a list of events, a list of imports, or any visual help on google, mcforums, mcreator coding forums, forgeforums, planetminecraft, or youtube. Even if someone helps me create this event I'm still doomed. There is no info I can read from to learn how to use new events.
All I ask if anyone could help me is to show me where I can go to read about this stuff and learn. I can't put 2+2 together because I can't find anywhere to learn it from. My very last hope is reddit. I hope to hear from anyone soon. Thanks
I have a tutorial on event handling here: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-event-handling.html
Before you even do that you might want to check out my other tutorials on how to set things up and such: http://jabelarminecraft.blogspot.com/
This is it!!! Thank you so much. I've finally found the answer to my problem at just a glance, and will read it all to learn it all. This is simple the most complete guide I've seen. Once again thank you so much! I'll favorite all of this.
I'm exhausted... This is my wip Handler. This has my mod completely at a stall. If I can't do this I'm gonna have to change my mod idea to something more fitting of my abilities.
import net.minecraftforge.event.terraingen.PopulateChunkEvent;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraft.world.World;
import net.minecraft.server.MinecraftServer;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import java.util.Random;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.FMLCommonHandler;
public class myVanillaEdits
{
public static class MyBlockEventClass
{
@SubscribeEvent
public void onBlockBreak(BlockEvent.HarvestDropsEvent event)
{
if(event.state.getBlock() == Blocks.sand)
{
}
}
public void load()
{
MinecraftForge.EVENT_BUS.register(new MyBlockEventClass());
}
}
I know I have some imports I dont need, but I have it there because I will be doing some other stuff elsewhere and I don't wanna forget the names for now. Ok now I'm trying to make a test event handler to make sand drop sticks when broken. I have not finished, and was trying to correct the many errors I had (mostly fixed importing stuff), however this last one error got me on the ropes and I have no way to fix it.
Gradle says:
myVanillaEdits.java:35: error: cannot find symbol
if(event.state.getBlock() == Blocks.sand)
^
symbol: variable state
location: variable event of type HarvestDropsEvent
1 error
Am I mistakenly using code from another MC version? This makes no sense. I could never find a 'state' variable that extends from HarvestDropsEvent. I just coded it like that because it's what I saw other people do.
Las
Yes. In your code "state" is a direct field reference. In newer versions of Forge, it was changed to a getter function getState(). Using a getter function is considered better program practice.
Note that you should have been able to figure this out by letting Eclipse help you. In Eclipse it should have said there was an error there -- I'm not even sure how you ran the code as it will usually tell you there are problems if you try to run it. In Eclipse if you hover over the error (in this case the word "state" should have had a red underline) it will give you suggestions on how to fix the problem.
Also, the Minecraft and the Forge code change frequently. So if you have code that was supposed to work but some method or field isn't recognized, you can look at the Type Hierarchy of the class and it will show all the methods and fields available. Usually it is fairly obvious what the new method is based on the name. For example, I think if you looked at the BlockEvent class' Type Hierarchy you probably could have guessed that the getState() method might be related to your problem.
Lastly, you can look at the code for BlockEvent directly and look for the state field. If you did, you'd see that it is (now) private meaning you couldn't access it directly and you should know then that you need to look for a getter method for it.
Anyway, thought you might want some tips since this is a common thing you'll face in modding.
Reply with the classes you have:
Main class
Client Proxy
Common Proxy
Event Handler (Obviously)
Those are the core classes. You can add items in main but I prefer to make a separate class for it. I can't really code that well either, so I can offer little help but I am good at setting up mods. Also, don't add imports before you use them. Simply press ctrl + shift + O to auto import everything.
You might want something like:
onItemRightClick
if (handheld item is axe) {
if (hovered over block is birch wood) {
(do what jabelar said to replace the block)
(do what he said to drop the item)
(if you want, you can make the axe lose one point of durability here)
}
}
do a right click event, not a harvest event or else it will fire when you pick up an item.
An SF Towny server! Join our Discord: Click here
Ok let me explain why I am having so much trouble. Firstly I am not using eclipse to build my code. Why? Because I can't set up my workspace because of this:
C:\Users\Jonathan\Downloads\Minecraft Modding\forge-1.7.10-10.13.4.1558-1.7.10-s
rc>gradlew setupDecompWorkspace
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '9.0.4'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
C:\Users\Jonathan\Downloads\Minecraft Modding\forge-1.7.10-10.13.4.1558-1.7.10-s
rc>
I am using the mcreator code viewing tab which allows you to write code but not help you build code like in eclipse. I refrained from saying that for obvious reasons. I've read all the hate all around the forum when someone mentions mcreator. The problem is atm it's the only tool I have for learning code until I figure out how to set up eclipse.
I DO wanna use eclipse but its broken in my computer and I don't know how to fix it.
Thanks for the reply Arisa. I will add that to my notes and give it a shot.
I will help you with Eclipse:
Create a new folder. Title it what you like. I assume this is your Minecraft Modding folder. That forge package needs to be extracted into the folder. Once it is extracted, run the command in cmd. If it opens powershell, then hit windows + R, and in the text box that pops up, type cmd. Then, you are going to type what is in the quotes, but not the quotes: "CD C:\Users\Jonathan\Downloads\Minecraft Modding".
I think your issue is you are trying to run the command in the forge zip folder when you actually need to extract the contents and run it in the folder you extracted the zip into.
Tell me how you do. GL
- GargatheOro
An SF Towny server! Join our Discord: Click here
Thanks so far so good. I was trying to do this command I saw online from inside the forge folder I extracted ''gradlew setupDecompWorkspace'' Do I even do that?
My problem now is when I try to open the ''Minecraft'' folder inside eclipse i get this error:
The project description file (.project) for 'Minecraft' is missing. This file contains important information about the project. The project will not function properly until this file is restored.
Any command I type leads to:
C:\Users\Jonathan\Downloads\Minecraft Modding\forge-1.7.10-10.13.4.1558-1.7.10-s
rc>gradlew setupDecompWorkspace
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '9.0.4'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
C:\Users\Jonathan\Downloads\Minecraft Modding\forge-1.7.10-10.13.4.1558-1.7.10-s
rc>
Oh ok. I have had that happen before. Simply delete everything in that folder and restart.
An SF Towny server! Join our Discord: Click here
Oh wow I think I solved it. Forge doesnt use java 9 I found out. Downloaded and installed JDK for java 8 and its starting to work now. Thanks for the tips!
Lol good catch! Good luck on your developing! I should be able to help you a little with stuff like this since I code for 1.7.10.
Tip: Add "Resolved" in the tags if your issue is solved. If not, next order of business? Still need help translating that stuff into code?
An SF Towny server! Join our Discord: Click here
Well I got the help of eclipse now, but I'm still pretty clueless on how to define 'when breaking a block' I keep getting errors. It's still the same one. I tried state, getState, getState(), drop, Drops, but nothing seems to hint me a clue.
package mod.myevents.main;
import java.util.Collection;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@Mod(modid = "myEvents", name = "myEvents", version = "Alpha 0.0")
public class myEvents {
public myEvents() {
}
@EventHandler
public static void PreInit(FMLPreInitializationEvent event) {
}
@EventHandler
public static void Init(FMLInitializationEvent event) {
}
public void load() {
MinecraftForge.EVENT_BUS.register(new myEvents());
}
@SubscribeEvent
public void onBlockBreak(BlockEvent.HarvestDropsEvent event) {
if(event.getState().getBlock() == Blocks.sand) {
event.drops.addAll((Collection<? extends ItemStack>) Items.stick);
}
}
}
EDIT: The initial method I wanted to do is way too complicated for me to understand right now, so I'm just trying to get a feel of it by having sand drop a stick when broken by the player.
EDIT: The initial method I wanted to use is way too complicated for me right now, so I'm just trying to make sand drop 1 stick when broken by the player. Hopefully I get a better understanding on how to do this with such a simple event handler.
But there are no 'getters'. All of the options eclipse gives me end up in error. They don't relate to HarvestDropsEvent even when that's exactly what eclipse is suggesting me to do, even looking at the hierarchy.
@SubscribeEvent
public void onBlockBreak(BlockEvent.HarvestDropsEvent event) {
if(event.drop() == Blocks.sand) {
event.drops.add(Items.stick);
}
}
Nothing pops up that would lead me to this logic: if(Me, breakBlock(block=sand) then(sand, drops(Item.Stick)).
Thanks for the help guys. I'm just gonna go play a bunch of mods till I find a mod that does exactly what I need to do and hope it's open source so I can take a look at it, and then try to assemble something myself. I'm not ready to assemble code of my own yet. I don't understand.
Ok 8 months later and I am finally getting somewhere. I finally was able to make my first event handlers work on this weekend. I now have a few event handlers that work almost as intended. I'm so hyped up. I understand minecraft code a lot more than earlier this year through trial and error. That being said I'm still no pro and greatly lack on my ability to use eclipse to help me fix my phrases.
I have this code and I'm trying for it to work with any pickaxe, but I dont know how to do it. I only know how to specify for a specific pickaxe such as Items.iron_pickaxe. Could anyone teach me how to make picks, axes, shovels into any_pickaxe for my code?:
@SubscribeEvent
public void onBlockRightClicked(PlayerInteractEvent event) {
if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) return;
ItemStack heldItem = event.entityPlayer.getHeldItem();
Block block = event.world.getBlock(event.x, event.y, event.z);
if (block == Blocks.grass && heldItem != null && heldItem.getItem() == Items.iron_pickaxe) {
int X = event.x, Y = event.y, Z = event.z;
if (event.world.getBlock(X, Y, Z) == Blocks.grass) {
event.world.setBlock(X, Y, Z, Blocks.stone);
}
}
}
This is test code. When I figure out how to do this I will be able to use this in my more to bring 1.13 stripped log functionality to 1.7.10. I need your help! Thanks
Unfortunately you're going to have to hard-code the check for certain tools in.
Meaning that tools/pickaxes that mods add into the game will needed to be supported by the code and not automatically, assuming the tools that mods have don't use a easy system to tell what-is-what (like an interface you can check if the item's class is an instance of.)
I have some code I've written up for you. It explains some of the basics, but if you have questions about something, let me know and I'll try to answer it to the best of my ability.
https://pastebin.com/RksmmCSq