Skip to content

Commit

Permalink
Return insertion remainder in compat item storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Oct 4, 2023
1 parent 22f4bbd commit 4c6d1bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ public static FluidVariant toFluidStorageView(FluidStack stack) {
return !stack.isEmpty() ? FluidVariant.of(stack.getFluid(), stack.getTag()) : FluidVariant.blank();
}

private ForgeCompatUtil() {
}
private ForgeCompatUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Iterator;
import java.util.List;

import com.google.common.primitives.Ints;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import org.jetbrains.annotations.NotNull;
Expand All @@ -40,21 +41,23 @@ public ForgeItemStorage(IItemHandler handler) {

@Override
public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) {
int inserted = ItemHandlerHelper.insertItem(this.handler, resource.toStack((int) maxAmount), true).getCount();
int normalMaxAmount = Ints.saturatedCast(maxAmount);
int inserted = ItemHandlerHelper.insertItem(this.handler, resource.toStack(normalMaxAmount), true).getCount();
transaction.addCloseCallback((context, result) -> {
if (result.wasCommitted()) {
ItemHandlerHelper.insertItem(this.handler, resource.toStack((int) maxAmount), false);
ItemHandlerHelper.insertItem(this.handler, resource.toStack(normalMaxAmount), false);
}
});
return inserted;
return normalMaxAmount - inserted;
}

@Override
public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) {
int extracted = extractItem(this.handler, resource, (int) maxAmount, true).getCount();
int normalMaxAmount = Ints.saturatedCast(maxAmount);
int extracted = extractItem(this.handler, resource, normalMaxAmount, true).getCount();
transaction.addCloseCallback((context, result) -> {
if (result.wasCommitted()) {
extractItem(this.handler, resource, (int) maxAmount, false);
extractItem(this.handler, resource, normalMaxAmount, false);
}
});
return extracted;
Expand All @@ -63,8 +66,8 @@ public long extract(ItemVariant resource, long maxAmount, TransactionContext tra
@Override
public Iterator<StorageView<ItemVariant>> iterator() {
List<StorageView<ItemVariant>> views = new ArrayList<>();
for (int i = 0; i < handler.getSlots(); i++) {
views.add(new ForgeItemView(handler, i));
for (int i = 0; i < this.handler.getSlots(); i++) {
views.add(new ForgeItemView(this.handler, i));
}
return views.iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public int getSlots() {
if (!simulate) {
transaction.commit();
}
return resource.toStack(inserted);
int remainder = stack.getCount() - inserted;
return resource.toStack(remainder);
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ fabric-client-tags-api-v1-version=1.1.1
loom.platform=forge
forge_version=1.20.1-47.1.3
pack_format=15
forgified_version=1.9.23
forgified_version=1.9.24
forge_fabric_loader_version=2.3.4+0.14.21+1.20.1

0 comments on commit 4c6d1bc

Please sign in to comment.