Jump to content

[1.8] Giving an item to the player after a delay


JimiIT92

Recommended Posts

Currently i want to make a reloadable gun. So when the player right click the empty gun it should be reloaded and then he can shoot again. For this mechanic there's no problem, except that i want the recharg not to be instant. So, for instance, i want that when the player right click the empty gun to reload it after 5 seconds he have the "filled" gun, so there is a delay of 5 seconds between a shot and another :) How can i do that? :)

If needs, this is the empty gun class

package com.konnor;

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

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
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 ItemEmptyGun extends ItemMod
{
private String support;
private String cane;

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

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
	BlockPos pos = player.getPosition();
	Random rand = new Random();
	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);

	Item gun = null;
	if(this == ModItems.empty_oak_iron_hand_gun)
		gun = ModItems.oak_iron_hand_gun;
	else if(this == ModItems.empty_oak_gold_hand_gun)
		gun = ModItems.oak_gold_hand_gun;
	else if(this == ModItems.empty_oak_silver_hand_gun)
		gun = ModItems.oak_silver_hand_gun;
	else if(this == ModItems.empty_birch_iron_hand_gun)
		gun = ModItems.birch_iron_hand_gun;
	else if(this == ModItems.empty_birch_gold_hand_gun)
		gun = ModItems.birch_gold_hand_gun;
	else if(this == ModItems.empty_birch_silver_hand_gun)
		gun = ModItems.birch_silver_hand_gun;
	else if(this == ModItems.empty_dark_oak_iron_hand_gun)
		 gun = ModItems.dark_oak_iron_hand_gun;
	else if(this == ModItems.empty_dark_oak_gold_hand_gun)
		gun = ModItems.dark_oak_gold_hand_gun;
	else if(this == ModItems.empty_dark_oak_silver_hand_gun)
		gun = ModItems.dark_oak_silver_hand_gun;
	else if(this == ModItems.empty_oak_iron_gun)
		gun = ModItems.oak_iron_gun;
	else if(this == ModItems.empty_oak_gold_gun)
		gun = ModItems.oak_gold_gun;
	else if(this == ModItems.empty_oak_silver_gun)
		gun = ModItems.oak_silver_gun;
	else if(this == ModItems.empty_birch_iron_gun)
		gun = ModItems.birch_iron_gun;
	else if(this == ModItems.empty_birch_gold_gun)
		gun = ModItems.birch_gold_gun;
	else if(this == ModItems.empty_birch_silver_gun)
		gun = ModItems.birch_silver_gun;
	else if(this == ModItems.empty_dark_oak_iron_gun)
		gun = ModItems.dark_oak_iron_gun;
	else if(this == ModItems.empty_dark_oak_gold_gun)
		gun = ModItems.dark_oak_gold_gun;
	else if(this == ModItems.empty_dark_oak_silver_gun)
		gun = ModItems.dark_oak_silver_gun;
	else if(this == ModItems.empty_oak_iron_musket)
		gun = ModItems.oak_iron_musket;
	else if(this == ModItems.empty_oak_gold_musket)
		gun = ModItems.oak_gold_musket;
	else if(this == ModItems.empty_oak_silver_musket)
		gun = ModItems.oak_silver_musket;
	else if(this == ModItems.empty_birch_iron_musket)
		gun = ModItems.birch_iron_musket;
	else if(this == ModItems.empty_birch_gold_musket)
		gun = ModItems.birch_gold_musket;
	else if(this == ModItems.empty_birch_silver_musket)
		gun = ModItems.birch_silver_musket;
	else if(this == ModItems.empty_dark_oak_iron_musket)
		gun = ModItems.dark_oak_iron_musket;
	else if(this == ModItems.empty_dark_oak_gold_musket)
		gun = ModItems.dark_oak_gold_musket;
	else if(this == ModItems.empty_dark_oak_silver_musket)
		gun = ModItems.dark_oak_silver_musket;
	else if(this == ModItems.empty_rapier_gun)
		gun = ModItems.rapier_gun;
	else if(this == ModItems.empty_rifle_axe)
		gun = ModItems.rifle_axe;

	if(player.inventory.hasItem(ModItems.bullet) || player.capabilities.isCreativeMode)
	{
		player.setCurrentItemOrArmor(0, new ItemStack(gun));
	}

	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.empty_rapier_gun && this != ModItems.empty_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

To make a delay, use tick countdown.

Whenever gun shot, on filled gun itemstack add nbt int with value of countdown in ticks.

Then, each tick, filled gun dicreases countdown by 1 and when it reaches 0, it replaces itself with gun ready to shoot.

 

For ticking, use

Item.onUpdate[code].
Link to comment
Share on other sites

I've figured it out by doing this in the empty gun class

package com.konnor;

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

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
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 ItemEmptyGun extends ItemMod
{
private String support;
private String cane;

private boolean ready = false;
private boolean reload = false;
public ItemEmptyGun(String par1, String par2)
{
	super();
	this.support = "material." + par1;
	this.cane = "material." + par2;
	this.setMaxStackSize(1);
	this.setCreativeTab((CreativeTabs)null);
}

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{


	if(player.inventory.hasItem(ModItems.bullet) || player.capabilities.isCreativeMode)
	{
		reload = true;
	}

	return stack;
}

@Override
public void onUpdate(ItemStack stack, World worldIn, net.minecraft.entity.Entity entityIn, int itemSlot, boolean isSelected) 
{
	if(stack.getTagCompound() != null && reload)
	{
		BlockPos pos = ((EntityPlayer)entityIn).getPosition();
		Random rand = new Random();
		int current_delay = stack.getTagCompound().getInteger("delay")-1;

		if(current_delay == 0)
		{
			worldIn.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);
			Item gun = null;
			if(this == ModItems.empty_oak_iron_hand_gun)
				gun = ModItems.oak_iron_hand_gun;
			else if(this == ModItems.empty_oak_gold_hand_gun)
				gun = ModItems.oak_gold_hand_gun;
			else if(this == ModItems.empty_oak_silver_hand_gun)
				gun = ModItems.oak_silver_hand_gun;
			else if(this == ModItems.empty_birch_iron_hand_gun)
				gun = ModItems.birch_iron_hand_gun;
			else if(this == ModItems.empty_birch_gold_hand_gun)
				gun = ModItems.birch_gold_hand_gun;
			else if(this == ModItems.empty_birch_silver_hand_gun)
				gun = ModItems.birch_silver_hand_gun;
			else if(this == ModItems.empty_dark_oak_iron_hand_gun)
				 gun = ModItems.dark_oak_iron_hand_gun;
			else if(this == ModItems.empty_dark_oak_gold_hand_gun)
				gun = ModItems.dark_oak_gold_hand_gun;
			else if(this == ModItems.empty_dark_oak_silver_hand_gun)
				gun = ModItems.dark_oak_silver_hand_gun;
			else if(this == ModItems.empty_oak_iron_gun)
				gun = ModItems.oak_iron_gun;
			else if(this == ModItems.empty_oak_gold_gun)
				gun = ModItems.oak_gold_gun;
			else if(this == ModItems.empty_oak_silver_gun)
				gun = ModItems.oak_silver_gun;
			else if(this == ModItems.empty_birch_iron_gun)
				gun = ModItems.birch_iron_gun;
			else if(this == ModItems.empty_birch_gold_gun)
				gun = ModItems.birch_gold_gun;
			else if(this == ModItems.empty_birch_silver_gun)
				gun = ModItems.birch_silver_gun;
			else if(this == ModItems.empty_dark_oak_iron_gun)
				gun = ModItems.dark_oak_iron_gun;
			else if(this == ModItems.empty_dark_oak_gold_gun)
				gun = ModItems.dark_oak_gold_gun;
			else if(this == ModItems.empty_dark_oak_silver_gun)
				gun = ModItems.dark_oak_silver_gun;
			else if(this == ModItems.empty_oak_iron_musket)
				gun = ModItems.oak_iron_musket;
			else if(this == ModItems.empty_oak_gold_musket)
				gun = ModItems.oak_gold_musket;
			else if(this == ModItems.empty_oak_silver_musket)
				gun = ModItems.oak_silver_musket;
			else if(this == ModItems.empty_birch_iron_musket)
				gun = ModItems.birch_iron_musket;
			else if(this == ModItems.empty_birch_gold_musket)
				gun = ModItems.birch_gold_musket;
			else if(this == ModItems.empty_birch_silver_musket)
				gun = ModItems.birch_silver_musket;
			else if(this == ModItems.empty_dark_oak_iron_musket)
				gun = ModItems.dark_oak_iron_musket;
			else if(this == ModItems.empty_dark_oak_gold_musket)
				gun = ModItems.dark_oak_gold_musket;
			else if(this == ModItems.empty_dark_oak_silver_musket)
				gun = ModItems.dark_oak_silver_musket;
			else if(this == ModItems.empty_rapier_gun)
				gun = ModItems.rapier_gun;
			else if(this == ModItems.empty_rifle_axe)
				gun = ModItems.rifle_axe;
			((EntityPlayer)entityIn).setCurrentItemOrArmor(0, new ItemStack(gun));
			reload = false;
		}

		else
			stack.getTagCompound().setInteger("delay", current_delay);
	}
};

@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.empty_rapier_gun && this != ModItems.empty_rifle_axe)
	{			
		list.add(StatCollector.translateToLocal("support.name") + ": "  + chat + StatCollector.translateToLocal(support));
		list.add(StatCollector.translateToLocal("material.name") + ": "  + chat2 + StatCollector.translateToLocal(cane));
	}
}
}

 

And this in the filled gun class

package com.konnor;

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

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
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;

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(player.inventory.hasItem(ModItems.bullet) || player.capabilities.isCreativeMode)
	{
		if(!player.capabilities.isCreativeMode)
			player.inventory.consumeInventoryItem(ModItems.bullet);

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



	Item gun = null;
	if(this == ModItems.oak_iron_hand_gun)
		gun = ModItems.empty_oak_iron_hand_gun;
	else if(this == ModItems.oak_gold_hand_gun)
		gun = ModItems.empty_oak_gold_hand_gun;
	else if(this == ModItems.oak_silver_hand_gun)
		gun = ModItems.empty_oak_silver_hand_gun;
	else if(this == ModItems.birch_iron_hand_gun)
		gun = ModItems.empty_birch_iron_hand_gun;
	else if(this == ModItems.birch_gold_hand_gun)
		gun = ModItems.empty_birch_gold_hand_gun;
	else if(this == ModItems.birch_silver_hand_gun)
		gun = ModItems.empty_birch_silver_hand_gun;
	else if(this == ModItems.dark_oak_iron_hand_gun)
		 gun = ModItems.empty_dark_oak_iron_hand_gun;
	else if(this == ModItems.dark_oak_gold_hand_gun)
		gun = ModItems.empty_dark_oak_gold_hand_gun;
	else if(this == ModItems.dark_oak_silver_hand_gun)
		gun = ModItems.empty_dark_oak_silver_hand_gun;
	else if(this == ModItems.oak_iron_gun)
		gun = ModItems.empty_oak_iron_gun;
	else if(this == ModItems.oak_gold_gun)
		gun = ModItems.empty_oak_gold_gun;
	else if(this == ModItems.oak_silver_gun)
		gun = ModItems.empty_oak_silver_gun;
	else if(this == ModItems.birch_iron_gun)
		gun = ModItems.empty_birch_iron_gun;
	else if(this == ModItems.birch_gold_gun)
		gun = ModItems.empty_birch_gold_gun;
	else if(this == ModItems.birch_silver_gun)
		gun = ModItems.empty_birch_silver_gun;
	else if(this == ModItems.dark_oak_iron_gun)
		gun = ModItems.empty_dark_oak_iron_gun;
	else if(this == ModItems.dark_oak_gold_gun)
		gun = ModItems.empty_dark_oak_gold_gun;
	else if(this == ModItems.dark_oak_silver_gun)
		gun = ModItems.empty_dark_oak_silver_gun;
	else if(this == ModItems.oak_iron_musket)
		gun = ModItems.empty_oak_iron_musket;
	else if(this == ModItems.oak_gold_musket)
		gun = ModItems.empty_oak_gold_musket;
	else if(this == ModItems.oak_silver_musket)
		gun = ModItems.empty_oak_silver_musket;
	else if(this == ModItems.birch_iron_musket)
		gun = ModItems.empty_birch_iron_musket;
	else if(this == ModItems.birch_gold_musket)
		gun = ModItems.empty_birch_gold_musket;
	else if(this == ModItems.birch_silver_musket)
		gun = ModItems.empty_birch_silver_musket;
	else if(this == ModItems.dark_oak_iron_musket)
		gun = ModItems.empty_dark_oak_iron_musket;
	else if(this == ModItems.dark_oak_gold_musket)
		gun = ModItems.empty_dark_oak_gold_musket;
	else if(this == ModItems.dark_oak_silver_musket)
		gun = ModItems.empty_dark_oak_silver_musket;
	else if(this == ModItems.rapier_gun)
		gun = ModItems.empty_rapier_gun;
	else if(this == ModItems.rifle_axe)
		gun = ModItems.empty_rifle_axe;

	ItemStack item = new ItemStack(gun);
	NBTTagCompound nbttagcompound = new NBTTagCompound();
        nbttagcompound.setInteger("delay", 50);
        item.setTagCompound(nbttagcompound);
        
	player.setCurrentItemOrArmor(0, item);
}
	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));
	}
}
}

 

But now i got 2 problems:

1. How to avoid the item flickering in hand while reloading?

2. Sometimes  i need to shoot 2 times before the gun it really shot :/

Here's how it works now, if i wait some ticks before shoot everything works fine, but if i shoot as soon as it reloads then the "2 shots problem" comes out :/

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

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

To avoid the "flickering" when reloading (which is due to Minecraft thinking that a change to the itemstack NBT means its actually a new stack) you need to store the time when the reload finishes rather than making a counter that counts down every tick.

 

So you would store "world.getWorldTime() + reloadSpeed" and then you'll compare the stored value with "world.getWorldTime()."  When world.getWorldTime() is greater than the stored value, the gun has reloaded.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Just for info: There's also a method that stops flickering that you could override (

shouldCauseReequipAnimation

) if using countdown way.

 

Is that new with 1.8?  Neat.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Just for info: There's also a method that stops flickering that you could override (

shouldCauseReequipAnimation

) if using countdown way.

 

Is that new with 1.8?  Neat.

Yes. And it was added ~5 moths ago.

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



×
×
  • Create New...

Important Information

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