-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1bceee
commit eb03b82
Showing
4 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/gregtech/api/capability/impl/EUToFEProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package gregtech.api.capability.impl; | ||
|
||
import gregtech.api.capability.GregtechCapabilities; | ||
import gregtech.common.ConfigHolder; | ||
import net.minecraft.tileentity.TileEntity; | ||
import net.minecraft.util.EnumFacing; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.capabilities.ICapabilityProvider; | ||
import net.minecraftforge.energy.CapabilityEnergy; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class EUToFEProvider implements ICapabilityProvider { | ||
|
||
private final TileEntity tileEntity; | ||
private GTEnergyWrapper wrapper; | ||
|
||
/** | ||
* Lock used for concurrency protection between hasCapability and getCapability. | ||
*/ | ||
ReentrantLock lock = new ReentrantLock(); | ||
|
||
public EUToFEProvider(TileEntity tileEntity) { | ||
this.tileEntity = tileEntity; | ||
} | ||
|
||
@Override | ||
public boolean hasCapability(@Nonnull Capability<?> capability, EnumFacing facing) { | ||
|
||
if (!ConfigHolder.U.energyOptions.nativeEUToFE) | ||
return false; | ||
|
||
if (lock.isLocked() || (capability != CapabilityEnergy.ENERGY && capability != GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER)) | ||
return false; | ||
|
||
// Wrap FE Machines with a GTEU EnergyContainer | ||
if (wrapper == null) wrapper = new GTEnergyWrapper(tileEntity); | ||
|
||
lock.lock(); | ||
try { | ||
return wrapper.isValid(facing); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T getCapability(@Nonnull Capability<T> capability, EnumFacing facing) { | ||
|
||
if (!ConfigHolder.U.energyOptions.nativeEUToFE) | ||
return null; | ||
|
||
if (lock.isLocked() || !hasCapability(capability, facing)) | ||
return null; | ||
|
||
if (wrapper == null) wrapper = new GTEnergyWrapper(tileEntity); | ||
|
||
lock.lock(); | ||
try { | ||
return wrapper.isValid(facing) ? (T) wrapper : null; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
Oops, something went wrong.