Jump to content

Is it ok to run this constantly onUpdate in an item?


American2050

Recommended Posts

Basically it's a tool, that while active, will scan the player inventory looking for cobblestone, and if it finds cobblestone, it deletes it.

 

	@Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
    {

	if(this.deleteCobble && isSelected){
		if(entity instanceof EntityPlayer){

			EntityPlayer thePlayer = (EntityPlayer) entity;
			Item cobblestone = new ItemStack(Blocks.COBBLESTONE,1,0).getItem();

				for(int x=0; x<36; x++){
					ItemStack theStack = thePlayer.inventory.getStackInSlot(x);
						if(theStack!=null){
							Item theItem = theStack.getItem();
								if(theItem.equals(cobblestone)){
									thePlayer.inventory.removeStackFromSlot(x);
								}
						}
				}
		}
	}
    }

 

 

Could this be a problem?

 

Link to comment
Share on other sites

1. Item#onUpdate() is fired every tick whenever Item is inside player's inventory (and maybe other entity inventories, idk).

* Yes, you can do whetever you want, and as long as it is not too heavy (we are talking ridiculously heavy operations).

 

2. This is retarded:

Item cobblestone = new ItemStack(Blocks.COBBLESTONE,1,0).getItem();

Just do Item.getItemFromBlock(Blocks...) (you can also do other way around with Block.getBlockFromItem).

 

EDIT

3. Totally forgot:

YOU CAN'T have any non-shared fields in Item class. If "deleteCobble" should be per item boolean - you must place it inside ItemStack's NBT.

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

Link to comment
Share on other sites

1. Item#onUpdate() is fired every tick whenever Item is inside player's inventory (and maybe other entity inventories, idk).

* Yes, you can do whetever you want, and as long as it is not too heavy (we are talking ridiculously heavy operations).

 

2. This is retarded:

Item cobblestone = new ItemStack(Blocks.COBBLESTONE,1,0).getItem();

Just do Item.getItemFromBlock(Blocks...) (you can also do other way around with Block.getBlockFromItem).

 

EDIT

3. Totally forgot:

YOU CAN'T have any non-shared fields in Item class. If "deleteCobble" should be per item boolean - you must place it inside ItemStack's NBT.

 

About point 3, thanks, I'm aware already that this is not a good practice as 2 items in my inventory would start doing the same. I know I have to move that to NBT's but I'm still "raw" on working with those.

Hopefully I can learn that soon to fix this.

 

Point 2: Thanks for the headsup.

Link to comment
Share on other sites

Guys I believe it's first time I use NBTs  ::)

 

I'm not sure all I understood about NBTs I implemented correctly, if anyone want to take a look and comment I would be really appreciate.

 

As always thanks for your time, and thanks for helping me learn something new every day.

 

package com.mramericanmike.mikedongles.items;

import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.mramericanmike.mikedongles.ModInfo;
import com.mramericanmike.mikedongles.creativetab.ModCreativeTab;

public class PickappaDongle extends ItemPickaxe
{
private static String deleteMode = "-";

private static boolean getBool = false;

private static final String TAG_PICKAPPA = "pickappa";
private static final String TAG_PICKAPPA_MODE_DESC = "pickappa.mode.desc";
private static final String TAG_PICKAPPA_MODE = "pickappa.mode";
private static final String TAG_PICKAPPA_COBBLESTONE = "pickappa.cobblestone";

private static int base=0;
private static int max=9;


public PickappaDongle(ToolMaterial material, String name) {
	super(material);
	this.setRegistryName(name);
	this.setUnlocalizedName(ModInfo.MODID.toLowerCase() + ":" + name);
	this.setMaxDamage(material.getMaxUses());
	this.setMaxStackSize(1);
	this.setCreativeTab(ModCreativeTab.MOD_TAB);

	}


@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {

	if(!world.isRemote && player.isSneaking()){

		boolean bool = getNBTTagBoolean(stack, TAG_PICKAPPA_COBBLESTONE);

		deleteMode = getNBTTagString(stack, TAG_PICKAPPA_MODE);

		if(bool == false){


			if(deleteMode == "-"){

				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				player.addChatComponentMessage(new TextComponentString("Delete Cobblestone Mode HotBar Enabled"));
				this.updateNBTData(stack, TAG_PICKAPPA_MODE_DESC, "Delete Cobblestone From HotBar");
				this.updateNBTData(stack, TAG_PICKAPPA_MODE, "A");
				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				this.base=0;
				this.max=9;

				return EnumActionResult.SUCCESS;
			}

			else if(deleteMode == "A"){

				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				player.addChatComponentMessage(new TextComponentString("Delete Cobblestone Mode Inventory Enabled"));
				this.updateNBTData(stack, TAG_PICKAPPA_MODE_DESC, "Delete Cobblestone From Inventory");
				this.updateNBTData(stack, TAG_PICKAPPA_MODE, "B");
				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				this.base=9;
				this.max=36;

				return EnumActionResult.SUCCESS;
			}

			else if(deleteMode == "B"){

				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				player.addChatComponentMessage(new TextComponentString("Delete Cobblestone Mode Whole Inventory Enabled"));
				this.updateNBTData(stack, TAG_PICKAPPA_MODE_DESC, "Delete Cobblestone From Whole Inventory");
				this.updateNBTData(stack, TAG_PICKAPPA_MODE, "C");
				this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, true);
				this.base=0;
				this.max=36;

				return EnumActionResult.SUCCESS;
			}

		}

		else if(bool == true){
			player.addChatComponentMessage(new TextComponentString("Delete Cobblestone Disabled"));
			this.updateNBTData(stack, TAG_PICKAPPA_MODE_DESC, "Delete Cobblestone Disabled");
			this.updateNBTData(stack, TAG_PICKAPPA_COBBLESTONE, false);

			if(deleteMode == "C"){
				this.updateNBTData(stack, TAG_PICKAPPA_MODE, "-");
			}

			return EnumActionResult.SUCCESS;

		}

	}

	return EnumActionResult.PASS;

    }

@Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
    {	
	if(getNBTTagBoolean(stack, TAG_PICKAPPA_COBBLESTONE) && isSelected){
		if(entity instanceof EntityPlayer){

			EntityPlayer thePlayer = (EntityPlayer) entity;
			Item cobblestone = Item.getItemFromBlock(Blocks.COBBLESTONE);

				for(int slot = base; slot<max; slot++){
					ItemStack theStack = thePlayer.inventory.getStackInSlot(slot);
						if(theStack!=null){
							Item theItem = theStack.getItem();
								if(theItem.equals(cobblestone)){
									thePlayer.inventory.removeStackFromSlot(slot);
								}
						}
				}
		}
	}
    }


    @Override
    @SideOnly(Side.CLIENT)
    public boolean hasEffect(ItemStack stack)
    {
        return getNBTTagBoolean(stack, TAG_PICKAPPA_COBBLESTONE);
    }

    
    
    
    @Override
    public void onCreated(ItemStack itemStack, World world, EntityPlayer player){
    	createNBTData(itemStack);
    }
    
    
    
    private static void createNBTData(ItemStack itemStack){
    	
    	NBTTagCompound data = new NBTTagCompound();
    	data.setBoolean(TAG_PICKAPPA_COBBLESTONE, false);
    	
    	data.setString(TAG_PICKAPPA_MODE_DESC, "Delete Cobblestone Disabled");
    	
    	data.setString(TAG_PICKAPPA_MODE, "-");
    	
    	itemStack.setTagInfo(TAG_PICKAPPA, data);
    	
    	
    }
    
    private static void updateNBTData(ItemStack itemStack, String tag, String value){
    	
    	if(itemStack.getTagCompound()!=null){
    		
    		NBTTagCompound data = itemStack.getTagCompound().getCompoundTag(TAG_PICKAPPA);
    		
    		data.setString(tag, value);
    		
    		itemStack.setTagInfo(TAG_PICKAPPA, data);
    		
    	}
    	
    }
    
    private static void updateNBTData(ItemStack itemStack, String tag, boolean bool){
    	
    	if(itemStack.getTagCompound()!=null){
    		
    		NBTTagCompound data = itemStack.getTagCompound().getCompoundTag(TAG_PICKAPPA);
    		
    		data.setBoolean(tag, bool);
    		
    		itemStack.setTagInfo(TAG_PICKAPPA, data);
    		
    	}
    	
    }


    
    private static boolean getNBTTagBoolean(ItemStack itemStack, String tag){
    	
    	if(itemStack.getTagCompound()!=null){
    		NBTTagCompound data = itemStack.getTagCompound().getCompoundTag(TAG_PICKAPPA);
    		return data.getBoolean(tag);
    	}
    	else{
    		return false;
    	}
    }
    
    private static String getNBTTagString(ItemStack itemStack, String tag){
    	
    	if(itemStack.getTagCompound()!=null){
    		NBTTagCompound data = itemStack.getTagCompound().getCompoundTag(TAG_PICKAPPA);
    		return data.getString(tag);
    	}
    	else{
    		return "-";
    	}
    }
    

    @Override
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool)
    {

    	String description = this.getNBTTagString(stack, TAG_PICKAPPA_MODE_DESC);
    	list.add(description);
    
    }
    

}

Link to comment
Share on other sites

Storing the descriptions in NBT is pointless, no one looks at them or know they're there but you. unless the go see it in an NBT explorer.

 

I use that here

 

@Override
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool)
    {

    	String description = this.getNBTTagString(stack, TAG_PICKAPPA_MODE_DESC);
    	list.add(description);
    
    }

Link to comment
Share on other sites

I mean - it works (I guess) so it's good, but that doesn't mean it isn't bad.

 

What I want to say is that when dealing with NBT: more utilities = more mess.

 

Seriously, NBT is literally Map<String, NBTBase> which can build up to some big structure, so having any utility methods such as get/set is just mess in code. Not only mess but also more operations, thus slower.

 

E.g: You are using:

getNBTTagBoolean(stack, TAG_PICKAPPA_COBBLESTONE);
deleteMode = getNBTTagString(stack, TAG_PICKAPPA_MODE);

Which requires almost 2 times more checks and getters on NBT. Totally pointless. If you need NBT - just get it once, make local NBTTagCompount reference and then read/write directly.

 

My opinion.

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

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.