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

Wireless Transmitter and Range Upgrade #427

Merged
merged 12 commits into from
Aug 27, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Wireless Grid
- Creative Wireless Grid
- Wireless Transmitter
- Range Upgrade
- Creative Range Upgrade
- Fully charged Controller variants to the creative mode tab.

### Changed

Expand All @@ -22,6 +26,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Fixed inactive Grid slots still rendering resources.
- Fixed being able to interact with inactive Grid.
- Fixed nearly on/off Controller model not being rendered correctly on Forge.
- Fixed Controller energy tooltip not working.

## [2.0.0-milestone.2.14] - 2023-08-19

Expand Down
4 changes: 2 additions & 2 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<suppressions>
<!-- Test directories don't need a package-info.java file -->
<suppress checks="JavadocPackage" files="test[\\/].*.java"/>
<!-- Mod initializer can be longer due to initialization logic -->
<suppress checks="FileLength" files="ModInitializer.*.java"/>
<!-- Mod initializer can be longer due to initialization logic, config can be long too -->
<suppress checks="FileLength" files="(ModInitializer.*\.java|ConfigImpl\.java)"/>
<!-- Shadow target contains underscore -->
<suppress checks="MemberName" files="ModelBakerImplMixin.java"/>
<suppress checks="HideUtilityClassConstructor" files="GridClearPacket.java"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
Expand All @@ -16,6 +18,7 @@ public class GraphNetworkComponent implements NetworkComponent {

private final Network network;
private final Set<NetworkNodeContainer> containers = new HashSet<>();
private final Map<Class<?>, Set<NetworkNodeContainer>> containerIndex = new HashMap<>();

public GraphNetworkComponent(final Network network) {
this.network = network;
Expand All @@ -25,16 +28,53 @@ public Set<NetworkNodeContainer> getContainers() {
return Collections.unmodifiableSet(containers);
}

@SuppressWarnings("unchecked")
public <T> Set<T> getContainers(final Class<T> clazz) {
return (Set<T>) Collections.unmodifiableSet(containerIndex.getOrDefault(clazz, Collections.emptySet()));
}

@Override
public void onContainerAdded(final NetworkNodeContainer container) {
LOGGER.debug("Container {} added to network {}", container, network.hashCode());
containers.add(container);
addToIndex(container);
}

@Override
public void onContainerRemoved(final NetworkNodeContainer container) {
LOGGER.debug("Container {} removed from network {}", container, network.hashCode());
containers.remove(container);
deleteFromIndex(container);
}

private void addToIndex(final NetworkNodeContainer container) {
final Class<? extends NetworkNodeContainer> clazz = container.getClass();
addToIndex(clazz, container);
for (final Class<?> iface : clazz.getInterfaces()) {
addToIndex(iface, container);
}
}

private void addToIndex(final Class<?> indexKey, final NetworkNodeContainer container) {
containerIndex.computeIfAbsent(indexKey, k -> new HashSet<>()).add(container);
}

private void deleteFromIndex(final NetworkNodeContainer container) {
final Class<? extends NetworkNodeContainer> clazz = container.getClass();
deleteFromIndex(clazz, container);
for (final Class<?> iface : clazz.getInterfaces()) {
deleteFromIndex(iface, container);
}
}

private void deleteFromIndex(final Class<?> indexKey, final NetworkNodeContainer container) {
final Set<? extends NetworkNodeContainer> index = containerIndex.get(indexKey);
if (index != null) {
index.remove(container);
if (index.isEmpty()) {
containerIndex.remove(indexKey);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package com.refinedmods.refinedstorage2.api.network.impl.component;

import com.refinedmods.refinedstorage2.api.network.impl.NetworkImpl;
import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.network.test.NetworkTestFixtures;

import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class GraphNetworkComponentTest {
GraphNetworkComponent sut;

@BeforeEach
void setUp() {
sut = new GraphNetworkComponent(new NetworkImpl(NetworkTestFixtures.NETWORK_COMPONENT_MAP_FACTORY));
}

@Test
void shouldAddContainer() {
// Arrange
final NetworkNodeContainer container1 = () -> new SimpleNetworkNode(0);
final NetworkNodeContainer container2 = () -> new SimpleNetworkNode(0);

// Act
sut.onContainerAdded(container1);
sut.onContainerAdded(container2);

// Assert
assertThat(sut.getContainers()).containsExactlyInAnyOrder(container1, container2);
}

@Test
void shouldRemoveContainer() {
// Arrange
final NetworkNodeContainer container1 = () -> new SimpleNetworkNode(0);
final NetworkNodeContainer container2 = () -> new SimpleNetworkNode(0);
sut.onContainerAdded(container1);
sut.onContainerAdded(container2);

// Act
sut.onContainerRemoved(container1);

// Assert
assertThat(sut.getContainers()).containsExactly(container2);
}

@Test
void shouldNotRetrieveContainersByClassThatDontExist() {
// Act
final Set<NetworkNodeContainer1> containers = sut.getContainers(NetworkNodeContainer1.class);

// Assert
assertThat(containers).isEmpty();
}

@Test
void shouldAddAndRetrieveSingleContainerByClass() {
// Arrange
final NetworkNodeContainer1 container1 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container1);
sut.onContainerAdded(container2);

// Act
final Set<NetworkNodeContainer1> containers = sut.getContainers(NetworkNodeContainer1.class);

// Assert
assertThat(containers).containsExactly(container1);
}

@Test
void shouldAddAndRetrieveMultipleContainersByClass() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
final Set<NetworkNodeContainer1> containers = sut.getContainers(NetworkNodeContainer1.class);

// Assert
assertThat(containers).containsExactlyInAnyOrder(container11, container12);
}

@Test
void shouldAddAndRetrieveMultipleContainersByInterface() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
final Set<BothImplements> containers = sut.getContainers(BothImplements.class);

// Assert
assertThat(containers).containsExactlyInAnyOrder(container11, container12, container2);
}

@Test
void shouldRemoveSingleContainerAndRetrieveByClass() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
sut.onContainerRemoved(container12);

final Set<NetworkNodeContainer1> containers = sut.getContainers(NetworkNodeContainer1.class);

// Assert
assertThat(containers).containsExactly(container11);
}

@Test
void shouldRemoveSingleContainerAndRetrieveByInterface() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
sut.onContainerRemoved(container12);

final Set<BothImplements> containers = sut.getContainers(BothImplements.class);

// Assert
assertThat(containers).containsExactlyInAnyOrder(container11, container2);
}

@Test
void shouldRemoveMultipleContainersAndRetrieveByClass() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
sut.onContainerRemoved(container11);
sut.onContainerRemoved(container12);

final Set<NetworkNodeContainer1> containers1 = sut.getContainers(NetworkNodeContainer1.class);
final Set<NetworkNodeContainer2> containers2 = sut.getContainers(NetworkNodeContainer2.class);
final Set<BothImplements> containersByIface = sut.getContainers(BothImplements.class);

// Assert
assertThat(containers1).isEmpty();
assertThat(containers2).containsExactly(container2);
assertThat(containersByIface).containsExactly(container2);
}

@Test
void shouldRemoveMultipleContainersAndRetrieveByInterface() {
// Arrange
final NetworkNodeContainer1 container11 = new NetworkNodeContainer1();
final NetworkNodeContainer1 container12 = new NetworkNodeContainer1();
final NetworkNodeContainer2 container2 = new NetworkNodeContainer2();
sut.onContainerAdded(container11);
sut.onContainerAdded(container12);
sut.onContainerAdded(container2);

// Act
sut.onContainerRemoved(container11);
sut.onContainerRemoved(container12);
sut.onContainerRemoved(container2);

final Set<BothImplements> containers = sut.getContainers(BothImplements.class);

// Assert
assertThat(containers).isEmpty();
}

private static class NetworkNodeContainer1 implements NetworkNodeContainer, BothImplements {
@Override
public NetworkNode getNode() {
return new SimpleNetworkNode(0);
}
}

private static class NetworkNodeContainer2 implements NetworkNodeContainer, BothImplements {
@Override
public NetworkNode getNode() {
return new SimpleNetworkNode(0);
}
}

private interface BothImplements {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.platform.api.blockentity.constructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.blockentity.destructor.DestructorStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.blockentity.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.api.grid.GridExtractionStrategy;
import com.refinedmods.refinedstorage2.platform.api.grid.GridExtractionStrategyFactory;
Expand All @@ -29,6 +30,7 @@
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
import com.refinedmods.refinedstorage2.platform.api.upgrade.BuiltinUpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.api.upgrade.UpgradeRegistry;

import java.util.Collection;
Expand Down Expand Up @@ -76,6 +78,8 @@ public interface PlatformApi {

UpgradeRegistry getUpgradeRegistry();

BuiltinUpgradeDestinations getBuiltinUpgradeDestinations();

void requestNetworkNodeInitialization(NetworkNodeContainer container, Level level, Runnable callback);

void requestNetworkNodeRemoval(NetworkNodeContainer container, Level level);
Expand Down Expand Up @@ -127,4 +131,8 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe
void registerIngredientConverter(IngredientConverter converter);

IngredientConverter getIngredientConverter();

void addWirelessTransmitterRangeModifier(WirelessTransmitterRangeModifier rangeModifier);

WirelessTransmitterRangeModifier getWirelessTransmitterRangeModifier();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.platform.api.blockentity.constructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.blockentity.destructor.DestructorStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.blockentity.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.api.grid.GridExtractionStrategy;
import com.refinedmods.refinedstorage2.platform.api.grid.GridExtractionStrategyFactory;
Expand All @@ -29,6 +30,7 @@
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.storage.type.StorageType;
import com.refinedmods.refinedstorage2.platform.api.upgrade.BuiltinUpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.api.upgrade.UpgradeRegistry;

import java.util.Collection;
Expand Down Expand Up @@ -131,6 +133,11 @@ public UpgradeRegistry getUpgradeRegistry() {
return ensureLoaded().getUpgradeRegistry();
}

@Override
public BuiltinUpgradeDestinations getBuiltinUpgradeDestinations() {
return ensureLoaded().getBuiltinUpgradeDestinations();
}

@Override
public void requestNetworkNodeInitialization(final NetworkNodeContainer container,
final Level level,
Expand Down Expand Up @@ -254,6 +261,16 @@ public IngredientConverter getIngredientConverter() {
return ensureLoaded().getIngredientConverter();
}

@Override
public void addWirelessTransmitterRangeModifier(final WirelessTransmitterRangeModifier rangeModifier) {
ensureLoaded().addWirelessTransmitterRangeModifier(rangeModifier);
}

@Override
public WirelessTransmitterRangeModifier getWirelessTransmitterRangeModifier() {
return ensureLoaded().getWirelessTransmitterRangeModifier();
}

private PlatformApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("Platform API not loaded yet");
Expand Down
Loading