|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2024 Team Galacticraft |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package dev.galacticraft.machinelib.api.item; |
| 24 | + |
| 25 | +import com.google.common.collect.Iterators; |
| 26 | +import dev.galacticraft.machinelib.api.filter.ResourceFilter; |
| 27 | +import dev.galacticraft.machinelib.api.filter.ResourceFilters; |
| 28 | +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; |
| 29 | +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; |
| 30 | +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; |
| 31 | +import net.fabricmc.fabric.api.transfer.v1.storage.StorageView; |
| 32 | +import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant; |
| 33 | +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; |
| 34 | +import net.minecraft.world.item.ItemStack; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | + |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.Iterator; |
| 39 | + |
| 40 | +public abstract class GenericItemBackedStorage<T, V extends TransferVariant<T>> implements Storage<V>, StorageView<V> { |
| 41 | + protected final ItemVariant item; |
| 42 | + protected final ContainerItemContext context; |
| 43 | + protected final ResourceFilter<T> filter; |
| 44 | + |
| 45 | + protected GenericItemBackedStorage(ItemStack stack, ContainerItemContext context, ResourceFilter<T> filter) { |
| 46 | + this.item = ItemVariant.of(stack); |
| 47 | + this.context = context; |
| 48 | + this.filter = filter; |
| 49 | + } |
| 50 | + |
| 51 | + protected GenericItemBackedStorage(ItemStack stack, ContainerItemContext context) { |
| 52 | + this(stack, context, ResourceFilters.any()); |
| 53 | + } |
| 54 | + |
| 55 | + protected abstract long getRawMaxInput(); |
| 56 | + protected abstract long getRawMaxOutput(); |
| 57 | + protected abstract long getRawCapacity(); |
| 58 | + protected abstract long getRawAmount(); |
| 59 | + protected abstract boolean setRawContents(V variant, long amount, TransactionContext transaction); |
| 60 | + |
| 61 | + @Override |
| 62 | + public long insert(V resource, long maxAmount, TransactionContext transaction) { |
| 63 | + if (!this.item.equals(this.context.getItemVariant())) return 0; |
| 64 | + if (this.filter.test(resource.getObject(), resource.getComponents()) && (this.getResource().isBlank() || this.getResource().equals(resource))) { |
| 65 | + long stored = this.getRawAmount(); |
| 66 | + maxAmount = Math.min(this.getRawCapacity() - stored, maxAmount / this.context.getAmount()); |
| 67 | + maxAmount = Math.min(maxAmount, this.getRawMaxInput()); |
| 68 | + if (this.setRawContents(resource, stored + maxAmount, transaction)) { |
| 69 | + return maxAmount * this.context.getAmount(); |
| 70 | + } |
| 71 | + } |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public long extract(V resource, long maxAmount, TransactionContext transaction) { |
| 77 | + assert !resource.isBlank(); |
| 78 | + if (!this.item.equals(this.context.getItemVariant())) return 0; |
| 79 | + if (this.getResource().equals(resource)) { |
| 80 | + long stored = this.getRawAmount(); |
| 81 | + maxAmount = Math.min(stored, maxAmount / this.context.getAmount()); |
| 82 | + maxAmount = Math.min(maxAmount, this.getRawMaxOutput()); |
| 83 | + if (this.setRawContents(resource, stored - maxAmount, transaction)) { |
| 84 | + return maxAmount * this.context.getAmount(); |
| 85 | + } |
| 86 | + } |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isResourceBlank() { |
| 92 | + return this.getResource().isBlank(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public long getAmount() { |
| 97 | + if (!this.item.equals(this.context.getItemVariant())) return 0; |
| 98 | + return this.getRawAmount() * this.context.getAmount(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public long getCapacity() { |
| 103 | + if (!this.item.equals(this.context.getItemVariant())) return 0; |
| 104 | + return this.getRawCapacity() * this.context.getAmount(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public @NotNull Iterator<StorageView<V>> iterator() { |
| 109 | + if (!this.item.equals(this.context.getItemVariant())) return Collections.emptyIterator(); |
| 110 | + return Iterators.singletonIterator(this); |
| 111 | + } |
| 112 | +} |
0 commit comments