Skip to content

Commit

Permalink
refactor: split up configuration read/writes from unique read/writes
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Oct 31, 2023
1 parent 36e1ac0 commit 8728857
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 208 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.refinedmods.refinedstorage2.platform.api.blockentity;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntityType;

public interface ConfigurationCardTarget {
BlockEntityType<?> getBlockEntityType();

void writeConfiguration(CompoundTag tag);

void readConfiguration(CompoundTag tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.refinedmods.refinedstorage2.api.network.component.EnergyNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.blockentity.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.api.blockentity.ConfigurationCardTarget;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.block.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.block.ColorableBlock;
Expand All @@ -25,7 +26,7 @@

public abstract class AbstractInternalNetworkNodeContainerBlockEntity<T extends AbstractNetworkNode>
extends AbstractNetworkNodeContainerBlockEntity<T>
implements PlayerAware {
implements PlayerAware, ConfigurationCardTarget {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInternalNetworkNodeContainerBlockEntity.class);

private static final String TAG_REDSTONE_MODE = "rm";
Expand Down Expand Up @@ -56,23 +57,38 @@ && getNode().getNetwork() != null
@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.putInt(TAG_REDSTONE_MODE, RedstoneModeSettings.getRedstoneMode(getRedstoneMode()));
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);
if (tag.contains(TAG_REDSTONE_MODE)) {
redstoneMode = RedstoneModeSettings.getRedstoneMode(tag.getInt(TAG_REDSTONE_MODE));
}
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 BlockEntityType<?> getBlockEntityType() {
return getType();
}

public void updateActiveness(final BlockState state,
@Nullable final BooleanProperty activenessProperty) {
final boolean newActive = isActive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public abstract class AbstractLevelInteractingNetworkNodeContainerBlockEntity<T extends AbstractNetworkNode>
extends AbstractInternalNetworkNodeContainerBlockEntity<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(
AbstractLevelInteractingNetworkNodeContainerBlockEntity.class
AbstractLevelInteractingNetworkNodeContainerBlockEntity.class
);

protected AbstractLevelInteractingNetworkNodeContainerBlockEntity(
Expand All @@ -28,7 +28,6 @@ protected AbstractLevelInteractingNetworkNodeContainerBlockEntity(

// used to handle rotations
@Override
@SuppressWarnings("deprecation")
public void setBlockState(final BlockState newBlockState) {
super.setBlockState(newBlockState);
if (!(level instanceof ServerLevel serverLevel)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ protected AbstractSchedulingNetworkNodeContainerBlockEntity(
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
schedulingMode.writeToTag(tag);
filter.save(tag);
}

@Override
public void load(final CompoundTag tag) {
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
schedulingMode.load(tag);
filter.load(tag);
super.load(tag);
}

public void setSchedulingModeType(final SchedulingModeType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ private CompositeImporterTransferStrategy createStrategy(final ServerLevel serve
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(getNode().getFilterMode()));
filter.save(tag);
}

@Override
public void load(final CompoundTag tag) {
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
if (tag.contains(TAG_FILTER_MODE)) {
getNode().setFilterMode(FilterModeSettings.getFilterMode(tag.getInt(TAG_FILTER_MODE)));
}

filter.load(tag);

super.load(tag);
}

public boolean isFuzzyMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public StorageConfigurationContainerImpl(final StorageConfiguration config,
final FilterWithFuzzyMode filter,
final Runnable listener,
final Supplier<RedstoneMode> redstoneModeSupplier,
final Consumer<RedstoneMode> redstoneModeConsumer
) {
final Consumer<RedstoneMode> redstoneModeConsumer) {
this.config = config;
this.filter = filter;
this.listener = listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ public void postDoWork() {
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
tag.putBoolean(TAG_DROP_ITEMS, dropItems);
}

@Override
public void load(final CompoundTag tag) {
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
if (tag.contains(TAG_DROP_ITEMS)) {
dropItems = tag.getBoolean(TAG_DROP_ITEMS);
}
super.load(tag);
}

public boolean isDropItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ public void setFilterMode(final FilterMode mode) {
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(filter.getMode()));
tag.putBoolean(TAG_PICKUP_ITEMS, pickupItems);
filterWithFuzzyMode.save(tag);
}

@Override
public void load(final CompoundTag tag) {
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
filterWithFuzzyMode.load(tag);
if (tag.contains(TAG_FILTER_MODE)) {
filter.setMode(FilterModeSettings.getFilterMode(tag.getInt(TAG_FILTER_MODE)));
}
if (tag.contains(TAG_PICKUP_ITEMS)) {
pickupItems = tag.getBoolean(TAG_PICKUP_ITEMS);
}
super.load(tag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,31 @@ public DetectorBlockEntity(final BlockPos pos, final BlockState state) {
initialize();
}


@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
filter.save(tag);
tag.putDouble(TAG_AMOUNT, amount);
tag.putInt(TAG_MODE, DetectorModeSettings.getDetectorMode(getNode().getMode()));
}

@Override
public void load(final CompoundTag tag) {
super.load(tag);
initialize();
propagateAmount();
}

@Override
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
filter.load(tag);
if (tag.contains(TAG_AMOUNT)) {
this.amount = tag.getDouble(TAG_AMOUNT);
}
if (tag.contains(TAG_MODE)) {
getNode().setMode(DetectorModeSettings.getDetectorMode(tag.getInt(TAG_MODE)));
}
initialize();
propagateAmount();
super.load(tag);
}

public void setAmount(final double amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,28 @@ public void activenessChanged(final BlockState state,
@Override
public void load(final CompoundTag tag) {
fromClientTag(tag);

if (tag.contains(TAG_DISK_INVENTORY)) {
ContainerUtil.read(tag.getCompound(TAG_DISK_INVENTORY), diskInventory);
}
super.load(tag);
}

@Override
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
configContainer.load(tag);
filter.load(tag);

super.load(tag);
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
tag.put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory));
}

@Override
public void writeConfiguration(final CompoundTag tag) {
super.writeConfiguration(tag);
configContainer.save(tag);
filter.save(tag);
}
Expand Down
Loading

0 comments on commit 8728857

Please sign in to comment.