Applied-Energistics-2-tiler.../src/main/java/appeng/items/misc/ItemEncodedPattern.java

192 lines
5.3 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.items.misc;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import net.minecraft.entity.player.EntityPlayer;
2014-07-02 03:44:54 +02:00
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
2015-12-24 02:07:03 +01:00
2014-07-02 03:44:54 +02:00
import appeng.api.AEApi;
import appeng.api.implementations.ICraftingPatternItem;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.storage.data.IAEItemStack;
2016-12-08 12:42:00 +01:00
import appeng.core.AppEng;
import appeng.core.localization.GuiText;
import appeng.helpers.PatternHelper;
import appeng.items.AEBaseItem;
import appeng.util.Platform;
public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternItem
{
// rather simple client side caching.
private static final Map<ItemStack, ItemStack> SIMPLE_CACHE = new WeakHashMap<ItemStack, ItemStack>();
public ItemEncodedPattern()
{
2014-12-29 15:13:47 +01:00
this.setMaxStackSize( 1 );
}
@Override
2016-12-08 12:42:00 +01:00
public ActionResult<ItemStack> onItemRightClick( final World w, final EntityPlayer player, final EnumHand hand )
{
2016-12-08 12:42:00 +01:00
this.clearPattern( player.getHeldItemMainhand(), player );
2016-12-08 12:42:00 +01:00
return new ActionResult<ItemStack>( EnumActionResult.SUCCESS, player.getHeldItemMainhand() );
}
@Override
2016-12-08 12:42:00 +01:00
public EnumActionResult onItemUseFirst( final EntityPlayer player, final World world, final BlockPos pos, final EnumFacing side, final float hitX, final float hitY, final float hitZ, final EnumHand hand )
{
2016-12-08 12:42:00 +01:00
return this.clearPattern( player.getHeldItemMainhand(), player ) ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
}
2015-09-30 14:24:40 +02:00
private boolean clearPattern( final ItemStack stack, final EntityPlayer player )
2014-07-02 03:44:54 +02:00
{
if( player.isSneaking() )
2014-07-02 03:44:54 +02:00
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
2014-07-12 21:27:06 +02:00
return false;
2015-04-29 02:30:53 +02:00
}
2014-07-12 21:27:06 +02:00
2015-09-30 14:24:40 +02:00
final InventoryPlayer inv = player.inventory;
2014-07-12 21:27:06 +02:00
2016-12-08 12:42:00 +01:00
ItemStack is = AEApi.instance().definitions().materials().blankPattern().maybeStack( stack.getCount() ).orElse( null );
if( is != null )
2014-07-02 03:44:54 +02:00
{
for( int s = 0; s < player.inventory.getSizeInventory(); s++ )
2014-07-12 21:27:06 +02:00
{
if( inv.getStackInSlot( s ) == stack )
{
inv.setInventorySlotContents( s, is );
return true;
}
2014-07-12 21:27:06 +02:00
}
2014-07-02 03:44:54 +02:00
}
}
return false;
}
@Override
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack stack, final EntityPlayer player, final List<String> lines, final boolean displayMoreInfo )
{
2016-12-08 12:42:00 +01:00
final ICraftingPatternDetails details = this.getPatternForItem( stack, player.world );
if( details == null )
{
lines.add( TextFormatting.RED + GuiText.InvalidPattern.getLocal() );
return;
}
2015-09-30 14:24:40 +02:00
final boolean isCrafting = details.isCraftable();
final boolean substitute = details.canSubstitute();
2015-09-30 14:24:40 +02:00
final IAEItemStack[] in = details.getCondensedInputs();
final IAEItemStack[] out = details.getCondensedOutputs();
2015-09-30 14:24:40 +02:00
final String label = ( isCrafting ? GuiText.Crafts.getLocal() : GuiText.Creates.getLocal() ) + ": ";
final String and = ' ' + GuiText.And.getLocal() + ' ';
final String with = GuiText.With.getLocal() + ": ";
boolean first = true;
2015-09-30 14:24:40 +02:00
for( final IAEItemStack anOut : out )
{
if( anOut == null )
{
continue;
}
lines.add( ( first ? label : and ) + anOut.getStackSize() + ' ' + Platform.getItemDisplayName( anOut ) );
first = false;
}
first = true;
2015-09-30 14:24:40 +02:00
for( final IAEItemStack anIn : in )
{
if( anIn == null )
{
continue;
}
lines.add( ( first ? with : and ) + anIn.getStackSize() + ' ' + Platform.getItemDisplayName( anIn ) );
first = false;
}
if( isCrafting )
{
final String substitutionLabel = GuiText.Substitute.getLocal() + " ";
final String canSubstitute = substitute ? GuiText.Yes.getLocal() : GuiText.No.getLocal();
lines.add( substitutionLabel + canSubstitute );
}
}
@Override
2015-09-30 14:24:40 +02:00
public ICraftingPatternDetails getPatternForItem( final ItemStack is, final World w )
{
try
{
return new PatternHelper( is, w );
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
{
return null;
}
}
2015-09-30 14:24:40 +02:00
public ItemStack getOutput( final ItemStack item )
{
2015-01-01 22:13:10 +01:00
ItemStack out = SIMPLE_CACHE.get( item );
if( out != null )
2015-04-29 02:30:53 +02:00
{
return out;
2015-04-29 02:30:53 +02:00
}
2016-12-08 12:42:00 +01:00
final World w = AppEng.proxy.getWorld();
if( w == null )
2015-04-29 02:30:53 +02:00
{
return ItemStack.EMPTY;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final ICraftingPatternDetails details = this.getPatternForItem( item, w );
if( details == null )
2015-04-29 02:30:53 +02:00
{
return ItemStack.EMPTY;
2015-04-29 02:30:53 +02:00
}
2015-01-01 22:13:10 +01:00
SIMPLE_CACHE.put( item, out = details.getCondensedOutputs()[0].getItemStack() );
return out;
}
}