Posted January 11, 201510 yr How would you check for an item if in the inventory would allow the player to fly. I'm assuming you would do this through the Item class. I tried using the onUpdate as it calls a tick every time the item is in your inventory. Though it doesn't have a Player Entity parameter to allow flying. Any idea's? Thanks.
January 11, 201510 yr the best way to do this is using PlayerTickEvent to check if the player has the item in their inventory and if so allow them to fly. One thing to keep in mind is that a lot of other mods allow flying the same way so be sure not to break them. By that i mean if you were to just set hasFlight to false every tick if the player dose not have the item that would break other mods. I am the author of Draconic Evolution
January 11, 201510 yr Author Thanks for the reply. I went ahead and just used a LivingUpdateEvent instead. I was not sure If I was able to have the event be inside my item class, so I just made it separately. I made it so the player can only fly if the item is held, although I still want it to be if its in the inventory, which I'm not sure how to do. I know you can get the inventory of the player and even the container, but I'm not sure how I would use that in order check all the slots and return my item. Also not sure if I messed up on breaking other mods . Hope not! Class (for reference): package com.mjj.colormod.event; import com.mjj.colormod.handler.ItemHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; public class ArtifactEvent { @SubscribeEvent public void onLivingUpdateEvent(LivingUpdateEvent event){ if(event.entity instanceof EntityPlayer){ EntityPlayer player = (EntityPlayer) event.entity; ItemStack heldItem = player.getHeldItem(); if(heldItem != null && heldItem.getItem() == ItemHandler.crypticArtifact){ player.capabilities.allowFlying = true; }else{ if(player.capabilities.isFlying == true){ player.capabilities.isFlying = player.capabilities.isCreativeMode ? true : false; player.capabilities.allowFlying = player.capabilities.isCreativeMode ? true : false; } } } } }
January 11, 201510 yr 1. Use the item's onUpdate method, since you're checking for your item 2. Don't use LivingUpdateEvent for players, the PlayerTickEvent is for that. BEFORE ASKING FOR HELP READ THE EAQ! I'll help if I can. Apologies if I do something obviously stupid. If you don't know basic Java yet, go and follow these tutorials.
January 11, 201510 yr You can't use the item's update method because that is not called if the item is not in your inventory - e.g. you toss it with 'Q' while flying, and then you will have permanent flying. PlayerTickEvent has a player parameter, so you can check player.getHeldItem() is your item (if you require it to be held) or iterate through the player's inventory (don't do this if you don't have to) to see if they have it. http://i.imgur.com/NdrFdld.png[/img]
January 11, 201510 yr Author Ok but I want to iterate through the players inventory because if your holding this item, you wont be able to build and do stuff like that while flying. Also why is it bad to iterate through it. With I'm assuming the use of a for loop?
January 11, 201510 yr You dont actually have to iterate through the players inventory yourself there is already a method for that player.inventory.hasItemStack(stack) or player.inventory.hasItem(item) I am the author of Draconic Evolution
January 11, 201510 yr You dont actually have to iterate through the players inventory yourself there is already a method for that player.inventory.hasItemStack(stack) or player.inventory.hasItem(item) Which iterates through the inventory, but you are right, one does not have to do so manually. @OP If you want the player to be able to do other things while flying, then you have to potentially check the entire inventory every tick. It's not that terrible, but it's always better to avoid extra processing when you can. When you can't, well, computers these days are pretty dang fast. http://i.imgur.com/NdrFdld.png[/img]
January 11, 201510 yr You dont actually have to iterate through the players inventory yourself there is already a method for that player.inventory.hasItemStack(stack) or player.inventory.hasItem(item) Which iterates through the inventory, but you are right, one does not have to do so manually. Yea thats what i meant you don't have to do it yourself because there is a method that dose it for you. I am the author of Draconic Evolution
January 11, 201510 yr Author So I could be doing something obviously wrong? But the cryptic artifact isn't allowing flight when in the inventory. package com.mjj.colormod.event; import com.mjj.colormod.handler.ItemHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; public class ArtifactEvent { @SubscribeEvent public void onTickPlayerEvent(PlayerTickEvent event){ if(event.player instanceof EntityPlayer){ EntityPlayer player = (EntityPlayer) event.player; if(player.inventory.hasItem(ItemHandler.crypticArtifact)){ player.capabilities.allowFlying = true; }else{ if(player.capabilities.isFlying == true){ player.capabilities.isFlying = player.capabilities.isCreativeMode ? true : false; player.capabilities.allowFlying = player.capabilities.isCreativeMode ? true : false; } } } } }
January 11, 201510 yr That should work add some debug lines to see whats actually going on. And show how you registered your event handler. I am the author of Draconic Evolution
January 11, 201510 yr Author Debug lines? System.out.println()? Heres where I registered @EventHandler public void preInit(FMLPreInitializationEvent event) { MinecraftForge.EVENT_BUS.register(new ArtifactEvent());}
January 11, 201510 yr Author Don't think I'm doing this right: package com.mjj.colormod.event; import com.mjj.colormod.handler.ItemHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ArtifactEvent { public World world; @SubscribeEvent public void onTickPlayerEvent(PlayerTickEvent event){ if(!world.isRemote){ if(event.phase == Phase.START){ EntityPlayer player = (EntityPlayer) event.player; if(player.inventory.hasItem(ItemHandler.crypticArtifact)){ player.capabilities.allowFlying = true; }else{ if(event.phase == Phase.END){ if(player.capabilities.isFlying == true){ player.capabilities.isFlying = player.capabilities.isCreativeMode ? true : false; player.capabilities.allowFlying = player.capabilities.isCreativeMode ? true : false; } } } } } } }
January 11, 201510 yr Author Must still be doing this wrong? package com.mjj.colormod.event; import com.mjj.colormod.handler.ItemHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ArtifactEvent { public World world; @SubscribeEvent public void onTickPlayerEvent(PlayerTickEvent event){ if(!event.player.worldObj.isRemote){ if(event.phase == Phase.START){ EntityPlayer player = (EntityPlayer) event.player; if(player.inventory.hasItem(ItemHandler.crypticArtifact)){ player.capabilities.allowFlying = true; }else{ if(player.capabilities.isFlying == true){ player.capabilities.isFlying = player.capabilities.isCreativeMode ? true : false; player.capabilities.allowFlying = player.capabilities.isCreativeMode ? true : false; } } if(event.phase == Phase.END){ } } } } }
January 11, 201510 yr It's been a while since I've messed with flying, but movement is usually handled on the client side, so you must inform the client of the player's changed capabilities (I don't think it happens automatically), which you can do with a vanilla packet: ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(new C13PacketPlayerAbilities(player.capabilities)); Alternatively, you could let the tick logic run on both sides, rather than just the server. http://i.imgur.com/NdrFdld.png[/img]
January 12, 201510 yr Author Well doesn't this check for if I'm on the server? So removing the "!" would make it check for client? Not to keen on the whole client and server side stuff. (!event.player.worldObj.isRemote
January 12, 201510 yr Well doesn't this check for if I'm on the server? So removing the "!" would make it check for client? Not to keen on the whole client and server side stuff. (!event.player.worldObj.isRemote Yes, but you don't want it to run just on the client. Please read my response again: you can EITHER run the code ONLY on the server, in which case you may need to send that packet to update the client, OR you can try running the code ON BOTH sides, which means you wouldn't check world.isRemote at all (assuming that whatever method/event you are in does in fact run on both sides, which PlayerTickEvent does). http://i.imgur.com/NdrFdld.png[/img]
January 12, 201510 yr Author I want to run it on both, so I removed !event.player.worldObj.isRemote Looks like this: package com.mjj.colormod.event; import com.mjj.colormod.handler.ItemHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.network.play.client.C13PacketPlayerAbilities; import net.minecraft.world.World; public class ArtifactEvent { @SubscribeEvent public void onTickPlayerEvent(PlayerTickEvent event){ //if(!event.player.worldObj.isRemote){ if(event.phase == Phase.START){ EntityPlayer player = (EntityPlayer) event.player; if(player.inventory.hasItem(ItemHandler.crypticArtifact)){ player.capabilities.allowFlying = true; }else{ if(player.capabilities.isFlying == true){ player.capabilities.isFlying = player.capabilities.isCreativeMode ? true : false; player.capabilities.allowFlying = player.capabilities.isCreativeMode ? true : false; } } if(event.phase == Phase.END){ } } } } //} Still doesn't work though?
January 12, 201510 yr Did you register your event listener? @Mod.EventHandler public void init(FMLInitializationEvent event) { // TickEvents are on the FML bus: FMLCommonHandler.instance().bus().register(new ArtifactEvent()); } http://i.imgur.com/NdrFdld.png[/img]
January 12, 201510 yr Author Wow.. It was a simple as that. I was registering it with this code: MinecraftForge.EVENT_BUS.register(new ArtifactEvent()); Not this: FMLCommonHandler.instance().bus().register(new ArtifactEvent()); Thanks coolAlias!
January 12, 201510 yr There are two main event busses MinecraftForge.EVENT_BUS and FMLCommonHandler.instance().bus() a good way to check which one the event needs to be registered to is to look at the location of the event in the package system This is in the fml package so it is an fml event. import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent; Alternatively if it is a forge event it will be in the minecraftforge package. import net.minecraftforge.event.entity.player.PlayerUseItemEvent; I am the author of Draconic Evolution
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.