2020-04-17 20:03:54 +02:00
|
|
|
package de.ellpeck.prettypipes.pipe.modules.retrieval;
|
|
|
|
|
|
|
|
import de.ellpeck.prettypipes.Registry;
|
|
|
|
import de.ellpeck.prettypipes.items.IModule;
|
|
|
|
import de.ellpeck.prettypipes.items.ModuleItem;
|
|
|
|
import de.ellpeck.prettypipes.items.ModuleTier;
|
2022-06-30 16:35:01 +02:00
|
|
|
import de.ellpeck.prettypipes.misc.DirectionSelector;
|
2020-04-17 20:03:54 +02:00
|
|
|
import de.ellpeck.prettypipes.misc.ItemFilter;
|
|
|
|
import de.ellpeck.prettypipes.network.PipeNetwork;
|
2021-12-02 14:44:26 +01:00
|
|
|
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
|
2020-05-07 15:48:46 +02:00
|
|
|
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
2022-06-30 16:00:33 +02:00
|
|
|
import net.minecraft.core.Direction;
|
2021-12-02 16:55:04 +01:00
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2022-06-30 16:00:33 +02:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
2020-04-17 20:03:54 +02:00
|
|
|
|
|
|
|
public class RetrievalModuleItem extends ModuleItem {
|
2021-12-02 16:55:04 +01:00
|
|
|
|
2020-04-17 20:03:54 +02:00
|
|
|
private final int maxExtraction;
|
|
|
|
private final int speed;
|
2020-04-17 22:29:02 +02:00
|
|
|
private final boolean preventOversending;
|
2020-04-17 20:03:54 +02:00
|
|
|
public final int filterSlots;
|
|
|
|
|
|
|
|
public RetrievalModuleItem(String name, ModuleTier tier) {
|
|
|
|
super(name);
|
|
|
|
this.maxExtraction = tier.forTier(1, 8, 16);
|
|
|
|
this.speed = tier.forTier(40, 20, 10);
|
|
|
|
this.filterSlots = tier.forTier(3, 6, 9);
|
2020-04-17 22:29:02 +02:00
|
|
|
this.preventOversending = tier.forTier(false, true, true);
|
2020-04-17 20:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public void tick(ItemStack module, PipeBlockEntity tile) {
|
2020-11-28 02:15:29 +01:00
|
|
|
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
|
2020-05-03 15:51:16 +02:00
|
|
|
return;
|
2022-07-01 00:05:11 +02:00
|
|
|
var directions = this.getDirectionSelector(module, tile).directions();
|
2022-06-30 16:35:01 +02:00
|
|
|
var network = PipeNetwork.get(tile.getLevel());
|
2021-12-02 17:46:56 +01:00
|
|
|
var equalityTypes = ItemFilter.getEqualityTypes(tile);
|
2020-11-22 19:29:04 +01:00
|
|
|
// loop through filters to see which items to pull
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var subFilter : tile.getFilters()) {
|
|
|
|
for (var f = 0; f < subFilter.getSlots(); f++) {
|
|
|
|
var filtered = subFilter.getStackInSlot(f);
|
2020-11-22 19:29:04 +01:00
|
|
|
if (filtered.isEmpty())
|
|
|
|
continue;
|
2021-12-02 17:46:56 +01:00
|
|
|
var copy = filtered.copy();
|
2020-11-22 19:29:04 +01:00
|
|
|
copy.setCount(this.maxExtraction);
|
2022-07-01 00:05:11 +02:00
|
|
|
var dest = tile.getAvailableDestination(directions, copy, true, this.preventOversending);
|
|
|
|
if (dest.getRight().isEmpty())
|
2020-11-22 19:29:04 +01:00
|
|
|
continue;
|
2022-07-01 00:05:11 +02:00
|
|
|
var remain = dest.getRight().copy();
|
2020-11-22 19:29:04 +01:00
|
|
|
// are we already waiting for crafting results? If so, don't request those again
|
2021-12-02 16:55:04 +01:00
|
|
|
remain.shrink(network.getCurrentlyCraftingAmount(tile.getBlockPos(), copy, equalityTypes));
|
2022-07-01 00:05:11 +02:00
|
|
|
if (network.requestItem(tile.getBlockPos(), dest.getLeft(), remain, equalityTypes).isEmpty())
|
2020-11-22 19:29:04 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-04-17 20:03:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:29:02 +02:00
|
|
|
@Override
|
2022-06-30 16:00:33 +02:00
|
|
|
public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) {
|
2022-07-01 00:05:11 +02:00
|
|
|
return !this.getDirectionSelector(module, tile).has(direction);
|
2020-04-17 22:29:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 20:03:54 +02:00
|
|
|
@Override
|
2022-06-30 16:00:33 +02:00
|
|
|
public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) {
|
2022-07-01 00:05:11 +02:00
|
|
|
return !this.getDirectionSelector(module, tile).has(direction);
|
2020-04-17 20:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
|
2020-04-17 20:03:54 +02:00
|
|
|
return !(other instanceof RetrievalModuleItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
|
2020-04-17 20:03:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 16:55:04 +01:00
|
|
|
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
|
|
|
|
return new RetrievalModuleContainer(Registry.retrievalModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
|
2020-04-17 20:03:54 +02:00
|
|
|
}
|
2021-03-03 00:10:24 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-02 14:44:26 +01:00
|
|
|
public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
|
2021-12-02 17:46:56 +01:00
|
|
|
var filter = new ItemFilter(this.filterSlots, module, tile);
|
2021-03-03 00:10:24 +01:00
|
|
|
filter.canModifyWhitelist = false;
|
|
|
|
filter.isWhitelist = true;
|
|
|
|
return filter;
|
|
|
|
}
|
2022-06-30 16:35:01 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) {
|
|
|
|
return new DirectionSelector(module, tile);
|
|
|
|
}
|
2020-04-17 20:03:54 +02:00
|
|
|
}
|