Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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

  • 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;
			}
		}
	}
}
}

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

 

If you don't know basic Java yet, go and follow these tutorials.

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.

  • 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?

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

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.

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

  • 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;
			}
		}
	}
}
}

  • Author

Debug lines? System.out.println()?

 

Heres where I registered

@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new ArtifactEvent());}

  • 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;
					}
				}
			}
		}
	}
}
}

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

				}
			}
		}
	}
}

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.

  • 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

 

 

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

  • 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?

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

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.