Fixes #3217: Allow caps to be used as P2P attunements. (#3257)

Adds a more abstract approach to map a cap to a certain type instead of
the hardcoded ForgeEnergy case.
Reorders the check from exact ItemStack > ModId > ForgeEnergy to
ItemStack > Cap > ModId. Thus the mod wildcard will be used last.
Support for ForgeEnergy and FluidHandlerItems by default.
Result for items supporting multiple capabilities is not defined.
This commit is contained in:
yueh
2017-12-01 21:14:11 +01:00
committed by GitHub
parent b70c86542b
commit 7ceaa4cc3e
2 changed files with 43 additions and 20 deletions
@@ -21,6 +21,7 @@ package appeng.core.features.registries;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -30,6 +31,8 @@ import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.oredict.OreDictionary;
import appeng.api.AEApi;
@@ -49,6 +52,7 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry
private final Map<ItemStack, TunnelType> tunnels = new HashMap<>( INITIAL_CAPACITY );
private final Map<String, TunnelType> modIdTunnels = new HashMap<>( INITIAL_CAPACITY );
private final Map<Capability<?>, TunnelType> capTunnels = new HashMap<>( INITIAL_CAPACITY );
public void configure()
{
@@ -159,6 +163,12 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry
this.addNewAttunement( parts.cableDenseSmart().stack( c, 1 ), TunnelType.ME );
}
/**
* attune based caps
*/
this.addNewAttunement( Capabilities.FORGE_ENERGY, TunnelType.FE_POWER );
this.addNewAttunement( CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, TunnelType.FLUID );
/**
* attune based on the ItemStack's modId
*/
@@ -186,6 +196,16 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry
this.modIdTunnels.put( modId, type );
}
@Override
public void addNewAttunement( @Nonnull final Capability<?> cap, @Nullable final TunnelType type )
{
if( type == null || cap == null )
{
return;
}
this.capTunnels.put( cap, type );
}
@Override
public void addNewAttunement( @Nonnull final ItemStack trigger, @Nullable final TunnelType type )
{
@@ -203,39 +223,40 @@ public final class P2PTunnelRegistry implements IP2PTunnelRegistry
{
if( !trigger.isEmpty() )
{
// if( FluidRegistry.isContainer( trigger ) )
// {
// return TunnelType.FLUID;
// }
for( final ItemStack is : this.tunnels.keySet() )
// First match exact items
for( final Entry<ItemStack, TunnelType> entry : this.tunnels.entrySet() )
{
final ItemStack is = entry.getKey();
if( is.getItem() == trigger.getItem() && is.getItemDamage() == OreDictionary.WILDCARD_VALUE )
{
return this.tunnels.get( is );
return entry.getValue();
}
if( ItemStack.areItemsEqual( is, trigger ) )
{
return this.tunnels.get( is );
return entry.getValue();
}
}
// Try by ModId next
for( final String modId : this.modIdTunnels.keySet() )
{
if( trigger.getItem().getRegistryName() != null && trigger.getItem().getRegistryName().getResourceDomain().equals( modId ) )
{
return this.modIdTunnels.get( modId );
}
}
// Next, check if the Item you're holding supports Forge Energy
// Next, check if the Item you're holding supports any registered capability
for( EnumFacing face : EnumFacing.VALUES )
{
if( trigger.hasCapability( Capabilities.FORGE_ENERGY, face ) )
for( Entry<Capability<?>, TunnelType> entry : this.capTunnels.entrySet() )
{
return TunnelType.FE_POWER;
if( trigger.hasCapability( entry.getKey(), face ) )
{
return entry.getValue();
}
}
}
// Use the mod id as last option.
for( final Entry<String, TunnelType> entry : this.modIdTunnels.entrySet() )
{
if( trigger.getItem().getRegistryName() != null && trigger.getItem().getRegistryName().getResourceDomain().equals( entry.getKey() ) )
{
return entry.getValue();
}
}
}