Posted July 17, 201510 yr so im making a item that allows flying, but if the item is not in the inventory I want it to disable flying, and Ive tried doing this multiple ways. problem is not matter what I haven't been able to do it. so here is my last attempt at it. if there are any problems with the code please let me know. any help is greatly appreciated. thanks. onUpdate method: public class DnaZeusInfused extends Item { public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) { super.onUpdate(stack, world, entity, par4, par5); { EntityPlayer player = (EntityPlayer) entity; boolean hasItem = player.inventory.hasItem(ModItems.DnaZeusInfused); if (player.inventory.hasItem(ModItems.DnaZeusInfused)) { player.capabilities.allowFlying = true; } else if (hasItem == false) { player.capabilities.allowFlying = false; } if (player.capabilities.isFlying) { player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 1, 1)); player.addPotionEffect(new PotionEffect(Potion.resistance.id, 1, 1)); } else { player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 0, 0)); player.addPotionEffect(new PotionEffect(Potion.resistance.id, 0, 0)); } } } Im serious don't look at it!!
July 17, 201510 yr That's because the Item#onUpdate (and any other such methods, e.g. onArmorTick) are only called while the item is in your inventory. To change something when it is NOT there, you have to be able to check from somewhere else, such as the PlayerTickEvent. http://i.imgur.com/NdrFdld.png[/img]
July 17, 201510 yr Author where can I find a example of a playertickevent current code: public void playerTick(PlayerTickEvent event) { boolean hasItem = event.player.inventory.hasItem(ModItems.DnaZeusInfused); if (event.player.inventory.hasItem(ModItems.DnaZeusInfused)) { event.player.capabilities.allowFlying = true; if (hasItem == false) { event.player.capabilities.allowFlying = false; } } } Im serious don't look at it!!
July 17, 201510 yr Do you know how to use events? You need to put @SubscribeEvent annotation above your method and then register the containing class to the appropriate event bus - in this case, TickEvents are fired on the FML bus: FMLCommonHandler.instance().bus().register(new ClassContainingYourEvent()); Also, you check if the player has the same item twice. hasItem = checkInventory. if (checkInventory again, even though I just checked it...) You may want to consider using more curly braces to make your logic clearer as well. if (condition) do something // vs. if (condition) { do something } The second is much less prone to mistakes, especially as nesting gets deeper. http://i.imgur.com/NdrFdld.png[/img]
July 17, 201510 yr Author ok thanks it works now, well, not exactly the way I wanted it to, if your still flying it doesn't disable your ability to fly, but after you land it does. how should I fix this. Im serious don't look at it!!
July 17, 201510 yr ok thanks it works now, well, not exactly the way I wanted it to, if your still flying it doesn't disable your ability to fly, but after you land it does. how should I fix this. capabilities.isFlying... Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
July 17, 201510 yr Author and what do I do if other flying items don't work now because of the event? also will this cause compatibility errors with other mods? Edit: Should I add potion effects on the server side? Im serious don't look at it!!
July 18, 201510 yr and what do I do if other flying items don't work now because of the event? also will this cause compatibility errors with other mods? Edit: Should I add potion effects on the server side? There is no way for you to know what other mods might be adding flying, but if you and they both code correctly, you should be okay. What I mean is, each mod should be applying player.capabilities.allowFlying=true every tick that it is allowed, and only turning it off ONCE, so if another mod still has a situation that allows flying, you turn it off, but they immediately turn it back on. It gets trickier with setting isFlying to false, because if you do that right away, even if the other mod allows flying again, the player will still fall out of the sky, so you should set isFlying to false on the NEXT tick IF the player is still not allowed to fly. Potions: Yes, apply them on the server. As a general rule, apply EVERYTHING on the server. http://i.imgur.com/NdrFdld.png[/img]
July 19, 201510 yr Author how do I get the next tick of the player may I ask? Im serious don't look at it!!
July 20, 201510 yr how do I get the next tick of the player may I ask? Player tick events??? There's value for each entity of how long it has lived. Now just mpa that value and check in next tick... Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
July 20, 201510 yr Author yeah but it wont just let me add 1 to the time existed. I need an example, please. Im serious don't look at it!!
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.