108 lines
3.1 KiB
Java
108 lines
3.1 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
|
|
package dan200.computercraft.shared.common;
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
import dan200.computercraft.shared.util.AbstractRecipe;
|
|
import dan200.computercraft.shared.util.Colour;
|
|
import dan200.computercraft.shared.util.ColourTracker;
|
|
import dan200.computercraft.shared.util.ColourUtils;
|
|
import net.minecraft.inventory.IInventory;
|
|
import net.minecraft.item.EnumDyeColor;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.crafting.IRecipeSerializer;
|
|
import net.minecraft.item.crafting.RecipeSerializers;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraft.world.World;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
public class ColourableRecipe extends AbstractRecipe
|
|
{
|
|
public ColourableRecipe( ResourceLocation id )
|
|
{
|
|
super( id );
|
|
}
|
|
|
|
@Override
|
|
public boolean matches( @Nonnull IInventory inv, @Nonnull World world )
|
|
{
|
|
boolean hasColourable = false;
|
|
boolean hasDye = false;
|
|
for( int i = 0; i < inv.getSizeInventory(); i++ )
|
|
{
|
|
ItemStack stack = inv.getStackInSlot( i );
|
|
if( stack.isEmpty() ) continue;
|
|
|
|
if( stack.getItem() instanceof IColouredItem )
|
|
{
|
|
if( hasColourable ) return false;
|
|
hasColourable = true;
|
|
}
|
|
else if( ColourUtils.getStackColour( stack ) != null )
|
|
{
|
|
hasDye = true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return hasColourable && hasDye;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public ItemStack getCraftingResult( @Nonnull IInventory inv )
|
|
{
|
|
ItemStack colourable = ItemStack.EMPTY;
|
|
|
|
ColourTracker tracker = new ColourTracker();
|
|
|
|
for( int i = 0; i < inv.getSizeInventory(); i++ )
|
|
{
|
|
ItemStack stack = inv.getStackInSlot( i );
|
|
|
|
if( stack.isEmpty() ) continue;
|
|
|
|
if( stack.getItem() instanceof IColouredItem )
|
|
{
|
|
colourable = stack;
|
|
}
|
|
else
|
|
{
|
|
EnumDyeColor dye = ColourUtils.getStackColour( stack );
|
|
if( dye == null ) continue;
|
|
|
|
Colour colour = Colour.fromInt( 15 - dye.getId() );
|
|
tracker.addColour( colour.getR(), colour.getG(), colour.getB() );
|
|
}
|
|
}
|
|
|
|
if( colourable.isEmpty() ) return ItemStack.EMPTY;
|
|
return ((IColouredItem) colourable.getItem()).withColour( colourable, tracker.getColour() );
|
|
}
|
|
|
|
@Override
|
|
public boolean canFit( int x, int y )
|
|
{
|
|
return x >= 2 && y >= 2;
|
|
}
|
|
|
|
@Override
|
|
@Nonnull
|
|
public IRecipeSerializer<?> getSerializer()
|
|
{
|
|
return SERIALIZER;
|
|
}
|
|
|
|
public static final IRecipeSerializer<?> SERIALIZER = new RecipeSerializers.SimpleSerializer<>(
|
|
ComputerCraft.MOD_ID + ":colour", ColourableRecipe::new
|
|
);
|
|
}
|