1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-07 04:22:53 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/turtle/FurnaceRefuelHandler.java
Jonathan Coates bb1183d274
Update to Minecraft 1.17.1
- Remap everything to use MojMap class names too. This is what Forge
   uses, so \o/.

   This does NOT currently rename any classes to use suffix notation or
   BlockEntity. That will come in a later change. We do however rename
   references of "World" to "Level".

 - Move the test mod into a separate "cctest" source set. As Forge now
   works using Jigsaw we cannot have multiple mods defining the same
   package, which causes problems with our JUnit test classes.

 - Remove our custom test framework and replace it with vanilla's (this
   is no longer stripped from the jar). RIP kotlin coroutines.

   It's still worth using Kotlin here though, just for extension
   methods.

 - Other 1.17-related changes:
    - Use separate tile/block entity tick methods for server and client
      side, often avoiding ticking at all on the client.

    - Switch much of the monitor rendering code to use vanilla's
      built-in shader system. It's still an incredibly ugly hack, so not
      really expecting this to work with any rendering mods, but we'll
      cross that bridge when we come to it.
2021-08-06 17:18:09 +01:00

64 lines
2.4 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.turtle;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.event.TurtleRefuelEvent;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import javax.annotation.Nonnull;
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
public final class FurnaceRefuelHandler implements TurtleRefuelEvent.Handler
{
private static final FurnaceRefuelHandler INSTANCE = new FurnaceRefuelHandler();
private FurnaceRefuelHandler()
{
}
@Override
public int refuel( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack currentStack, int slot, int limit )
{
int fuelSpaceLeft = turtle.getFuelLimit() - turtle.getFuelLevel();
int fuelPerItem = getFuelPerItem( turtle.getItemHandler().getStackInSlot( slot ) );
int fuelItemLimit = (int) Math.ceil( fuelSpaceLeft / (double) fuelPerItem );
if( limit > fuelItemLimit ) limit = fuelItemLimit;
ItemStack stack = turtle.getItemHandler().extractItem( slot, limit, false );
int fuelToGive = fuelPerItem * stack.getCount();
// Store the replacement item in the inventory
ItemStack replacementStack = stack.getItem().getContainerItem( stack );
if( !replacementStack.isEmpty() )
{
ItemStack remainder = InventoryUtil.storeItems( replacementStack, turtle.getItemHandler(), turtle.getSelectedSlot() );
if( !remainder.isEmpty() )
{
WorldUtil.dropItemStack( remainder, turtle.getLevel(), turtle.getPosition(), turtle.getDirection().getOpposite() );
}
}
return fuelToGive;
}
private static int getFuelPerItem( @Nonnull ItemStack stack )
{
return (ForgeHooks.getBurnTime( stack, null ) * 5) / 100;
}
@SubscribeEvent
public static void onTurtleRefuel( TurtleRefuelEvent event )
{
if( event.getHandler() == null && getFuelPerItem( event.getStack() ) > 0 ) event.setHandler( INSTANCE );
}
}