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:

  Reveal hidden contents

 

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
  On 5/11/2017 at 3:14 AM, 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.

Expand  

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Dear Brunoe Quick Hack (Team) Thank you so much for your remarkable help in recovering my funds after I was scammed. As a busy surgeon and single mother to three children, the loss was overwhelming.Web-Site: Brunoequickhack.COM Your team's hard work, expertise, and unwavering support gave me hope and helped restore my financial stability. I admire your professionalism and the way you genuinely care about your clients. I highly recommend your services to anyone who has fallen prey to scammers. You are truly life savers. Whats-App: +1705-(7842)-635 With heartfelt thanks, Contact information: BrunoequickhackATgmail.cOM Dear Brunoe Quick Hack (Team) Thank you so much for your remarkable help in recovering my funds after I was scammed. As a busy surgeon and single mother to three children, the loss was overwhelming.Web-Site: Brunoequickhack.COM Your team's hard work, expertise, and unwavering support gave me hope and helped restore my financial stability. I admire your professionalism and the way you genuinely care about your clients. I highly recommend your services to anyone who has fallen prey to scammers. You are truly life savers. Whats-App: +1705-(7842)-635 With heartfelt thanks, Contact information: BrunoequickhackATgmail.cOM
    • ok i tried to disable the last mod's i installed and it is some of then, so now im going to do the classic enable and disable trick to find out which mod cause the crash. thanks for the help  
    • ok  make it work (java not being recognize) by downloading the zip putting it in the java folder and manually setting the path but is still give the same crush report https://pastebin.com/PPmR6jN0
    • Install this one: https://www.azul.com/downloads/?version=java-17-lts&os=windows&architecture=x86-64-bit&package=jdk#zulu After installation, set the java version in your Launcher / Java settings
    • it say it requires java 17 so when i go to install it is say "the installer has encounter an unexpected error installing this package." error code 2503 and 2502. https://pastebin.com/AL6AHMM9
  • Topics

×
×
  • Create New...

Important Information

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