Jump to content

[1.7.10] [Resolved] How to check Item in Slot - TileEntity Interface


Alanzote

Recommended Posts

  • 3 months later...

Hi, sorry for the delay, I've been searching for a way to do this, I think I found it:

public void updateEntity(){
          if(ItemStack.areItemStacksEqual(new ItemStack(Item.Cobblestone), slots))
          {
              power += 10;
          }
          else
          {

          }
}

I need to check the Item in slot, here is my ItemStack variable that stores the content:

private ItemStack[] slots = new ItemStack[2];

but It says that I am using a wrong type of ItemStack in the 2Nd variable of the Content, here is all my TileEntity Code:

package com.gpa.startrekmod.tileentities;

import java.util.List;

import com.gpa.startrekmod.blocks.STModBlocks;
import com.gpa.startrekmod.items.STModItems;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityReplicatorBlock extends TileEntity implements ISidedInventory {
private ItemStack[] slots = new ItemStack[2];
private final String IventoryName = "Replicator";
private int facingDirection;
public int maxPower = 100000;
public float power;
public void updateEntity(){
if(ItemStack.areItemStacksEqual(new ItemStack(STModItems.EnergyCell), slots))
{
	power += 10;
}
else
{

}
if(power > maxPower) power = maxPower;
}
public int getPowerScaled(int scaled)
{
	return (int) (this.power * scaled / this.maxPower);
}
@Override
public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	facingDirection = nbt.getInteger("facingDirection");
	NBTTagList list = nbt.getTagList("Slots", 10);
	this.slots = new ItemStack[getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++){
		NBTTagCompound  item = list.getCompoundTagAt(i);
		byte b = item.getByte("Item");

		if(b >= 0 && b < this.slots.length){
			this.slots[b] = ItemStack.loadItemStackFromNBT(item);
		}
	}

	this.power = nbt.getInteger("REPPower");

	if(nbt.hasKey("CustomName")){
		this.setInventoryName(nbt.getString("CustomName"));
	}
}
@Override
public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	nbt.setFloat("REPPower", this.power);
	nbt.setInteger("facingDirection", facingDirection);
	NBTTagList list = new NBTTagList();

	for(int i = 0; i < this.slots.length; i++)
	{
		if(this.slots[i] != null){
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("item", (byte)i);
			this.slots[i].writeToNBT(item);
			list.appendTag(item);
		}
	}
	nbt.setTag("Slots", list);

	if(this.hasCustomInventoryName()){
		nbt.setString("CustomName", this.getInventoryName());
	}
}

public int getSizeInventory() {
	return this.slots.length;
}
public ItemStack getStackInSlot(int i) {
	return this.slots[i];
}
public ItemStack decrStackSize(int i, int j) {
	if(this.slots[i] != null)
	{
		ItemStack itemstack;
		if(this.slots[i].stackSize <= j)
		{
			itemstack = this.slots[i];
			this.slots[i] = null;
			return itemstack;
		}else{
			itemstack = this.slots[i].splitStack(j);
			if(this.slots[i].stackSize == 0)
			{
				this.slots[i] = null;
			}
			return itemstack;
		}
	}
	return null;
}
public ItemStack getStackInSlotOnClosing(int i) {
	if(this.slots[i] != null)
	{
		ItemStack itemstack = this.slots[i];
		this.slots[i] = null;
		return itemstack;
	}
	return null;
}
public void setInventorySlotContents(int i, ItemStack itemstack) {
	this.slots[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
		itemstack.stackSize = this.getInventoryStackLimit();
	}
}
public String getInventoryName() {
	return null;
}
public void setInventoryName() {

}
public boolean hasCustomInventoryName() {
	return false;
}
public int getInventoryStackLimit() {
	return 64;
}
public void setInventoryName(String string){

}
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	return false;
}
public void openInventory() {
}
public void closeInventory() {
}
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
	return false;
}
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
	return null;
}
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_,
		int p_102007_3_) {
	return false;
}
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_,
		int p_102008_3_) {
	return false;
}
@Override
public Packet getDescriptionPacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
}
}

For some reason the spoiler tab is not working.

 

Thanks.

Link to comment
Share on other sites

I used slots with the ID of the slot after it:

slots[1]

I got no error, but I am getting a minecraft crash Report, It's about the ticking:

 

 

---- Minecraft Crash Report ----

// You should try our sister game, Minceraft!

 

Time: 13/02/15 19:10

Description: Ticking block entity

 

java.lang.ArrayIndexOutOfBoundsException: 3

at com.gpa.startrekmod.tileentities.TileEntityReplicatorBlock.updateEntity(TileEntityReplicatorBlock.java:28)

at net.minecraft.world.World.updateEntities(World.java:2160)

at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:515)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:703)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at com.gpa.startrekmod.tileentities.TileEntityReplicatorBlock.updateEntity(TileEntityReplicatorBlock.java:28)

 

-- Block entity being ticked --

Details:

Name: TileEntityReplicatorBlock // com.gpa.startrekmod.tileentities.TileEntityReplicatorBlock

Block type: ID #165 (tile.Replicator // com.gpa.startrekmod.blocks.STReplicator)

Block data value: 0 / 0x0 / 0b0000

Block location: World: (-30,70,276), Chunk: (at 2,4,4 in -2,17; contains blocks -32,0,272 to -17,255,287), Region: (-1,0; contains chunks -32,0 to -1,31, blocks -512,0,0 to -1,255,511)

Actual block type: ID #165 (tile.Replicator // com.gpa.startrekmod.blocks.STReplicator)

Actual block data value: 0 / 0x0 / 0b0000

Stacktrace:

at net.minecraft.world.World.updateEntities(World.java:2160)

at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:515)

 

-- Affected level --

Details:

Level name: ST

All players: 0 total; []

Chunk stats: ServerChunkCache: 625 Drop: 0

Level seed: -4128128808810088779

Level generator: ID 00 - default, ver 1. Features enabled: true

Level generator options:

Level spawn location: World: (-32,64,251), Chunk: (at 0,4,11 in -2,15; contains blocks -32,0,240 to -17,255,255), Region: (-1,0; contains chunks -32,0 to -1,31, blocks -512,0,0 to -1,255,511)

Level time: 28892 game time, 717 day time

Level dimension: 0

Level storage version: 0x04ABD - Anvil

Level weather: Rain time: 11822 (now: false), thunder time: 37238 (now: false)

Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true

Stacktrace:

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:703)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752)

 

-- System Details --

Details:

Minecraft Version: 1.7.10

Operating System: Windows 8.1 (amd64) version 6.3

Java Version: 1.8.0_11, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 738745824 bytes (704 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

IntCache: cache: 0, tcache: 0, allocated: 12, tallocated: 94

FML: MCP v9.05 FML v7.10.85.1291 Minecraft Forge 10.13.2.1291 4 mods loaded, 4 mods active

mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

FML{7.10.85.1291} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.2.1291.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Forge{10.13.2.1291} [Minecraft Forge] (forgeSrc-1.7.10-10.13.2.1291.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

gpa_startrekmod{0.1.2} [star Trek Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Profiler Position: N/A (disabled)

Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

Player Count: 0 / 8; []

Type: Integrated Server (map_client.txt)

Is Modded: Definitely; Client brand changed to 'fml,forge')

 

 

Thanks!

Link to comment
Share on other sites

I've tried accessing these arrays: 1 and 2.

It is still now working.

private ItemStack[] slots = new ItemStack[2];

2 elements, meaning indices 0 and 1.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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.



×
×
  • Create New...

Important Information

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