feat: integrate ComputerCraft compat

This commit is contained in:
Timo Ley 2023-01-29 23:11:27 +01:00
parent 8a6290228b
commit b0f7d24c59
9 changed files with 258 additions and 27 deletions

View file

@ -44,6 +44,7 @@ dependencies {
implementation "codechicken:CodeChickenLib:1.7.10-1.1.3.141:dev"
implementation "codechicken:NotEnoughItems:1.7.10-1.0.5.120:dev"
implementation "codechicken:CodeChickenCore:1.7.10-1.0.7.48:dev"
implementation 'dan200:ComputerCraft:1.75:deobf'
}
processResources {

View file

@ -1,6 +1,7 @@
package com.eloraam.redpower;
import com.eloraam.redpower.compat.BlockMachineCompat;
import com.eloraam.redpower.compat.ComputercraftInterop;
import com.eloraam.redpower.compat.ItemMachineCompat;
import com.eloraam.redpower.compat.RenderBlueEngine;
import com.eloraam.redpower.compat.TileBlueEngine;
@ -69,7 +70,9 @@ public class RedPowerCompat implements IGuiHandler {
if (event.getSide().isClient()) {
this.registerRenderers();
}
if (Loader.isModLoaded("ComputerCraft")) {
ComputercraftInterop.initInterop();
}
NetworkRegistry.INSTANCE.registerGuiHandler(instance, instance);
}

View file

@ -4,7 +4,9 @@ import com.eloraam.redpower.core.Config;
import com.eloraam.redpower.core.CoreEvents;
import com.eloraam.redpower.core.CoreLib;
import com.eloraam.redpower.core.CoverRecipe;
import com.eloraam.redpower.core.IRedPowerConnectableAdaptor;
import com.eloraam.redpower.core.PacketHandler;
import com.eloraam.redpower.core.BaseConnectableAdaptor;
import com.eloraam.redpower.core.RenderHighlight;
import com.eloraam.redpower.core.RenderSimpleCovered;
import com.eloraam.redpower.core.TileCovered;
@ -22,6 +24,9 @@ import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ICrafting;
@ -45,6 +50,7 @@ public class RedPowerCore {
public static int nullBlockModel = -1;
@SideOnly(Side.CLIENT)
public static IIcon missing;
public static List<IRedPowerConnectableAdaptor> redPowerAdaptors = new ArrayList<>();
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
@ -54,7 +60,8 @@ public class RedPowerCore {
if (FMLCommonHandler.instance().getSide().isClient()) {
MinecraftForge.EVENT_BUS.register(instance);
}
redPowerAdaptors = new ArrayList<>();
redPowerAdaptors.add(new BaseConnectableAdaptor());
}
@EventHandler

View file

@ -0,0 +1,37 @@
package com.eloraam.redpower.compat;
import com.eloraam.redpower.core.IRedPowerConnectableAdaptor;
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
import net.minecraft.tileentity.TileEntity;
public class ComputerCraftConnectableAdaptor implements IRedPowerConnectableAdaptor {
@Override
public boolean canHandle(TileEntity tile) {
return tile instanceof TileComputerBase;
}
@Override
public int getPoweringMask(int ch, TileEntity tile) {
TileComputerBase computer = (TileComputerBase) tile;
int direction = computer.getDirection();
return ComputercraftInterop.getComputerPoweringMask(ch, computer, direction);
}
@Override
public int getConnectableMask(TileEntity tile) {
return -1;
}
@Override
public int getConnectClass(int var1, TileEntity tile) {
return ComputercraftInterop.getComputerConnectClass();
}
@Override
public int getCornerPowerMode(TileEntity tile) {
return 0;
}
}

View file

@ -0,0 +1,52 @@
package com.eloraam.redpower.compat;
import com.eloraam.redpower.RedPowerCore;
import com.eloraam.redpower.core.RedPowerLib;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
public class ComputercraftInterop {
private static int computerConnectClass = 1337;
public static void initInterop() {
addComputerConnectMappings();
RedPowerCore.redPowerAdaptors.add(new ComputerCraftConnectableAdaptor());
ComputerCraft.registerBundledRedstoneProvider(new RedPowerBundledProvider());
}
public static int getComputerConnectClass() {
return computerConnectClass;
}
public static void addComputerConnectMappings() {
RedPowerLib.addCompatibleMapping(0, computerConnectClass);
RedPowerLib.addCompatibleMapping(18, computerConnectClass);
for (int i = 0; i < 16; ++i) {
RedPowerLib.addCompatibleMapping(1 + i, computerConnectClass);
RedPowerLib.addCompatibleMapping(19 + i, computerConnectClass);
}
}
public static int getComputerPoweringMask(int ch, TileComputerBase computer, int direction) {
if (ch == 0) {
int mask = 0;
for (int side = 0; side < 6; ++side) {
if (computer.getRedstoneOutput(side) == 0)
continue;
mask |= RedPowerLib.getConDirMask(side);
}
return mask;
}
int mask = 0;
for (int side = 0; side < 6; ++side) {
int channelMask = computer.getBundledRedstoneOutput(side);
if ((channelMask & 1 << ch - 1) <= 0)
continue;
mask |= RedPowerLib.getConDirMask(side);
}
return mask;
}
}

View file

@ -0,0 +1,30 @@
package com.eloraam.redpower.compat;
import com.eloraam.redpower.core.IRedPowerConnectable;
import com.eloraam.redpower.core.RedPowerLib;
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
import net.minecraft.util.Facing;
import net.minecraft.world.World;
public class RedPowerBundledProvider implements IBundledRedstoneProvider {
@Override
public int getBundledRedstoneOutput(World world, int x, int y, int z, int side) {
if (world.getTileEntity(x, y, z) instanceof IRedPowerConnectable) {
int offsetX = x + Facing.offsetsXForSide[side];
int offsetY = y + Facing.offsetsYForSide[side];
int offsetZ = z + Facing.offsetsZForSide[side];
int offsetSide = Facing.oppositeSide[side];
int cons = RedPowerLib.getConDirMask(offsetSide);
int combination = 0;
for (int c = 0; c < 16; ++c) {
if (RedPowerLib.getPowerState(world, offsetX, offsetY, offsetZ, cons, c + 1) <= 0) continue;
combination |= 1 << c;
}
return combination;
}
return -1;
}
}

View file

@ -0,0 +1,32 @@
package com.eloraam.redpower.core;
import net.minecraft.tileentity.TileEntity;
public class BaseConnectableAdaptor implements IRedPowerConnectableAdaptor {
@Override
public int getPoweringMask(int var1, TileEntity tile) {
return ((IRedPowerConnectable)tile).getPoweringMask(var1);
}
@Override
public int getConnectableMask(TileEntity tile) {
return ((IConnectable)tile).getConnectableMask();
}
@Override
public int getConnectClass(int var1, TileEntity tile) {
return ((IConnectable)tile).getConnectClass(var1);
}
@Override
public int getCornerPowerMode(TileEntity tile) {
return ((IConnectable)tile).getCornerPowerMode();
}
@Override
public boolean canHandle(TileEntity tile) {
return tile instanceof IRedPowerConnectable;
}
}

View file

@ -0,0 +1,17 @@
package com.eloraam.redpower.core;
import net.minecraft.tileentity.TileEntity;
public interface IRedPowerConnectableAdaptor {
boolean canHandle(TileEntity tile);
int getPoweringMask(int var1, TileEntity tile);
int getConnectableMask(TileEntity tile);
int getConnectClass(int var1, TileEntity tile);
int getCornerPowerMode(TileEntity tile);
}

View file

@ -6,6 +6,9 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import com.eloraam.redpower.RedPowerCore;
import net.minecraft.block.Block;
import net.minecraft.block.BlockButton;
import net.minecraft.block.BlockRedstoneWire;
@ -212,13 +215,16 @@ public class RedPowerLib {
}
private static int getSidePowerMask(IBlockAccess iba, int x, int y, int z, int ch, int side) {
IRedPowerConnectable irp = CoreLib.getTileEntity(iba, x, y, z, IRedPowerConnectable.class);
TileEntity tile = CoreLib.getTileEntity(iba, x, y, z, TileEntity.class);
int mask = getConDirMask(side);
if (irp != null) {
int m = irp.getPoweringMask(ch);
m = (m & 1431655765) << 1 | (m & 715827882) >> 1;
return m & mask;
} else if (ch != 0) {
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
int m = adp.getPoweringMask(ch, tile);
m = (m & 1431655765) << 1 | (m & 715827882) >> 1;
return m & mask;
}
}
if (ch != 0) {
return 0;
} else {
return isWeakPoweringTo(iba, x, y, z, side) ? mask & 16777215 : (isPoweringTo(iba, x, y, z, side) ? mask : 0);
@ -355,10 +361,10 @@ public class RedPowerLib {
if (iba.isAirBlock(i, j, k)) {
return 0;
} else {
IConnectable rpa = CoreLib.getTileEntity(iba, i, j, k, IConnectable.class);
if (rpa != null) {
int md = rpa.getConnectClass(side);
return isCompatible(md, pcl) ? rpa.getConnectableMask() : 0;
TileEntity tile = CoreLib.getTileEntity(iba, i, j, k, TileEntity.class);
if (isConnectable(tile)) {
int md = getConnectClass(side, tile);
return isCompatible(md, pcl) ? getConnectableMask(tile) : 0;
} else if (!isCompatible(0, pcl)) {
return 0;
} else if (block == Blocks.piston || block == Blocks.sticky_piston) {
@ -395,9 +401,9 @@ public class RedPowerLib {
if (iba.isAirBlock(i, j, k)) {
return 0;
} else {
IConnectable rpa = CoreLib.getTileEntity(iba, i, j, k, IConnectable.class);
if (rpa != null) {
int cc2 = rpa.getCornerPowerMode();
TileEntity tile = CoreLib.getTileEntity(iba, i, j, k, TileEntity.class);
if (isConnectable(tile)) {
int cc2 = getCornerPowerMode(tile);
if (cc == 0 || cc2 == 0) {
return 0;
} else if (cc == 2 && cc2 == 2) {
@ -405,8 +411,8 @@ public class RedPowerLib {
} else if (cc == 3 && cc2 == 1) {
return 0;
} else {
int pc = rpa.getConnectClass(side);
return isCompatible(pc, pcl) ? rpa.getConnectableMask() : 0;
int pc = getConnectClass(side, tile);
return isCompatible(pc, pcl) ? getConnectableMask(tile) : 0;
}
} else {
return 0;
@ -552,14 +558,19 @@ public class RedPowerLib {
}
public static int getTileCurrentStrength(World world, int i, int j, int k, int cons, int ch) {
IRedPowerConnectable irp = CoreLib.getTileEntity(world, i, j, k, IRedPowerConnectable.class);
if (irp == null) {
TileEntity tile = CoreLib.getTileEntity(world, i, j, k, TileEntity.class);
if (tile == null) {
return -1;
} else if (irp instanceof IRedPowerWiring) {
IRedPowerWiring irw = (IRedPowerWiring)irp;
} else if (tile instanceof IRedPowerWiring) {
IRedPowerWiring irw = (IRedPowerWiring)tile;
return irw.getCurrentStrength(cons, ch);
} else {
return (irp.getPoweringMask(ch) & cons) > 0 ? 255 : -1;
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return (adp.getPoweringMask(ch, tile) & cons) > 0 ? 255 : -1;
}
}
return -1;
}
}
@ -571,14 +582,19 @@ public class RedPowerLib {
int irp1 = world.getBlockMetadata(i, j, k);
return irp1 > 0 ? irp1 : -1;
} else {
IRedPowerConnectable irp = CoreLib.getTileEntity(world, i, j, k, IRedPowerConnectable.class);
if (irp == null) {
TileEntity tile = CoreLib.getTileEntity(world, i, j, k, TileEntity.class);
if (tile == null) {
return -1;
} else if (irp instanceof IRedPowerWiring) {
IRedPowerWiring irw = (IRedPowerWiring)irp;
} else if (tile instanceof IRedPowerWiring) {
IRedPowerWiring irw = (IRedPowerWiring)tile;
return irw.getCurrentStrength(cons, ch);
} else {
return (irp.getPoweringMask(ch) & cons) > 0 ? 255 : -1;
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return (adp.getPoweringMask(ch, tile) & cons) > 0 ? 255 : -1;
}
}
return -1;
}
}
}
@ -875,6 +891,42 @@ public class RedPowerLib {
return a == b || powerClassMapping.contains(new RedPowerLib.PowerClassCompat(a, b));
}
public static boolean isConnectable(TileEntity tile) {
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return true;
}
}
return tile instanceof IConnectable;
}
public static int getConnectableMask(TileEntity tile) {
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return adp.getConnectableMask(tile);
}
}
return ((IConnectable)tile).getConnectableMask();
}
public static int getConnectClass(int var1, TileEntity tile) {
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return adp.getConnectClass(var1, tile);
}
}
return ((IConnectable)tile).getConnectClass(var1);
}
public static int getCornerPowerMode(TileEntity tile) {
for (IRedPowerConnectableAdaptor adp : RedPowerCore.redPowerAdaptors) {
if (adp.canHandle(tile)) {
return adp.getCornerPowerMode(tile);
}
}
return ((IConnectable)tile).getCornerPowerMode();
}
public static class PowerClassCompat {
private final int a;
private final int b;