Skip to content

Commit

Permalink
Merge pull request #577 from Dertiende/1.20
Browse files Browse the repository at this point in the history
port last patch 1.19->1.20
  • Loading branch information
McJty authored May 18, 2024
2 parents 92fac84 + b9fb9de commit ca7dee6
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 242 deletions.
40 changes: 39 additions & 1 deletion src/main/java/mcjty/xnet/apiimpl/ConnectedBlock.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package mcjty.xnet.apiimpl;

import mcjty.rftoolsbase.api.xnet.keys.SidedConsumer;
import mcjty.xnet.modules.cables.blocks.ConnectorTileEntity;
import net.minecraft.core.BlockPos;

public record ConnectedBlock<T>(SidedConsumer sidedConsumer, T settings, BlockPos connectorPos){
import javax.annotation.Nonnull;


public class ConnectedBlock<T> {
@Nonnull private final SidedConsumer sidedConsumer;
@Nonnull private final T settings;
@Nonnull private final BlockPos connectorPos;
@Nonnull private final BlockPos blockPos;
@Nonnull private final ConnectorTileEntity connectorEntity;

public ConnectedBlock(@Nonnull SidedConsumer sidedConsumer, @Nonnull T settings, @Nonnull BlockPos connectorPos,
@Nonnull BlockPos blockPos, @Nonnull ConnectorTileEntity connectorEntity) {
this.sidedConsumer = sidedConsumer;
this.settings = settings;
this.connectorPos = connectorPos;
this.blockPos = blockPos;
this.connectorEntity = connectorEntity;
}

@Nonnull
public SidedConsumer sidedConsumer() {return sidedConsumer;}

@Nonnull
public T settings() {return settings;}

@Nonnull
public BlockPos connectorPos() {return connectorPos;}

@Nonnull
public BlockPos getBlockPos() {
return blockPos;
}

@Nonnull
public ConnectorTileEntity getConnectorEntity() {
return connectorEntity;
}

}
25 changes: 25 additions & 0 deletions src/main/java/mcjty/xnet/apiimpl/ConnectedInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mcjty.xnet.apiimpl;

import mcjty.rftoolsbase.api.xnet.keys.SidedConsumer;
import mcjty.xnet.apiimpl.logic.ConnectedEntity;
import mcjty.xnet.modules.cables.blocks.ConnectorTileEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;

import javax.annotation.Nonnull;

public class ConnectedInventory<T, H> extends ConnectedEntity<T> {
@Nonnull private final H handler;

public ConnectedInventory(@Nonnull SidedConsumer sidedConsumer, @Nonnull T settings, @Nonnull BlockPos connectorPos,
@Nonnull BlockPos blockPos, @Nonnull BlockEntity connectedEntity,
@Nonnull ConnectorTileEntity connectorEntity, @Nonnull H handler) {
super(sidedConsumer, settings, connectorPos, blockPos, connectedEntity, connectorEntity);
this.handler = handler;
}

@Nonnull
public H getHandler() {
return handler;
}
}
80 changes: 47 additions & 33 deletions src/main/java/mcjty/xnet/apiimpl/energy/EnergyChannelSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class EnergyChannelSettings extends DefaultChannelSettings implements ICh
public static final ResourceLocation iconGuiElements = new ResourceLocation(XNet.MODID, "textures/gui/guielements.png");

// Cache data
private List<EnergyConnectedBlock> energyExtractors = null;
private List<EnergyConnectedBlock> energyConsumers = null;
private List<EnergyConnectedEntity> energyExtractors = null;
private List<EnergyConnectedEntity> energyConsumers = null;
private long maxConsume = 0; // Maximum RF that all consumers can accept per tick

@Override
Expand Down Expand Up @@ -79,28 +79,21 @@ public void tick(int channel, IControllerContext context) {
Map<BlockPos, Integer> alreadyHandled = new HashMap<>();

List<Pair<ConnectorTileEntity, Integer>> energyProducers = new ArrayList<>();
for (EnergyConnectedBlock extractor : energyExtractors) {
for (EnergyConnectedEntity extractor : energyExtractors) {
BlockPos connectorPos = extractor.connectorPos();
if (connectorPos == null) {
continue;
}

Direction side = extractor.sidedConsumer().side();
BlockPos energyPos = connectorPos.relative(side);
if (!LevelTools.isLoaded(world, energyPos)) {
if (!LevelTools.isLoaded(world, extractor.getBlockPos())) {
continue;
}

BlockEntity te = world.getBlockEntity(energyPos);
BlockEntity te = extractor.getConnectedEntity();
// @todo report error somewhere?
if (!isEnergyTE(te, side.getOpposite())) {
continue;
}
EnergyConnectorSettings settings = extractor.settings();
ConnectorTileEntity connectorTE = (ConnectorTileEntity) world.getBlockEntity(connectorPos);
if (connectorTE == null) {
continue;
}
ConnectorTileEntity connectorTE = extractor.getConnectorEntity();

if (checkRedstone(world, settings, connectorPos) || !context.matchColor(settings.getColorsMask())) {
continue;
Expand Down Expand Up @@ -163,20 +156,14 @@ public void tick(int channel, IControllerContext context) {
private long insertEnergy(@Nonnull IControllerContext context, long energy) {
long total = 0;
Level world = context.getControllerWorld();
for (EnergyConnectedBlock consumer : energyConsumers) {
for (EnergyConnectedEntity consumer : energyConsumers) {
EnergyConnectorSettings settings = consumer.settings();
BlockPos connectorPos = consumer.connectorPos();
if (connectorPos == null) {
if (!LevelTools.isLoaded(world, consumer.getBlockPos())) {
continue;
}
Direction side = consumer.sidedConsumer().side();
BlockPos connectedBlockPos = connectorPos.relative(side);
if (!LevelTools.isLoaded(world, connectedBlockPos)) {
continue;
}
BlockEntity te = world.getBlockEntity(connectedBlockPos);
BlockEntity te = consumer.getConnectedEntity();
// @todo report error somewhere?
if (!isEnergyTE(te, settings.getFacing()) || checkRedstone(world, settings, connectorPos) || !context.matchColor(settings.getColorsMask())) {
if (!isEnergyTE(te, settings.getFacing()) || checkRedstone(world, settings, consumer.connectorPos()) || !context.matchColor(settings.getColorsMask())) {
continue;
}

Expand Down Expand Up @@ -232,24 +219,28 @@ private void updateCache(int channel, IControllerContext context) {
Level world = context.getControllerWorld();
for (var entry : connectors.entrySet()) {
EnergyConnectorSettings con = (EnergyConnectorSettings) entry.getValue();
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().consumerId());
Integer rate = getRateOrMax(con, connectorPos, world);
EnergyConnectedEntity connectedBlock = getConnectedBlockInfo(context, entry, world, con);
if (connectedBlock == null) {
continue;
}
if (con.getEnergyMode() == InsExtMode.EXT) {
energyExtractors.add(new EnergyConnectedBlock(entry.getKey(), con, connectorPos, rate));
energyExtractors.add(connectedBlock);
} else {
energyConsumers.add(new EnergyConnectedBlock(entry.getKey(), con, connectorPos, rate));
maxConsume += rate;
energyConsumers.add(connectedBlock);
maxConsume += connectedBlock.rate();
}
}

connectors = context.getRoutedConnectors(channel);
for (var entry : connectors.entrySet()) {
EnergyConnectorSettings con = (EnergyConnectorSettings) entry.getValue();
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().consumerId());
Integer rate = getRateOrMax(con, connectorPos, world);
EnergyConnectedEntity connectedBlock = getConnectedBlockInfo(context, entry, world, con);
if (connectedBlock == null) {
continue;
}
if (con.getEnergyMode() == InsExtMode.INS) {
energyConsumers.add(new EnergyConnectedBlock(entry.getKey(), con, connectorPos, rate));
maxConsume += rate;
energyConsumers.add(connectedBlock);
maxConsume += connectedBlock.rate();
}
}

Expand All @@ -258,7 +249,30 @@ private void updateCache(int channel, IControllerContext context) {
}
}

private static Integer getRateOrMax(EnergyConnectorSettings con, BlockPos connectorPos, Level world) {
@Nullable
private EnergyConnectedEntity getConnectedBlockInfo(
IControllerContext context, Map.Entry<SidedConsumer, IConnectorSettings> entry, @Nonnull Level world, @Nonnull EnergyConnectorSettings con
) {
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().consumerId());
if (connectorPos == null) {
return null;
}
ConnectorTileEntity connectorTileEntity = (ConnectorTileEntity) world.getBlockEntity(connectorPos);
if (connectorTileEntity == null) {
return null;
}

BlockPos connectedBlockPos = connectorPos.relative(entry.getKey().side());
BlockEntity connectedEntity = world.getBlockEntity(connectedBlockPos);
if (connectedEntity == null) {
return null;
}
Integer rate = getRateOrMax(con, connectorPos, world);
return new EnergyConnectedEntity(entry.getKey(), con, connectorPos, connectedBlockPos, connectedEntity, connectorTileEntity, rate);
}

private static Integer getRateOrMax(@Nonnull EnergyConnectorSettings con, @Nonnull BlockPos connectorPos,
@Nonnull Level world) {
Integer rate = con.getRate();
if (rate == null) {
boolean advanced = ConnectorBlock.isAdvancedConnector(world, connectorPos);
Expand Down

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/mcjty/xnet/apiimpl/energy/EnergyConnectedEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mcjty.xnet.apiimpl.energy;

import mcjty.rftoolsbase.api.xnet.keys.SidedConsumer;
import mcjty.xnet.apiimpl.logic.ConnectedEntity;
import mcjty.xnet.modules.cables.blocks.ConnectorTileEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;

import javax.annotation.Nonnull;

public final class EnergyConnectedEntity extends ConnectedEntity<EnergyConnectorSettings> {
private final int rate;

public EnergyConnectedEntity(@Nonnull SidedConsumer sidedConsumer, @Nonnull EnergyConnectorSettings settings,
@Nonnull BlockPos connectorPos, @Nonnull BlockPos blockPos, @Nonnull BlockEntity connectedEntity,
@Nonnull ConnectorTileEntity connectorEntity, int rate) {
super(sidedConsumer, settings, connectorPos, blockPos, connectedEntity, connectorEntity);
this.rate = rate;
}

public int rate() {return rate;}
}
Loading

0 comments on commit ca7dee6

Please sign in to comment.