Skip to content

Commit

Permalink
Merge pull request #433 from refinedmods/refactor/GH-88/network-bound…
Browse files Browse the repository at this point in the history
…-item

refactor: remove duplication around energy item / block item
  • Loading branch information
raoulvdberge authored Sep 1, 2023
2 parents 2076765 + 2cd3946 commit f7c71e1
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.refinedmods.refinedstorage2.platform.api.grid.GridScrollingStrategyFactory;
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 @@ -75,8 +77,6 @@ public interface PlatformApi {

MutableComponent createTranslation(String category, String value, Object... args);

MutableComponent createStoredWithCapacityTranslation(long stored, long capacity, double pct);

ComponentMapFactory<NetworkComponent, Network> getNetworkComponentMapFactory();

PlatformRegistry<GridSynchronizer> getGridSynchronizerRegistry();
Expand Down Expand Up @@ -143,5 +143,9 @@ GridScrollingStrategy createGridScrollingStrategy(AbstractContainerMenu containe

Optional<EnergyStorage> getEnergyStorage(ItemStack stack);

EnergyItemHelper getEnergyItemHelper();

EnergyStorage asItemEnergyStorage(EnergyStorage energyStorage, ItemStack stack);

NetworkBoundItemHelper getNetworkBoundItemHelper();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.refinedmods.refinedstorage2.platform.api.grid.GridScrollingStrategyFactory;
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 @@ -121,13 +123,6 @@ public MutableComponent createTranslation(final String category, final String va
return ensureLoaded().createTranslation(category, value, args);
}

@Override
public MutableComponent createStoredWithCapacityTranslation(final long stored,
final long capacity,
final double pct) {
return ensureLoaded().createStoredWithCapacityTranslation(stored, capacity, pct);
}

@Override
public ComponentMapFactory<NetworkComponent, Network> getNetworkComponentMapFactory() {
return ensureLoaded().getNetworkComponentMapFactory();
Expand Down Expand Up @@ -286,11 +281,21 @@ public Optional<EnergyStorage> getEnergyStorage(final ItemStack stack) {
return ensureLoaded().getEnergyStorage(stack);
}

@Override
public EnergyItemHelper getEnergyItemHelper() {
return ensureLoaded().getEnergyItemHelper();
}

@Override
public EnergyStorage asItemEnergyStorage(final EnergyStorage energyStorage, final ItemStack stack) {
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
@@ -1,25 +1,26 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.annotation.Nullable;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
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 {
protected AbstractEnergyBlockItem(final Block block, final Properties properties) {
private final EnergyItemHelper helper;

protected AbstractEnergyBlockItem(final Block block, final Properties properties, final EnergyItemHelper helper) {
super(block, properties);
this.helper = helper;
}

@Override
Expand All @@ -30,49 +31,38 @@ public void appendHoverText(
final TooltipFlag flag
) {
super.appendHoverText(stack, level, lines, flag);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> {
final long stored = energyStorage.getStored();
final long capacity = energyStorage.getCapacity();
final double pct = stored / (double) capacity;
lines.add(PlatformApi.INSTANCE.createStoredWithCapacityTranslation(stored, capacity, pct)
.withStyle(ChatFormatting.GRAY));
});
helper.addTooltip(stack, lines);
}

@Override
public boolean isBarVisible(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).isPresent();
return helper.isBarVisible(stack);
}

@Override
public int getBarWidth(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> (int) Math.round(
(energyStorage.getStored() / (double) energyStorage.getCapacity()) * 13D
)).orElse(0);
return helper.getBarWidth(stack);
}

@Override
public int getBarColor(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> Mth.hsvToRgb(
Math.max(0.0F, (float) energyStorage.getStored() / (float) energyStorage.getCapacity()) / 3.0F,
1.0F,
1.0F
)).orElse(0);
return helper.getBarColor(stack);
}

public ItemStack createAtEnergyCapacity() {
final ItemStack stack = new ItemStack(this);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> energyStorage.receive(
energyStorage.getCapacity(),
Action.EXECUTE
));
return stack;
return helper.createAtEnergyCapacity(this);
}

public static Stream<ItemStack> createAllAtEnergyCapacity(final List<Supplier<BlockItem>> items) {
return items.stream().map(Supplier::get)
.filter(AbstractEnergyBlockItem.class::isInstance)
.map(AbstractEnergyBlockItem.class::cast)
.map(AbstractEnergyBlockItem::createAtEnergyCapacity);
@Override
protected boolean updateCustomBlockEntityTag(
final BlockPos pos,
final Level level,
@Nullable final Player player,
final ItemStack stack,
final BlockState blockState
) {
final boolean result = super.updateCustomBlockEntityTag(pos, level, player, stack, blockState);
helper.passEnergyToBlockEntity(pos, level, stack);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;

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

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
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 {
protected AbstractEnergyItem(final Properties properties) {
private final EnergyItemHelper helper;

protected AbstractEnergyItem(final Properties properties, final EnergyItemHelper helper) {
super(properties);
this.helper = helper;
}

@Override
Expand All @@ -27,42 +27,25 @@ public void appendHoverText(
final TooltipFlag flag
) {
super.appendHoverText(stack, level, lines, flag);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> {
final long stored = energyStorage.getStored();
final long capacity = energyStorage.getCapacity();
final double pct = stored / (double) capacity;
lines.add(PlatformApi.INSTANCE.createStoredWithCapacityTranslation(stored, capacity, pct)
.withStyle(ChatFormatting.GRAY));
});
helper.addTooltip(stack, lines);
}

@Override
public boolean isBarVisible(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).isPresent();
return helper.isBarVisible(stack);
}

@Override
public int getBarWidth(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> (int) Math.round(
(energyStorage.getStored() / (double) energyStorage.getCapacity()) * 13D
)).orElse(0);
return helper.getBarWidth(stack);
}

@Override
public int getBarColor(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> Mth.hsvToRgb(
Math.max(0.0F, (float) energyStorage.getStored() / (float) energyStorage.getCapacity()) / 3.0F,
1.0F,
1.0F
)).orElse(0);
return helper.getBarColor(stack);
}

public ItemStack createAtEnergyCapacity() {
final ItemStack stack = new ItemStack(this);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> energyStorage.receive(
energyStorage.getCapacity(),
Action.EXECUTE
));
return stack;
return helper.createAtEnergyCapacity(this);
}
}
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,25 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import java.util.List;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.apiguardian.api.API;

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

boolean isBarVisible(ItemStack stack);

int getBarWidth(ItemStack stack);

int getBarColor(ItemStack stack);

ItemStack createAtEnergyCapacity(Item item);

void passEnergyToBlockEntity(BlockPos pos, Level level, ItemStack 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);
}
Loading

0 comments on commit f7c71e1

Please sign in to comment.