Applied-Energistics-2-tiler.../items/parts/ItemPart.java

208 lines
5.1 KiB
Java
Raw Normal View History

package appeng.items.parts;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
2014-02-09 02:34:52 +01:00
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
2014-02-09 02:34:52 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2014-02-09 02:34:52 +01:00
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import appeng.api.AEApi;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IItemGroup;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartItem;
2014-02-09 06:08:27 +01:00
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
import appeng.core.features.AEFeatureHandler;
import appeng.core.features.ItemStackSrc;
2014-01-20 17:41:37 +01:00
import appeng.core.localization.GuiText;
import appeng.items.AEBaseItem;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-01-20 17:41:37 +01:00
public class ItemPart extends AEBaseItem implements IPartItem, IItemGroup
{
class PartTypeIst
{
PartType part;
int varient;
@SideOnly(Side.CLIENT)
2014-02-09 02:34:52 +01:00
IIcon ico;
};
HashMap<Integer, PartTypeIst> dmgToPart = new HashMap();
public ItemPart() {
super( ItemPart.class );
setfeature( EnumSet.of( AEFeature.Core ) );
AEApi.instance().partHelper().setItemBusRenderer( this );
setHasSubtypes( true );
}
public ItemStackSrc createPart(PartType mat, Enum varient)
{
try
{
// I think this still works?
ItemStack is = new ItemStack( this );
mat.getPart().getConstructor( ItemStack.class ).newInstance( is );
}
catch (Throwable e)
{
return null; // part not supported..
}
String name = varient == null ? mat.name() : mat.name() + "." + varient.name();
int varID = varient == null ? 0 : varient.ordinal();
// verify
for (PartTypeIst p : dmgToPart.values())
{
if ( p.part == mat && p.varient == varID )
throw new RuntimeException( "Cannot create the same material twice..." );
}
boolean enabled = true;
for (AEFeature f : mat.getFeature())
2014-02-09 06:08:27 +01:00
enabled = enabled && AEConfig.instance.isFeatureEnabled( f );
if ( enabled )
{
2014-02-09 06:08:27 +01:00
int newPartNum = AEConfig.instance.get( "parts", name, AEConfig.instance.getFreePart() ).getInt();
ItemStackSrc output = new ItemStackSrc( this, newPartNum );
PartTypeIst pti = new PartTypeIst();
pti.part = mat;
pti.varient = varID;
dmgToPart.put( newPartNum, pti );
return output;
}
return null;
}
public PartType getTypeByStack(ItemStack is)
{
if ( is == null )
return null;
PartTypeIst pt = dmgToPart.get( is.getItemDamage() );
if ( pt != null )
return pt.part;
return null;
}
@Override
@SideOnly(Side.CLIENT)
public int getSpriteNumber()
{
return 0;
}
@Override
2014-02-09 02:34:52 +01:00
public IIcon getIconFromDamage(int dmg)
{
2014-02-09 02:34:52 +01:00
IIcon ico = dmgToPart.get( dmg ).ico;
return ico;
}
@Override
public String getUnlocalizedName(ItemStack is)
2014-01-20 17:41:37 +01:00
{
return "item.appliedenergistics2." + getname( is );
}
public String getname(ItemStack is)
{
return AEFeatureHandler.getName( ItemPart.class, getTypeByStack( is ).name() );
}
2014-01-20 17:41:37 +01:00
@Override
2014-02-09 02:34:52 +01:00
public String getItemStackDisplayName(ItemStack is)
2014-01-20 17:41:37 +01:00
{
PartType pt = getTypeByStack( is );
if ( pt == null )
return "Unnamed";
Enum[] varients = pt.getVarients();
2014-01-20 17:41:37 +01:00
if ( varients != null )
2014-02-09 02:34:52 +01:00
return super.getItemStackDisplayName( is ) + " - " + varients[dmgToPart.get( is.getItemDamage() ).varient].toString();
2014-01-20 17:41:37 +01:00
2014-01-31 18:32:30 +01:00
if ( pt.getExtraName() != null )
2014-02-09 02:34:52 +01:00
return super.getItemStackDisplayName( is ) + " - " + pt.getExtraName().getLocal();
2014-01-31 18:32:30 +01:00
2014-02-09 02:34:52 +01:00
return super.getItemStackDisplayName( is );
2014-01-20 17:41:37 +01:00
}
@Override
2014-02-09 02:34:52 +01:00
public void registerIcons(IIconRegister par1IconRegister)
{
for (Entry<Integer, PartTypeIst> part : dmgToPart.entrySet())
{
2014-01-20 17:41:37 +01:00
String tex = "appliedenergistics2:" + getname( new ItemStack( this, 1, part.getKey() ) );
part.getValue().ico = par1IconRegister.registerIcon( tex );
}
}
@Override
public boolean onItemUse(ItemStack is, EntityPlayer player, World w, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
{
return AEApi.instance().partHelper().placeBus( is, x, y, z, side, player, w );
}
@Override
public IPart createPartFromItemStack(ItemStack is)
{
try
{
PartType t = getTypeByStack( is );
if ( t != null )
return t.getPart().getConstructor( ItemStack.class ).newInstance( is );
}
catch (Throwable e)
{
throw new RuntimeException( "Unable to construct IBusPart from IBusItem : " + getTypeByStack( is ).getPart().getName()
+ " ; Possibly didn't have correct constructor( ItemStack )", e );
}
return null;
}
@Override
2014-02-09 02:34:52 +01:00
public void getSubItems(Item number, CreativeTabs tab, List cList)
{
for (Entry<Integer, PartTypeIst> part : dmgToPart.entrySet())
cList.add( new ItemStack( this, 1, part.getKey() ) );
}
public int varientOf(int itemDamage)
{
return dmgToPart.get( itemDamage ).varient;
}
2014-01-20 17:41:37 +01:00
@Override
public String getUnlocalizedGroupName(ItemStack is)
{
switch (getTypeByStack( is ))
{
case ImportBus:
case ExportBus:
return GuiText.IOBuses.getUnlocalized();
default:
}
return null;
}
}