Skip to content

Commit

Permalink
Output oversized stacks instead of splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Aug 27, 2022
1 parent 5508fca commit 8b3e88b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ org.gradle.daemon=false
mc_version=1.18.2
group=net.permutated
mod_id=exmachinis
version=1.0.0
version=1.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static List<ItemStack> getHammerResult(ItemStack stack) {
.map(ExNihiloRegistries.HAMMER_REGISTRY::getResult)
.orElseGet(Collections::emptyList)
.stream()
.filter(chance -> ThreadLocalRandom.current().nextFloat() <= chance.getChance())
.filter(chance -> chance.getChance() == 1.0 || ThreadLocalRandom.current().nextFloat() <= chance.getChance())
.map(chance -> chance.getStack().copy())
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import net.permutated.exmachinis.recipes.CompactingRecipe;
import net.permutated.exmachinis.util.WorkStatus;

import java.util.List;

import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStack;
import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStackCount;

public class FluxCompactorTile extends AbstractMachineTile {
public FluxCompactorTile(BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -138,12 +136,10 @@ public void tick() {

private boolean processResults(IItemHandler itemHandler, CompactingRecipe recipe, int multiplier, boolean simulate) {
// process compacting results
List<ItemStack> multiplied = multiplyStack(recipe.getOutput(), multiplier);
for (var output : multiplied) {
var response = ItemHandlerHelper.insertItemStacked(itemHandler, output, simulate);
if (!response.isEmpty()) {
workStatus = WorkStatus.INVENTORY_FULL;
}
ItemStack output = multiplyStackCount(recipe.getOutput(), multiplier);
var response = ItemHandlerHelper.insertItemStacked(itemHandler, output, simulate);
if (!response.isEmpty()) {
workStatus = WorkStatus.INVENTORY_FULL;
}
return workStatus == WorkStatus.WORKING;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.permutated.exmachinis.ModRegistry;
import net.permutated.exmachinis.compat.exnihilo.ExNihiloAPI;
import net.permutated.exmachinis.machines.base.AbstractMachineBlock;
import net.permutated.exmachinis.machines.base.AbstractMachineTile;
import net.permutated.exmachinis.compat.exnihilo.ExNihiloAPI;
import net.permutated.exmachinis.util.WorkStatus;

import java.util.List;

import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStack;
import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStackCount;

public class FluxHammerTile extends AbstractMachineTile {
public FluxHammerTile(BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -123,8 +121,7 @@ public void tick() {
private boolean processResults(IItemHandler itemHandler, ItemStack stack, int multiplier, boolean simulate) {
// process hammer results
ExNihiloAPI.getHammerResult(stack).stream()
.map(result -> multiplyStack(result, multiplier))
.flatMap(List::stream)
.map(result -> multiplyStackCount(result, multiplier))
.map(output -> ItemHandlerHelper.insertItemStacked(itemHandler, output, simulate))
.forEach(response -> {
if (!response.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.permutated.exmachinis.ModRegistry;
import net.permutated.exmachinis.compat.exnihilo.ExNihiloAPI;
import net.permutated.exmachinis.machines.base.AbstractMachineBlock;
import net.permutated.exmachinis.machines.base.AbstractMachineTile;
import net.permutated.exmachinis.compat.exnihilo.ExNihiloAPI;
import net.permutated.exmachinis.util.WorkStatus;

import java.util.List;

import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStack;
import static net.permutated.exmachinis.util.ItemStackUtil.multiplyStackCount;

public class FluxSieveTile extends AbstractMachineTile {
public FluxSieveTile(BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -135,8 +133,7 @@ public void tick() {
private boolean processResults(IItemHandler itemHandler, ItemStack meshStack, ItemStack stack, int multiplier, boolean simulate) {
// process sieve results
ExNihiloAPI.getSieveResult(stack, meshStack, isWaterlogged()).stream()
.map(result -> multiplyStack(result, multiplier))
.flatMap(List::stream)
.map(result -> multiplyStackCount(result, multiplier))
.map(output -> ItemHandlerHelper.insertItemStacked(itemHandler, output, simulate))
.forEach(response -> {
if (!response.isEmpty()) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/permutated/exmachinis/util/ItemStackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public static List<ItemStack> multiplyStack(ItemStack input, int times) {
output.add(copyStackWithSize(input, amount));
return output;
}

/**
* Multiply input stack, returning a single ItemStack that can be over the maxStackSize.
* While not explicitly allowed, most ItemHandlers should safely insert oversized stacks.
* @param input the input stack to copy.
* @param times the multiplier
* @return the multiplied ItemStack
*/
public static ItemStack multiplyStackCount(ItemStack input, int times) {
int count = input.getCount() * times;
return copyStackWithSize(input, count);
}
}

0 comments on commit 8b3e88b

Please sign in to comment.