added random and round robin sorting modifiers

Closes #52
This commit is contained in:
Ell 2020-12-04 19:32:16 +01:00
parent 06561fe278
commit 0255340626
13 changed files with 130 additions and 7 deletions

View file

@ -152,6 +152,7 @@ public final class Registry {
registry.register(new RedstoneModuleItem("redstone_module"));
registry.register(new FilterIncreaseModuleItem("filter_increase_modifier"));
registry.registerAll(createTieredModule("crafting_module", CraftingModuleItem::new));
registry.registerAll(Arrays.stream(SortingModuleItem.Type.values()).map(t -> new SortingModuleItem(t.name().toLowerCase(Locale.ROOT) + "_sorting_modifier", t)).toArray(Item[]::new));
ForgeRegistries.BLOCKS.getValues().stream()
.filter(b -> b.getRegistryName().getNamespace().equals(PrettyPipes.ID))

View file

@ -1,8 +1,7 @@
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
@ -11,7 +10,6 @@ import net.minecraftforge.items.IItemHandler;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public interface IModule {
@ -40,4 +38,6 @@ public interface IModule {
int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack);
ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack);
Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index);
}

View file

@ -2,16 +2,15 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@ -20,6 +19,7 @@ import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
public abstract class ModuleItem extends Item implements IModule {
@ -92,4 +92,9 @@ public abstract class ModuleItem extends Item implements IModule {
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack) {
return stack;
}
@Override
public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) {
return null;
}
}

View file

@ -144,7 +144,9 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
if (startPipe == null)
return stack;
this.startProfile("find_destination");
for (BlockPos pipePos : this.getOrderedNetworkNodes(startPipePos)) {
List<BlockPos> nodes = this.getOrderedNetworkNodes(startPipePos);
for (int i = 0; i < nodes.size(); i++) {
BlockPos pipePos = nodes.get(startPipe.getNextNode(nodes, i));
if (!this.world.isBlockLoaded(pipePos))
continue;
PipeTileEntity pipe = this.getPipe(pipePos);

View file

@ -390,6 +390,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
return (this.world.getGameTime() + this.workRandomizer.get()) % speed == 0;
}
public int getNextNode(List<BlockPos> nodes, int index) {
return this.streamModules()
.map(m -> m.getRight().getCustomNextNode(m.getLeft(), this, nodes, index))
.filter(Objects::nonNull).findFirst().orElse(index);
}
@Override
public void remove() {
super.remove();

View file

@ -0,0 +1,49 @@
package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import java.util.List;
public class SortingModuleItem extends ModuleItem {
private final Type type;
public SortingModuleItem(String name, Type type) {
super(name);
this.type = type;
this.setRegistryName(name);
}
@Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) {
return !(other instanceof SortingModuleItem);
}
@Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) {
return false;
}
@Override
public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) {
switch (this.type) {
case ROUND_ROBIN:
// store an ever-increasing index and choose destinations based on that
int next = module.hasTag() ? module.getTag().getInt("last") + 1 : 0;
module.getOrCreateTag().putInt("last", next);
return next % nodes.size();
case RANDOM:
return tile.getWorld().rand.nextInt(nodes.size());
}
return null;
}
public enum Type {
ROUND_ROBIN,
RANDOM
}
}

View file

@ -32,6 +32,8 @@
"item.prettypipes.mod_filter_modifier": "Mod Filter Modifier",
"item.prettypipes.redstone_module": "Redstone Module",
"item.prettypipes.filter_increase_modifier": "Filter Increase Modifier",
"item.prettypipes.random_sorting_modifier": "Random Sorting Modifier",
"item.prettypipes.round_robin_sorting_modifier": "Round Robin Sorting Modifier",
"info.prettypipes.extraction_module": "Pulls items from adjacent inventories\nFilters and pull rates vary by tier\nHigh tiers prevent over-sending",
"info.prettypipes.filter_module": "Restricts flow from pipes into adjacent inventories\nFilter amount varies by tier",
"info.prettypipes.speed_module": "Increases speed of items exiting adjacent inventories\nSpeed varies by tier",
@ -49,6 +51,8 @@
"info.prettypipes.pressurizer": "Drastically increases item speed in the entire pipe network\nRequires FE (or RF) for each item transferred",
"info.prettypipes.filter_increase_modifier": "Adds additional filter slots to the pipe\nSlots will be used by any module that filters items",
"info.prettypipes.crafting_module": "Allows automatically crafting items into other items using the connected block\nDoesn't automatically extract from the block\nInput and output slots vary by tier\nSupports auto-filling from JEI if installed",
"info.prettypipes.random_sorting_modifier": "Causes extracted items to choose destinations randomly\nPriority modules will be ignored",
"info.prettypipes.round_robin_sorting_modifier": "Causes extracted items to choose destinations in a round robin fashion\nOrdering is based on distance and priority",
"block.prettypipes.pipe": "Pipe",
"block.prettypipes.item_terminal": "Item Terminal",
"block.prettypipes.crafting_terminal": "Crafting Terminal",

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "prettypipes:item/random_sorting_modifier"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "prettypipes:item/round_robin_sorting_modifier"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 B

View file

@ -0,0 +1,22 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
" H ",
"RMR",
" R "
],
"key": {
"R": {
"item": "minecraft:redstone"
},
"H": {
"item": "minecraft:dispenser"
},
"M": {
"item": "prettypipes:blank_module"
}
},
"result": {
"item": "prettypipes:random_sorting_modifier"
}
}

View file

@ -0,0 +1,22 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
" H ",
"RMR",
" R "
],
"key": {
"R": {
"item": "minecraft:redstone"
},
"H": {
"item": "minecraft:arrow"
},
"M": {
"item": "prettypipes:blank_module"
}
},
"result": {
"item": "prettypipes:round_robin_sorting_modifier"
}
}