feat: more generic approach to energy compat

This commit is contained in:
Timo Ley 2022-12-09 22:33:37 +01:00
parent 95d0145266
commit 1a532aa4dd
9 changed files with 56 additions and 106 deletions

View file

@ -1,20 +1,40 @@
package universalelectricity.compat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraft.tileentity.TileEntity;
import universalelectricity.api.CompatibilityModule;
import universalelectricity.compat.ic2.IC2CompatModule;
import universalelectricity.compat.ic2.EventHandler;
import universalelectricity.compat.ic2.TickHandler;
import universalelectricity.prefab.tile.ElectricTileDriver;
public class CompatHandler {
static Map<TileEntity, ElectricTileDriver> ticklist = new ConcurrentHashMap<>();
public static void initCompatHandlers() {
FMLCommonHandler.instance().bus().register(new CompatTickHandler());
if (Loader.isModLoaded("IC2")) {
CompatibilityModule.register(new IC2CompatModule());
MinecraftForge.EVENT_BUS.register(new EventHandler());
FMLCommonHandler.instance().bus().register(new TickHandler());
}
}
public static void tick() {
for(TileEntity t : ticklist.keySet()) {
ElectricTileDriver driver = ticklist.get(t);
if (!driver.tick()) {
System.out.println("Remove driver for tile at x:" + t.xCoord + " y:" + t.yCoord + " z:" + t.zCoord);
ticklist.remove(t);
driver.invalidate();
}
}
}
public static void registerTile(TileEntity tile) {
if (!ticklist.containsKey(tile) && CompatibilityModule.isHandler(tile)) {
ticklist.put(tile, new ElectricTileDriver(tile));
}
}

View file

@ -1,15 +1,15 @@
package universalelectricity.compat.ic2;
package universalelectricity.compat;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
public class TickHandler {
public class CompatTickHandler {
@SubscribeEvent
public void onTick(ServerTickEvent event) {
if (event.phase == Phase.END) {
EnergyNetCache.tickAll();
CompatHandler.tick();
}
}

View file

@ -1,40 +0,0 @@
package universalelectricity.compat.ic2;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import universalelectricity.core.Pair;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.tile.ElectricTileDriver;
public class EnergyNetCache {
private static Map<Pair<World, Vector3>, ElectricTileDriver> tiles = new ConcurrentHashMap<>();
public static Pair<World, Vector3> toKey(TileEntity tile) {
return new Pair<World, Vector3>(tile.getWorldObj(), new Vector3(tile));
}
public static void load(TileEntity tile) {
tiles.put(toKey(tile), new ElectricTileDriver(tile));
}
public static void unload(TileEntity tile) {
ElectricTileDriver handler = tiles.get(toKey(tile));
if (handler != null) handler.invalidate();
tiles.remove(toKey(tile));
}
public static void tickAll() {
for (ElectricTileDriver handler : tiles.values()) {
handler.tick();
}
}
public static boolean contains(TileEntity tile) {
return tiles.containsKey(toKey(tile));
}
}

View file

@ -1,32 +0,0 @@
package universalelectricity.compat.ic2;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
import cpw.mods.fml.relauncher.Side;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import mekanism.api.energy.ICableOutputter;
import mekanism.api.energy.IStrictEnergyAcceptor;
import net.minecraft.tileentity.TileEntity;
import universalelectricity.core.block.IConnector;
public class EventHandler {
@SubscribeEvent
public void onLoadTile(EnergyTileLoadEvent event) {
TileEntity te = (TileEntity) event.energyTile;
if (te instanceof IConnector || te instanceof IStrictEnergyAcceptor || te instanceof ICableOutputter) return;
if (te instanceof IEnergySink || te instanceof IEnergySource) EnergyNetCache.load(te);
}
@SubscribeEvent
public void onUnloadTile(EnergyTileUnloadEvent event) {
TileEntity te = (TileEntity) event.energyTile;
EnergyNetCache.unload(te);
}
}

View file

@ -1,21 +0,0 @@
package universalelectricity.compat.ic2;
public class IC2CompatHelper {
public static int voltToTier(double volt) {
if (volt <= 120.0) return 1;
if (volt <= 240.0) return 2;
if (volt <= 480.0) return 3;
return 4;
}
public static double tierToVolt(int tier) {
switch (tier) {
case 1: return 120.0;
case 2: return 240.0;
case 3: return 480.0;
default: return 960.0;
}
}
}

View file

@ -162,7 +162,7 @@ public class IC2CompatModule extends CompatibilityModule {
TileEntity tile = getENetTile(handler);
if (tile instanceof IEnergySink) {
return IC2CompatHelper.tierToVolt(((IEnergySink)tile).getSinkTier());
return tierToVolt(((IEnergySink)tile).getSinkTier());
}
return 0;
@ -173,7 +173,7 @@ public class IC2CompatModule extends CompatibilityModule {
TileEntity tile = getENetTile(handler);
if (tile instanceof IEnergySource) {
return IC2CompatHelper.tierToVolt(((IEnergySource)tile).getSourceTier());
return tierToVolt(((IEnergySource)tile).getSourceTier());
}
return 0;
@ -222,5 +222,21 @@ public class IC2CompatModule extends CompatibilityModule {
}
return 0;
}
public static int voltToTier(double volt) {
if (volt <= 120.0) return 1;
if (volt <= 240.0) return 2;
if (volt <= 480.0) return 3;
return 4;
}
public static double tierToVolt(int tier) {
switch (tier) {
case 1: return 120.0;
case 2: return 240.0;
case 3: return 480.0;
default: return 960.0;
}
}
}

View file

@ -17,7 +17,7 @@ public class UniversalElectricity {
public static final Configuration CONFIGURATION = new Configuration(new File(Loader.instance().getConfigDir(), "UniversalElectricity.cfg"));
public static double UE_IC2_RATIO = CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio;
public static double UE_RF_RATIO = CompatibilityType.REDSTONE_FLUX.reciprocal_ratio;
public static boolean isVoltageSensitive = false;
public static boolean isVoltageSensitive = true;
public static boolean isNetworkActive = false;
public static final Material machine = new Material(MapColor.ironColor);

View file

@ -9,6 +9,7 @@ import java.util.Set;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import universalelectricity.api.CompatibilityModule;
import universalelectricity.core.UniversalElectricity;
import universalelectricity.core.electricity.ElectricityNetworkHelper;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.electricity.IElectricityNetwork;
@ -26,7 +27,8 @@ public class ElectricTileDriver {
ElectricityNetworkHelper.invalidate(handler);
}
public void tick() {
public boolean tick() {
if (handler.isInvalid()) return false;
Map<ForgeDirection, IElectricityNetwork> networks = getNetworks();
Set<ForgeDirection> inputSides = new HashSet<>();
if (CompatibilityModule.canReceive(handler, ForgeDirection.UNKNOWN)) {
@ -35,6 +37,7 @@ public class ElectricTileDriver {
if (CompatibilityModule.canExtract(handler, ForgeDirection.UNKNOWN)) {
produce(networks, inputSides);
}
return networks.size() > 0;
}
public Set<ForgeDirection> consume(Map<ForgeDirection, IElectricityNetwork> networks) {
@ -50,7 +53,7 @@ public class ElectricTileDriver {
inputSides.add(side);
net.startRequesting(handler, wattsPerSide / voltage, voltage);
ElectricityPack receivedPack = net.consumeElectricity(handler);
if (receivedPack.voltage > voltage) {
if (receivedPack.voltage > voltage && UniversalElectricity.isVoltageSensitive) {
handler.getWorldObj().createExplosion(null, handler.xCoord, handler.yCoord, handler.zCoord, 1, true);
return EnumSet.allOf(ForgeDirection.class);
}

View file

@ -11,8 +11,10 @@ import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import universalelectricity.compat.CompatHandler;
import universalelectricity.core.block.IConductor;
import universalelectricity.core.block.INetworkProvider;
import universalelectricity.core.block.ISelfDriven;
import universalelectricity.core.electricity.ElectricityNetwork;
import universalelectricity.core.electricity.ElectricityNetworkHelper;
import universalelectricity.core.electricity.IElectricityNetwork;
@ -34,6 +36,8 @@ public abstract class TileEntityConductor extends TileEntityAdvanced implements
this.visuallyConnected[side.ordinal()] = true;
if(tileEntity.getClass() == this.getClass() && tileEntity instanceof INetworkProvider) {
this.getNetwork().mergeConnection(((INetworkProvider)tileEntity).getNetwork());
} else if (!(tileEntity instanceof ISelfDriven)) {
CompatHandler.registerTile(tileEntity);
}
return;