Golitan11 Posted April 3, 2013 Posted April 3, 2013 I am running Forge 1.5.1 and I would like that when you mine a certain ore and then you get the block in your inventory, you get damaged and if you keep it too long, you will die (just like hunger damage). Thanks! Quote
Draco18s Posted April 3, 2013 Posted April 3, 2013 You can't do it as a Block. You'll need to have the ore block itself drop an Item version, which can damage the player. The Item class has a function called...onUpdate that will let you do what you need to. Here's an example: public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5) { double r = new Random().nextDouble(); if(r < 0.005 * par1ItemStack.stackSize) { EntityPlayer p = (EntityPlayer)par3Entity; p.attackEntityFrom(DamageSource.generic, 1); } } I used a custom damage source ("cold") but I've replaced it above with Generic damage. This code is great for stacking items, as the stack will only hurt you once. What this does is increase the frequency that the player takes damage (rather than the amount) based on stack size. Figure that that function is called 40 times a second when calculating how often you want the player to take damage (and there's a 1-2 second damage immunity effect after taking damage). Which is why the random chance is so small (half a percent per item in the stack). This code also means that roughly speaking, having 10 items in a stack is as bad for your health as 10 single items. Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.