Jump to content

Help checking if an item is valid for a slot.


mason12041204

Recommended Posts

So I wrote some code in the "isItemValidForSlot" method in my TileEntity class. It seems like it should work, but I'm obviously doing something wrong as it doesn't work when I load up the game. I can still put whatever I want into my container. Code is down below. Thanks for helping.

 

 

package donut.mce.tileEntity;

 

import donut.mce.Reference;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Items;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.tileentity.TileEntity;

 

public class TileEntityRecordStand extends TileEntity implements IInventory {

 

private ItemStack[] inventory;

private String customName;

 

 

 

// Change TileEntityRecordStand to the new block name.

public TileEntityRecordStand() {

this.inventory = new ItemStack[this.getSizeInventory()];

}

 

public String getCustomName() {

return this.customName;

}

 

public void setCustomName(String customName) {

this.customName = customName;

}

 

// Make sure to change string to new block name.

@Override

public String getName() {

return this.hasCustomName() ? this.customName : Reference.MOD_ID + "TileEntityRecordStand";

}

 

@Override

public boolean hasCustomName() {

return this.customName != null && !this.customName.equals("");

}

 

// This is the amount of slots the GUI is going to have.

@Override

public int getSizeInventory() {

return 12;

}

 

@Override

public ItemStack getStackInSlot(int index) {

if(index < 0 || index >= this.getSizeInventory()) {

return null;

}

return this.inventory[index];

}

 

@Override

public ItemStack decrStackSize(int index, int count) {

//ItemStack stack = getStackInSlot(index);

if(this.getStackInSlot(index) != null) {

ItemStack itemstack;

 

if(this.getStackInSlot(index).stackSize <= count) {

itemstack = this.getStackInSlot(index);

this.setInventorySlotContents(index, null);

this.markDirty();

return itemstack;

} else {

itemstack = this.getStackInSlot(index).splitStack(count);

 

if(this.getStackInSlot(index).stackSize <= 0) {

this.setInventorySlotContents(index, null);

} else {

this.setInventorySlotContents(index, this.getStackInSlot(index));

}

 

this.markDirty();

return itemstack;

}

} else {

return null;

}

}

 

@Override

public ItemStack removeStackFromSlot(int index) {

ItemStack stack = this.getStackInSlot(index);

this.setInventorySlotContents(index, null);

return stack;

}

 

@Override

public void setInventorySlotContents(int index, ItemStack stack) {

if(index < 0 || index >= this.getSizeInventory())

return;

 

if(stack != null && stack.stackSize > this.getInventoryStackLimit())

stack.stackSize = this.getInventoryStackLimit();

 

if(stack != null && stack.stackSize == 0)

stack = null;

 

this.inventory[index] = stack;

this.markDirty();

}

 

// This is the amount of items can be in a stack once they are in a GUI slot.

@Override

public int getInventoryStackLimit() {

return 1;

}

 

@Override

public boolean isUseableByPlayer(EntityPlayer player) {

return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 36;

}

 

// This will be triggered upon opening the GUI.

@Override

public void openInventory(EntityPlayer player) {

 

}

 

// This will be triggered upon closing the GUI.

@Override

public void closeInventory(EntityPlayer player) {

 

}

 

@Override

public boolean isItemValidForSlot(int index, ItemStack stack) {

if(this.inventory[0].getItem() == Items.RECORD_11) {

return true;

}

return false;

}

 

@Override

public int getField(int id) {

return 0;

}

 

@Override

public void setField(int id, int value) {

 

}

 

@Override

public int getFieldCount() {

return 0;

}

 

@Override

public void clear() {

for(int i = 0; i < this.getSizeInventory(); i++) {

this.setInventorySlotContents(i, null);

}

}

 

@Override

public NBTTagCompound writeToNBT(NBTTagCompound nbt) {

super.writeToNBT(nbt);

 

NBTTagList list = new NBTTagList();

for(int i = 0; i < this.getSizeInventory(); ++i) {

if(this.getStackInSlot(i) != null) {

NBTTagCompound stackTag = new NBTTagCompound();

stackTag.setByte("Slot", ((byte)i));

this.getStackInSlot(i).writeToNBT(stackTag);

list.appendTag(stackTag);

}

}

nbt.setTag("Items", list);

 

if(this.hasCustomName()) {

nbt.setString("CustomName", this.getCustomName());

}

return nbt;

}

 

@Override

public void readFromNBT(NBTTagCompound nbt) {

super.readFromNBT(nbt);

 

NBTTagList list = nbt.getTagList("Items", 10);

for(int i = 0; i < list.tagCount(); ++i) {

NBTTagCompound stackTag = list.getCompoundTagAt(i);

int slot = stackTag.getByte("Slot") & 255;

this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));

}

 

if(nbt.hasKey("CustomName", 8)) {

this.setCustomName(nbt.getString("CustomName"));

}

}

 

}

 

Link to comment
Share on other sites

So I wrote some code in the "isItemValidForSlot" method in my TileEntity class. It seems like it should work, but I'm obviously doing something wrong as it doesn't work when I load up the game. I can still put whatever I want into my container. Code is down below. Thanks for helping.

 

 

package donut.mce.tileEntity;

 

import donut.mce.Reference;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Items;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.tileentity.TileEntity;

 

public class TileEntityRecordStand extends TileEntity implements IInventory {

 

private ItemStack[] inventory;

private String customName;

 

 

 

// Change TileEntityRecordStand to the new block name.

public TileEntityRecordStand() {

this.inventory = new ItemStack[this.getSizeInventory()];

}

 

public String getCustomName() {

return this.customName;

}

 

public void setCustomName(String customName) {

this.customName = customName;

}

 

// Make sure to change string to new block name.

@Override

public String getName() {

return this.hasCustomName() ? this.customName : Reference.MOD_ID + "TileEntityRecordStand";

}

 

@Override

public boolean hasCustomName() {

return this.customName != null && !this.customName.equals("");

}

 

// This is the amount of slots the GUI is going to have.

@Override

public int getSizeInventory() {

return 12;

}

 

@Override

public ItemStack getStackInSlot(int index) {

if(index < 0 || index >= this.getSizeInventory()) {

return null;

}

return this.inventory[index];

}

 

@Override

public ItemStack decrStackSize(int index, int count) {

//ItemStack stack = getStackInSlot(index);

if(this.getStackInSlot(index) != null) {

ItemStack itemstack;

 

if(this.getStackInSlot(index).stackSize <= count) {

itemstack = this.getStackInSlot(index);

this.setInventorySlotContents(index, null);

this.markDirty();

return itemstack;

} else {

itemstack = this.getStackInSlot(index).splitStack(count);

 

if(this.getStackInSlot(index).stackSize <= 0) {

this.setInventorySlotContents(index, null);

} else {

this.setInventorySlotContents(index, this.getStackInSlot(index));

}

 

this.markDirty();

return itemstack;

}

} else {

return null;

}

}

 

@Override

public ItemStack removeStackFromSlot(int index) {

ItemStack stack = this.getStackInSlot(index);

this.setInventorySlotContents(index, null);

return stack;

}

 

@Override

public void setInventorySlotContents(int index, ItemStack stack) {

if(index < 0 || index >= this.getSizeInventory())

return;

 

if(stack != null && stack.stackSize > this.getInventoryStackLimit())

stack.stackSize = this.getInventoryStackLimit();

 

if(stack != null && stack.stackSize == 0)

stack = null;

 

this.inventory[index] = stack;

this.markDirty();

}

 

// This is the amount of items can be in a stack once they are in a GUI slot.

@Override

public int getInventoryStackLimit() {

return 1;

}

 

@Override

public boolean isUseableByPlayer(EntityPlayer player) {

return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 36;

}

 

// This will be triggered upon opening the GUI.

@Override

public void openInventory(EntityPlayer player) {

 

}

 

// This will be triggered upon closing the GUI.

@Override

public void closeInventory(EntityPlayer player) {

 

}

 

@Override

public boolean isItemValidForSlot(int index, ItemStack stack) {

if(this.inventory[0].getItem() == Items.RECORD_11) {

return true;

}

return false;

}

 

@Override

public int getField(int id) {

return 0;

}

 

@Override

public void setField(int id, int value) {

 

}

 

@Override

public int getFieldCount() {

return 0;

}

 

@Override

public void clear() {

for(int i = 0; i < this.getSizeInventory(); i++) {

this.setInventorySlotContents(i, null);

}

}

 

@Override

public NBTTagCompound writeToNBT(NBTTagCompound nbt) {

super.writeToNBT(nbt);

 

NBTTagList list = new NBTTagList();

for(int i = 0; i < this.getSizeInventory(); ++i) {

if(this.getStackInSlot(i) != null) {

NBTTagCompound stackTag = new NBTTagCompound();

stackTag.setByte("Slot", ((byte)i));

this.getStackInSlot(i).writeToNBT(stackTag);

list.appendTag(stackTag);

}

}

nbt.setTag("Items", list);

 

if(this.hasCustomName()) {

nbt.setString("CustomName", this.getCustomName());

}

return nbt;

}

 

@Override

public void readFromNBT(NBTTagCompound nbt) {

super.readFromNBT(nbt);

 

NBTTagList list = nbt.getTagList("Items", 10);

for(int i = 0; i < list.tagCount(); ++i) {

NBTTagCompound stackTag = list.getCompoundTagAt(i);

int slot = stackTag.getByte("Slot") & 255;

this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));

}

 

if(nbt.hasKey("CustomName", 8)) {

this.setCustomName(nbt.getString("CustomName"));

}

}

 

}

 

Switch over to the IItemHandler capability.

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

GUIs bypass isItemValid checks, you will also need a custom Slot class.

 

Also switch over to capabilities.

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

Okay, I got it to work, but I'm still having trouble rendering more than one TESR item in my tile entity. I'm trying to render a couple records, but whenever I try and rotate or translate my second disc, it is not doing what it's supposed to. Like I'll edit one of the axis and it will move it on the X and Y axis WTF!? I don't know if I have to do something to render more than 1 item. It could be that the second disc is still being affected from the first disc rotations and translations, idk. Any ideas? Thanks!

 

 

package donut.mce.render;

 

import donut.mce.tileEntity.TileEntityRecordStand;

import net.minecraft.client.Minecraft;

import net.minecraft.client.renderer.GlStateManager;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.entity.item.EntityItem;

import net.minecraft.init.Items;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

 

public class TileEntityRecordStandRenderer extends TileEntitySpecialRenderer<TileEntityRecordStand> {

 

// The item to be spawned in, is whatever you put where Items.RECORD_CAT is.

private static final EntityItem ITEM = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, new ItemStack(Items.RECORD_CAT));

private static final EntityItem ITEM2 = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, new ItemStack(Items.RECORD_BLOCKS));

 

// Change TileEntityRecordStand to the new block name.

@Override

public void renderTileEntityAt(TileEntityRecordStand te, double x, double y, double z, float partialTicks, int destroyStage) {

super.renderTileEntityAt(te, x, y, z, partialTicks, destroyStage);

 

ITEM.hoverStart = 0F;

 

// This is to translate render items.

GlStateManager.pushMatrix(); {

GlStateManager.translate(x, y, z);

GlStateManager.rotate(92F, 0, 1, 0);

GlStateManager.rotate(20F, 0.25F, 0, 0);

GlStateManager.translate(-0.4, 0.29, -0.03);

Minecraft.getMinecraft().getRenderManager().doRenderEntity(ITEM, 0, 0, 0, 0F, 0F, false);

}

GlStateManager.popMatrix();

 

GlStateManager.pushMatrix(); {

GlStateManager.translate(x, y, z);

GlStateManager.translate(0.8, 0.2, 0.3);

GlStateManager.rotate(10F, 0, 0, 0.3F);

Minecraft.getMinecraft().getRenderManager().doRenderEntity(ITEM2, 0, 0, 0, 0F, 0F, false);

}

GlStateManager.popMatrix();

}

}

 

 

Link to comment
Share on other sites

static

 

Do you know what that means?

 

Also, you set the HoverStart for the one item, but not the other.

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

Shouldn't you be getting your items from the TE instance?

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

Don't think so? The records are rendering in just fine, I just am wondering how I render in the first record. And then render in the second one and so on. When I try to render in the second record, it seems to be doing all the translations and rotations I did to the first record. How do I stop the first set of translations and rotations from affecting my second record?

Link to comment
Share on other sites

Don't think so? The records are rendering in just fine, I just am wondering how I render in the first record. And then render in the second one and so on. When I try to render in the second record, it seems to be doing all the translations and rotations I did to the first record. How do I stop the first set of translations and rotations from affecting my second record?

 

pushMatrix and popMatrix, which you're already doing.

I suspect the reason is that

 

Also, you set the HoverStart for the one item, but not the other.

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.