From a8adc849cc8f1295fd03065c1b6cfb1dda55f5f9 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sat, 3 Aug 2024 14:12:02 +0200 Subject: [PATCH] refactor: make ResourceAmount an immutable record --- ...CraftingPatternClientTooltipComponent.java | 6 ++-- .../autocrafting/PatternGridBlockEntity.java | 4 +-- .../common/autocrafting/PatternRendering.java | 4 +-- ...ocessingPatternClientTooltipComponent.java | 12 ++++---- .../grid/AbstractGridContainerMenu.java | 8 ++--- .../common/storage/StorageCodecs.java | 8 ++--- .../storage/portablegrid/PortableGrid.java | 2 +- .../common/support/AbstractBaseScreen.java | 4 +-- .../support/resource/ResourceCodecs.java | 8 ++--- .../resource/ResourceContainerImpl.java | 16 +++++----- .../resource/ResourceContainerSlot.java | 8 ++--- .../ResourceClientTooltipComponent.java | 10 +++---- .../refinedstorage/fabric/PlatformImpl.java | 4 +-- .../ResourceContainerFluidStorageAdapter.java | 4 +-- .../grid/watcher/GridWatcherRegistration.java | 6 ++-- .../refinedstorage/neoforge/PlatformImpl.java | 4 +-- .../ResourceContainerFluidHandlerAdapter.java | 4 +-- .../refinedstorage/common/GameTestUtil.java | 30 +++++++++---------- .../impl/node/relay/RelayOutputStorage.java | 14 ++++----- .../StorageTransferNetworkNode.java | 6 ++-- .../storage/StorageNetworkComponentImpl.java | 2 +- .../node/iface/InterfaceExportStateImpl.java | 14 ++++----- .../api/resource/ResourceAmount.java | 30 ++----------------- .../api/resource/list/ResourceList.java | 4 +-- .../composite/CompositeStorageImpl.java | 4 +-- .../api/storage/external/ExternalStorage.java | 2 +- .../api/storage/TransferHelperTest.java | 12 ++++---- 27 files changed, 102 insertions(+), 128 deletions(-) diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/CraftingPatternClientTooltipComponent.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/CraftingPatternClientTooltipComponent.java index e06c204a1..52c833fb3 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/CraftingPatternClientTooltipComponent.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/CraftingPatternClientTooltipComponent.java @@ -49,14 +49,14 @@ public CraftingPatternClientTooltipComponent(final int width, this.width = width; this.height = height; this.craftingPattern = craftingPattern; - final ItemResource outputResource = craftingPattern.output().getResource() instanceof ItemResource itemResource + final ItemResource outputResource = craftingPattern.output().resource() instanceof ItemResource itemResource ? itemResource : null; this.outputStack = outputResource != null - ? outputResource.toItemStack(craftingPattern.output().getAmount()) + ? outputResource.toItemStack(craftingPattern.output().amount()) : null; this.outputText = outputResource != null - ? Component.literal(String.format("%dx ", craftingPattern.output().getAmount())) + ? Component.literal(String.format("%dx ", craftingPattern.output().amount())) .append(outputResource.toItemStack().getHoverName()) .withStyle(ChatFormatting.GRAY) : null; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternGridBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternGridBlockEntity.java index cf5e736fc..7881f4a9b 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternGridBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternGridBlockEntity.java @@ -244,11 +244,11 @@ private ItemStack createProcessingPattern() { final ItemStack result = createPatternStack(PatternType.PROCESSING); final List> inputs = new ArrayList<>(); for (int i = 0; i < processingInput.size(); ++i) { - inputs.add(Optional.ofNullable(processingInput.get(i)).map(ResourceAmount::copy)); + inputs.add(Optional.ofNullable(processingInput.get(i))); } final List> outputs = new ArrayList<>(); for (int i = 0; i < processingOutput.size(); ++i) { - outputs.add(Optional.ofNullable(processingOutput.get(i)).map(ResourceAmount::copy)); + outputs.add(Optional.ofNullable(processingOutput.get(i))); } final ProcessingPatternState patternProcessingState = new ProcessingPatternState( inputs, diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java index 165abc671..ca1143815 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java @@ -28,12 +28,12 @@ public static Optional getOutput(final ItemStack stack) { } return RefinedStorageApi.INSTANCE.getPattern(stack, level).map(pattern -> { if (pattern instanceof CraftingPattern craftingPattern - && craftingPattern.output().getResource() instanceof ItemResource itemResource) { + && craftingPattern.output().resource() instanceof ItemResource itemResource) { return itemResource.toItemStack(); } if (pattern instanceof ProcessingPattern processingPattern && processingPattern.outputs().size() == 1 - && processingPattern.outputs().getFirst().getResource() instanceof ItemResource itemResource) { + && processingPattern.outputs().getFirst().resource() instanceof ItemResource itemResource) { return itemResource.toItemStack(); } return null; diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/ProcessingPatternClientTooltipComponent.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/ProcessingPatternClientTooltipComponent.java index 5131a51a2..b3a04f21e 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/ProcessingPatternClientTooltipComponent.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/ProcessingPatternClientTooltipComponent.java @@ -60,14 +60,14 @@ private static List getOutputText(final ProcessingPatternState state) private static Component getOutputText(final ResourceAmount resourceAmount) { final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering( - resourceAmount.getResource() + resourceAmount.resource() ); final String displayAmount = rendering.getDisplayedAmount( - resourceAmount.getAmount(), + resourceAmount.amount(), false ); return Component.literal(String.format("%sx ", displayAmount)) - .append(rendering.getDisplayName(resourceAmount.getResource())) + .append(rendering.getDisplayName(resourceAmount.resource())) .withStyle(ChatFormatting.GRAY); } @@ -113,10 +113,10 @@ private void renderMatrixSlots(final int x, graphics.blitSprite(SLOT, slotX, slotY, 18, 18); slots.get(idx).ifPresent(resourceAmount -> { final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering( - resourceAmount.getResource() + resourceAmount.resource() ); - rendering.render(resourceAmount.getResource(), graphics, slotX + 1, slotY + 1); - AbstractBaseScreen.renderResourceAmount(graphics, slotX + 1, slotY + 1, resourceAmount.getAmount(), + rendering.render(resourceAmount.resource(), graphics, slotX + 1, slotY + 1); + AbstractBaseScreen.renderResourceAmount(graphics, slotX + 1, slotY + 1, resourceAmount.amount(), rendering); }); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java index 678bf3ca6..45fdf9efa 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java @@ -101,10 +101,10 @@ protected AbstractGridContainerMenu( this.active = gridData.active(); final GridViewBuilder viewBuilder = createViewBuilder(); - gridData.resources().forEach(gridResource -> viewBuilder.withResource( - gridResource.resourceAmount().getResource(), - gridResource.resourceAmount().getAmount(), - gridResource.trackedResource().orElse(null) + gridData.resources().forEach(resource -> viewBuilder.withResource( + resource.resourceAmount().resource(), + resource.resourceAmount().amount(), + resource.trackedResource().orElse(null) )); this.view = viewBuilder.build(); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageCodecs.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageCodecs.java index 9ec837f8f..cfd0c8031 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageCodecs.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageCodecs.java @@ -67,7 +67,7 @@ static StorageData ofSameTypeStorage( ? Optional.of(limitedStorage.getCapacity()) : Optional.empty(); final List> resources = storage.getAll().stream() - .filter(resourceAmount -> valid.test(resourceAmount.getResource())) + .filter(resourceAmount -> valid.test(resourceAmount.resource())) .map(resourceAmount -> getResource(storage, caster, resourceAmount)) .toList(); return new StorageData<>(capacity, resources); @@ -79,8 +79,8 @@ private static StorageResource getResource( final ResourceAmount resourceAmount ) { return new StorageResource<>( - caster.apply(resourceAmount.getResource()), - resourceAmount.getAmount(), + caster.apply(resourceAmount.resource()), + resourceAmount.amount(), getChanged(storage, resourceAmount) ); } @@ -90,7 +90,7 @@ private static Optional getChanged(final Storage storage, if (!(storage instanceof TrackedStorage trackedStorage)) { return Optional.empty(); } - return trackedStorage.findTrackedResourceByActorType(resourceAmount.getResource(), PlayerActor.class) + return trackedStorage.findTrackedResourceByActorType(resourceAmount.resource(), PlayerActor.class) .map(StorageChangedByAt::ofTrackedResource); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java index 22e2c6dde..90d548274 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java @@ -106,7 +106,7 @@ public List getResources(final Class act final RootStorage rootStorage = storage.getRootStorage(); return rootStorage.getAll().stream().map(resource -> new TrackedResourceAmount( resource, - rootStorage.findTrackedResourceByActorType(resource.getResource(), actorType).orElse(null) + rootStorage.findTrackedResourceByActorType(resource.resource(), actorType).orElse(null) )).toList(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java index 713e825c2..a1c8f07e6 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java @@ -250,7 +250,7 @@ private List getResourceSlotHelpTooltip(final ItemStack resourceSlot.getPrimaryResourceFactory().create(carried).ifPresent(primaryResourceInstance -> lines.add( MouseClientTooltipComponent.resource( MouseClientTooltipComponent.Type.LEFT, - primaryResourceInstance.getResource(), + primaryResourceInstance.resource(), null ) )); @@ -258,7 +258,7 @@ private List getResourceSlotHelpTooltip(final ItemStack final var result = alternativeResourceFactory.create(carried); result.ifPresent(alternativeResourceInstance -> lines.add(MouseClientTooltipComponent.resource( MouseClientTooltipComponent.Type.RIGHT, - alternativeResourceInstance.getResource(), + alternativeResourceInstance.resource(), null ))); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceCodecs.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceCodecs.java index 2c1e07eaa..2ebd5e36f 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceCodecs.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceCodecs.java @@ -37,8 +37,8 @@ public final class ResourceCodecs { .codec() .dispatch(PlatformResourceKey::getResourceType, ResourceType::getMapCodec); public static final Codec AMOUNT_CODEC = RecordCodecBuilder.create(instance -> instance.group( - CODEC.fieldOf("resource").forGetter(resourceAmount -> (PlatformResourceKey) resourceAmount.getResource()), - Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::getAmount) + CODEC.fieldOf("resource").forGetter(resourceAmount -> (PlatformResourceKey) resourceAmount.resource()), + Codec.LONG.fieldOf("amount").forGetter(ResourceAmount::amount) ).apply(instance, ResourceAmount::new)); public static final Codec> AMOUNT_OPTIONAL_CODEC = AMOUNT_CODEC.optionalFieldOf("resource") .codec(); @@ -64,12 +64,12 @@ public void encode(final RegistryFriendlyByteBuf buf, final PlatformResourceKey }; public static final StreamCodec AMOUNT_STREAM_CODEC = StreamCodec.of( (buf, resourceAmount) -> { - final ResourceKey resourceKey = resourceAmount.getResource(); + final ResourceKey resourceKey = resourceAmount.resource(); if (!(resourceKey instanceof PlatformResourceKey platformResourceKey)) { throw new DecoderException("Cannot encode non-platform resource key"); } STREAM_CODEC.encode(buf, platformResourceKey); - buf.writeLong(resourceAmount.getAmount()); + buf.writeLong(resourceAmount.amount()); }, buf -> { final PlatformResourceKey resourceKey = STREAM_CODEC.decode(buf); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java index 8e62a7724..287d1865d 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java @@ -95,7 +95,7 @@ public long getAmount(final int index) { if (slot == null) { return 0; } - return slot.getResourceAmount().getAmount(); + return slot.getResourceAmount().amount(); } @Override @@ -116,7 +116,7 @@ public void setAmount(final int index, final long amount) { if (slot == null) { return; } - final long newAmount = MathUtil.clamp(amount, 0, getMaxAmount(slot.getResourceAmount().getResource())); + final long newAmount = MathUtil.clamp(amount, 0, getMaxAmount(slot.getResourceAmount().resource())); if (newAmount == 0) { remove(index); } else { @@ -190,7 +190,7 @@ public Set getUniqueResources() { if (slot == null) { continue; } - result.add(slot.getResourceAmount().getResource()); + result.add(slot.getResourceAmount().resource()); } return result; } @@ -203,7 +203,7 @@ public List getResources() { if (slot == null) { continue; } - result.add(slot.getResourceAmount().getResource()); + result.add(slot.getResourceAmount().resource()); } return result; } @@ -290,7 +290,7 @@ public long insert(final ResourceKey resource, final long amount, final Action a final ResourceAmount slot = get(i); if (slot == null) { remainder -= insertIntoEmptySlot(i, platformResource, action, remainder); - } else if (slot.getResource().equals(resource)) { + } else if (slot.resource().equals(resource)) { remainder -= insertIntoExistingSlot( i, platformResource, @@ -322,7 +322,7 @@ private long insertIntoExistingSlot(final int slotIndex, final Action action, final long amount, final ResourceAmount existing) { - final long spaceRemaining = resource.getInterfaceExportLimit() - existing.getAmount(); + final long spaceRemaining = resource.getInterfaceExportLimit() - existing.amount(); final long inserted = Math.min(spaceRemaining, amount); if (action == Action.EXECUTE) { grow(slotIndex, inserted); @@ -335,11 +335,11 @@ public long extract(final ResourceKey resource, final long amount, final Action long extracted = 0; for (int i = 0; i < size(); ++i) { final ResourceAmount slotContents = get(i); - if (slotContents == null || !resource.equals(slotContents.getResource())) { + if (slotContents == null || !resource.equals(slotContents.resource())) { continue; } final long stillNeeded = amount - extracted; - final long toExtract = Math.min(slotContents.getAmount(), stillNeeded); + final long toExtract = Math.min(slotContents.amount(), stillNeeded); if (action == Action.EXECUTE) { shrink(i, toExtract); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerSlot.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerSlot.java index b8eff1aed..fed125bb0 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerSlot.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerSlot.java @@ -13,8 +13,8 @@ class ResourceContainerSlot { ResourceContainerSlot(final ResourceAmount resourceAmount) { this.resourceAmount = resourceAmount; - this.stackRepresentation = resourceAmount.getResource() instanceof ItemResource itemResource - ? itemResource.toItemStack(resourceAmount.getAmount()) + this.stackRepresentation = resourceAmount.resource() instanceof ItemResource itemResource + ? itemResource.toItemStack(resourceAmount.amount()) : ItemStack.EMPTY; } @@ -23,7 +23,7 @@ ResourceAmount getResourceAmount() { } PlatformResourceKey getPlatformResource() { - return (PlatformResourceKey) resourceAmount.getResource(); + return (PlatformResourceKey) resourceAmount.resource(); } ItemStack getStackRepresentation() { @@ -31,7 +31,7 @@ ItemStack getStackRepresentation() { } ResourceContainerSlot withAmount(final long newAmount) { - return new ResourceContainerSlot(new ResourceAmount(resourceAmount.getResource(), newAmount)); + return new ResourceContainerSlot(new ResourceAmount(resourceAmount.resource(), newAmount)); } @Override diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/tooltip/ResourceClientTooltipComponent.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/tooltip/ResourceClientTooltipComponent.java index c5bb0b3a4..4a43b83f1 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/tooltip/ResourceClientTooltipComponent.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/tooltip/ResourceClientTooltipComponent.java @@ -33,8 +33,8 @@ public int getWidth(final Font font) { @Override public void renderImage(final Font font, final int x, final int y, final GuiGraphics graphics) { - RefinedStorageApi.INSTANCE.getResourceRendering(resourceAmount.getResource()).render( - resourceAmount.getResource(), + RefinedStorageApi.INSTANCE.getResourceRendering(resourceAmount.resource()).render( + resourceAmount.resource(), graphics, x, y @@ -50,10 +50,10 @@ public void renderImage(final Font font, final int x, final int y, final GuiGrap private static Component getNameWithAmount(final ResourceAmount resourceAmount) { final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering( - resourceAmount.getResource() + resourceAmount.resource() ); - final String amount = rendering.getDisplayedAmount(resourceAmount.getAmount(), true); - final Component displayName = rendering.getDisplayName(resourceAmount.getResource()); + final String amount = rendering.getDisplayedAmount(resourceAmount.amount(), true); + final Component displayName = rendering.getDisplayName(resourceAmount.resource()); if (amount.isEmpty()) { return displayName; } diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java index 6cdaf7999..840267efc 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java @@ -175,7 +175,7 @@ public Optional drainContainer(final ItemStack container) @Override public Optional fillContainer(final ItemStack container, final ResourceAmount resourceAmount) { - if (!(resourceAmount.getResource() instanceof FluidResource fluidResource)) { + if (!(resourceAmount.resource() instanceof FluidResource fluidResource)) { return Optional.empty(); } final SimpleSingleStackStorage interceptingStorage = new SimpleSingleStackStorage(container); @@ -186,7 +186,7 @@ public Optional fillContainer(final ItemStack container, return Optional.empty(); } try (Transaction tx = Transaction.openOuter()) { - final long inserted = storage.insert(toFluidVariant(fluidResource), resourceAmount.getAmount(), tx); + final long inserted = storage.insert(toFluidVariant(fluidResource), resourceAmount.amount(), tx); return Optional.of(new FluidOperationResult( interceptingStorage.getStack(), fluidResource, diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/resource/ResourceContainerFluidStorageAdapter.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/resource/ResourceContainerFluidStorageAdapter.java index ab9b06a3e..222d680db 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/resource/ResourceContainerFluidStorageAdapter.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/resource/ResourceContainerFluidStorageAdapter.java @@ -88,11 +88,11 @@ private StorageViewImpl(final int index) { public long extract(final FluidVariant resource, final long maxAmount, final TransactionContext transaction) { final ResourceAmount resourceAmount = resourceContainer.get(index); if (resourceAmount == null - || !(resourceAmount.getResource() instanceof FluidResource fluidResource) + || !(resourceAmount.resource() instanceof FluidResource fluidResource) || !resource.equals(toFluidVariant(fluidResource))) { return 0; } - final long extracted = Math.min(maxAmount, resourceAmount.getAmount()); + final long extracted = Math.min(maxAmount, resourceAmount.amount()); if (extracted > 0) { updateSnapshots(transaction); } diff --git a/refinedstorage-grid-api/src/main/java/com/refinedmods/refinedstorage/api/grid/watcher/GridWatcherRegistration.java b/refinedstorage-grid-api/src/main/java/com/refinedmods/refinedstorage/api/grid/watcher/GridWatcherRegistration.java index 5dced34a3..03ce20edb 100644 --- a/refinedstorage-grid-api/src/main/java/com/refinedmods/refinedstorage/api/grid/watcher/GridWatcherRegistration.java +++ b/refinedstorage-grid-api/src/main/java/com/refinedmods/refinedstorage/api/grid/watcher/GridWatcherRegistration.java @@ -26,10 +26,10 @@ void attach(final RootStorage rootStorage, final boolean replay) { rootStorage.addListener(listener); if (replay) { rootStorage.getAll().forEach(resourceAmount -> watcher.onChanged( - resourceAmount.getResource(), - resourceAmount.getAmount(), + resourceAmount.resource(), + resourceAmount.amount(), rootStorage.findTrackedResourceByActorType( - resourceAmount.getResource(), + resourceAmount.resource(), actorType ).orElse(null) )); diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java index 8c8cf08ab..1e91d7032 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java @@ -155,11 +155,11 @@ public Optional drainContainer(final ItemStack container) @Override public Optional fillContainer(final ItemStack container, final ResourceAmount resourceAmount) { - if (!(resourceAmount.getResource() instanceof FluidResource fluidResource)) { + if (!(resourceAmount.resource() instanceof FluidResource fluidResource)) { return Optional.empty(); } return FluidUtil.getFluidHandler(container).map(handler -> { - final FluidStack fluidStack = toFluidStack(fluidResource, resourceAmount.getAmount()); + final FluidStack fluidStack = toFluidStack(fluidResource, resourceAmount.amount()); final long filled = handler.fill(fluidStack, IFluidHandler.FluidAction.EXECUTE); return new FluidOperationResult(handler.getContainer(), fluidResource, filled); }); diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/support/resource/ResourceContainerFluidHandlerAdapter.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/support/resource/ResourceContainerFluidHandlerAdapter.java index d14f2cbd7..f05f44227 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/support/resource/ResourceContainerFluidHandlerAdapter.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/support/resource/ResourceContainerFluidHandlerAdapter.java @@ -30,10 +30,10 @@ public int getTanks() { @Override public FluidStack getFluidInTank(final int tank) { final ResourceAmount resourceAmount = container.get(tank); - if (resourceAmount == null || !(resourceAmount.getResource() instanceof FluidResource fluidResource)) { + if (resourceAmount == null || !(resourceAmount.resource() instanceof FluidResource fluidResource)) { return FluidStack.EMPTY; } - return toFluidStack(fluidResource, resourceAmount.getAmount()); + return toFluidStack(fluidResource, resourceAmount.amount()); } @Override diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java index b78757abb..2c68b9160 100644 --- a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java @@ -174,23 +174,23 @@ private static Runnable resourceContainerContainsExactly(final GameTestHelper he final boolean contains = IntStream.range(0, container.size()) .mapToObj(container::get) .anyMatch(resource -> resource != null - && resource.getResource().equals(expectedStack.getResource()) - && resource.getAmount() == expectedStack.getAmount()); + && resource.resource().equals(expectedStack.resource()) + && resource.amount() == expectedStack.amount()); helper.assertTrue(contains, "Expected resource is missing from " + displayName.getString() + ": " - + expectedStack + " with count: " + expectedStack.getAmount()); + + expectedStack + " with count: " + expectedStack.amount()); } for (int i = 0; i < container.size(); i++) { final ResourceAmount resource = container.get(i); if (resource != null) { final boolean wasExpected = Arrays.stream(expected).anyMatch( - expectedResource -> expectedResource.getResource().equals(resource.getResource()) - && expectedResource.getAmount() == resource.getAmount() + expectedResource -> expectedResource.resource().equals(resource.resource()) + && expectedResource.amount() == resource.amount() ); helper.assertTrue(wasExpected, "Unexpected resource found in " + displayName.getString() + ": " - + resource.getResource() + " with count: " + resource.getAmount()); + + resource.resource() + " with count: " + resource.amount()); } } }; @@ -206,18 +206,18 @@ public static Runnable containerContainsExactly(final GameTestHelper helper, for (final ResourceAmount expectedStack : expected) { final boolean contains = IntStream.range(0, containerBlockEntity.getContainerSize()) .mapToObj(containerBlockEntity::getItem) - .anyMatch(inContainer -> asResource(inContainer).equals(expectedStack.getResource()) - && inContainer.getCount() == expectedStack.getAmount()); + .anyMatch(inContainer -> asResource(inContainer).equals(expectedStack.resource()) + && inContainer.getCount() == expectedStack.amount()); helper.assertTrue(contains, "Expected resource is missing from container: " - + expectedStack + " with count: " + expectedStack.getAmount()); + + expectedStack + " with count: " + expectedStack.amount()); } for (int i = 0; i < containerBlockEntity.getContainerSize(); i++) { final ItemStack inContainer = containerBlockEntity.getItem(i); if (!inContainer.isEmpty()) { final boolean wasExpected = Arrays.stream(expected).anyMatch( - expectedStack -> expectedStack.getResource().equals(asResource(inContainer)) - && expectedStack.getAmount() == inContainer.getCount() + expectedStack -> expectedStack.resource().equals(asResource(inContainer)) + && expectedStack.amount() == inContainer.getCount() ); helper.assertTrue(wasExpected, "Unexpected resource found in container: " + inContainer.getDescriptionId() + " with count: " + inContainer.getCount()); @@ -234,14 +234,14 @@ public static Runnable storageContainsExactly(final GameTestHelper helper, for (final ResourceAmount expectedResource : expected) { final boolean contains = storage.getAll() .stream() - .anyMatch(inStorage -> inStorage.getResource().equals(expectedResource.getResource()) - && inStorage.getAmount() == expectedResource.getAmount()); + .anyMatch(inStorage -> inStorage.resource().equals(expectedResource.resource()) + && inStorage.amount() == expectedResource.amount()); helper.assertTrue(contains, "Expected resource is missing from storage: " + expectedResource); } for (final ResourceAmount inStorage : storage.getAll()) { final boolean wasExpected = Arrays.stream(expected).anyMatch( - expectedResource -> expectedResource.getResource().equals(inStorage.getResource()) - && expectedResource.getAmount() == inStorage.getAmount() + expectedResource -> expectedResource.resource().equals(inStorage.resource()) + && expectedResource.amount() == inStorage.amount() ); helper.assertTrue(wasExpected, "Unexpected resource found in storage: " + inStorage); } diff --git a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/relay/RelayOutputStorage.java b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/relay/RelayOutputStorage.java index 6fb25259f..90674582f 100644 --- a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/relay/RelayOutputStorage.java +++ b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/relay/RelayOutputStorage.java @@ -65,16 +65,16 @@ private void reset(final Runnable action) { void setDelegate(@Nullable final StorageNetworkComponent delegate) { if (this.delegate != null) { parentComposites.forEach(parent -> getAll().forEach(resourceAmount -> parent.removeFromCache( - resourceAmount.getResource(), - resourceAmount.getAmount() + resourceAmount.resource(), + resourceAmount.amount() ))); this.delegate.removeListener(this); } this.delegate = delegate; if (delegate != null) { parentComposites.forEach(parent -> getAll().forEach(resourceAmount -> parent.addToCache( - resourceAmount.getResource(), - resourceAmount.getAmount() + resourceAmount.resource(), + resourceAmount.amount() ))); delegate.addListener(this); } @@ -142,7 +142,7 @@ public Collection getAll() { } return delegate.getAll() .stream() - .filter(resourceAmount -> filter.isAllowed(resourceAmount.getResource())) + .filter(resourceAmount -> filter.isAllowed(resourceAmount.resource())) .toList(); } @@ -153,8 +153,8 @@ public long getStored() { } return delegate.getAll() .stream() - .filter(resourceAmount -> filter.isAllowed(resourceAmount.getResource())) - .mapToLong(ResourceAmount::getAmount) + .filter(resourceAmount -> filter.isAllowed(resourceAmount.resource())) + .mapToLong(ResourceAmount::amount) .sum(); } diff --git a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/storagetransfer/StorageTransferNetworkNode.java b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/storagetransfer/StorageTransferNetworkNode.java index a6d0bcbf3..7216fd858 100644 --- a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/storagetransfer/StorageTransferNetworkNode.java +++ b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/node/storagetransfer/StorageTransferNetworkNode.java @@ -121,11 +121,11 @@ private boolean transfer(final Storage source, final Storage destination, final long remainder = transferQuota; final Collection sourceContents = new LinkedHashSet<>(source.getAll()); for (final ResourceAmount resourceAmount : sourceContents) { - final ResourceKey resource = resourceAmount.getResource(); + final ResourceKey resource = resourceAmount.resource(); if (!filter.isAllowed(resource)) { continue; } - final long amount = Math.min(remainder, resourceAmount.getAmount()); + final long amount = Math.min(remainder, resourceAmount.amount()); final long transferred = TransferHelper.transfer(resource, amount, actor, source, destination, source); remainder -= transferred; if (remainder == 0) { @@ -136,7 +136,7 @@ private boolean transfer(final Storage source, final Storage destination, final } private boolean hasNoExtractableResources(final Storage source) { - return source.getAll().stream().noneMatch(resourceAmount -> filter.isAllowed(resourceAmount.getResource())); + return source.getAll().stream().noneMatch(resourceAmount -> filter.isAllowed(resourceAmount.resource())); } private boolean storageIsFull(final Storage storage) { diff --git a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/storage/StorageNetworkComponentImpl.java b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/storage/StorageNetworkComponentImpl.java index d0535d17c..1d4babcbb 100644 --- a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/storage/StorageNetworkComponentImpl.java +++ b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/storage/StorageNetworkComponentImpl.java @@ -43,7 +43,7 @@ public void onContainerRemoved(final NetworkNodeContainer container) { public List getResources(final Class actorType) { return getAll().stream().map(resourceAmount -> new TrackedResourceAmount( resourceAmount, - findTrackedResourceByActorType(resourceAmount.getResource(), actorType).orElse(null) + findTrackedResourceByActorType(resourceAmount.resource(), actorType).orElse(null) )).toList(); } diff --git a/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/node/iface/InterfaceExportStateImpl.java b/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/node/iface/InterfaceExportStateImpl.java index 5aa92b389..115fd22b2 100644 --- a/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/node/iface/InterfaceExportStateImpl.java +++ b/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/node/iface/InterfaceExportStateImpl.java @@ -83,7 +83,7 @@ public ResourceKey getRequestedResource(final int slotIndex) { if (resourceAmount == null) { return null; } - return resourceAmount.getResource(); + return resourceAmount.resource(); } @Override @@ -93,7 +93,7 @@ public long getRequestedAmount(final int slotIndex) { if (resourceAmount == null) { return 0L; } - return resourceAmount.getAmount(); + return resourceAmount.amount(); } @Nullable @@ -104,7 +104,7 @@ public ResourceKey getExportedResource(final int slotIndex) { if (resourceAmount == null) { return null; } - return resourceAmount.getResource(); + return resourceAmount.resource(); } @Override @@ -114,7 +114,7 @@ public long getExportedAmount(final int slotIndex) { if (resourceAmount == null) { return 0L; } - return resourceAmount.getAmount(); + return resourceAmount.amount(); } @Override @@ -131,12 +131,12 @@ public void setCurrentlyExported(final int index, final ResourceKey resource, fi public void shrinkExportedAmount(final int slotIndex, final long amount) { validateIndex(slotIndex); final ResourceAmount resourceAmount = this.current.get(slotIndex); - if (resourceAmount.getAmount() - amount <= 0) { + if (resourceAmount.amount() - amount <= 0) { this.current.remove(slotIndex); } else { this.current.put( slotIndex, - new ResourceAmount(resourceAmount.getResource(), resourceAmount.getAmount() - amount) + new ResourceAmount(resourceAmount.resource(), resourceAmount.amount() - amount) ); } } @@ -147,7 +147,7 @@ public void growExportedAmount(final int slotIndex, final long amount) { final ResourceAmount resourceAmount = this.current.get(slotIndex); this.current.put( slotIndex, - new ResourceAmount(resourceAmount.getResource(), resourceAmount.getAmount() + amount) + new ResourceAmount(resourceAmount.resource(), resourceAmount.amount() + amount) ); } diff --git a/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/ResourceAmount.java b/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/ResourceAmount.java index c815886da..bcf027bba 100644 --- a/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/ResourceAmount.java +++ b/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/ResourceAmount.java @@ -6,41 +6,15 @@ /** * A class representing a resource and a corresponding amount. - * The resource cannot be mutated but the amount can be modified. */ @API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2") -public final class ResourceAmount { - private final ResourceKey resource; - private final long amount; - +public record ResourceAmount(ResourceKey resource, long amount) { /** * @param resource the resource, must be non-null * @param amount the amount, must be larger than 0 */ - public ResourceAmount(final ResourceKey resource, final long amount) { + public ResourceAmount { validate(resource, amount); - this.resource = resource; - this.amount = amount; - } - - public ResourceKey getResource() { - return resource; - } - - public long getAmount() { - return amount; - } - - public ResourceAmount copy() { - return new ResourceAmount(resource, amount); - } - - @Override - public String toString() { - return "ResourceAmount{" - + "resource=" + resource - + ", amount=" + amount - + '}'; } public static void validate(final ResourceKey resource, final long amount) { diff --git a/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/list/ResourceList.java b/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/list/ResourceList.java index aeeb1e3d5..44f86eaf3 100644 --- a/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/list/ResourceList.java +++ b/refinedstorage-resource-api/src/main/java/com/refinedmods/refinedstorage/api/resource/list/ResourceList.java @@ -32,7 +32,7 @@ public interface ResourceList { * @return the result of the operation */ default OperationResult add(ResourceAmount resourceAmount) { - return add(resourceAmount.getResource(), resourceAmount.getAmount()); + return add(resourceAmount.resource(), resourceAmount.amount()); } /** @@ -54,7 +54,7 @@ default OperationResult add(ResourceAmount resourceAmount) { * @return a result if the removal operation was successful, otherwise an empty {@link Optional} */ default Optional remove(ResourceAmount resourceAmount) { - return remove(resourceAmount.getResource(), resourceAmount.getAmount()); + return remove(resourceAmount.resource(), resourceAmount.amount()); } /** diff --git a/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/composite/CompositeStorageImpl.java b/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/composite/CompositeStorageImpl.java index 845623a76..829ba79bd 100644 --- a/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/composite/CompositeStorageImpl.java +++ b/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/composite/CompositeStorageImpl.java @@ -211,8 +211,8 @@ private void addContentOfSourceToList(final Storage source) { private void removeContentOfSourceFromList(final Storage source) { source.getAll().forEach(resourceAmount -> list.remove( - resourceAmount.getResource(), - resourceAmount.getAmount() + resourceAmount.resource(), + resourceAmount.amount() )); } } diff --git a/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/external/ExternalStorage.java b/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/external/ExternalStorage.java index e8ce6b289..5e063794a 100644 --- a/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/external/ExternalStorage.java +++ b/refinedstorage-storage-api/src/main/java/com/refinedmods/refinedstorage/api/storage/external/ExternalStorage.java @@ -128,7 +128,7 @@ public Collection getAll() { @Override public long getStored() { - return getAll().stream().mapToLong(ResourceAmount::getAmount).sum(); + return getAll().stream().mapToLong(ResourceAmount::amount).sum(); } @Override diff --git a/refinedstorage-storage-api/src/test/java/com/refinedmods/refinedstorage/api/storage/TransferHelperTest.java b/refinedstorage-storage-api/src/test/java/com/refinedmods/refinedstorage/api/storage/TransferHelperTest.java index 633034084..d25d33057 100644 --- a/refinedstorage-storage-api/src/test/java/com/refinedmods/refinedstorage/api/storage/TransferHelperTest.java +++ b/refinedstorage-storage-api/src/test/java/com/refinedmods/refinedstorage/api/storage/TransferHelperTest.java @@ -74,8 +74,8 @@ void shouldTransferCorrectly(final Transfer transfer) { if (transfer.amountInSource != null) { source.insert( - transfer.amountInSource.getResource(), - transfer.amountInSource.getAmount(), + transfer.amountInSource.resource(), + transfer.amountInSource.amount(), Action.EXECUTE, EmptyActor.INSTANCE ); @@ -83,8 +83,8 @@ void shouldTransferCorrectly(final Transfer transfer) { if (transfer.amountInDestination != null) { destination.insert( - transfer.amountInDestination.getResource(), - transfer.amountInDestination.getAmount(), + transfer.amountInDestination.resource(), + transfer.amountInDestination.amount(), Action.EXECUTE, EmptyActor.INSTANCE ); @@ -92,8 +92,8 @@ void shouldTransferCorrectly(final Transfer transfer) { // Act final long transferred = TransferHelper.transfer( - transfer.amountToTransfer.getResource(), - transfer.amountToTransfer.getAmount(), + transfer.amountToTransfer.resource(), + transfer.amountToTransfer.amount(), EmptyActor.INSTANCE, source, destination,