Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ repositories {
includeGroup "curse.maven"
}
}
maven {
url "https://squiddev.cc/maven/"
content {
includeGroup("cc.tweaked")
}
}
mavenCentral()
}

Expand Down Expand Up @@ -151,6 +157,10 @@ dependencies {
exclude group: "it.unimi.dsi", module: "fastutil"
}
implementation fg.deobf("icyllis.modernui:ModernUI-Forge:${minecraft_version}-${modernui_forge_version}")

compileOnly("cc.tweaked:cc-tweaked-${minecraft_version}-core-api:1.110.3")
compileOnly(fg.deobf("cc.tweaked:cc-tweaked-${minecraft_version}-forge-api:1.110.3"))
runtimeOnly(fg.deobf("cc.tweaked:cc-tweaked-${minecraft_version}-forge:1.110.3"))
}

processResources {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/sonar/fluxnetworks/FluxNetworks.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sonar.fluxnetworks.common.integration.cctweaked.PeripheralHandlerWrapper;

import javax.annotation.Nonnull;

Expand All @@ -19,12 +20,17 @@ public class FluxNetworks {

private static boolean sCuriosLoaded;
private static boolean sModernUILoaded;
private static boolean sComputercraftLoaded;

public FluxNetworks() {
sCuriosLoaded = ModList.get().isLoaded("curios");
sModernUILoaded = ModList.get().isLoaded("modernui");
sComputercraftLoaded = ModList.get().isLoaded("computercraft");

FluxConfig.init();

if (sComputercraftLoaded)
PeripheralHandlerWrapper.init();
}

public static boolean isCuriosLoaded() {
Expand All @@ -34,6 +40,9 @@ public static boolean isCuriosLoaded() {
public static boolean isModernUILoaded() {
return sModernUILoaded;
}
public static boolean issComputercraftLoaded() {
return sComputercraftLoaded;
}

@Nonnull
public static ResourceLocation location(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package sonar.fluxnetworks.common.integration.cctweaked;

import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.ObjectLuaTable;
import dan200.computercraft.api.peripheral.IPeripheral;
import org.jetbrains.annotations.Nullable;
import sonar.fluxnetworks.api.device.IFluxDevice;
import sonar.fluxnetworks.common.device.TileFluxDevice;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class CCTPeripheral implements IPeripheral {

record NetworkMemberRecord(String name, String accessLevel) {
}

record NetworkDeviceRecord(
String type,
long maxTransferLimit,
boolean isChunkLoaded,

boolean isForcedChunkLoaded,
String customName,
boolean limitDisabled,

Map<String, Object> position,
boolean surgeMode,
long transferBuffer,
long transferChange
) {}

private final TileFluxDevice device;

public CCTPeripheral(TileFluxDevice device) {
this.device = device;
}

@Override
public String getType() {
return "flux-device";
}

@Override
public boolean equals(@Nullable IPeripheral other) {
return false;
}

@LuaFunction(mainThread = true)
public String getNetworkName() {
if (!isValid()) return null;

String name = device.getNetwork().getNetworkName();
if (name.isEmpty())
return null;
return name;
}

private Map<String, Object> objectToMap(Object object) {
Map<String, Object> map = new HashMap<>();
Field[] fields = object.getClass().getDeclaredFields();

for (Field field: fields) {
field.setAccessible(true);
try {
map.put(field.getName(), field.get(object));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return map;
}

@LuaFunction(mainThread = true)
public int getNetworkID() {
return device.getNetwork().getNetworkID();
}

@LuaFunction(mainThread = true)
public long getEnergyInput() {
return device.getNetwork().getStatistics().energyInput;
}

@LuaFunction(mainThread = true)
public long getEnergyOutput() {
return device.getNetwork().getStatistics().energyOutput;
}

@LuaFunction(mainThread = true)
public long getEnergy() {
return device.getNetwork().getStatistics().totalEnergy;
}

@LuaFunction(mainThread = true)
public long getEnergyBuffer() {
return device.getNetwork().getStatistics().totalBuffer;
}

@LuaFunction(mainThread = true)
public long getPlugsCount() {
return device.getNetwork().getStatistics().fluxPlugCount;
}

@LuaFunction(mainThread = true)
public long getPointCount() {
return device.getNetwork().getStatistics().fluxPointCount;
}

@LuaFunction(mainThread = true)
public long getStorageCount() {
return device.getNetwork().getStatistics().fluxStorageCount;
}

@LuaFunction(mainThread = true)
public long getControllerCount() {
return device.getNetwork().getStatistics().fluxControllerCount;
}

@LuaFunction(mainThread = true)
public long getAVGTickUs() {
return device.getNetwork().getStatistics().averageTickMicro;
}

@LuaFunction(mainThread = true)
public String getSecurityLevel() {
return device.getNetwork().getSecurityLevel().toString();
}

@LuaFunction(mainThread = true)
public String getOwner() {
if (!isValid()) return null;

return device.getNetwork().getOwnerUUID().toString();
}

@LuaFunction(mainThread = true)
public ObjectLuaTable getMembers() {
if (!isValid()) return null;

return new ObjectLuaTable(
device.getNetwork().getAllMembers().stream().collect(Collectors.toMap(
item -> item.getPlayerUUID().toString(),
item -> objectToMap(new NetworkMemberRecord(item.getCachedName(), item.getAccessLevel().toString()))
)
)
);
}

@LuaFunction(mainThread = true)
public ObjectLuaTable getConnections() {
if (!isValid()) return null;

Map<Integer, Object> map = new HashMap<>();
int i = 0;
for (IFluxDevice device : device.getNetwork().getAllConnections()) {
map.put(
i++,
objectToMap(
new NetworkDeviceRecord(
device.getDeviceType().toString(),
device.getMaxTransferLimit(),
device.isChunkLoaded(),
device.isForcedLoading(),
device.getCustomName(),
device.getDisableLimit(),
objectToMap(device.getGlobalPos()),
device.getSurgeMode(),
device.getTransferBuffer(),
device.getTransferChange()
)
)
);
}

return new ObjectLuaTable(map);
}

@LuaFunction(mainThread = true)
public boolean isValid() {
return device.getNetwork().isValid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sonar.fluxnetworks.common.integration.cctweaked;

import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;
import sonar.fluxnetworks.common.device.TileFluxController;
import sonar.fluxnetworks.common.device.TileFluxDevice;

public class PeripheralHandler implements IPeripheralProvider {

@Override
public LazyOptional<IPeripheral> getPeripheral(Level world, BlockPos pos, Direction side) {
BlockEntity tile = world.getBlockEntity(pos);

if (tile instanceof TileFluxDevice fluxDevice) return LazyOptional.of(() -> new CCTPeripheral(fluxDevice));
return LazyOptional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sonar.fluxnetworks.common.integration.cctweaked;

import dan200.computercraft.api.ForgeComputerCraftAPI;

/**
* This class exists, so we do not import PeripheralHandler directly into the FluxNetworks class
* as PeripheralHandler implements a computercraft class. This would break if the computercraft mod
* is not available
*/
public class PeripheralHandlerWrapper {

public static void init() {
ForgeComputerCraftAPI.registerPeripheralProvider(new PeripheralHandler());
}

}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ license="MIT"
versionRange="[3.7.0,)"
ordering="NONE"
side="BOTH"
[[dependencies.fluxnetworks]]
modId="computercraft"
mandatory=false
versionRange="[1.110.3,)"
ordering="NONE"
side="BOTH"
[[dependencies.fluxnetworks]]
modId="jei"
mandatory=false
Expand Down