Skip to content

Commit

Permalink
Merge pull request #441 from refinedmods/feature/GH-204/config-card
Browse files Browse the repository at this point in the history
Configuration card
  • Loading branch information
raoulvdberge authored Oct 31, 2023
2 parents 83b5155 + 8330c0d commit 10d6e82
Show file tree
Hide file tree
Showing 32 changed files with 653 additions and 221 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Curios integration on Forge.
- Trinkets integration on Fabric.
- Storage Monitor
- Configuration Card. It copies device configurations and can transfer upgrades.

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.refinedmods.refinedstorage2.platform.api.blockentity;

import java.util.List;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.2")
public interface ConfigurationCardTarget {
void writeConfiguration(CompoundTag tag);

void readConfiguration(CompoundTag tag);

List<Item> getUpgradeItems();

boolean addUpgradeItem(Item upgradeItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.type.StorageTypes;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.common.item.ConfigurationCardItem;
import com.refinedmods.refinedstorage2.platform.common.item.FluidStorageDiskItem;
import com.refinedmods.refinedstorage2.platform.common.item.FortuneUpgradeItem;
import com.refinedmods.refinedstorage2.platform.common.item.ItemStorageDiskItem;
Expand Down Expand Up @@ -321,6 +322,10 @@ private void registerSimpleItems(final RegistryCallback<Item> callback) {
for (final ProcessorItem.Type type : ProcessorItem.Type.values()) {
registerProcessor(callback, type);
}
Items.INSTANCE.setConfigurationCard(callback.register(
ContentIds.CONFIGURATION_CARD,
ConfigurationCardItem::new
));
}

private void registerProcessor(final RegistryCallback<Item> callback, final ProcessorItem.Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
Expand All @@ -29,7 +30,7 @@ public class StorageMonitorBlock extends AbstractDirectionalBlock<BiDirection> i
new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getStorageMonitor);

public StorageMonitorBlock() {
super(BlockConstants.PROPERTIES.strength(1.5F, 6.0F));
super(BlockConstants.PROPERTIES.strength(2.5F, 6.0F));
}

@Override
Expand Down Expand Up @@ -60,6 +61,10 @@ public InteractionResult use(final BlockState state,
if (player.isCrouching()) {
return super.use(state, level, pos, player, hand, hit);
}
final BiDirection direction = getDirection(state);
if (direction == null || hit.getDirection() != direction.asDirection()) {
return InteractionResult.FAIL;
}
if (!level.isClientSide()) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof StorageMonitorBlockEntity storageMonitor) {
Expand All @@ -83,19 +88,23 @@ public void attack(final BlockState state, final Level level, final BlockPos pos
if (direction == null) {
return;
}
final Direction hitDirection = getHitDirection(level, player);
if (hitDirection != direction.asDirection()) {
return;
}
storageMonitor.extract(player);
}

private Direction getHitDirection(final Level level, final Player player) {
final Vec3 base = player.getEyePosition(1.0F);
final Vec3 look = player.getLookAngle();
final Vec3 target = base.add(look.x * 20, look.y * 20, look.z * 20);
final BlockHitResult hitResult = level.clip(new ClipContext(
return level.clip(new ClipContext(
base,
target,
ClipContext.Block.OUTLINE,
ClipContext.Fluid.NONE,
player
));
if (hitResult.getDirection() != direction.asDirection()) {
return;
}
storageMonitor.extract(player);
)).getDirection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
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;
import com.refinedmods.refinedstorage2.platform.common.util.RedstoneMode;

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

Expand All @@ -17,6 +20,7 @@
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;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
Expand All @@ -25,7 +29,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 +60,43 @@ && 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 List<Item> getUpgradeItems() {
return Collections.emptyList();
}

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

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 @@ -5,12 +5,16 @@
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;

import java.util.ArrayList;
import java.util.List;

import com.google.common.util.concurrent.RateLimiter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -63,6 +67,24 @@ private void upgradeContainerChanged() {
}
}

@Override
public List<Item> getUpgradeItems() {
final List<Item> upgradeItems = new ArrayList<>();
for (int i = 0; i < upgradeContainer.getContainerSize(); ++i) {
final ItemStack itemStack = upgradeContainer.getItem(i);
if (itemStack.isEmpty()) {
continue;
}
upgradeItems.add(itemStack.getItem());
}
return upgradeItems;
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addItem(new ItemStack(upgradeItem)).isEmpty();
}

@Override
public void saveAdditional(final CompoundTag tag) {
super.saveAdditional(tag);
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,17 +64,17 @@ 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) {
public void readConfiguration(final CompoundTag tag) {
super.readConfiguration(tag);
filter.load(tag);
if (tag.contains(TAG_AMOUNT)) {
this.amount = tag.getDouble(TAG_AMOUNT);
Expand All @@ -84,7 +84,6 @@ public void load(final CompoundTag tag) {
}
initialize();
propagateAmount();
super.load(tag);
}

public void setAmount(final double amount) {
Expand Down
Loading

0 comments on commit 10d6e82

Please sign in to comment.