Skip to content

Commit

Permalink
port Native EU to FE
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Aug 13, 2021
1 parent d1bceee commit eb03b82
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechCapabilities.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package gregtech.api.capability;

import gregtech.api.GTValues;
import gregtech.api.capability.impl.EUToFEProvider;
import gregtech.api.capability.tool.ICutterItem;
import gregtech.api.capability.tool.IScrewdriverItem;
import gregtech.api.capability.tool.ISoftHammerItem;
import gregtech.api.capability.tool.IWrenchItem;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod.EventBusSubscriber(modid = GTValues.MODID)
public class GregtechCapabilities {

@CapabilityInject(IEnergyContainer.class)
Expand All @@ -33,4 +41,10 @@ public class GregtechCapabilities {
@CapabilityInject(IMultiblockController.class)
public static Capability<IMultiblockController> CAPABILITY_MULTIBLOCK_CONTROLLER = null;

private static final ResourceLocation CAPABILITY_EU_TO_FE = new ResourceLocation(GTValues.MODID, "fe_capability");

@SubscribeEvent
public static void attachTileCapability(AttachCapabilitiesEvent<TileEntity> event) {
event.addCapability(CAPABILITY_EU_TO_FE, new EUToFEProvider(event.getObject()));
}
}
67 changes: 67 additions & 0 deletions src/main/java/gregtech/api/capability/impl/EUToFEProvider.java
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();
}
}
}
Loading

0 comments on commit eb03b82

Please sign in to comment.