Jump to content

Item in inventory?


vandy22

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

				}
			}
		}
	}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
  • Topics

×
×
  • Create New...

Important Information

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