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

152 lines
3.9 KiB
Java
Raw Normal View History

package appeng.items.misc;
import java.util.EnumSet;
import java.util.List;
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
{
public ItemEncodedPattern() {
super( ItemEncodedPattern.class );
2014-09-20 23:26:30 +02:00
setFeature( EnumSet.of( AEFeature.Patterns ) );
setMaxStackSize( 1 );
2014-04-23 03:36:26 +02:00
if ( Platform.isClient() )
MinecraftForgeClient.registerItemRenderer( this, new ItemEncodedPatternRenderer() );
}
2014-07-12 21:27:06 +02:00
private boolean clearPattern(ItemStack stack, EntityPlayer player)
2014-07-02 03:44:54 +02:00
{
2014-07-12 21:27:06 +02:00
if ( player.isSneaking() )
2014-07-02 03:44:54 +02:00
{
2014-07-12 21:27:06 +02:00
if ( Platform.isClient() )
return false;
InventoryPlayer inv = player.inventory;
for (int s = 0; s < player.inventory.getSizeInventory(); s++)
2014-07-02 03:44:54 +02:00
{
2014-07-12 21:27:06 +02:00
if ( inv.getStackInSlot( s ) == stack )
{
inv.setInventorySlotContents( s, AEApi.instance().materials().materialBlankPattern.stack( stack.stackSize ) );
return true;
}
2014-07-02 03:44:54 +02:00
}
}
return false;
}
@Override
2014-07-12 21:27:06 +02:00
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
{
return clearPattern( stack, player );
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World w, EntityPlayer player)
2014-07-02 03:44:54 +02:00
{
2014-07-12 21:27:06 +02:00
clearPattern( stack, player );
return stack;
2014-07-02 03:44:54 +02:00
}
@Override
public void addCheckedInformation(ItemStack stack, EntityPlayer player, List<String> lines, boolean displayAdditionalInformation )
{
ICraftingPatternDetails details = 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;
}
}
2014-09-21 02:44:23 +02:00
// rather simple client side caching.
static final WeakHashMap<ItemStack, ItemStack> simpleCache = new WeakHashMap<ItemStack, ItemStack>();
public ItemStack getOutput(ItemStack item)
{
ItemStack out = simpleCache.get( item );
if ( out != null )
return out;
World w = CommonHelper.proxy.getWorld();
if ( w == null )
return null;
ICraftingPatternDetails details = getPatternForItem( item, w );
if ( details == null )
return null;
2014-09-20 22:30:12 +02:00
simpleCache.put( item, out = details.getCondensedOutputs()[0].getItemStack() );
return out;
}
@Override
public ICraftingPatternDetails getPatternForItem(ItemStack is, World w)
{
try
{
2014-07-02 03:44:54 +02:00
return new PatternHelper( is, w );
}
catch (Throwable t)
{
return null;
}
}
}