Jump to content

Recommended Posts

Posted

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!!

Posted

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!!

Posted

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.

Posted

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.

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.