Jump to content

Recommended Posts

Posted

So far i was trying to make a gun that after each shot it has to be reload. So the scenario is: shoot-reload-shoot-reload an so on. However adding a reload state makes the gun do weird things wich you can see here

https://www.youtube.com/watch?v=dU6lfGEXyx0&feature=youtu.be

 

This is the item class that i use for each gun in the mod. ItemMod is just a class that extends Item and do nothing special (just setting the creative tab and unlocalized name)

package com.konnor;

import java.util.List;
import java.util.Random;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

public class ItemGun extends ItemMod
{
private String support;
private String cane;

private boolean reload = false;

public ItemGun(String par1, String par2)
{
	super();
	this.support = "material." + par1;
	this.cane = "material." + par2;
	this.setMaxStackSize(1);
}

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
	BlockPos pos = player.getPosition();
	Random rand = new Random();
	if(reload)
	{
		world.playSound((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), "connorkpt:reload", 0.2F + rand.nextFloat() * 0.2F, 0.9F + rand.nextFloat() * 0.15F, false);

		reload = false;
	}
	else if((player.inventory.hasItem(ModItems.bullet) || player.capabilities.isCreativeMode))
	{
		IInventory inv = player.inventory;
		for(int i=0; i < inv.getSizeInventory(); i++)
		{
			if(inv.getStackInSlot(i) != null)
			{
				ItemStack j = inv.getStackInSlot(i);
				if(j.getItem() != null && j.getItem() == ModItems.bullet)
				{
					inv.decrStackSize(i, 1);
					i = inv.getSizeInventory();
				}
			}
		}
		if (!world.isRemote)
		{
			world.spawnEntityInWorld(new EntityBullet(world, player));
		}
		world.playSound((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), "connorkpt:gun", 0.2F + rand.nextFloat() * 0.2F, 0.9F + rand.nextFloat() * 0.15F, false);

		reload = true;
	}

	return stack;
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean Adva)
{
	EnumChatFormatting chat;
	EnumChatFormatting chat2;
	if(this.support.equals("material.oak"))
		chat = EnumChatFormatting.YELLOW;
	else if(this.support.equals("material.birch"))
		chat = EnumChatFormatting.WHITE;
	else
		chat = EnumChatFormatting.GRAY;
	if(this.cane.equals("material.gold"))
		chat2 = EnumChatFormatting.YELLOW;
	else if(this.cane.equals("material.silver"))
		chat2 = EnumChatFormatting.WHITE;
	else
		chat2 = EnumChatFormatting.GRAY;
	if(this != ModItems.rapier_gun && this != ModItems.rifle_axe)
	{			
		list.add(StatCollector.translateToLocal("support.name") + ": "  + chat + StatCollector.translateToLocal(support));
		list.add(StatCollector.translateToLocal("material.name") + ": "  + chat2 + StatCollector.translateToLocal(cane));
	}
}
}

Don't blame me if i always ask for your help. I just want to learn to be better :)

Posted

First of all:

for(int i=0; i < inv.getSizeInventory(); i++)
		{
			if(inv.getStackInSlot(i) != null)
			{
				ItemStack j = inv.getStackInSlot(i);
				if(j.getItem() != null && j.getItem() == ModItems.bullet)
				{
					inv.decrStackSize(i, 1);
					i = inv.getSizeInventory();
				}
			}
		}

That will decrease 1 form all the bullet stacks that is in your inventory. A boolean should fix it.

 

Than, there is a thing player.posX, player.posY, player.posZ.

 

Finally:

(player.inventory.hasItem(ModItems.bullet)

Returns true for some reason. How did you register your items?

Posted

The for cycle only decrease the first stack of bullets that encount, i've already tested it ;) The player.posX/Y/Z thing is used when the sound should be played.

The items are registered as usual, in the init method of the main mod file, using GameRegistry.registerItem(item, name);

Don't blame me if i always ask for your help. I just want to learn to be better :)

Posted

You know there is a function called consumeInventoryItem, which just consumes the item.

 

Also, items must be registered in the

preInit

. NOT the

init

phase.

 

Now, the last most important thing is: you cannot store values inside your item classes. There are only one instance of every item, which makes every field in that class, static throughout all items in the game. Which means all items will reload at the same time. Not good...

Do it with metadata instead. 0 equals no reload, 1 is reload. Got it?

I might be terribly wrong.. Like really, really wrong. But I'm just trying to help.

Posted

i was trying doing that to avoid using metadata and sub-items :/ But i'll do it. So every time i click i should change the item in hand of player, right? Also why the item registration should be done in the preInit? I always do that in the init and it never gives me a problem :/

Don't blame me if i always ask for your help. I just want to learn to be better :)

Posted

Yes, update the item the player is holding. :)

Also why the item registration should be done in the preInit? I always do that in the init and it never gives me a problem :/

It works yes, but the there is a reason to why there are different phases. If I remember correctly, preInit is registration, init is crafting recipes, forge hooks..., and postInit is bridging to other mods.

I might be terribly wrong.. Like really, really wrong. But I'm just trying to help.

Posted

No. I guess it would have an impact if somebody wanted to bridge over to your mod. Also, their are some stuff like, tabs before blocks and items, items and blocks before crafting recipes.. etc. And the different states do help prevent that. After all, you cannot use any of your items before the items have been instantiated. ;)

I might be terribly wrong.. Like really, really wrong. But I'm just trying to help.

Posted

Yes, the order i already know, infact in my mod the tabs and blocks are the first two things registered xD

Don't blame me if i always ask for your help. I just want to learn to be better :)

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.