From 7ceaa4cc3e36c68e2f00f023cd300441b66f3046 Mon Sep 17 00:00:00 2001 From: yueh Date: Fri, 1 Dec 2017 21:14:11 +0100 Subject: [PATCH] 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. --- .../api/features/IP2PTunnelRegistry.java | 2 + .../registries/P2PTunnelRegistry.java | 61 +++++++++++++------ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/api/java/appeng/api/features/IP2PTunnelRegistry.java b/src/api/java/appeng/api/features/IP2PTunnelRegistry.java index acfe7f229..a44eb77d8 100644 --- a/src/api/java/appeng/api/features/IP2PTunnelRegistry.java +++ b/src/api/java/appeng/api/features/IP2PTunnelRegistry.java @@ -28,6 +28,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import net.minecraft.item.ItemStack; +import net.minecraftforge.common.capabilities.Capability; import appeng.api.config.TunnelType; @@ -47,6 +48,7 @@ public interface IP2PTunnelRegistry */ void addNewAttunement( @Nonnull ItemStack trigger, @Nullable TunnelType type ); void addNewAttunement( @Nonnull String ModId, @Nullable TunnelType type ); + void addNewAttunement( @Nonnull Capability cap, @Nullable TunnelType type ); /** * returns null if no attunement can be found. diff --git a/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java b/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java index 6c3a9a15b..8d3d1100d 100644 --- a/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java +++ b/src/main/java/appeng/core/features/registries/P2PTunnelRegistry.java @@ -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 tunnels = new HashMap<>( INITIAL_CAPACITY ); private final Map modIdTunnels = new HashMap<>( INITIAL_CAPACITY ); + private final Map, 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 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, 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 entry : this.modIdTunnels.entrySet() ) + { + if( trigger.getItem().getRegistryName() != null && trigger.getItem().getRegistryName().getResourceDomain().equals( entry.getKey() ) ) + { + return entry.getValue(); } } }