This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic computercraft integration
- Loading branch information
Showing
9 changed files
with
198 additions
and
1 deletion.
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
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
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
11 changes: 11 additions & 0 deletions
11
src/main/java/gregification/modules/computercraft/ComputerCraftConfig.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,11 @@ | ||
package gregification.modules.computercraft; | ||
|
||
import gregification.Gregification; | ||
import net.minecraftforge.common.config.Config; | ||
import net.minecraftforge.common.config.Config.LangKey; | ||
|
||
|
||
@LangKey("gregification.config.open_computers") | ||
@Config(modid = Gregification.MODID, name = Gregification.MODID + "/computercraft", category = "ComputerCraft") | ||
public class ComputerCraftConfig { | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/gregification/modules/computercraft/ComputerCraftModule.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,47 @@ | ||
package gregification.modules.computercraft; | ||
|
||
import dan200.computercraft.api.ComputerCraftAPI; | ||
import dan200.computercraft.api.peripheral.IPeripheral; | ||
import dan200.computercraft.api.peripheral.IPeripheralProvider; | ||
import gregification.base.BaseConfig; | ||
import gregification.base.ModIDs; | ||
import gregification.base.Module; | ||
import gregification.modules.computercraft.peripheral.MetaTileEntityPeripheral; | ||
import gregtech.api.metatileentity.MetaTileEntity; | ||
import gregtech.api.util.GTUtility; | ||
import net.minecraft.util.EnumFacing; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import net.minecraftforge.fml.common.Loader; | ||
import net.minecraftforge.fml.common.event.FMLInitializationEvent; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
@Module.Root(name = "Gregification: Computercraft") | ||
public class ComputerCraftModule implements Module { | ||
|
||
@Log | ||
public static Logger logger; | ||
|
||
@Override | ||
public boolean isModuleActive() { | ||
return BaseConfig.enableComputerCraftModule && Loader.isModLoaded(ModIDs.MODID_CC); | ||
} | ||
|
||
@Override | ||
public void init(FMLInitializationEvent event) { | ||
ComputerCraftAPI.registerPeripheralProvider(new IPeripheralProvider() { | ||
@Nullable | ||
@Override | ||
public IPeripheral getPeripheral(@Nonnull World world, @Nonnull BlockPos blockPos, @Nonnull EnumFacing enumFacing) { | ||
MetaTileEntity metaTileEntity = GTUtility.getMetaTileEntity(world, blockPos); | ||
if (metaTileEntity != null) { | ||
return new MetaTileEntityPeripheral(metaTileEntity, enumFacing); | ||
} | ||
return null; | ||
} | ||
}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/gregification/modules/computercraft/peripheral/MetaTileEntityPeripheral.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,44 @@ | ||
package gregification.modules.computercraft.peripheral; | ||
|
||
import dan200.computercraft.api.lua.ILuaContext; | ||
import dan200.computercraft.api.lua.LuaException; | ||
import dan200.computercraft.api.peripheral.IComputerAccess; | ||
import dan200.computercraft.api.peripheral.IPeripheral; | ||
import gregtech.api.metatileentity.MetaTileEntity; | ||
import net.minecraft.util.EnumFacing; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import java.util.Arrays; | ||
|
||
public class MetaTileEntityPeripheral implements IPeripheral { | ||
private final MetaTileEntityPeripheralFunction[] functions; | ||
|
||
public MetaTileEntityPeripheral(MetaTileEntity metaTileEntity, EnumFacing enumFacing) { | ||
this.functions = Arrays.stream(new MetaTileEntityPeripheralFunction[]{ | ||
new MetaTileEntityPeripheralEnergyContainerFunction(metaTileEntity, enumFacing), | ||
new MetaTileEntityPeripheralTierFunction(metaTileEntity), | ||
}).filter(MetaTileEntityPeripheralFunction::isEnabled).toArray(MetaTileEntityPeripheralFunction[]::new); | ||
} | ||
|
||
@Nonnull | ||
public String getType() { | ||
return "gregtech_meta_timer_entity"; | ||
} | ||
|
||
@Nonnull | ||
public String[] getMethodNames() { | ||
return Arrays.stream(functions).map(MetaTileEntityPeripheralFunction::getName).toArray(String[]::new); | ||
} | ||
|
||
@Nullable | ||
public Object[] callMethod(@Nonnull IComputerAccess iComputerAccess, @Nonnull ILuaContext iLuaContext, int i, @Nonnull Object[] objects) throws LuaException, InterruptedException { | ||
return functions[i].call(iComputerAccess, iLuaContext, i, objects); | ||
} | ||
|
||
public boolean equals(@Nullable IPeripheral iPeripheral) { | ||
return iPeripheral == this; | ||
} | ||
|
||
} | ||
|
44 changes: 44 additions & 0 deletions
44
...ion/modules/computercraft/peripheral/MetaTileEntityPeripheralEnergyContainerFunction.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,44 @@ | ||
package gregification.modules.computercraft.peripheral; | ||
|
||
import dan200.computercraft.api.lua.ILuaContext; | ||
import dan200.computercraft.api.peripheral.IComputerAccess; | ||
import gregtech.api.capability.GregtechCapabilities; | ||
import gregtech.api.capability.IEnergyContainer; | ||
import gregtech.api.metatileentity.MetaTileEntity; | ||
import net.minecraft.util.EnumFacing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class MetaTileEntityPeripheralEnergyContainerFunction implements MetaTileEntityPeripheralFunction { | ||
|
||
private final IEnergyContainer energyContainer; | ||
|
||
public MetaTileEntityPeripheralEnergyContainerFunction(MetaTileEntity metaTileEntity, EnumFacing enumFacing) { | ||
this.energyContainer = metaTileEntity.getCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, enumFacing); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return this.energyContainer != null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "getEnergy"; | ||
} | ||
|
||
@Override | ||
public Object[] call(IComputerAccess iComputerAccess, ILuaContext iLuaContext, int i, Object[] objects) { | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("stored", energyContainer.getEnergyStored()); | ||
data.put("capacity", energyContainer.getEnergyCapacity()); | ||
data.put("average_input", energyContainer.getInputPerSec()); | ||
data.put("average_output", energyContainer.getOutputPerSec()); | ||
data.put("input_voltage", energyContainer.getInputVoltage()); | ||
data.put("output_voltage", energyContainer.getOutputVoltage()); | ||
data.put("input_amperage", energyContainer.getInputAmperage()); | ||
data.put("output_amperage", energyContainer.getOutputAmperage()); | ||
return new Object[]{data}; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...java/gregification/modules/computercraft/peripheral/MetaTileEntityPeripheralFunction.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,13 @@ | ||
package gregification.modules.computercraft.peripheral; | ||
|
||
import dan200.computercraft.api.lua.ILuaContext; | ||
import dan200.computercraft.api.lua.LuaException; | ||
import dan200.computercraft.api.peripheral.IComputerAccess; | ||
|
||
interface MetaTileEntityPeripheralFunction { | ||
boolean isEnabled(); | ||
|
||
String getName(); | ||
|
||
Object[] call(IComputerAccess iComputerAccess, ILuaContext iLuaContext, int i, Object[] objects) throws LuaException, InterruptedException; | ||
} |
30 changes: 30 additions & 0 deletions
30
.../gregification/modules/computercraft/peripheral/MetaTileEntityPeripheralTierFunction.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,30 @@ | ||
package gregification.modules.computercraft.peripheral; | ||
|
||
import dan200.computercraft.api.lua.ILuaContext; | ||
import dan200.computercraft.api.peripheral.IComputerAccess; | ||
import gregtech.api.metatileentity.MetaTileEntity; | ||
import gregtech.api.metatileentity.TieredMetaTileEntity; | ||
|
||
class MetaTileEntityPeripheralTierFunction implements MetaTileEntityPeripheralFunction { | ||
|
||
private final TieredMetaTileEntity tieredMetaTileEntity; | ||
|
||
public MetaTileEntityPeripheralTierFunction(MetaTileEntity metaTileEntity) { | ||
this.tieredMetaTileEntity = metaTileEntity instanceof TieredMetaTileEntity ? (TieredMetaTileEntity) metaTileEntity : null; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return this.tieredMetaTileEntity != null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "getTier"; | ||
} | ||
|
||
@Override | ||
public Object[] call(IComputerAccess iComputerAccess, ILuaContext iLuaContext, int i, Object[] objects) { | ||
return new Object[]{tieredMetaTileEntity.getTier()}; | ||
} | ||
} |