Posted August 13, 201312 yr Hello! I am trying to create a weapon, where if you are swinging, and you click again, it cancels the current swing and calls another one. So it basically makes it swing faster. So I got the on click method, and here is the code I have so far: public boolean onEntitySwing(EntityLiving entityLiving, ItemStack stack) { if(EntityPlayerBase.isSwingInProgress) { } return true; } } I am getting errors EntityPlayerBase.isSwingInProgress, but that's most likely because i'm not importing it correctly...also, does anyone know of a swing method that does damage? If someone could have that would be great
August 13, 201312 yr You use a static call with EntityPlayerBase, while isSwingInProgress is instance dependent. You can use entity.causeDamage(args); //name of the method may not be accurate
August 16, 201312 yr Author You use a static call with EntityPlayerBase, while isSwingInProgress is instance dependent. You can use entity.causeDamage(args); //name of the method may not be accurate Sorry, but what is a static call? And instance dependent isSwingInProgress means would be in an if statement, right? Also, what kind of parameter does a method tagged with the @ForgeSubscribe annotation require?
August 17, 201312 yr Basic Java: With a static call, you use a static field or static method. Foo.staticMethod(); is a static call, because Foo is the class, not an instance. public class Foo{ public boolean init= false; public Foo() { init=true; } public static void staticMethod() { System.out.println("Hello world"); } public boolean void nonStaticMethod() { return this.init; } } To use nonStaticMethod(), you need an instance of Foo. Foo instance = new Foo();//this is creating a new instance, instance.nonStaticMethod(); In your case you have to get your instance from somewhere... A method with @ForgeSubscribe needs one of the Event class as argument.
August 17, 201312 yr Author Basic Java: With a static call, you use a static field or static method. Foo.staticMethod(); is a static call, because Foo is the class, not an instance. public class Foo{ public boolean init= false; public Foo() { init=true; } public static void staticMethod() { System.out.println("Hello world"); } public boolean void nonStaticMethod() { return this.init; } } To use nonStaticMethod(), you need an instance of Foo. Foo instance = new Foo();//this is creating a new instance, instance.nonStaticMethod(); In your case you have to get your instance from somewhere... A method with @ForgeSubscribe needs one of the Event class as argument. Ah, thank you! I love learning more about Java!(not being sarcastic) But, umm, how would I call EntityPlayerBase? Also, just to let you know I'm not completely a n00b, I did make this attempt at it, but it is very buggy and it only works if you're hitting a mob. public static class DaggerSwing { @ForgeSubscribe public boolean onEntitySwing(LivingAttackEvent event) { if(FMLClientHandler.instance().getClient().thePlayer.isSwingInProgress = true) { Minecraft.getMinecraft().thePlayer.swingItem(); } return false; } }
August 17, 201312 yr @ForgeSubscribe public boolean onEntitySwing(LivingAttackEvent event) { This is correct event use, congrats Now, read the LivingAttackEvent class. This class contains a few (non static ) fields. Use them with the "event" argument. If you have Eclipse, you can also use "Find call hierarchy" with a right click on the event constructor (in LivingAttackEvent), to see where the event comes from, and what each field contains.
August 17, 201312 yr Author @ForgeSubscribe public boolean onEntitySwing(LivingAttackEvent event) { This is correct event use, congrats Now, read the LivingAttackEvent class. This class contains a few (non static ) fields. Use them with the "event" argument. If you have Eclipse, you can also use getCallHierarchy with a right click on the event constructor (in LivingAttackEvent), to see where the event comes from, and what each field contains. Thank you! But the only problem is that LivingAttackEvent is only called when I attack an entity. I need this to work whenever. If that's possible. Also, when I right click on LivingAttackEvent, it just shows the things that are methods that are using it.
August 17, 201312 yr Author Ah, I think I got it. Just need to restart my computer so I have enough memory to run Minecraft to test it xP Here it is: public boolean onLeftClickEntity(EntityLivingBase entityLiving) { if(entityLiving.isSwingInProgress) { Minecraft.getMinecraft().thePlayer.swingItem(); } return false; } So, I think how it works is, umm, when the Entity left clicks, it checks if another swing is in progress, then sends out another swing. If not it just sends out a normal swing like usual. I think.
August 17, 201312 yr You should try again "Find call hierarchy". This method is called when an item is left clicked on the given entity. As the comment says, this is called just before EntityLivingAttackEvent. Prefer: public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5) {}
August 17, 201312 yr Author You should try again "Find call hierarchy". This method is called when an item is left clicked on the given entity. As the comment says, this is called just before EntityLivingAttackEvent. Prefer: public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5) {} Um, is Find Call Hierarchy the same as Open Call Hierarchy? If so, It doesn't tell me much...just says: onLeftClickEntity(EntityLivingBase) : boolean - NoctusRealmItemsBlock.Items.ItemDagger (Which is where my class is) But anyway, back to coding. How would I tell when the player left clicks in onUpdate? I don't see how I could do that...
August 17, 201312 yr Yes, "Open call hierarchy" (other language ), then you can follow the calls by clicking again on the line You are already doing the click check with if(entityLiving.isSwingInProgress)
August 17, 201312 yr Author If I did this though: public void onUpdate(ItemStack itemStack, World world, Entity entity, EntityLivingBase entityLiving) { if(entityLiving.isSwingInProgress) { Minecraft.getMinecraft().thePlayer.swingItem(); } } Then if I clicked, wouldn't swings just go on forever?
August 17, 201312 yr That wouldn't work at all since you changed the method args and calling swingItem() when the swing is in progress doesn't do anything (see swingItem() method) public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isCurrentItem) { if(entity instanceof EntityPlayer && isCurrentItem) { EntityPlayer player = (EntityPlayer)entity; if(player.isSwingInProgress) { player.isSwingInProgress = false; } } } This might work.
August 17, 201312 yr Author That wouldn't work at all since you changed the method args and calling swingItem() when the swing is in progress doesn't do anything (see swingItem() method) public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isCurrentItem) { if(entity instanceof EntityPlayer && isCurrentItem) { EntityPlayer player = (EntityPlayer)entity; if(player.isSwingInProgress) { player.isSwingInProgress = false; } } } This might work. Doesn't work, the swing happens(I tested with hitting an entity) but it doesn't show the swing.
August 17, 201312 yr Yes, as a side effect, the animation packet isn't sent. You wanted to cancel the swing, here it is.
August 17, 201312 yr Author Yes, as a side effect, the animation packet isn't sent. You wanted to cancel the swing, here it is. Cancel the swing? No, let me explain this again. I want it so that when you left click when there is already a click in progress, it starts a new swing, so you can swing faster. I am trying to make a dagger xP
August 17, 201312 yr Only one swing can happen at a time, this is hardcoded in the swing behaviour. So, you want to cancel the first swing. Since you know player.swingItem() ; the rest is your code. No Forge nor Minecraft can help you.
August 17, 201312 yr Author Only one swing can happen at a time, this is hardcoded in the swing behaviour. So, you want to cancel the first swing. Since you know player.swingItem() ; the rest is your code. No Forge nor Minecraft can help you. OH! Now I see what you did. Ok. So I tried this: public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isCurrentItem) { if(entity instanceof EntityPlayer && isCurrentItem) { EntityPlayer player = (EntityPlayer)entity; if(player.isSwingInProgress) { player.isSwingInProgress = false; Minecraft.getMinecraft().thePlayer.swingItem(); } } } But this still didn't show any graphical swing. Do you know any way I could show the graphical swing?
August 18, 201312 yr Author I was thinking about sending a packet and rendering the animation yourself. So, sending a packet would be like a parameter, wouldn't it? for ex. public void onUpdate(lalalalal EntityLivingBase entityLiving) Then, I would...um...hehe I have noooo idea...so I need help with that xP Lol, I should have read this: http://www.minecraftforge.net/wiki/Packet_Handling
August 18, 201312 yr Author OH, I see now. So, I assume this would be sending a packet from client->server?
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.