Jump to content

[1.8] Issues with reloadable gun


JimiIT92

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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

    • Slot deposit 3000 adalah situs slot deposit 3000 via dana yang super gacor dimana para pemain dijamin garansi wd hari ini juga hanya dengan modal receh berupa deposit sebesar 3000 baik via dana, ovo, gopay maupun linkaja untuk para pemain pengguna e-wallet di seluruh Indonesia.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT 3000 ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • OLXTOTO: Menikmati Sensasi Bermain Togel dan Slot dengan Aman dan Mengasyikkan Dunia perjudian daring terus berkembang dengan cepat, dan salah satu situs yang telah menonjol dalam pasar adalah OLXTOTO. Sebagai platform resmi untuk permainan togel dan slot, OLXTOTO telah memenangkan kepercayaan banyak pemain dengan menyediakan pengalaman bermain yang aman, adil, dan mengasyikkan. DAFTAR OLXTOTO DISINI <a href="https://imgbb.com/"><img src="https://i.ibb.co/GnjSVpx/daftar1-480x480.webp" alt="daftar1-480x480" border="0" /></a> Keamanan Sebagai Prioritas Utama Salah satu aspek utama yang membuat OLXTOTO begitu menonjol adalah komitmennya terhadap keamanan pemain. Dengan menggunakan teknologi enkripsi terkini, situs ini memastikan bahwa semua informasi pribadi dan keuangan para pemain tetap aman dan terlindungi dari akses yang tidak sah. Beragam Permainan yang Menarik Di OLXTOTO, pemain dapat menemukan beragam permainan yang menarik untuk dinikmati. Mulai dari permainan klasik seperti togel hingga slot modern dengan fitur-fitur inovatif, ada sesuatu untuk setiap selera dan preferensi. Grafik yang memukau dan efek suara yang mengagumkan menambah keseruan setiap putaran. Peluang Menang yang Tinggi Salah satu hal yang paling menarik bagi para pemain adalah peluang menang yang tinggi yang ditawarkan oleh OLXTOTO. Dengan pembayaran yang adil dan peluang yang setara bagi semua pemain, setiap taruhan memberikan kesempatan nyata untuk memenangkan hadiah besar. Layanan Pelanggan yang Responsif Tim layanan pelanggan OLXTOTO siap membantu para pemain dengan setiap pertanyaan atau masalah yang mereka hadapi. Dengan layanan yang ramah dan responsif, pemain dapat yakin bahwa mereka akan mendapatkan bantuan yang mereka butuhkan dengan cepat dan efisien. Kesimpulan OLXTOTO telah membuktikan dirinya sebagai salah satu situs terbaik untuk penggemar togel dan slot online. Dengan fokus pada keamanan, beragam permainan yang menarik, peluang menang yang tinggi, dan layanan pelanggan yang luar biasa, tidak mengherankan bahwa situs ini telah menjadi pilihan utama bagi banyak pemain. Jadi, jika Anda mencari pengalaman bermain yang aman, adil, dan mengasyikkan, jangan ragu untuk bergabung dengan OLXTOTO hari ini dan rasakan sensasi kemenangan!
    • Slot deposit dana adalah situs slot deposit dana yang juga menerima dari e-wallet lain seperti deposit via dana, ovo, gopay & linkaja terlengkap saat ini, sehingga para pemain yang tidak memiliki rekening bank lokal bisa tetap bermain slot dan terbantu dengan adanya fitur tersebut.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT DANA ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • Slot deposit dana adalah situs slot deposit dana minimal 5000 yang dijamin garansi super gacor dan gampang menang, dimana para pemain yang tidak memiliki rekening bank lokal tetap dalam bermain slot dengan melakukan deposit dana serta e-wallet lainnya seperti ovo, gopay maupun linkaja lengkap. Agar para pecinta slot di seluruh Indonesia tetap dapat menikmati permainan tanpa halangan apapun khususnya metode deposit, dimana ketersediaan cara deposit saat ini yang lebih beragam tentunya sangat membantu para pecinta slot.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT DANA ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
    • Slot deposit pulsa adalah situs slot deposit pulsa tanpa potongan apapun yang dijamin garansi terpercaya, dimana kamu bisa bermain slot dengan melakukan deposit pulsa dan tanpa dikenakan potongan apapun sehingga dana yang masuk ke dalam akun akan 100% utuh. Proses penarikan dana juga dijamin gampang dan tidak sulit sehingga kamu tidak perlu khawatir akan kemenangan yang akan kamu peroleh dengan sangat mudah jika bermain disini.   DAFTAR & LOGIN AKUN PRO SLOT DEPOSIT PULSA TANPA POTONGAN ⭐⭐⭐ KLIK DISINI ⭐⭐⭐  
  • Topics

×
×
  • Create New...

Important Information

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