Skip to content

Commit

Permalink
refactor: class hierarchy of network node containers
Browse files Browse the repository at this point in the history
Introduced NetworkNodeContainerBlockEntityImpl, that inherits from the API block entity. It only has
 code for activeness checking.

Modified AbstractInternalNetworkNodeContainerBlockEntity to have redstone mode, placed by and
 configuration card code.

Modified cable and network receiver to inherit from NetworkNodeContainerBlockEntityImpl
since they don't need redstone mode
and especially not configuration card support.
  • Loading branch information
raoulvdberge committed Oct 31, 2023
1 parent 07e5445 commit ec2ad06
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface Grid {

Storage<ItemResource> getItemStorage();

boolean isActive();
boolean isGridActive();

<T> List<TrackedResourceAmount<T>> getResources(StorageChannelType<T> type, Class<? extends Actor> actorType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.refinedmods.refinedstorage2.api.network.impl.component.EnergyNetworkComponentImpl;
import com.refinedmods.refinedstorage2.api.network.impl.component.GraphNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.impl.component.StorageNetworkComponentImpl;
import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.PlatformApiProxy;
import com.refinedmods.refinedstorage2.platform.common.block.ControllerType;
Expand All @@ -16,7 +17,7 @@
import com.refinedmods.refinedstorage2.platform.common.block.StorageMonitorBlock;
import com.refinedmods.refinedstorage2.platform.common.block.entity.ControllerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.ImporterBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.SimpleNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.NetworkNodeContainerBlockEntityImpl;
import com.refinedmods.refinedstorage2.platform.common.block.entity.constructor.ConstructorBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.constructor.ItemDropConstructorStrategyFactory;
import com.refinedmods.refinedstorage2.platform.common.block.entity.constructor.PlaceBlockConstructorStrategy;
Expand Down Expand Up @@ -479,11 +480,11 @@ protected final void registerBlockEntities(
) {
BlockEntities.INSTANCE.setCable(callback.register(
CABLE,
() -> typeFactory.create((pos, state) -> new SimpleNetworkNodeContainerBlockEntity(
() -> typeFactory.create((pos, state) -> new NetworkNodeContainerBlockEntityImpl<>(
BlockEntities.INSTANCE.getCable(),
pos,
state,
Platform.INSTANCE.getConfig().getCable().getEnergyUsage()
new SimpleNetworkNode(Platform.INSTANCE.getConfig().getCable().getEnergyUsage())
), Blocks.INSTANCE.getCable().toArray())
));
BlockEntities.INSTANCE.setController(callback.register(
Expand Down Expand Up @@ -573,11 +574,11 @@ protected final void registerBlockEntities(
));
BlockEntities.INSTANCE.setNetworkReceiver(callback.register(
NETWORK_RECEIVER,
() -> typeFactory.create((pos, state) -> new SimpleNetworkNodeContainerBlockEntity(
() -> typeFactory.create((pos, state) -> new NetworkNodeContainerBlockEntityImpl<>(
BlockEntities.INSTANCE.getNetworkReceiver(),
pos,
state,
Platform.INSTANCE.getConfig().getNetworkReceiver().getEnergyUsage()
new SimpleNetworkNode(Platform.INSTANCE.getConfig().getNetworkReceiver().getEnergyUsage())
), Blocks.INSTANCE.getNetworkReceiver().toArray())
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.block;

import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.block.entity.SimpleNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.NetworkNodeContainerBlockEntityImpl;
import com.refinedmods.refinedstorage2.platform.common.block.ticker.AbstractBlockEntityTicker;
import com.refinedmods.refinedstorage2.platform.common.block.ticker.NetworkNodeBlockEntityTicker;
import com.refinedmods.refinedstorage2.platform.common.content.BlockColorMap;
Expand Down Expand Up @@ -38,7 +39,7 @@

public class CableBlock extends AbstractBaseBlock
implements ColorableBlock<CableBlock>, SimpleWaterloggedBlock, EntityBlock, BlockItemProvider {
private static final AbstractBlockEntityTicker<SimpleNetworkNodeContainerBlockEntity> TICKER =
private static final AbstractBlockEntityTicker<NetworkNodeContainerBlockEntityImpl<SimpleNetworkNode>> TICKER =
new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getCable);

private final DyeColor color;
Expand Down Expand Up @@ -116,22 +117,14 @@ public VoxelShape getShape(final BlockState state,

@Override
public BlockEntity newBlockEntity(final BlockPos pos, final BlockState state) {
return new SimpleNetworkNodeContainerBlockEntity(
return new NetworkNodeContainerBlockEntityImpl<>(
BlockEntities.INSTANCE.getCable(),
pos,
state,
Platform.INSTANCE.getConfig().getCable().getEnergyUsage()
new SimpleNetworkNode(Platform.INSTANCE.getConfig().getCable().getEnergyUsage())
);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(final Level level,
final BlockState blockState,
final BlockEntityType<T> type) {
return TICKER.get(level, type);
}

@Override
public BlockColorMap<CableBlock> getBlockColorMap() {
return Blocks.INSTANCE.getCable();
Expand All @@ -146,4 +139,12 @@ public MutableComponent getName() {
public BlockItem createBlockItem() {
return new NamedBlockItem(this, new Item.Properties(), name);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(final Level level,
final BlockState blockState,
final BlockEntityType<T> type) {
return TICKER.get(level, type);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.block;

import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.block.entity.SimpleNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.NetworkNodeContainerBlockEntityImpl;
import com.refinedmods.refinedstorage2.platform.common.block.ticker.AbstractBlockEntityTicker;
import com.refinedmods.refinedstorage2.platform.common.block.ticker.NetworkNodeBlockEntityTicker;
import com.refinedmods.refinedstorage2.platform.common.content.BlockColorMap;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class NetworkReceiverBlock extends AbstractBaseBlock implements Colorable
BlockItemProvider, EntityBlock {
public static final BooleanProperty ACTIVE = BooleanProperty.create("active");

private static final AbstractBlockEntityTicker<SimpleNetworkNodeContainerBlockEntity> TICKER =
private static final AbstractBlockEntityTicker<NetworkNodeContainerBlockEntityImpl<SimpleNetworkNode>> TICKER =
new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getNetworkReceiver, ACTIVE);

private final DyeColor color;
Expand Down Expand Up @@ -76,11 +77,11 @@ public BlockItem createBlockItem() {
@Nullable
@Override
public BlockEntity newBlockEntity(final BlockPos pos, final BlockState state) {
return new SimpleNetworkNodeContainerBlockEntity(
return new NetworkNodeContainerBlockEntityImpl<>(
BlockEntities.INSTANCE.getNetworkReceiver(),
pos,
state,
Platform.INSTANCE.getConfig().getNetworkReceiver().getEnergyUsage()
new SimpleNetworkNode(Platform.INSTANCE.getConfig().getNetworkReceiver().getEnergyUsage())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.slf4j.LoggerFactory;

public abstract class AbstractLevelInteractingNetworkNodeContainerBlockEntity<T extends AbstractNetworkNode>
extends AbstractInternalNetworkNodeContainerBlockEntity<T> {
extends AbstractRedstoneModeNetworkNodeContainerBlockEntity<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(
AbstractLevelInteractingNetworkNodeContainerBlockEntity.class
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.refinedmods.refinedstorage2.platform.common.block.entity;

import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.blockentity.ConfigurationCardTarget;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.util.RedstoneMode;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public abstract class AbstractRedstoneModeNetworkNodeContainerBlockEntity<T extends AbstractNetworkNode>
extends NetworkNodeContainerBlockEntityImpl<T> implements PlayerAware, ConfigurationCardTarget {
private static final String TAG_REDSTONE_MODE = "rm";
private static final String TAG_PLACED_BY_PLAYER_ID = "pbpid";

private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
@Nullable
private UUID placedByPlayerId;

protected AbstractRedstoneModeNetworkNodeContainerBlockEntity(final BlockEntityType<?> type,
final BlockPos pos,
final BlockState state,
final T node) {
super(type, pos, state, node);
}

@Override
protected boolean isActive() {
return super.isActive() && level != null && redstoneMode.isActive(level.hasNeighborSignal(worldPosition));
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
writeConfiguration(tag);
if (placedByPlayerId != null) {
tag.putUUID(TAG_PLACED_BY_PLAYER_ID, placedByPlayerId);
}
}

@Override
public void writeConfiguration(final CompoundTag tag) {
tag.putInt(TAG_REDSTONE_MODE, RedstoneModeSettings.getRedstoneMode(getRedstoneMode()));
}

@Override
public void load(final CompoundTag tag) {
super.load(tag);
readConfiguration(tag);
if (tag.hasUUID(TAG_PLACED_BY_PLAYER_ID)) {
placedByPlayerId = tag.getUUID(TAG_PLACED_BY_PLAYER_ID);
}
}

@Override
public void readConfiguration(final CompoundTag tag) {
if (tag.contains(TAG_REDSTONE_MODE)) {
redstoneMode = RedstoneModeSettings.getRedstoneMode(tag.getInt(TAG_REDSTONE_MODE));
}
}

@Override
public List<Item> getUpgradeItems() {
return Collections.emptyList();
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return false;
}

public RedstoneMode getRedstoneMode() {
return redstoneMode;
}

public void setRedstoneMode(final RedstoneMode redstoneMode) {
this.redstoneMode = redstoneMode;
setChanged();
}

@Override
public void setPlacedBy(final UUID playerId) {
this.placedByPlayerId = playerId;
setChanged();
}

protected final Player getFakePlayer(final ServerLevel serverLevel) {
return Platform.INSTANCE.getFakePlayer(serverLevel, placedByPlayerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ControllerBlockEntity extends AbstractInternalNetworkNodeContainerBlockEntity<ControllerNetworkNode>
public class ControllerBlockEntity extends AbstractRedstoneModeNetworkNodeContainerBlockEntity<ControllerNetworkNode>
implements ExtendedMenuProvider, EnergyBlockEntity {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBlockEntity.class);

Expand Down
Loading

0 comments on commit ec2ad06

Please sign in to comment.