Parts/Materials both now have better registration, and both use DamageValues.

This commit is contained in:
AlgorithmX2 2014-01-03 12:30:08 -06:00
parent 3220a2e07c
commit 35a21d472f
5 changed files with 168 additions and 41 deletions

View file

@ -3,6 +3,7 @@ package appeng.core;
import java.io.File; import java.io.File;
import java.util.EnumSet; import java.util.EnumSet;
import net.minecraftforge.common.Property;
import appeng.api.config.CondenserOuput; import appeng.api.config.CondenserOuput;
import appeng.api.config.PowerUnits; import appeng.api.config.PowerUnits;
import appeng.api.config.Settings; import appeng.api.config.Settings;
@ -155,6 +156,22 @@ public class Configuration extends net.minecraftforge.common.Configuration imple
super.save(); super.save();
} }
public int getFreeMaterial()
{
int min = 0;
for (Property p : getCategory( "materials" ).getValues().values())
min = Math.max( min, p.getInt() + 1 );
return min;
}
public int getFreePart()
{
int min = 0;
for (Property p : getCategory( "parts" ).getValues().values())
min = Math.max( min, p.getInt() + 1 );
return min;
}
@Override @Override
public IConfigManager getConfigManager() public IConfigManager getConfigManager()
{ {

View file

@ -17,6 +17,7 @@ import appeng.api.definitions.Blocks;
import appeng.api.definitions.Items; import appeng.api.definitions.Items;
import appeng.api.definitions.Materials; import appeng.api.definitions.Materials;
import appeng.api.definitions.Parts; import appeng.api.definitions.Parts;
import appeng.api.implementations.ITileStorageMonitorable;
import appeng.api.networking.energy.IEnergyGrid; import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.pathing.IPathingGrid; import appeng.api.networking.pathing.IPathingGrid;
import appeng.api.networking.spatial.ISpatialCache; import appeng.api.networking.spatial.ISpatialCache;
@ -159,17 +160,32 @@ public class Registration
} }
} }
AEItemDefinition partItem = (AEFeatureHandler) addFeature( ItemPart.class );
Class partClass = parts.getClass(); Class partClass = parts.getClass();
for (PartType part : PartType.values()) for (PartType type : PartType.values())
{ {
try try
{ {
Field f = partClass.getField( "part" + part.name() ); Field f = partClass.getField( "part" + type.name() );
f.set( parts, addFeature( ItemPart.class, part ) ); Enum varients[] = type.getVarients();
if ( varients == null )
{
ItemStack is = ((ItemPart) partItem.item()).createPart( type, null );
f.set( parts, new DamagedItemDefinition( is ) );
}
else
{
for (Enum v : varients)
{
ItemStack is = ((ItemPart) partItem.item()).createPart( type, v );
f.set( parts, new DamagedItemDefinition( is ) );
}
}
} }
catch (Throwable err) catch (Throwable err)
{ {
AELog.severe( "Error creating part: " + part.name() ); AELog.severe( "Error creating part: " + type.name() );
throw new RuntimeException( err ); throw new RuntimeException( err );
} }
} }
@ -358,6 +374,7 @@ public class Registration
AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIPowerEmitter", IPowerEmitter.class ); AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIPowerEmitter", IPowerEmitter.class );
AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIPowerReceptor", IPowerReceptor.class ); AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIPowerReceptor", IPowerReceptor.class );
AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIFluidHandler", IFluidHandler.class ); AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerIFluidHandler", IFluidHandler.class );
AEApi.instance().partHelper().registerNewLayer( "appeng.api.parts.layers.LayerITileStorageMonitorable", ITileStorageMonitorable.class );
TickRegistry.registerTickHandler( TickHandler.instance, Side.SERVER ); TickRegistry.registerTickHandler( TickHandler.instance, Side.SERVER );
TickRegistry.registerTickHandler( TickHandler.instance, Side.CLIENT ); TickRegistry.registerTickHandler( TickHandler.instance, Side.CLIENT );

View file

@ -1,6 +1,7 @@
package appeng.items.materials; package appeng.items.materials;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap;
import java.util.List; import java.util.List;
import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.client.renderer.texture.IconRegister;
@ -22,8 +23,7 @@ import appeng.items.AEBaseItem;
public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgradeModule public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgradeModule
{ {
private int currentMaterial = 0; HashMap<Integer, MaterialType> dmgToMaterial = new HashMap();
private final MaterialType material[] = new MaterialType[MaterialType.values().length];
public ItemMaterial() { public ItemMaterial() {
super( ItemMaterial.class ); super( ItemMaterial.class );
@ -32,6 +32,8 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
public ItemStack createMaterial(MaterialType mat) public ItemStack createMaterial(MaterialType mat)
{ {
String name = mat.name();
if ( mat.damageValue == -1 ) if ( mat.damageValue == -1 )
{ {
boolean enabled = true; boolean enabled = true;
@ -40,14 +42,15 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
if ( enabled ) if ( enabled )
{ {
material[currentMaterial] = mat; int newMaterialNum = Configuration.instance.get( "materials", name, Configuration.instance.getFreeMaterial() ).getInt();
mat.damageValue = currentMaterial; mat.damageValue = newMaterialNum;
ItemStack output = new ItemStack( this ); ItemStack output = new ItemStack( this, 1, newMaterialNum );
output.setItemDamage( currentMaterial ); output.setItemDamage( newMaterialNum );
currentMaterial++;
dmgToMaterial.put( newMaterialNum, mat );
if ( mat.getOreName() != null ) if ( mat.getOreName() != null )
OreDictionary.registerOre( mat.getOreName(), this ); OreDictionary.registerOre( mat.getOreName(), output );
return output; return output;
} }
@ -60,13 +63,13 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
public MaterialType getTypeByStack(ItemStack is) public MaterialType getTypeByStack(ItemStack is)
{ {
return material[is.getItemDamage()]; return dmgToMaterial.get( is.getItemDamage() );
} }
@Override @Override
public Icon getIconFromDamage(int dmg) public Icon getIconFromDamage(int dmg)
{ {
return material[dmg].icon; return dmgToMaterial.get( dmg ).icon;
} }
@Override @Override
@ -78,10 +81,13 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
@Override @Override
public void registerIcons(IconRegister par1IconRegister) public void registerIcons(IconRegister par1IconRegister)
{ {
for (int x = 0; x < currentMaterial; x++) for (MaterialType mat : MaterialType.values())
{ {
String tex = "appliedenergistics2:" + getUnlocalizedName( new ItemStack( this, 1, x ) ); if ( mat.damageValue != -1 )
material[x].icon = par1IconRegister.registerIcon( tex ); {
String tex = "appliedenergistics2:" + getUnlocalizedName( new ItemStack( this, 1, mat.damageValue ) );
mat.icon = par1IconRegister.registerIcon( tex );
}
} }
} }
@ -171,7 +177,10 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
@Override @Override
public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List cList) public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List cList)
{ {
for (int x = 0; x < currentMaterial; x++) for (MaterialType mat : MaterialType.values())
cList.add( new ItemStack( this, 1, x ) ); {
if ( mat.damageValue >= 0 )
cList.add( new ItemStack( this, 1, mat.damageValue ) );
}
} }
} }

View file

@ -1,14 +1,22 @@
package appeng.items.parts; package appeng.items.parts;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs; import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.world.World; import net.minecraft.world.World;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.parts.IPartItem;
import appeng.api.parts.IPart; import appeng.api.parts.IPart;
import appeng.api.parts.IPartItem;
import appeng.core.Configuration;
import appeng.core.features.AEFeature;
import appeng.core.features.AEFeatureHandler;
import appeng.items.AEBaseItem; import appeng.items.AEBaseItem;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
@ -16,17 +24,62 @@ import cpw.mods.fml.relauncher.SideOnly;
public class ItemPart extends AEBaseItem implements IPartItem public class ItemPart extends AEBaseItem implements IPartItem
{ {
final PartType part; class PartTypeIst
{
public ItemPart(PartType type) { PartType part;
super( ItemPart.class, type.name() ); int varient;
setfeature( type.getFeature() );
@SideOnly(Side.CLIENT)
Icon ico;
};
HashMap<Integer, PartTypeIst> dmgToPart = new HashMap();
public ItemPart() {
super( ItemPart.class );
setfeature( EnumSet.of( AEFeature.Core ) );
AEApi.instance().partHelper().setItemBusRenderer( this ); AEApi.instance().partHelper().setItemBusRenderer( this );
part = type; setHasSubtypes( true );
if ( type == PartType.CableSmart || type == PartType.CableCovered || type == PartType.CableGlass ) }
public ItemStack createPart(PartType mat, Enum varient)
{
String name = varient == null ? mat.name() : mat.name() + "." + varient.name();
int varID = varient == null ? 0 : varient.ordinal();
// verify
for (PartTypeIst p : dmgToPart.values())
{ {
setHasSubtypes( true ); if ( p.part == mat && p.varient == varID )
throw new RuntimeException( "Cannot create the same material twice..." );
} }
boolean enabled = true;
for (AEFeature f : mat.getFeature())
enabled = enabled && Configuration.instance.isFeatureEnabled( f );
if ( enabled )
{
int newPartNum = Configuration.instance.get( "parts", name, Configuration.instance.getFreePart() ).getInt();
ItemStack output = new ItemStack( this );
output.setItemDamage( newPartNum );
PartTypeIst pti = new PartTypeIst();
pti.part = mat;
pti.varient = varID;
dmgToPart.put( newPartNum, pti );
return output;
}
return null;
}
public PartType getTypeByStack(ItemStack is)
{
return dmgToPart.get( is.getItemDamage() ).part;
} }
@Override @Override
@ -37,8 +90,30 @@ public class ItemPart extends AEBaseItem implements IPartItem
} }
@Override @Override
public boolean onItemUse(ItemStack is, EntityPlayer player, World w, int x, int y, int z, int side, float hitX, float hitY, public Icon getIconFromDamage(int dmg)
float hitZ) {
Icon ico = dmgToPart.get( dmg ).ico;
return ico;
}
@Override
public String getUnlocalizedName(ItemStack is)
{
return AEFeatureHandler.getName( ItemPart.class, getTypeByStack( is ).name() );
}
@Override
public void registerIcons(IconRegister par1IconRegister)
{
for (Entry<Integer, PartTypeIst> part : dmgToPart.entrySet())
{
String tex = "appliedenergistics2:" + getUnlocalizedName( 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 ); return AEApi.instance().partHelper().placeBus( is, x, y, z, side, player, w );
} }
@ -48,26 +123,26 @@ public class ItemPart extends AEBaseItem implements IPartItem
{ {
try try
{ {
return part.getPart().getConstructor( ItemStack.class ).newInstance( is ); PartType t = getTypeByStack( is );
return t.getPart().getConstructor( ItemStack.class ).newInstance( is );
} }
catch (Throwable e) catch (Throwable e)
{ {
throw new RuntimeException( "Unable to construct IBusPart from IBusItem : " + part.getPart().getName() throw new RuntimeException( "Unable to construct IBusPart from IBusItem : " + getTypeByStack( is ).getPart().getName()
+ " ; Possibly didn't have correct constructor( ItemStack )", e ); + " ; Possibly didn't have correct constructor( ItemStack )", e );
} }
} }
@Override @Override
public void getSubItems(int number, CreativeTabs tab, List list) public void getSubItems(int number, CreativeTabs tab, List cList)
{ {
if ( part == PartType.CableSmart || part == PartType.CableCovered || part == PartType.CableGlass ) for (Entry<Integer, PartTypeIst> part : dmgToPart.entrySet())
{ cList.add( new ItemStack( this, 1, part.getKey() ) );
list.add( new ItemStack( this, 1, 16 ) ); }
for (int x = 0; x < 16; x++)
list.add( new ItemStack( this, 1, x ) ); public int varientOf(int itemDamage)
return; {
} return dmgToPart.get( itemDamage ).varient;
super.getSubItems( number, tab, list );
} }
} }

View file

@ -3,6 +3,7 @@ package appeng.items.parts;
import java.util.EnumSet; import java.util.EnumSet;
import appeng.api.parts.IPart; import appeng.api.parts.IPart;
import appeng.api.util.AEColor;
import appeng.core.features.AEFeature; import appeng.core.features.AEFeature;
import appeng.parts.automation.PartAnnihilationPlane; import appeng.parts.automation.PartAnnihilationPlane;
import appeng.parts.automation.PartExportBus; import appeng.parts.automation.PartExportBus;
@ -10,6 +11,7 @@ import appeng.parts.automation.PartFormationPlane;
import appeng.parts.automation.PartImportBus; import appeng.parts.automation.PartImportBus;
import appeng.parts.automation.PartLevelEmitter; import appeng.parts.automation.PartLevelEmitter;
import appeng.parts.misc.PartCableAnchor; import appeng.parts.misc.PartCableAnchor;
import appeng.parts.misc.PartInterface;
import appeng.parts.misc.PartInvertedToggleBus; import appeng.parts.misc.PartInvertedToggleBus;
import appeng.parts.misc.PartP2PTunnel; import appeng.parts.misc.PartP2PTunnel;
import appeng.parts.misc.PartStorageBus; import appeng.parts.misc.PartStorageBus;
@ -70,7 +72,9 @@ public enum PartType
StorageMonitor(AEFeature.StorageMonitor, PartStorageMonitor.class), StorageMonitor(AEFeature.StorageMonitor, PartStorageMonitor.class),
ConversionMonitor(AEFeature.PartConversionMonitor, PartConversionMonitor.class); ConversionMonitor(AEFeature.PartConversionMonitor, PartConversionMonitor.class),
Interface(AEFeature.Core, PartInterface.class);
private EnumSet<AEFeature> features; private EnumSet<AEFeature> features;
private Class<? extends IPart> myPart; private Class<? extends IPart> myPart;
@ -80,6 +84,11 @@ public enum PartType
myPart = c; myPart = c;
} }
public Enum[] getVarients()
{
return (this == CableSmart || this == CableCovered || this == CableGlass) ? AEColor.values() : null;
}
public EnumSet<AEFeature> getFeature() public EnumSet<AEFeature> getFeature()
{ {
return features; return features;