Jump to content

'ammo' detection assistance requested


PegBeard

Recommended Posts

I have made a projectile weapon which is able to shoot a variety of projectiles based on the ammunition in the players inventory. Currently, it fires all applicable types of ammo, but I would like it to only detect ammo that is on the players hotbar.

I have been playing around with getStackInSlot and designating the hotbar slots, but that isn't really doing what I want it to - unless I have missed something.

I am not overly familiar with all the functions of MC/Forge, as I've only been at this for a week or so and I was wondering if there was a way to detect a specific item in the players hotbar, that isn't the held item - specifically I want this to happen on right click with the weapon, to determine if applicable ammo is present and the weapon can be fired.

Link to comment
Share on other sites

The only way to do so is to iterate through each hotbar slot and check the inventory stack.

 

Unless your item can shoot itself, you can safely iterate without worrying about the currently held item, since the currently held item must, by definition, be the one that was right-clicked, i.e. your item, and wouldn't be a valid ammo type.

Link to comment
Share on other sites

This is the way I am attempting to check only the hotbar for the ammo, but it seems to checking for anything in the hotbar and then for the ammo in the rest of my inventory and firing from there.

 

	public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return event.result;
        }

        for (int i = 0; i < 9; i++)
        {
        	if (par3EntityPlayer.inventory.getStackInSlot(i) != null)
        	{
        		if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(Items.wheat_seeds)  || par3EntityPlayer.inventory.hasItem(Items.pumpkin_seeds) || par3EntityPlayer.inventory.hasItem(Items.melon_seeds)  || par3EntityPlayer.inventory.hasItem(DungeoneeringItems.ironNugget) || par3EntityPlayer.inventory.hasItem(DungeoneeringItems.pyriteDust))
        		{
        			par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
        		}
        	}
        }
  	  	return par1ItemStack;
    }

 

I am guessing this bit is my main issue - if (par3EntityPlayer.inventory.getStackInSlot(i) != null) - as I'm not specifying a specific item to search for and stop on, but it wouldn't except a specific Item and I'm not sure how getStackInSlot works, if I am honest.

I only have a basic understanding of Java, so I probably look like a bit of a numpty, but making an MC mod is an attempt to get better at it.

Link to comment
Share on other sites

I have no idea what are you trying to accomplish with those:

 

if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(Items.wheat_seeds)  || par3EntityPlayer.inventory.hasItem(Items.pumpkin_seeds) || par3EntityPlayer.inventory.hasItem(Items.melon_seeds)  || par3EntityPlayer.inventory.hasItem(DungeoneeringItems.ironNugget) || par3EntityPlayer.inventory.hasItem(DungeoneeringItems.pyriteDust))

 

You do not need to do so many checks, and most certainly you don't want to use #hasItem so much.

 

After this loop your "theAmmo" will be the first ItemStack found in hotbar that is instance of your ammo item (apple).

 

If you want item-comprasion:

ItemStack theAmmo = null;
for (int i = 0; i < 9; i++)
{
if (player.inventory.mainInventory[i] != null)
{
	Item item = player.inventory.mainInventory[i].getItem();
	if (item == Item.apple)
	{
		theAmmo = player.inventory.mainInventory[i];
		break; //breaks loop
	}
}
}

 

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

the trouble is the way your asking if the player has item

bad english advertisment

 

when you do

par3EntityPlayer.inventory.hasItem(Items.wheat_seeds)

you are asking if the player has wheatseeds no mather  where the sloth is

 

you must get the item from the slot doing something like

 

ItemStack cosastack = par3EntityPlayer.inventory.getStackInSlot(i);

Item cosa = cosastack.getItem();

 

and then compare it to what you want

 

if (cosa.equals(Items.wheat_seeds))

{

//do cosas or stuff

}

 

 

 

 

Link to comment
Share on other sites

Always great gifs from Ernio :P

 

@OP As others have said, you need to compare the ItemStack in the slot to what you are looking for, but also, if the player is in Creative Mode, you shouldn't even bother looping from 0-9 unless you need to set ammo for creative players, too, rather than just letting them shoot whatever the default is.

 

What I would do is create a list of Items / ItemStacks that are acceptable ammo, and compare those:

// Elsewhere in your code:
private final List<Item> acceptableAmmo = Lists.newArrayList();
static {
  acceptableAmmo.add(Items.wheat_seeds); // or whatever items you want as ammo
}


// In Item#onItemRightClick method:
ItemStack ammo = null;
// adding in '&& ammo == null' to the for loop will cause it to exit as soon as acceptable ammo is found
for (int i = 0; i < 9 && ammo == null; ++i) {
  ItemStack stack = player.inventory.getStackInSlot(i);
  if (stack != null && acceptableAmmo.contains(stack.getItem())) {
     ammo = stack; // now the loop will stop, and you have your ammo type
  }
}
if (ammo != null) {
  // here you can decrement the stack size of the ammo stack and spawn in correct projectile, etc.
}

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.



×
×
  • Create New...

Important Information

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