Jump to content

Block that holds only one item.


gmod622

Recommended Posts

Hey all!

 

So I'm creating a 'jar' that can hold 128 of an item. However, I can put multiple things in there, and this happens:

-5 seeds > places one sugarcane > 6 sugar cane inside the jar.

 

here is the onactivated:

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {

		if(!worldIn.isRemote) {
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			if(tileEntity instanceof TileEntityJar) {
				TileEntityJar jar = (TileEntityJar) tileEntity;
				if(heldItem != null) {
					jar.itemStored = heldItem.getItem();
					if(jar.itemStored == heldItem.getItem()) {
						if(jar.addItem()) {
							heldItem.stackSize--;
							return true;
					}
				}
				}
				jar.removeItem();
			}
		}
	return true;
}

 

I know exactly whats wrong here though, the heldItem is getting updated to a different item and my jar is accepting it, how would I make sure this doesnt happen?

 

here is the tileentity:

 

public class TileEntityJar extends TileEntity {

public int itemCount = 0;
public Item itemStored;

public boolean addItem() {
	if(itemCount < 128) {
		itemCount++;
		return true;
	}
	return false;
}

	public void removeItem() {
		if(!worldObj.isRemote) {
				if(itemCount > 0) {
					worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 1, new ItemStack(itemStored)));
					itemCount--;
				}
		}
	}


	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("itemCount", this.itemCount);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.itemCount = compound.getInteger("itemCount");
	}
}

Not new to java >> New to modding.

Link to comment
Share on other sites

Hey all!

 

So I'm creating a 'jar' that can hold 128 of an item. However, I can put multiple things in there, and this happens:

-5 seeds > places one sugarcane > 6 sugar cane inside the jar.

 

here is the onactivated:

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {

		if(!worldIn.isRemote) {
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			if(tileEntity instanceof TileEntityJar) {
				TileEntityJar jar = (TileEntityJar) tileEntity;
				if(heldItem != null) {
					jar.itemStored = heldItem.getItem();
					if(jar.itemStored == heldItem.getItem()) {
						if(jar.addItem()) {
							heldItem.stackSize--;
							return true;
					}
				}
				}
				jar.removeItem();
			}
		}
	return true;
}

 

I know exactly whats wrong here though, the heldItem is getting updated to a different item and my jar is accepting it, how would I make sure this doesnt happen?

 

here is the tileentity:

 

public class TileEntityJar extends TileEntity {

public int itemCount = 0;
public Item itemStored;

public boolean addItem() {
	if(itemCount < 128) {
		itemCount++;
		return true;
	}
	return false;
}

	public void removeItem() {
		if(!worldObj.isRemote) {
				if(itemCount > 0) {
					worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 1, new ItemStack(itemStored)));
					itemCount--;
				}
		}
	}


	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("itemCount", this.itemCount);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.itemCount = compound.getInteger("itemCount");
	}
}

Remove this line then start from there

jar.itemStored = heldItem.getItem();

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

jar.itemStored = heldItem.getItem(); //make the stored item the held item
if(jar.itemStored == heldItem.getItem()) { //now check if they're the same (of course they are!)

 

 

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

Yeah done this, however, it just won't let me put items in because it doesn't know what item needs to be stored.

 

What I want to happen is for one single item, when put it, to be whitelisted, while all others are blacklisted. until the whitelisted item is removed.

Not new to java >> New to modding.

Link to comment
Share on other sites

if(jar.itemStored == null) { //do something } ?

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

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {

		if(!worldIn.isRemote) {
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			if(tileEntity instanceof TileEntityJar) {
				TileEntityJar jar = (TileEntityJar) tileEntity;
				if(heldItem != null) {
					if(jar.itemStored == null) {
						jar.itemStored = heldItem.getItem();
					if(jar.itemStored == heldItem.getItem()) {
						if(jar.addItem()) {
							heldItem.stackSize--;
							return true;
							}
						}
					}
				}
				jar.removeItem();
			}
		}
	return true;
}

 

This allows me to put ONE seed in, then it extracts it and won't let me put anything in..

I'm feel really dumb, I have the problem in my head, but I just cant express it :/

Not new to java >> New to modding.

Link to comment
Share on other sites

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {

		if(!worldIn.isRemote) {
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			if(tileEntity instanceof TileEntityJar) {
				TileEntityJar jar = (TileEntityJar) tileEntity;
				if(heldItem != null) {
					if(jar.itemStored == null) {
						jar.itemStored = heldItem.getItem();
					if(jar.itemStored == heldItem.getItem()) {
						if(jar.addItem()) {
							heldItem.stackSize--;
							return true;
							}
						}
					}
				}
				jar.removeItem();
			}
		}
	return true;
}

 

This allows me to put ONE seed in, then it extracts it and won't let me put anything in..

I'm feel really dumb, I have the problem in my head, but I just cant express it :/

Remove the semi colon at the end of this line then fix the syntax error that shows up

if(jar.itemStored == null) {

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Whoops!

 

Its working great now!! Thanks!!

 

Actually it still whitelist the seed, even when there are none in there.

So:

10 seeds>extract>wont let flower in

 

Okay fixed it by setting the itemStored to null if itemCount is 0

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay another issue, on relog my items disappear. I do indeed have nbt tags being assigned but doesn't seem to work.

Here is the tile

package com.lambda.PlentifulMisc.blocks.tile;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityJar extends TileEntity {

public int itemCount = 0;
public Item itemStored;

public boolean addItem() {
	if(itemCount < 128) {
		itemCount++;
		markDirty();
		IBlockState state = worldObj.getBlockState(pos);
		worldObj.notifyBlockUpdate(pos, state, state, 3);
		return true;
	}
	return false;
}

	public void removeItem() {
		if(!worldObj.isRemote) {
				if(itemCount > 0) {
					worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 1, new ItemStack(itemStored)));
					itemCount--;
					markDirty();
					IBlockState state = worldObj.getBlockState(pos);
					worldObj.notifyBlockUpdate(pos, state, state, 3);
				}
		}
	}


	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("itemCount", this.itemCount);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.itemCount = compound.getInteger("itemCount");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound tag = pkt.getNbtCompound();
		this.readUpdateTag(tag);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound tag = new NBTTagCompound();
		this.writeUpdateTag(tag);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound tag = super.getUpdateTag();
		writeUpdateTag(tag);
		return tag;
	}

	public void writeUpdateTag(NBTTagCompound tag) {
		tag.setInteger("itemCount", this.itemCount);
	}

	public void readUpdateTag(NBTTagCompound tag) {
		this.itemCount = tag.getInteger("itemCount");
	}
}

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay another issue, on relog my items disappear. I do indeed have nbt tags being assigned but doesn't seem to work.

Here is the tile

package com.lambda.PlentifulMisc.blocks.tile;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityJar extends TileEntity {

public int itemCount = 0;
public Item itemStored;

public boolean addItem() {
	if(itemCount < 128) {
		itemCount++;
		markDirty();
		IBlockState state = worldObj.getBlockState(pos);
		worldObj.notifyBlockUpdate(pos, state, state, 3);
		return true;
	}
	return false;
}

	public void removeItem() {
		if(!worldObj.isRemote) {
				if(itemCount > 0) {
					worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 1, new ItemStack(itemStored)));
					itemCount--;
					markDirty();
					IBlockState state = worldObj.getBlockState(pos);
					worldObj.notifyBlockUpdate(pos, state, state, 3);
				}
		}
	}


	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("itemCount", this.itemCount);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.itemCount = compound.getInteger("itemCount");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound tag = pkt.getNbtCompound();
		this.readUpdateTag(tag);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound tag = new NBTTagCompound();
		this.writeUpdateTag(tag);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound tag = super.getUpdateTag();
		writeUpdateTag(tag);
		return tag;
	}

	public void writeUpdateTag(NBTTagCompound tag) {
		tag.setInteger("itemCount", this.itemCount);
	}

	public void readUpdateTag(NBTTagCompound tag) {
		this.itemCount = tag.getInteger("itemCount");
	}
}

You are not saving what item you have.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Ah, How would I store an Item / ItemStack

Youc could store an Items registry name. I don't know the code to save an ItemStack off of the top of my head, but all other storage TEs do it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

You mean

ItemStack#writeToNBT(...)

?

Gosh, so hard.

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

You mean

ItemStack#writeToNBT(...)

?

Gosh, so hard.

Sorry, its been a long time since i have had to save itemstacks, or rather type the line of code.  :-[

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Also, just as a heads-up to the OP:

Checking the stored item stack's Item isn't sufficient.  Blue, red, white, etc. wool will all be detected as "same" and get inserted.

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

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.