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

177 lines
4.8 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.EnumSet;
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.EnumChatFormatting;
import net.minecraft.world.World;
2014-04-23 03:36:26 +02:00
import net.minecraftforge.client.MinecraftForgeClient;
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;
2014-04-23 03:36:26 +02:00
import appeng.client.render.items.ItemEncodedPatternRenderer;
import appeng.core.CommonHelper;
import appeng.core.features.AEFeature;
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.setFeature( EnumSet.of( AEFeature.Patterns ) );
this.setMaxStackSize( 1 );
if( Platform.isClient() )
2014-04-23 03:36:26 +02:00
MinecraftForgeClient.registerItemRenderer( this, new ItemEncodedPatternRenderer() );
}
@Override
public ItemStack onItemRightClick( ItemStack stack, World w, EntityPlayer player )
{
this.clearPattern( stack, player );
return stack;
}
@Override
public boolean onItemUseFirst( ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ )
{
return this.clearPattern( stack, player );
}
private boolean clearPattern( ItemStack stack, EntityPlayer player )
2014-07-02 03:44:54 +02:00
{
if( player.isSneaking() )
2014-07-02 03:44:54 +02:00
{
if( Platform.isClient() )
2014-07-12 21:27:06 +02:00
return false;
InventoryPlayer inv = player.inventory;
for( int s = 0; s < player.inventory.getSizeInventory(); s++ )
2014-07-02 03:44:54 +02:00
{
if( inv.getStackInSlot( s ) == stack )
2014-07-12 21:27:06 +02:00
{
for( ItemStack blankPattern : AEApi.instance().definitions().materials().blankPattern().maybeStack( stack.stackSize ).asSet() )
{
inv.setInventorySlotContents( s, blankPattern );
}
2014-07-12 21:27:06 +02:00
return true;
}
2014-07-02 03:44:54 +02:00
}
}
return false;
}
@Override
public void addCheckedInformation( ItemStack stack, EntityPlayer player, List<String> lines, boolean displayAdditionalInformation )
{
2014-12-29 15:13:47 +01:00
ICraftingPatternDetails details = this.getPatternForItem( stack, player.worldObj );
if( details == null )
{
lines.add( EnumChatFormatting.RED + GuiText.InvalidPattern.getLocal() );
return;
}
boolean isCrafting = details.isCraftable();
2014-09-20 22:30:31 +02:00
IAEItemStack[] in = details.getCondensedInputs();
2014-09-20 22:30:12 +02:00
IAEItemStack[] out = details.getCondensedOutputs();
String label = ( isCrafting ? GuiText.Crafts.getLocal() : GuiText.Creates.getLocal() ) + ": ";
String and = ' ' + GuiText.And.getLocal() + ' ';
String with = GuiText.With.getLocal() + ": ";
boolean first = true;
for( IAEItemStack anOut : out )
{
if( anOut == null )
{
continue;
}
lines.add( ( first ? label : and ) + anOut.getStackSize() + ' ' + Platform.getItemDisplayName( anOut ) );
first = false;
}
first = true;
for( IAEItemStack anIn : in )
{
if( anIn == null )
{
continue;
}
lines.add( ( first ? with : and ) + anIn.getStackSize() + ' ' + Platform.getItemDisplayName( anIn ) );
first = false;
}
}
@Override
public ICraftingPatternDetails getPatternForItem( ItemStack is, World w )
{
try
{
return new PatternHelper( is, w );
}
catch( Throwable t )
{
return null;
}
}
public ItemStack getOutput( ItemStack item )
{
2015-01-01 22:13:10 +01:00
ItemStack out = SIMPLE_CACHE.get( item );
if( out != null )
return out;
World w = CommonHelper.proxy.getWorld();
if( w == null )
return null;
2014-12-29 15:13:47 +01:00
ICraftingPatternDetails details = this.getPatternForItem( item, w );
if( details == null )
return null;
2015-01-01 22:13:10 +01:00
SIMPLE_CACHE.put( item, out = details.getCondensedOutputs()[0].getItemStack() );
return out;
}
}