Jump to content

Update comparator signal when inventory changes


BlameTaw

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
  • Topics

×
×
  • Create New...

Important Information

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