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 456a3cf
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 212 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- The Wrench now dismantles devices when crouching.
- The Disk Drive in item form now supports rendering of disks that were dismantled.
- In order to retain Controller energy, the Controller must now be dismantled.
- All config and upgrades are transferred to the item.
- The Disk Drive in item form now supports rendering of disks that were dismantled.
- In order to retain Controller energy, the Controller must now be dismantled.
- All config and upgrades are transferred to the item.
- You can now use any Wrench from other mods in order to rotate or dismantle.
- Item and fluid storage blocks.
- Initial advancements.
Expand Down
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
@@ -0,0 +1,36 @@
package com.refinedmods.refinedstorage2.platform.common.block;

import com.refinedmods.refinedstorage2.platform.common.item.block.NamedBlockItem;

import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

public abstract class AbstractColoredBlock<T extends Block & BlockItemProvider>
extends AbstractBaseBlock implements ColorableBlock<T>, BlockItemProvider {
private final DyeColor color;
private final MutableComponent name;

protected AbstractColoredBlock(final Properties properties, final DyeColor color, final MutableComponent name) {
super(properties);
this.color = color;
this.name = name;
}

@Override
public MutableComponent getName() {
return name;
}

@Override
public DyeColor getColor() {
return color;
}

@Override
public BlockItem createBlockItem() {
return new NamedBlockItem(this, new Item.Properties(), name);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
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;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.Blocks;
import com.refinedmods.refinedstorage2.platform.common.item.block.NamedBlockItem;

import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand All @@ -36,23 +34,13 @@
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class CableBlock extends AbstractBaseBlock
public class CableBlock extends AbstractColoredBlock<CableBlock>
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;
private final MutableComponent name;

public CableBlock(final DyeColor color, final MutableComponent name) {
super(BlockConstants.CABLE_PROPERTIES);
this.color = color;
this.name = name;
}

@Override
public DyeColor getColor() {
return color;
super(BlockConstants.CABLE_PROPERTIES, color, name);
}

@Override
Expand Down Expand Up @@ -116,34 +104,24 @@ 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();
}

@Nullable
@Override
public MutableComponent getName() {
return name;
}

@Override
public BlockItem createBlockItem() {
return new NamedBlockItem(this, new Item.Properties(), name);
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,21 +1,19 @@
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;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.Blocks;
import com.refinedmods.refinedstorage2.platform.common.item.block.NamedBlockItem;

import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
Expand All @@ -26,20 +24,14 @@
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class NetworkReceiverBlock extends AbstractBaseBlock implements ColorableBlock<NetworkReceiverBlock>,
BlockItemProvider, EntityBlock {
public class NetworkReceiverBlock extends AbstractColoredBlock<NetworkReceiverBlock> implements 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;
private final MutableComponent name;

public NetworkReceiverBlock(final DyeColor color, final MutableComponent name) {
super(BlockConstants.PROPERTIES);
this.color = color;
this.name = name;
super(BlockConstants.PROPERTIES, color, name);
}

@Override
Expand All @@ -53,34 +45,19 @@ protected void createBlockStateDefinition(final StateDefinition.Builder<Block, B
builder.add(ACTIVE);
}

@Override
public MutableComponent getName() {
return name;
}

@Override
public BlockColorMap<NetworkReceiverBlock> getBlockColorMap() {
return Blocks.INSTANCE.getNetworkReceiver();
}

@Override
public DyeColor getColor() {
return color;
}

@Override
public BlockItem createBlockItem() {
return new NamedBlockItem(this, new Item.Properties(), name);
}

@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);
}
}
Loading

0 comments on commit 456a3cf

Please sign in to comment.