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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • 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;     }  
  • Topics

×
×
  • Create New...

Important Information

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