Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
feat: basic computercraft integration
Browse files Browse the repository at this point in the history
  • Loading branch information
123FLO321 committed Mar 30, 2023
1 parent 223be80 commit c1f00a5
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ dependencies {
implementation fg.deobf("curse.maven:tfcraft-302973:3268988")
implementation fg.deobf("curse.maven:ae2-extended-life-570458:4323863")
implementation fg.deobf("curse.maven:gregtech-food-option-477021:4423324")
implementation fg.deobf("curse.maven:cc-tweaked-282001:2718128")

if (project.use_mixins.toBoolean()) {
compileOnly 'zone.rong:mixinbooter:7.0'
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gregification/base/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public class BaseConfig {
@Comment({"Enable TerraFirmaCraft integration.", "Default: true"})
@RequiresMcRestart
public static boolean enableTerraFirmaCraftModule = true;


@Comment({"Enable ComputerCraft integration.", "Default: true"})
@RequiresMcRestart
public static boolean enableComputerCraftModule = true;

}
3 changes: 2 additions & 1 deletion src/main/java/gregification/base/ModIDs.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public class ModIDs {
MODID_THAUM = "thaumcraft",
GENDUSTRY = "gendustry",
MODID_GTFO = "gregtechfoodoption",
MODID_TFC = "tfc";
MODID_TFC = "tfc",
MODID_CC = "computercraft";
}
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 {
}
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;
}
});
}
}
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;
}

}

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};
}
}
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;
}
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()};
}
}

0 comments on commit c1f00a5

Please sign in to comment.