Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Importer GUI and respect filter. #195

Merged
merged 3 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import java.util.Objects;
import java.util.Set;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;

// TODO: Write a gametest.
// TODO: gui config etc.
public class ImporterNetworkNode extends AbstractNetworkNode {
private final long energyUsage;
private final Filter filter = new Filter();
Expand Down Expand Up @@ -55,6 +55,10 @@ public void setFilterMode(final FilterMode mode) {
filter.setMode(mode);
}

public void setNormalizer(final UnaryOperator<Object> normalizer) {
filter.setNormalizer(normalizer);
}

public void setFilterTemplates(final Set<Object> templates) {
filter.setTemplates(templates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public boolean transfer(final Filter filter, final Actor actor, final Network ne
final StorageChannel<T> storageChannel = network
.getComponent(StorageNetworkComponent.class)
.getStorageChannel(storageChannelType);
return transfer(actor, storageChannel);
return transfer(filter, actor, storageChannel);
}

private boolean transfer(final Actor actor, final StorageChannel<T> storageChannel) {
private boolean transfer(final Filter filter, final Actor actor, final StorageChannel<T> storageChannel) {
long totalTransferred = 0;
T workingResource = null;
final Iterator<T> iterator = source.getResources();
while (iterator.hasNext() && totalTransferred < transferQuota) {
final T resource = iterator.next();
if (workingResource != null) {
totalTransferred += performTransfer(storageChannel, actor, totalTransferred, workingResource, resource);
} else {
} else if (filter.isAllowed(resource)) {
final long transferred = performTransfer(storageChannel, actor, totalTransferred, resource);
if (transferred > 0) {
workingResource = resource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage2.api.network.node.importer;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.core.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.network.test.NetworkTestFixtures;
import com.refinedmods.refinedstorage2.api.network.test.extension.AddImporter;
import com.refinedmods.refinedstorage2.api.network.test.extension.InjectNetworkStorageChannel;
Expand All @@ -13,6 +14,8 @@
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl;

import java.util.Set;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -28,6 +31,7 @@ class ImporterNetworkNodeTest {
void testInitialState() {
// Assert
assertThat(sut.getEnergyUsage()).isEqualTo(5);
assertThat(sut.getFilterMode()).isEqualTo(FilterMode.BLOCK);
}

@Test
Expand Down Expand Up @@ -273,4 +277,232 @@ void testCoolDown(@InjectNetworkStorageChannel final StorageChannel<String> stor
new ResourceAmount<>("B", 5)
);
}

@Test
void shouldRespectAllowlist(@InjectNetworkStorageChannel final StorageChannel<String> storageChannel) {
// Arrange
sut.setFilterMode(FilterMode.ALLOW);
sut.setFilterTemplates(Set.of("A"));

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("B", "A")
.add("B", 10)
.add("A", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 1)
);
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("B", 10),
new ResourceAmount<>("A", 9)
);
}

@Test
void shouldRespectAllowlistWithNormalizer(
@InjectNetworkStorageChannel final StorageChannel<String> storageChannel
) {
// Arrange
sut.setFilterMode(FilterMode.ALLOW);
sut.setFilterTemplates(Set.of("A"));
sut.setNormalizer(value -> value instanceof String str && str.startsWith("A") ? "A" : value);

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("B", "A1", "A2")
.add("B", 10)
.add("A1", 1)
.add("A2", 1);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
10
);
sut.setTransferStrategy(strategy);

// Act
sut.update();
// cooldown
sut.update();
sut.update();
sut.update();
// Act 2
sut.update();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("A1", 1),
new ResourceAmount<>("A2", 1)
);
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("B", 10)
);
}

@Test
void shouldRespectAllowlistWithoutAlternative(
@InjectNetworkStorageChannel final StorageChannel<String> storageChannel
) {
// Arrange
sut.setFilterMode(FilterMode.ALLOW);
sut.setFilterTemplates(Set.of("A"));

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("B")
.add("B", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).isEmpty();
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("B", 10)
);
}

@Test
void shouldRespectEmptyAllowlist(@InjectNetworkStorageChannel final StorageChannel<String> storageChannel) {
// Arrange
sut.setFilterMode(FilterMode.ALLOW);
sut.setFilterTemplates(Set.of());

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("B", "A")
.add("B", 10)
.add("A", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).isEmpty();
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("B", 10),
new ResourceAmount<>("A", 10)
);
}

@Test
void shouldRespectBlocklist(@InjectNetworkStorageChannel final StorageChannel<String> storageChannel) {
// Arrange
sut.setFilterMode(FilterMode.BLOCK);
sut.setFilterTemplates(Set.of("A"));

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("A", "B")
.add("A", 10)
.add("B", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("B", 1)
);
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("A", 10),
new ResourceAmount<>("B", 9)
);
}

@Test
void shouldRespectBlocklistWithoutAlternative(
@InjectNetworkStorageChannel final StorageChannel<String> storageChannel
) {
// Arrange
sut.setFilterMode(FilterMode.BLOCK);
sut.setFilterTemplates(Set.of("A"));

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("A")
.add("A", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).isEmpty();
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 10)
);
}

@Test
void shouldRespectEmptyBlocklist(@InjectNetworkStorageChannel final StorageChannel<String> storageChannel) {
// Arrange
sut.setFilterMode(FilterMode.BLOCK);
sut.setFilterTemplates(Set.of());

storageChannel.addSource(new InMemoryStorageImpl<>());

final FakeImporterSource source = new FakeImporterSource("A", "B")
.add("A", 10)
.add("B", 10);

final ImporterTransferStrategy strategy = new ImporterTransferStrategyImpl<>(
source,
NetworkTestFixtures.STORAGE_CHANNEL_TYPE,
1
);
sut.setTransferStrategy(strategy);

// Act
sut.update();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 1)
);
assertThat(source.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount<>("A", 9),
new ResourceAmount<>("B", 10)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.Sounds;

import java.util.Optional;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -23,7 +24,10 @@
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;

public abstract class AbstractBaseBlock extends Block {
protected AbstractBaseBlock(final Properties properties) {
Expand All @@ -49,14 +53,31 @@ public InteractionResult use(final BlockState state,
final Player player,
final InteractionHand hand,
final BlockHitResult hit) {
return tryOpenScreen(state, level, pos, player)
return tryOpenScreen(state, level, pos, player, hit.getLocation())
.orElseGet(() -> super.use(state, level, pos, player, hand, hit));
}

@Nullable
protected VoxelShape getScreenOpenableShape(final BlockState state) {
return null;
}

private Optional<InteractionResult> tryOpenScreen(final BlockState state,
final Level level,
final BlockPos pos,
final Player player) {
final Player player,
final Vec3 hit) {
final VoxelShape screenOpenableShape = getScreenOpenableShape(state);
if (screenOpenableShape != null) {
final AABB aabb = screenOpenableShape.bounds().move(pos);
final boolean inBoundsX = hit.x >= aabb.minX && hit.x <= aabb.maxX;
final boolean inBoundsY = hit.y >= aabb.minY && hit.y <= aabb.maxY;
final boolean inBoundsZ = hit.z >= aabb.minZ && hit.z <= aabb.maxZ;
final boolean inBounds = inBoundsX && inBoundsY && inBoundsZ;
if (!inBounds) {
return Optional.empty();
}
}
final MenuProvider menuProvider = state.getMenuProvider(level, pos);
if (menuProvider != null) {
if (player instanceof ServerPlayer serverPlayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public VoxelShape getShape(final BlockState state,
return Shapes.or(super.getShape(state, world, pos, ctx), getLineShape(state));
}

@Override
@Nullable
protected VoxelShape getScreenOpenableShape(final BlockState state) {
return getLineShape(state);
}

private VoxelShape getLineShape(final BlockState state) {
final Direction direction = getDirection(state);
if (direction == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

// TODO: this should just block everything outgoing if directional, should also work when wrenching.
public abstract class AbstractInternalNetworkNodeContainerBlockEntity<T extends AbstractNetworkNode>
extends AbstractNetworkNodeContainerBlockEntity<T> {
private static final Logger LOGGER = LogManager.getLogger();
Expand Down
Loading