Skip to content

Commit

Permalink
refactor: api for network bound items
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Sep 1, 2023
1 parent 707b878 commit 2cd3946
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.refinedmods.refinedstorage2.platform.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage2.platform.api.integration.recipemod.IngredientConverter;
import com.refinedmods.refinedstorage2.platform.api.item.EnergyItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.StorageContainerItemHelper;
import com.refinedmods.refinedstorage2.platform.api.network.node.exporter.ExporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
Expand Down Expand Up @@ -145,4 +146,6 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe
EnergyItemHelper getEnergyItemHelper();

EnergyStorage asItemEnergyStorage(EnergyStorage energyStorage, ItemStack stack);

NetworkBoundItemHelper getNetworkBoundItemHelper();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.refinedmods.refinedstorage2.platform.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage2.platform.api.integration.recipemod.IngredientConverter;
import com.refinedmods.refinedstorage2.platform.api.item.EnergyItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.StorageContainerItemHelper;
import com.refinedmods.refinedstorage2.platform.api.network.node.exporter.ExporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
Expand Down Expand Up @@ -290,6 +291,11 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage, fina
return ensureLoaded().asItemEnergyStorage(energyStorage, stack);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return ensureLoaded().getNetworkBoundItemHelper();
}

private PlatformApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("Platform API not loaded yet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.1")
public abstract class AbstractEnergyBlockItem extends BlockItem implements EnergyItem {
private final EnergyItemHelper helper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.1")
public abstract class AbstractEnergyItem extends Item implements EnergyItem {
private final EnergyItemHelper helper;

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

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

import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.1")
public abstract class AbstractNetworkBoundEnergyItem extends AbstractEnergyItem {
protected final NetworkBoundItemHelper networkBoundItemHelper;

protected AbstractNetworkBoundEnergyItem(final Properties properties,
final EnergyItemHelper energyItemHelper,
final NetworkBoundItemHelper networkBoundItemHelper) {
super(properties, energyItemHelper);
this.networkBoundItemHelper = networkBoundItemHelper;
}

@Override
public InteractionResult useOn(final UseOnContext ctx) {
return networkBoundItemHelper.bind(ctx);
}

@Override
public Optional<TooltipComponent> getTooltipImage(final ItemStack stack) {
return networkBoundItemHelper.getTooltipImage(stack);
}

@Override
public void appendHoverText(final ItemStack stack,
@Nullable final Level level,
final List<Component> tooltip,
final TooltipFlag flag) {
super.appendHoverText(stack, level, tooltip, flag);
networkBoundItemHelper.addTooltip(stack, tooltip);
}

public boolean isBound(final ItemStack stack) {
return networkBoundItemHelper.isBound(stack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import java.util.List;
import java.util.Optional;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.1")
public interface NetworkBoundItemHelper {
void addTooltip(ItemStack stack, List<Component> lines);

InteractionResult bind(UseOnContext ctx);

Optional<TooltipComponent> getTooltipImage(ItemStack stack);

NetworkBoundItemSession openSession(ItemStack stack, ServerPlayer player, InteractionHand hand);

boolean isBound(ItemStack stack);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import com.refinedmods.refinedstorage2.api.network.Network;

import java.util.Optional;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.1")
public interface NetworkBoundItemSession {
Optional<Network> resolveNetwork();

boolean isActive();

void drainEnergy(long amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.refinedmods.refinedstorage2.platform.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage2.platform.api.integration.recipemod.IngredientConverter;
import com.refinedmods.refinedstorage2.platform.api.item.EnergyItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage2.platform.api.item.StorageContainerItemHelper;
import com.refinedmods.refinedstorage2.platform.api.network.node.exporter.ExporterTransferStrategyFactory;
import com.refinedmods.refinedstorage2.platform.api.network.node.externalstorage.PlatformExternalStorageProviderFactory;
Expand All @@ -46,6 +47,7 @@
import com.refinedmods.refinedstorage2.platform.common.internal.grid.CompositeGridScrollingStrategy;
import com.refinedmods.refinedstorage2.platform.common.internal.grid.NoOpGridSynchronizer;
import com.refinedmods.refinedstorage2.platform.common.internal.item.EnergyItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.item.NetworkBoundItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.item.StorageContainerItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.network.LevelConnectionProvider;
import com.refinedmods.refinedstorage2.platform.common.internal.registry.PlatformRegistryImpl;
Expand Down Expand Up @@ -134,6 +136,7 @@ public class PlatformApiImpl implements PlatformApi {
private final CompositeWirelessTransmitterRangeModifier wirelessTransmitterRangeModifier =
new CompositeWirelessTransmitterRangeModifier();
private final EnergyItemHelper energyItemHelper = new EnergyItemHelperImpl();
private final NetworkBoundItemHelper networkBoundItemHelper = new NetworkBoundItemHelperImpl();

@Override
public PlatformRegistry<StorageType<?>> getStorageTypeRegistry() {
Expand Down Expand Up @@ -414,4 +417,9 @@ public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage,
final ItemStack stack) {
return new ItemEnergyStorage(stack, energyStorage);
}

@Override
public NetworkBoundItemHelper getNetworkBoundItemHelper() {
return networkBoundItemHelper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ protected void addPlayerInventory(final Inventory inventory, final int inventory
for (int i = 0; i < 9; i++) {
final int x = inventoryX + i * 18;
final int y = inventoryY + 4 + (3 * 18);
final boolean disabled = disabledPlayerInventorySlot != null
&& disabledPlayerInventorySlot.getSlotIndex() == id;
final boolean disabled = disabledPlayerInventorySlot != null && disabledPlayerInventorySlot.isDisabled(id);
addSlot(disabled ? new DisabledSlot(inventory, id, x, y) : new Slot(inventory, id, x, y));
id++;
}
Expand Down Expand Up @@ -107,6 +106,6 @@ public void clicked(final int id, final int dragType, final ClickType clickType,
private boolean isSwappingDisabledSlotWithNumberKeys(final int dragType, final ClickType clickType) {
return disabledPlayerInventorySlot != null
&& clickType == ClickType.SWAP
&& dragType == disabledPlayerInventorySlot.getSlotIndex();
&& disabledPlayerInventorySlot.isDisabled(dragType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public ItemStack resolve(final Player player) {
return player.getInventory().getItem(slotIndex);
}

public int getSlotIndex() {
return slotIndex;
public boolean isDisabled(final int playerSlotIndex) {
return playerSlotIndex == slotIndex;
}

public void writeToBuf(final ByteBuf buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode;
import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode;
import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage2.api.network.Network;
import com.refinedmods.refinedstorage2.api.network.component.EnergyNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.impl.component.GraphNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridWatchers;
import com.refinedmods.refinedstorage2.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
Expand All @@ -19,68 +16,38 @@
import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.blockentity.wirelesstransmitter.WirelessTransmitter;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.api.network.node.PlatformNetworkNodeContainer;
import com.refinedmods.refinedstorage2.platform.api.item.NetworkBoundItemSession;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.common.internal.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.item.NetworkBoundItemContext;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.minecraft.server.MinecraftServer;

public class WirelessGrid implements Grid {
private final MinecraftServer server;
private final NetworkBoundItemContext ctx;
private final NetworkBoundItemSession session;
private final GridWatchers watchers;

public WirelessGrid(final MinecraftServer server, final NetworkBoundItemContext ctx) {
this.server = server;
this.ctx = ctx;
public WirelessGrid(final NetworkBoundItemSession session) {
this.session = session;
this.watchers = new GridWatchers(PlatformApi.INSTANCE.getStorageChannelTypeRegistry().getAll());
}

private Optional<Network> getNetwork() {
if (ctx.getNetworkReference() == null) {
return Optional.empty();
}
return Optional.ofNullable(server.getLevel(ctx.getNetworkReference().dimensionKey()))
.filter(level -> level.isLoaded(ctx.getNetworkReference().pos()))
.map(level -> level.getBlockEntity(ctx.getNetworkReference().pos()))
.filter(PlatformNetworkNodeContainer.class::isInstance)
.map(PlatformNetworkNodeContainer.class::cast)
.map(PlatformNetworkNodeContainer::getNode)
.map(NetworkNode::getNetwork)
.filter(this::isInRange);
}

private boolean isInRange(final Network network) {
return network.getComponent(GraphNetworkComponent.class)
.getContainers(WirelessTransmitter.class)
.stream()
.anyMatch(wirelessTransmitter -> wirelessTransmitter.isInRange(
ctx.getPlayerLevel(),
ctx.getPlayerPosition()
));
}

private Optional<StorageNetworkComponent> getStorage() {
return getNetwork().map(network -> network.getComponent(StorageNetworkComponent.class));
return session.resolveNetwork().map(network -> network.getComponent(StorageNetworkComponent.class));
}

@Override
public void addWatcher(final GridWatcher watcher, final Class<? extends Actor> actorType) {
getNetwork().ifPresent(network -> watchers.addWatcher(watcher, actorType, network));
session.resolveNetwork().ifPresent(network -> watchers.addWatcher(watcher, actorType, network));
}

@Override
public void removeWatcher(final GridWatcher watcher) {
getNetwork().ifPresent(network -> watchers.removeWatcher(watcher, network));
session.resolveNetwork().ifPresent(network -> watchers.removeWatcher(watcher, network));
}

@Override
Expand All @@ -91,10 +58,10 @@ public Storage<ItemResource> getItemStorage() {

@Override
public boolean isActive() {
final boolean networkActive = getNetwork().map(
final boolean networkActive = session.resolveNetwork().map(
network -> network.getComponent(EnergyNetworkComponent.class).getStored() > 0
).orElse(false);
return networkActive && ctx.isActive();
return networkActive && session.isActive();
}

@Override
Expand All @@ -109,7 +76,7 @@ public <T> GridOperations<T> createOperations(final PlatformStorageChannelType<T
return getStorage()
.map(storage -> storage.getStorageChannel(storageChannelType))
.map(storageChannel -> storageChannelType.createGridOperations(storageChannel, actor))
.map(gridOperations -> (GridOperations<T>) new WirelessGridOperations<>(gridOperations, ctx, watchers))
.map(gridOperations -> (GridOperations<T>) new WirelessGridOperations<>(gridOperations, session, watchers))
.orElseGet(this::createNoOpGridOperations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridWatchers;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;
import com.refinedmods.refinedstorage2.platform.api.item.NetworkBoundItemSession;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.item.NetworkBoundItemContext;

public class WirelessGridOperations<T> implements GridOperations<T> {
private final GridOperations<T> delegate;
private final NetworkBoundItemContext ctx;
private final NetworkBoundItemSession session;
private final GridWatchers watchers;

public WirelessGridOperations(final GridOperations<T> delegate,
final NetworkBoundItemContext ctx,
final NetworkBoundItemSession session,
final GridWatchers watchers) {
this.delegate = delegate;
this.ctx = ctx;
this.session = session;
this.watchers = watchers;
}

Expand All @@ -45,9 +45,9 @@ public boolean insert(final T resource,
}

private void drain(final long amount) {
final boolean wasActive = ctx.isActive();
ctx.drain(amount);
final boolean isActive = ctx.isActive();
final boolean wasActive = session.isActive();
session.drainEnergy(amount);
final boolean isActive = session.isActive();
if (wasActive != isActive) {
watchers.activeChanged(isActive);
}
Expand Down
Loading

0 comments on commit 2cd3946

Please sign in to comment.