Jump to content

Recommended Posts

Posted

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  ;)

Posted

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

Posted

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?

Posted

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.

Posted

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;


		}
	}

Posted

@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.

Posted

@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.

Posted

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.  :P

Posted

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) {}

Posted

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...O.o

Posted

Yes, "Open call hierarchy" (other language :P), then you can follow the calls by clicking again on the line :)

 

You are already doing the click check with

 if(entityLiving.isSwingInProgress)

 

Posted

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? :P

Posted

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.

Posted

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.

Posted

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

Posted

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.

Posted

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?

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.