Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have coded a custom generator and it functions 100% correctly apart from one issue.

 

When a hopper is attached and fuel items fed in, the first item is consumed without producing any energy.

 

This does not happen when fuel items are added via the inventory.

 

The relevant code:

 


public class TileEntityGenerator extends TileEntity implements ITickable
{
	public ItemStackHandler handler = new ItemStackHandler(1);
	public EnergyStorage storage = new EnergyStorage(100000);
	// energy is used as an easy means of saving level of storage to NBT.
	public int energy = storage.getEnergyStored();	
	public int burnTime = 0;
	public int maxItemBurnTime = 0;
	public int producingEnergy;
	
	
	@Override
	public void update() 
	{ 	
		if (burnTime == 0 && !handler.getStackInSlot(0).isEmpty() ) 
		{
			maxItemBurnTime = getItemMaxBurnTime(handler.getStackInSlot(0));
		}
		
		IBlockState iblockstate = world.getBlockState(pos);
		final int FLAGS = 3;  // Block update + send change to clients.
		world.notifyBlockUpdate(pos, iblockstate, iblockstate, FLAGS);
		
		producingEnergy = 0;
		if ( maxItemBurnTime > 0	&& !(storage.getEnergyStored() == storage.getMaxEnergyStored()) )
		{	
			if (burnTime == 0) {handler.getStackInSlot(0).shrink(1);}			
			producingEnergy = 1;
			burnTime++;
			storage.receiveEnergy(20, false);
			energy += 20;
			if (burnTime >= maxItemBurnTime)
			{
				burnTime = 0;
				maxItemBurnTime = 0;
			}			
		}
		
	}
	
		
	// returns the number of ticks the given item will burn. Returns 0 if the given item is not a valid fuel
	public static short getItemMaxBurnTime(ItemStack stack)
	{
		int burntime = TileEntityFurnace.getItemBurnTime(stack);  // just use the vanilla values
		return (short)MathHelper.clamp(burntime, 0, Short.MAX_VALUE);
	}


 

If I remove the line: if (burnTime == 0) {handler.getStackInSlot(0).shrink(1);}

The item is actually fed in from the hopper, which seems to indicate that the issue is with my code block rather than with Item Handlers, etc.

 

My Capabilities  just in case:

@SuppressWarnings("unchecked")
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) 
	{
		if(capability == CapabilityEnergy.ENERGY) return (T)this.storage;
		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 == CapabilityEnergy.ENERGY) return true;
		if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true;
		return super.hasCapability(capability, facing);
	}
	

This is my first forray into using Capabilities, but my limited understanding looking through the hopper code  tells me that it drives the fuel item insert, so I wonder if this could cause an issue.

I welcome any help, as I'm stumped with this one at the moment.

  • Author

Thanks for pointing those out, I will rethink & rewrite with both points in mind.

This illustrates once again the danger of using popular modding guides/examples to model ones code on, as all 3 I looked through do this ItemStack modification & run the update code on both client & server.

 

 

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.