package choonster.testmod3.capability;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import javax.annotation.Nullable;
/**
* A simple implementation of {@link ICapabilityProvider} that supports a single {@link Capability} handler instance.
*
* @author Choonster
*/
public class CapabilityProviderSimple<HANDLER> implements ICapabilityProvider {
/**
* The {@link Capability} instance to provide the handler for.
*/
protected final Capability<HANDLER> capability;
/**
* The {@link EnumFacing} to provide the handler for.
*/
protected final EnumFacing facing;
/**
* The handler instance to provide.
*/
protected final HANDLER instance;
public CapabilityProviderSimple(final HANDLER instance, final Capability<HANDLER> capability, @Nullable final EnumFacing facing) {
this.instance = instance;
this.capability = capability;
this.facing = facing;
}
/**
* Determines if this object has support for the capability in question on the specific side.
* The return value of this MIGHT change during runtime if this object gains or looses support
* for a capability.
* <p>
* Example:
* A Pipe getting a cover placed on one side causing it loose the Inventory attachment function for that side.
* <p>
* This is a light weight version of getCapability, intended for metadata uses.
*
* @param capability The capability to check
* @param facing The Side to check from:
* CAN BE NULL. Null is defined to represent 'internal' or 'self'
* @return True if this object supports the capability.
*/
@Override
public boolean hasCapability(final Capability<?> capability, @Nullable final EnumFacing facing) {
return capability == getCapability();
}
/**
* Retrieves the handler for the capability requested on the specific side.
* The return value CAN be null if the object does not support the capability.
* The return value CAN be the same for multiple faces.
*
* @param capability The capability to check
* @param facing The Side to check from:
* CAN BE NULL. Null is defined to represent 'internal' or 'self'
* @return The handler if this object supports the capability.
*/
@Override
@Nullable
public <T> T getCapability(final Capability<T> capability, @Nullable final EnumFacing facing) {
if (capability == getCapability()) {
return getCapability().cast(getInstance());
}
return null;
}
/**
* Get the {@link Capability} instance to provide the handler for.
*
* @return The Capability instance
*/
public final Capability<HANDLER> getCapability() {
return capability;
}
/**
* Get the {@link EnumFacing} to provide the handler for.
*
* @return The EnumFacing to provide the handler for
*/
@Nullable
public EnumFacing getFacing() {
return facing;
}
/**
* Get the handler instance.
*
* @return The handler instance
*/
public final HANDLER getInstance() {
return instance;
}
}