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>.
|
|
|
|
*/
|
|
|
|
|
2014-04-17 08:15:56 +02:00
|
|
|
package appeng.items.misc;
|
|
|
|
|
2014-11-28 04:36:46 +01:00
|
|
|
|
2014-04-17 08:15:56 +02:00
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.List;
|
2015-01-03 02:53:14 +01:00
|
|
|
import java.util.Map;
|
2014-08-15 02:59:09 +02:00
|
|
|
import java.util.WeakHashMap;
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.renderer.ItemMeshDefinition;
|
|
|
|
import net.minecraft.client.resources.model.ModelResourceLocation;
|
2014-04-17 08:15:56 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-07-02 03:44:54 +02:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2014-04-17 08:15:56 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraft.util.BlockPos;
|
2014-07-24 17:59:57 +02:00
|
|
|
import net.minecraft.util.EnumChatFormatting;
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2014-04-25 03:55:29 +02:00
|
|
|
import net.minecraft.world.World;
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2014-07-02 03:44:54 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-04-17 08:15:56 +02:00
|
|
|
import appeng.api.implementations.ICraftingPatternItem;
|
2014-05-09 06:10:12 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
2014-07-24 17:59:57 +02:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2015-06-16 02:44:59 +02:00
|
|
|
import appeng.client.ClientHelper;
|
2014-07-24 17:59:57 +02:00
|
|
|
import appeng.core.CommonHelper;
|
2014-04-17 08:15:56 +02:00
|
|
|
import appeng.core.features.AEFeature;
|
|
|
|
import appeng.core.localization.GuiText;
|
2014-04-25 03:55:29 +02:00
|
|
|
import appeng.helpers.PatternHelper;
|
2014-04-17 08:15:56 +02:00
|
|
|
import appeng.items.AEBaseItem;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2014-04-17 08:15:56 +02:00
|
|
|
public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternItem
|
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
// rather simple client side caching.
|
|
|
|
private static final Map<ItemStack, ItemStack> SIMPLE_CACHE = new WeakHashMap<ItemStack, ItemStack>();
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
public ItemEncodedPattern()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.setFeature( EnumSet.of( AEFeature.Patterns ) );
|
|
|
|
this.setMaxStackSize( 1 );
|
2014-04-17 08:15:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 02:44:59 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
ModelResourceLocation res;
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
ModelResourceLocation encodedPatternModel; // TODO: make encoded pattern model!
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerIcons(
|
2015-09-30 14:24:40 +02:00
|
|
|
final ClientHelper proxy,
|
|
|
|
final String name )
|
2015-06-16 02:44:59 +02:00
|
|
|
{
|
2015-09-30 14:26:54 +02:00
|
|
|
this.encodedPatternModel = this.res = proxy.setIcon( this, name );
|
2015-06-16 02:44:59 +02:00
|
|
|
|
|
|
|
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register( this, new ItemMeshDefinition(){
|
|
|
|
|
|
|
|
boolean recursive = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ModelResourceLocation getModelLocation(
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack stack )
|
2015-06-16 02:44:59 +02:00
|
|
|
{
|
2015-09-30 14:26:54 +02:00
|
|
|
if ( this.recursive == false )
|
2015-06-16 02:44:59 +02:00
|
|
|
{
|
|
|
|
this.recursive = true;
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemEncodedPattern iep = (ItemEncodedPattern) stack.getItem();
|
2015-06-16 02:44:59 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack is = iep.getOutput( stack );
|
2015-06-16 02:44:59 +02:00
|
|
|
if ( Minecraft.getMinecraft().thePlayer.isSneaking() )
|
|
|
|
{
|
2015-09-30 14:26:54 +02:00
|
|
|
return ItemEncodedPattern.this.encodedPatternModel;
|
2015-06-16 02:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.recursive = false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:26:54 +02:00
|
|
|
return ItemEncodedPattern.this.res;
|
2015-06-16 02:44:59 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public ItemStack onItemRightClick( final ItemStack stack, final World w, final EntityPlayer player )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
this.clearPattern( stack, player );
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public boolean onItemUseFirst(
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack stack,
|
|
|
|
final EntityPlayer player,
|
|
|
|
final World world,
|
|
|
|
final BlockPos pos,
|
|
|
|
final EnumFacing side,
|
|
|
|
final float hitX,
|
|
|
|
final float hitY,
|
|
|
|
final float hitZ )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
return this.clearPattern( stack, player );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( player.isSneaking() )
|
2014-07-02 03:44:54 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +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
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int s = 0; s < player.inventory.getSizeInventory(); s++ )
|
2014-07-02 03:44:54 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( inv.getStackInSlot( s ) == stack )
|
2014-07-12 21:27:06 +02:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final ItemStack blankPattern : AEApi.instance().definitions().materials().blankPattern().maybeStack( stack.stackSize ).asSet() )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
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
|
2015-09-30 14:24:40 +02:00
|
|
|
public void addCheckedInformation( final ItemStack stack, final EntityPlayer player, final List<String> lines, final boolean displayMoreInfo )
|
2014-04-17 08:15:56 +02:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ICraftingPatternDetails details = this.getPatternForItem( stack, player.worldObj );
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( details == null )
|
2014-07-24 17:59:57 +02:00
|
|
|
{
|
2014-11-04 15:31:55 +01:00
|
|
|
lines.add( EnumChatFormatting.RED + GuiText.InvalidPattern.getLocal() );
|
2014-04-17 08:15:56 +02:00
|
|
|
return;
|
2014-07-24 17:59:57 +02:00
|
|
|
}
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final boolean isCrafting = details.isCraftable();
|
2015-09-22 00:24:40 +02:00
|
|
|
final boolean substitute = details.canSubstitute();
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final IAEItemStack[] in = details.getCondensedInputs();
|
|
|
|
final IAEItemStack[] out = details.getCondensedOutputs();
|
2014-04-17 08:15:56 +02:00
|
|
|
|
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() + ": ";
|
2014-04-17 08:15:56 +02:00
|
|
|
|
|
|
|
boolean first = true;
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final IAEItemStack anOut : out )
|
2014-04-17 08:15:56 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( anOut == null )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-04-17 08:15:56 +02:00
|
|
|
continue;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
lines.add( ( first ? label : and ) + anOut.getStackSize() + ' ' + Platform.getItemDisplayName( anOut ) );
|
2014-04-17 08:15:56 +02:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = true;
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final IAEItemStack anIn : in )
|
2014-04-17 08:15:56 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( anIn == null )
|
2014-09-29 09:23:02 +02:00
|
|
|
{
|
2014-04-17 08:15:56 +02:00
|
|
|
continue;
|
2014-09-29 09:23:02 +02:00
|
|
|
}
|
2014-04-17 08:15:56 +02:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
lines.add( ( first ? with : and ) + anIn.getStackSize() + ' ' + Platform.getItemDisplayName( anIn ) );
|
2014-04-17 08:15:56 +02:00
|
|
|
first = false;
|
|
|
|
}
|
2015-09-22 00:24:40 +02:00
|
|
|
|
|
|
|
final String substitutionLabel = GuiText.Substitute.getLocal() + " ";
|
|
|
|
final String canSubstitute = substitute ? GuiText.Yes.getLocal() : GuiText.No.getLocal();
|
|
|
|
|
|
|
|
lines.add( substitutionLabel + canSubstitute );
|
2014-04-17 08:15:56 +02:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public ICraftingPatternDetails getPatternForItem( final ItemStack is, final World w )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new PatternHelper( is, w );
|
|
|
|
}
|
2015-09-30 14:24:40 +02:00
|
|
|
catch( final Throwable t )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 02:59:09 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public ItemStack getOutput( final ItemStack item )
|
2014-04-23 07:30:29 +02:00
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
ItemStack out = SIMPLE_CACHE.get( item );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( out != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-08-15 03:23:37 +02:00
|
|
|
return out;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-15 02:59:09 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final World w = CommonHelper.proxy.getWorld();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( w == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-04-23 07:30:29 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-04-23 07:30:29 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final ICraftingPatternDetails details = this.getPatternForItem( item, w );
|
2014-04-23 07:30:29 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( details == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-04-23 07:30:29 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-04-23 07:30:29 +02:00
|
|
|
|
2015-01-01 22:13:10 +01:00
|
|
|
SIMPLE_CACHE.put( item, out = details.getCondensedOutputs()[0].getItemStack() );
|
2014-08-15 03:23:37 +02:00
|
|
|
return out;
|
2014-04-17 08:15:56 +02:00
|
|
|
}
|
|
|
|
}
|