Jump to content

Recommended Posts

Posted (edited)

Hi all,

I'm new to the modding APIs and working on a very simple mod with some customized inventories. I am able to modify the signal for comparators based on inventory contents, but adding/removing an item from the inventory doesn't send a block update to the connected comparator itself. Placing a block next to the comparator or otherwise causing an update will make it output the proper signal. How can I make my tile entity update the comparator when the inventory updates?

 

I assume I have to use a custom ItemStackHandler or something else to trigger a block update when items are inserted or removed, but I'm not sure if that's correct, nor am I sure how to proceed if it is correct. If it's not correct, my second assumption would be something in the update function?

 

Thanks for the help,

Edited by BlameTaw
Posted

Using capability system for the inventory (Hence ItemStackHandler) is highly recommended, but not required.

 

Please post the relevant code. (Block, TileEntity, probably the inventory as well)

+ here's the explanation on capability system; https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

There are many utility classes in net.minecraftforge.items package which are useful for setting up inventory as capability.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted (edited)

Thanks for the response.

 

I have not had any difficulty setting up the inventory at all. I am using the capability system for that and using an ItemStackHandler. Currently it just contains a single stack of items and that's it. I'll be extending the ItemStackHandler to create more specific handling of the items once I can get this basic part working.

 

My issue is with updating the adjacent comparator when the inventory's contents change. Currently you have to cause a block update to the comparator before it will switch to an updated redstone signal.
 

Here are some images of what I'm talking about:

Spoiler

No items in inventory:

XX7qDpO.png
 

Items added to inventory:

TnXlbJu.png

 

Block update to comparator, and it shows the proper signal:

ufFuyHg.png

 

Here is the code for my block and tile entity. Currently VERY simplistic. I just want to get redstone controls working before messing everything else up. :P

 

BlockBasicBuffer.java: 

package com.blametaw.itembuffers.blocks;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import com.blametaw.itembuffers.Reference;

public class BlockBasicBuffer extends BlockContainer {

	public BlockBasicBuffer() {
		super(Material.IRON);
		setUnlocalizedName(Reference.ItemBufferBlocks.BASICBUFFER.getUnlocalizedName());
		setRegistryName(Reference.ItemBufferBlocks.BASICBUFFER.getRegistryName());
		
		setResistance(6.0f);
	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntityBasicBuffer();
	}
	
	@Override
	public void breakBlock(World world, BlockPos pos, IBlockState state) {
		TileEntityBasicBuffer te = (TileEntityBasicBuffer) world.getTileEntity(pos);
		IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
		for(int slot = 0; slot < handler.getSlots(); slot++){
			ItemStack stack = handler.getStackInSlot(slot);
			InventoryHelper.spawnItemStack(world,pos.getX(),pos.getY(),pos.getZ(),stack);
		}
		super.breakBlock(world, pos, state);
	}
	
	@Override
	public boolean hasTileEntity(IBlockState state){
		return true;
	}
	
	@SideOnly(Side.CLIENT)
	public BlockRenderLayer getBlockLayer()
	{
		return BlockRenderLayer.SOLID;
	}
	
	@Override
	public EnumBlockRenderType getRenderType(IBlockState iBlockState) {
		return EnumBlockRenderType.MODEL;
	}

	@Override
	public boolean hasComparatorInputOverride(IBlockState state) {
		return true;
	}
	
	@Override
	public int getComparatorInputOverride(IBlockState state, World world, BlockPos pos){
		TileEntityBasicBuffer te = (TileEntityBasicBuffer) world.getTileEntity(pos);
		IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
		if (handler.getStackInSlot(0) != null && handler.getStackInSlot(0).getCount() > 0){
			//Return 15 for now. Eventually will modulate based on inventory space.
			return 15;
		}
		return 0;
	}
}

 

TileEntityBasicBuffer.java:

package com.blametaw.itembuffers.blocks;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityBasicBuffer extends TileEntity implements ICapabilityProvider{
	
	private ItemStackHandler handler;
	
	public TileEntityBasicBuffer(){
		//Currently just a stack of 1 item.
		this.handler = new ItemStackHandler(1);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		//TODO: Read stuff here
		this.handler.deserializeNBT(nbt.getCompoundTag("ItemStackHandler"));
		super.readFromNBT(nbt);
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		//TODO: Write stuff here
		nbt.setTag("ItemStackHandler", this.handler.serializeNBT());
		return super.writeToNBT(nbt);
	}
	
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return (T) this.handler;
		}
		return super.getCapability(capability, facing);
	}
	
	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return true;
		}
		return super.hasCapability(capability, facing);
	}
}

 

Edited by BlameTaw
Added pictures.
Posted

I think you should override ItemStackHandler, and call the comparator update on any method which changes the inventory contents. (On drain/fill call when doDrain/doFill is true)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

How do I get a reference to the world from the ItemStackHandler object? Would I just have to pass a reference to the TileEntity itself in the ItemStackHandler subclass? I was hoping somehow inserting an item could trigger an update on the block itself and from there I could trigger another block update on the surrounding blocks to make the comparator update. It could easily be that I just don't understand the flow of the minecraft code yet though and adding a TileEntity reference would be the best option.

Posted
1 hour ago, BlameTaw said:

How do I get a reference to the world from the ItemStackHandler object? Would I just have to pass a reference to the TileEntity itself in the ItemStackHandler subclass? I was hoping somehow inserting an item could trigger an update on the block itself and from there I could trigger another block update on the surrounding blocks to make the comparator update. It could easily be that I just don't understand the flow of the minecraft code yet though and adding a TileEntity reference would be the best option.

Yes, you have to pass the tileentity reference to the inventory capability.

Usually item is inserted/extracted through the capability including hoppers and modded pipes, so it's the best place to check for inventory change and apply the comparator update.

 

Besides, forget about doFill. It was about fluids.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.