fixed message propagation for missing items

This commit is contained in:
Ell 2020-10-15 04:46:23 +02:00
parent f38ab16e4d
commit 4340b2ccb4
8 changed files with 40 additions and 27 deletions

View file

@ -10,6 +10,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
public interface IModule { public interface IModule {
@ -36,7 +37,7 @@ public interface IModule {
List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile); List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile);
int getCraftableAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, ItemEqualityType... equalityTypes); int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes);
ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, ItemStack stack, ItemEqualityType... equalityTypes); ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes);
} }

View file

@ -20,6 +20,7 @@ import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
public abstract class ModuleItem extends Item implements IModule { public abstract class ModuleItem extends Item implements IModule {
@ -83,12 +84,12 @@ public abstract class ModuleItem extends Item implements IModule {
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, ItemEqualityType... equalityTypes) { public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
return 0; return 0;
} }
@Override @Override
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, ItemStack stack, ItemEqualityType... equalityTypes) { public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
return stack; return stack;
} }
} }

View file

@ -43,6 +43,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -193,17 +194,17 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return remain; return remain;
} }
// check craftable items // check craftable items
return this.requestCraftedItem(destPipe, remain, equalityTypes); return this.requestCraftedItem(destPipe, null, remain, equalityTypes);
} }
public ItemStack requestCraftedItem(BlockPos destPipe, ItemStack stack, ItemEqualityType... equalityTypes) { public ItemStack requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
for (Pair<BlockPos, ItemStack> craftable : this.getAllCraftables(destPipe)) { for (Pair<BlockPos, ItemStack> craftable : this.getAllCraftables(destPipe)) {
if (!ItemEqualityType.compareItems(stack, craftable.getRight(), equalityTypes)) if (!ItemEqualityType.compareItems(stack, craftable.getRight(), equalityTypes))
continue; continue;
PipeTileEntity pipe = this.getPipe(craftable.getLeft()); PipeTileEntity pipe = this.getPipe(craftable.getLeft());
if (pipe == null) if (pipe == null)
continue; continue;
stack = pipe.craft(destPipe, stack, equalityTypes); stack = pipe.craft(destPipe, unavailableConsumer, stack, equalityTypes);
if (stack.isEmpty()) if (stack.isEmpty())
break; break;
} }
@ -264,7 +265,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return craftables; return craftables;
} }
public int getCraftableAmount(BlockPos node, ItemStack stack, ItemEqualityType... equalityTypes) { public int getCraftableAmount(BlockPos node, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
int total = 0; int total = 0;
for (Pair<BlockPos, ItemStack> pair : this.getAllCraftables(node)) { for (Pair<BlockPos, ItemStack> pair : this.getAllCraftables(node)) {
if (!ItemEqualityType.compareItems(pair.getRight(), stack, equalityTypes)) if (!ItemEqualityType.compareItems(pair.getRight(), stack, equalityTypes))
@ -273,7 +274,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
continue; continue;
PipeTileEntity pipe = this.getPipe(pair.getLeft()); PipeTileEntity pipe = this.getPipe(pair.getLeft());
if (pipe != null) if (pipe != null)
total += pipe.getCraftableAmount(stack, equalityTypes); total += pipe.getCraftableAmount(unavailableConsumer, stack, equalityTypes);
} }
return total; return total;
} }

View file

@ -42,6 +42,7 @@ import org.apache.commons.lang3.tuple.Triple;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -264,17 +265,17 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public int getCraftableAmount(ItemStack stack, ItemEqualityType... equalityTypes) { public int getCraftableAmount(Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
return this.streamModules() return this.streamModules()
.mapToInt(m -> m.getRight().getCraftableAmount(m.getLeft(), this, stack, equalityTypes)) .mapToInt(m -> m.getRight().getCraftableAmount(m.getLeft(), this, unavailableConsumer, stack, equalityTypes))
.sum(); .sum();
} }
public ItemStack craft(BlockPos destPipe, ItemStack stack, ItemEqualityType... equalityTypes) { public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator(); Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator();
while (modules.hasNext()) { while (modules.hasNext()) {
Pair<ItemStack, IModule> module = modules.next(); Pair<ItemStack, IModule> module = modules.next();
stack = module.getRight().craft(module.getLeft(), this, destPipe, stack, equalityTypes); stack = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, equalityTypes);
if (stack.isEmpty()) if (stack.isEmpty())
break; break;
} }

View file

@ -32,6 +32,7 @@ import org.apache.commons.lang3.tuple.Triple;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
public class CraftingModuleItem extends ModuleItem { public class CraftingModuleItem extends ModuleItem {
@ -118,6 +119,8 @@ public class CraftingModuleItem extends ModuleItem {
} }
} }
} }
if (dest == null)
continue;
for (NetworkLocation item : items) { for (NetworkLocation item : items) {
ItemStack requestRemain = network.requestExistingItem(item, request.getLeft(), dest.getLeft(), null, dest.getRight(), equalityTypes); ItemStack requestRemain = network.requestExistingItem(item, request.getLeft(), dest.getLeft(), null, dest.getRight(), equalityTypes);
remain.shrink(dest.getRight().getCount() - requestRemain.getCount()); remain.shrink(dest.getRight().getCount() - requestRemain.getCount());
@ -149,7 +152,7 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, ItemEqualityType... equalityTypes) { public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos()); List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos());
ItemStackHandler input = this.getInput(module); ItemStackHandler input = this.getInput(module);
@ -160,7 +163,7 @@ public class CraftingModuleItem extends ModuleItem {
ItemStack out = output.getStackInSlot(i); ItemStack out = output.getStackInSlot(i);
if (!out.isEmpty() && ItemEqualityType.compareItems(out, stack, equalityTypes)) { if (!out.isEmpty() && ItemEqualityType.compareItems(out, stack, equalityTypes)) {
// figure out how many crafting operations we can actually do with the input items we have in the network // figure out how many crafting operations we can actually do with the input items we have in the network
int availableCrafts = CraftingTerminalTileEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, null, equalityTypes); int availableCrafts = CraftingTerminalTileEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, equalityTypes);
if (availableCrafts > 0) if (availableCrafts > 0)
craftable += out.getCount() * availableCrafts; craftable += out.getCount() * availableCrafts;
} }
@ -169,9 +172,9 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, ItemStack stack, ItemEqualityType... equalityTypes) { public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
// check if we can craft the required amount of items // check if we can craft the required amount of items
int craftableAmount = this.getCraftableAmount(module, tile, stack, equalityTypes); int craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack, equalityTypes);
if (craftableAmount <= 0) if (craftableAmount <= 0)
return stack; return stack;
@ -189,7 +192,7 @@ public class CraftingModuleItem extends ModuleItem {
continue; continue;
ItemStack copy = in.copy(); ItemStack copy = in.copy();
copy.setCount(in.getCount() * toCraft); copy.setCount(in.getCount() * toCraft);
Pair<List<NetworkLock>, ItemStack> ret = ItemTerminalTileEntity.requestItemLater(tile.getWorld(), tile.getPos(), copy, items, equalityTypes); Pair<List<NetworkLock>, ItemStack> ret = ItemTerminalTileEntity.requestItemLater(tile.getWorld(), tile.getPos(), items, unavailableConsumer, copy, equalityTypes);
tile.craftIngredientRequests.addAll(ret.getLeft()); tile.craftIngredientRequests.addAll(ret.getLeft());
} }

View file

@ -27,6 +27,7 @@ public class CraftingTerminalBlock extends ItemTerminalBlock {
BlockPos pos = pipePos.offset(direction); BlockPos pos = pipePos.offset(direction);
CraftingTerminalTileEntity tile = Utility.getTileEntity(CraftingTerminalTileEntity.class, world, pos); CraftingTerminalTileEntity tile = Utility.getTileEntity(CraftingTerminalTileEntity.class, world, pos);
if (tile != null) { if (tile != null) {
remain = remain.copy();
int lowestSlot = -1; int lowestSlot = -1;
do { do {
for (int i = 0; i < tile.craftItems.getSlots(); i++) { for (int i = 0; i < tile.craftItems.getSlots(); i++) {

View file

@ -111,7 +111,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
int lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), this::getRequestedCraftItem, this::isGhostItem, s -> { int lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), this::getRequestedCraftItem, this::isGhostItem, s -> {
NetworkItem item = this.networkItems.get(s); NetworkItem item = this.networkItems.get(s);
return item != null ? item.getLocations() : Collections.emptyList(); return item != null ? item.getLocations() : Collections.emptyList();
}, s -> player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".not_found", s.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.RED)), UUID.randomUUID()), ItemEqualityType.NBT); }, onItemUnavailable(player), ItemEqualityType.NBT);
if (lowestAvailable > 0) { if (lowestAvailable > 0) {
// if we're limiting the amount, pretend we only have that amount available // if we're limiting the amount, pretend we only have that amount available
if (maxAmount < lowestAvailable) if (maxAmount < lowestAvailable)
@ -122,7 +122,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
continue; continue;
requested = requested.copy(); requested = requested.copy();
requested.setCount(lowestAvailable); requested.setCount(lowestAvailable);
this.requestItemImpl(requested); this.requestItemImpl(requested, onItemUnavailable(player));
} }
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending_ingredients", lowestAvailable).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID()); player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending_ingredients", lowestAvailable).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID());
} }
@ -186,7 +186,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
// check how many craftable items we have and add those on if we need to // check how many craftable items we have and add those on if we need to
if (available < lowestAvailable) { if (available < lowestAvailable) {
int craftable = network.getCraftableAmount(tile.getPos(), stack.stack, equalityTypes); int craftable = network.getCraftableAmount(tile.getPos(),unavailableConsumer, stack.stack, equalityTypes);
if (craftable > 0) if (craftable > 0)
available += craftable / entry.getValue().intValue(); available += craftable / entry.getValue().intValue();
} }

View file

@ -41,6 +41,7 @@ import org.apache.commons.lang3.tuple.Triple;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -142,19 +143,19 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
network.startProfile("terminal_request_item"); network.startProfile("terminal_request_item");
this.updateItems(); this.updateItems();
int requested = this.requestItemImpl(stack); int requested = this.requestItemImpl(stack, onItemUnavailable(player));
if (requested > 0) { if (requested > 0) {
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID()); player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID());
} else { } else {
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".not_found", stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.RED)), UUID.randomUUID()); onItemUnavailable(player).accept(stack);
} }
network.endProfile(); network.endProfile();
} }
public int requestItemImpl(ItemStack stack) { public int requestItemImpl(ItemStack stack, Consumer<ItemStack> unavailableConsumer) {
NetworkItem item = this.networkItems.get(new EquatableItemStack(stack)); NetworkItem item = this.networkItems.get(new EquatableItemStack(stack));
Collection<NetworkLocation> locations = item == null ? Collections.emptyList() : item.getLocations(); Collection<NetworkLocation> locations = item == null ? Collections.emptyList() : item.getLocations();
Pair<List<NetworkLock>, ItemStack> ret = requestItemLater(this.world, this.getConnectedPipe().getPos(), stack, locations, ItemEqualityType.NBT); Pair<List<NetworkLock>, ItemStack> ret = requestItemLater(this.world, this.getConnectedPipe().getPos(), locations, unavailableConsumer, stack, ItemEqualityType.NBT);
this.existingRequests.addAll(ret.getLeft()); this.existingRequests.addAll(ret.getLeft());
return stack.getCount() - ret.getRight().getCount(); return stack.getCount() - ret.getRight().getCount();
} }
@ -208,7 +209,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return new ItemTerminalContainer(Registry.itemTerminalContainer, window, player, this.pos); return new ItemTerminalContainer(Registry.itemTerminalContainer, window, player, this.pos);
} }
public static Pair<List<NetworkLock>, ItemStack> requestItemLater(World world, BlockPos destPipe, ItemStack stack, Collection<NetworkLocation> locations, ItemEqualityType... equalityTypes) { public static Pair<List<NetworkLock>, ItemStack> requestItemLater(World world, BlockPos destPipe, Collection<NetworkLocation> locations, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) {
List<NetworkLock> requests = new ArrayList<>(); List<NetworkLock> requests = new ArrayList<>();
ItemStack remain = stack.copy(); ItemStack remain = stack.copy();
PipeNetwork network = PipeNetwork.get(world); PipeNetwork network = PipeNetwork.get(world);
@ -236,7 +237,11 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
// check for craftable items // check for craftable items
if (!remain.isEmpty()) if (!remain.isEmpty())
remain = network.requestCraftedItem(destPipe, remain, equalityTypes); remain = network.requestCraftedItem(destPipe, unavailableConsumer, remain, equalityTypes);
return Pair.of(requests, remain); return Pair.of(requests, remain);
} }
public static Consumer<ItemStack> onItemUnavailable(PlayerEntity player) {
return s -> player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".not_found", s.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.RED)), UUID.randomUUID());
}
} }