From 3451451c4f241f20d10a818ff6e30e50e4512914 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 30 Jun 2022 16:00:33 +0200 Subject: [PATCH 1/5] added direction selector system --- .../java/de/ellpeck/prettypipes/Registry.java | 92 ++++++++-------- .../prettypipes/entities/PipeFrameEntity.java | 14 +-- .../de/ellpeck/prettypipes/items/IModule.java | 8 +- .../ellpeck/prettypipes/items/ModuleItem.java | 11 +- .../prettypipes/misc/DirectionSelector.java | 104 ++++++++++++++++++ .../ellpeck/prettypipes/misc/ItemFilter.java | 22 ++-- .../ellpeck/prettypipes/misc/ItemOrder.java | 2 +- .../prettypipes/misc/ItemTerminalWidget.java | 3 +- .../ellpeck/prettypipes/misc/PlayerPrefs.java | 16 +-- .../prettypipes/network/NetworkLocation.java | 2 +- .../ellpeck/prettypipes/network/PipeItem.java | 6 +- .../prettypipes/network/PipeNetwork.java | 4 +- .../prettypipes/packets/PacketButton.java | 6 + .../packets/PacketCraftingModuleTransfer.java | 4 +- .../prettypipes/packets/PacketGhostSlot.java | 2 +- .../prettypipes/packets/PacketHandler.java | 20 ++-- .../ellpeck/prettypipes/pipe/IPipeItem.java | 2 +- .../ellpeck/prettypipes/pipe/PipeBlock.java | 48 ++++---- .../prettypipes/pipe/PipeBlockEntity.java | 14 +-- .../pipe/containers/AbstractPipeGui.java | 4 +- .../pipe/modules/craft/CraftingModuleGui.java | 2 +- .../modules/craft/CraftingModuleItem.java | 10 +- .../extraction/ExtractionModuleContainer.java | 13 ++- .../extraction/ExtractionModuleGui.java | 3 +- .../extraction/ExtractionModuleItem.java | 44 +++++--- .../modules/insertion/FilterModuleGui.java | 3 +- .../modules/insertion/FilterModuleItem.java | 4 +- .../modifier/FilterModifierModuleGui.java | 2 +- .../modifier/FilterModifierModuleItem.java | 2 +- .../modules/retrieval/RetrievalModuleGui.java | 3 +- .../retrieval/RetrievalModuleItem.java | 9 +- .../stacksize/StackSizeModuleItem.java | 4 +- .../pressurizer/PressurizerBlock.java | 2 +- .../pressurizer/PressurizerGui.java | 2 +- .../terminal/CraftingTerminalBlock.java | 3 +- .../terminal/CraftingTerminalBlockEntity.java | 6 +- .../terminal/ItemTerminalBlock.java | 2 +- .../terminal/ItemTerminalBlockEntity.java | 6 +- .../containers/CraftingTerminalGui.java | 7 +- .../terminal/containers/ItemTerminalGui.java | 13 ++- .../assets/prettypipes/lang/en_us.json | 19 +++- 41 files changed, 348 insertions(+), 195 deletions(-) create mode 100644 src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java diff --git a/src/main/java/de/ellpeck/prettypipes/Registry.java b/src/main/java/de/ellpeck/prettypipes/Registry.java index 6c36e3e..0621c97 100644 --- a/src/main/java/de/ellpeck/prettypipes/Registry.java +++ b/src/main/java/de/ellpeck/prettypipes/Registry.java @@ -86,7 +86,7 @@ public final class Registry { public static final CreativeModeTab TAB = new CreativeModeTab(PrettyPipes.ID) { @Override public ItemStack makeIcon() { - return new ItemStack(wrenchItem); + return new ItemStack(Registry.wrenchItem); } }; @@ -127,10 +127,10 @@ public final class Registry { @SubscribeEvent public static void registerBlocks(RegistryEvent.Register event) { event.getRegistry().registerAll( - pipeBlock = new PipeBlock().setRegistryName("pipe"), - itemTerminalBlock = new ItemTerminalBlock().setRegistryName("item_terminal"), - craftingTerminalBlock = new CraftingTerminalBlock().setRegistryName("crafting_terminal"), - pressurizerBlock = new PressurizerBlock().setRegistryName("pressurizer") + Registry.pipeBlock = new PipeBlock().setRegistryName("pipe"), + Registry.itemTerminalBlock = new ItemTerminalBlock().setRegistryName("item_terminal"), + Registry.craftingTerminalBlock = new CraftingTerminalBlock().setRegistryName("crafting_terminal"), + Registry.pressurizerBlock = new PressurizerBlock().setRegistryName("pressurizer") ); } @@ -138,59 +138,59 @@ public final class Registry { public static void registerItems(RegistryEvent.Register event) { var registry = event.getRegistry(); registry.registerAll( - wrenchItem = new WrenchItem().setRegistryName("wrench"), - new Item(new Item.Properties().tab(TAB)).setRegistryName("blank_module"), - pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame") + Registry.wrenchItem = new WrenchItem().setRegistryName("wrench"), + new Item(new Item.Properties().tab(Registry.TAB)).setRegistryName("blank_module"), + Registry.pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame") ); - registry.registerAll(createTieredModule("extraction_module", ExtractionModuleItem::new)); - registry.registerAll(createTieredModule("filter_module", FilterModuleItem::new)); - registry.registerAll(createTieredModule("speed_module", SpeedModuleItem::new)); - registry.registerAll(createTieredModule("low_priority_module", LowPriorityModuleItem::new)); - registry.registerAll(createTieredModule("high_priority_module", HighPriorityModuleItem::new)); - registry.registerAll(createTieredModule("retrieval_module", RetrievalModuleItem::new)); + registry.registerAll(Registry.createTieredModule("extraction_module", ExtractionModuleItem::new)); + registry.registerAll(Registry.createTieredModule("filter_module", FilterModuleItem::new)); + registry.registerAll(Registry.createTieredModule("speed_module", SpeedModuleItem::new)); + registry.registerAll(Registry.createTieredModule("low_priority_module", LowPriorityModuleItem::new)); + registry.registerAll(Registry.createTieredModule("high_priority_module", HighPriorityModuleItem::new)); + registry.registerAll(Registry.createTieredModule("retrieval_module", RetrievalModuleItem::new)); registry.register(new StackSizeModuleItem("stack_size_module")); registry.registerAll(Arrays.stream(ItemEquality.Type.values()).map(t -> new FilterModifierModuleItem(t.name().toLowerCase(Locale.ROOT) + "_filter_modifier", t)).toArray(Item[]::new)); registry.register(new RedstoneModuleItem("redstone_module")); registry.register(new FilterIncreaseModuleItem("filter_increase_modifier")); - registry.registerAll(createTieredModule("crafting_module", CraftingModuleItem::new)); + registry.registerAll(Registry.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)) - .forEach(b -> registry.register(new BlockItem(b, new Item.Properties().tab(TAB)).setRegistryName(b.getRegistryName()))); + .forEach(b -> registry.register(new BlockItem(b, new Item.Properties().tab(Registry.TAB)).setRegistryName(b.getRegistryName()))); } @SubscribeEvent public static void registerBlockEntities(RegistryEvent.Register> event) { event.getRegistry().registerAll( - pipeBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(PipeBlockEntity::new, pipeBlock).build(null).setRegistryName("pipe"), - itemTerminalBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(ItemTerminalBlockEntity::new, itemTerminalBlock).build(null).setRegistryName("item_terminal"), - craftingTerminalBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(CraftingTerminalBlockEntity::new, craftingTerminalBlock).build(null).setRegistryName("crafting_terminal"), - pressurizerBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(PressurizerBlockEntity::new, pressurizerBlock).build(null).setRegistryName("pressurizer") + Registry.pipeBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(PipeBlockEntity::new, Registry.pipeBlock).build(null).setRegistryName("pipe"), + Registry.itemTerminalBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(ItemTerminalBlockEntity::new, Registry.itemTerminalBlock).build(null).setRegistryName("item_terminal"), + Registry.craftingTerminalBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(CraftingTerminalBlockEntity::new, Registry.craftingTerminalBlock).build(null).setRegistryName("crafting_terminal"), + Registry.pressurizerBlockEntity = (BlockEntityType) BlockEntityType.Builder.of(PressurizerBlockEntity::new, Registry.pressurizerBlock).build(null).setRegistryName("pressurizer") ); } @SubscribeEvent public static void registerEntities(RegistryEvent.Register> event) { event.getRegistry().registerAll( - pipeFrameEntity = (EntityType) EntityType.Builder.of(PipeFrameEntity::new, MobCategory.MISC).build("pipe_frame").setRegistryName("pipe_frame") + Registry.pipeFrameEntity = (EntityType) EntityType.Builder.of(PipeFrameEntity::new, MobCategory.MISC).build("pipe_frame").setRegistryName("pipe_frame") ); } @SubscribeEvent public static void registerContainers(RegistryEvent.Register> event) { event.getRegistry().registerAll( - pipeContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new MainPipeContainer(pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"), - itemTerminalContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new ItemTerminalContainer(itemTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("item_terminal"), - craftingTerminalContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new CraftingTerminalContainer(craftingTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("crafting_terminal"), - pressurizerContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new PressurizerContainer(pressurizerContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pressurizer"), - extractionModuleContainer = createPipeContainer("extraction_module"), - filterModuleContainer = createPipeContainer("filter_module"), - retrievalModuleContainer = createPipeContainer("retrieval_module"), - stackSizeModuleContainer = createPipeContainer("stack_size_module"), - filterIncreaseModuleContainer = createPipeContainer("filter_increase_module"), - craftingModuleContainer = createPipeContainer("crafting_module"), - filterModifierModuleContainer = createPipeContainer("filter_modifier_module") + Registry.pipeContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new MainPipeContainer(Registry.pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"), + Registry.itemTerminalContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new ItemTerminalContainer(Registry.itemTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("item_terminal"), + Registry.craftingTerminalContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new CraftingTerminalContainer(Registry.craftingTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("crafting_terminal"), + Registry.pressurizerContainer = (MenuType) IForgeMenuType.create((windowId, inv, data) -> new PressurizerContainer(Registry.pressurizerContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pressurizer"), + Registry.extractionModuleContainer = Registry.createPipeContainer("extraction_module"), + Registry.filterModuleContainer = Registry.createPipeContainer("filter_module"), + Registry.retrievalModuleContainer = Registry.createPipeContainer("retrieval_module"), + Registry.stackSizeModuleContainer = Registry.createPipeContainer("stack_size_module"), + Registry.filterIncreaseModuleContainer = Registry.createPipeContainer("filter_increase_module"), + Registry.craftingModuleContainer = Registry.createPipeContainer("crafting_module"), + Registry.filterModifierModuleContainer = Registry.createPipeContainer("filter_modifier_module") ); } @@ -217,21 +217,21 @@ public final class Registry { public static final class Client { public static void setup(FMLClientSetupEvent event) { - ItemBlockRenderTypes.setRenderLayer(pipeBlock, RenderType.cutout()); - BlockEntityRenderers.register(pipeBlockEntity, PipeRenderer::new); - EntityRenderers.register(pipeFrameEntity, PipeFrameRenderer::new); + ItemBlockRenderTypes.setRenderLayer(Registry.pipeBlock, RenderType.cutout()); + BlockEntityRenderers.register(Registry.pipeBlockEntity, PipeRenderer::new); + EntityRenderers.register(Registry.pipeFrameEntity, PipeFrameRenderer::new); - MenuScreens.register(pipeContainer, MainPipeGui::new); - MenuScreens.register(itemTerminalContainer, ItemTerminalGui::new); - MenuScreens.register(pressurizerContainer, PressurizerGui::new); - MenuScreens.register(craftingTerminalContainer, CraftingTerminalGui::new); - MenuScreens.register(extractionModuleContainer, ExtractionModuleGui::new); - MenuScreens.register(filterModuleContainer, FilterModuleGui::new); - MenuScreens.register(retrievalModuleContainer, RetrievalModuleGui::new); - MenuScreens.register(stackSizeModuleContainer, StackSizeModuleGui::new); - MenuScreens.register(filterIncreaseModuleContainer, FilterIncreaseModuleGui::new); - MenuScreens.register(craftingModuleContainer, CraftingModuleGui::new); - MenuScreens.register(filterModifierModuleContainer, FilterModifierModuleGui::new); + MenuScreens.register(Registry.pipeContainer, MainPipeGui::new); + MenuScreens.register(Registry.itemTerminalContainer, ItemTerminalGui::new); + MenuScreens.register(Registry.pressurizerContainer, PressurizerGui::new); + MenuScreens.register(Registry.craftingTerminalContainer, CraftingTerminalGui::new); + MenuScreens.register(Registry.extractionModuleContainer, ExtractionModuleGui::new); + MenuScreens.register(Registry.filterModuleContainer, FilterModuleGui::new); + MenuScreens.register(Registry.retrievalModuleContainer, RetrievalModuleGui::new); + MenuScreens.register(Registry.stackSizeModuleContainer, StackSizeModuleGui::new); + MenuScreens.register(Registry.filterIncreaseModuleContainer, FilterIncreaseModuleGui::new); + MenuScreens.register(Registry.craftingModuleContainer, CraftingModuleGui::new); + MenuScreens.register(Registry.filterModifierModuleContainer, FilterModifierModuleGui::new); } } } diff --git a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java index 890edf8..0481cd8 100644 --- a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java @@ -42,7 +42,7 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn @Override protected void defineSynchedData() { super.defineSynchedData(); - this.entityData.define(AMOUNT, -1); + this.entityData.define(PipeFrameEntity.AMOUNT, -1); } @Override @@ -53,7 +53,7 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn if (this.tickCount % 40 != 0) return; var network = PipeNetwork.get(this.level); - var attached = getAttachedPipe(this.level, this.pos, this.direction); + var attached = PipeFrameEntity.getAttachedPipe(this.level, this.pos, this.direction); if (attached != null) { var node = network.getNodeFromPipe(attached); if (node != null) { @@ -61,17 +61,17 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn if (!stack.isEmpty()) { var items = network.getOrderedNetworkItems(node); var amount = items.stream().mapToInt(i -> i.getItemAmount(this.level, stack)).sum(); - this.entityData.set(AMOUNT, amount); + this.entityData.set(PipeFrameEntity.AMOUNT, amount); return; } } } - this.entityData.set(AMOUNT, -1); + this.entityData.set(PipeFrameEntity.AMOUNT, -1); } @Override public boolean survives() { - return super.survives() && canPlace(this.level, this.pos, this.direction); + return super.survives() && PipeFrameEntity.canPlace(this.level, this.pos, this.direction); } private static BlockPos getAttachedPipe(Level world, BlockPos pos, Direction direction) { @@ -85,11 +85,11 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn } public static boolean canPlace(Level world, BlockPos pos, Direction direction) { - return getAttachedPipe(world, pos, direction) != null; + return PipeFrameEntity.getAttachedPipe(world, pos, direction) != null; } public int getAmount() { - return this.entityData.get(AMOUNT); + return this.entityData.get(PipeFrameEntity.AMOUNT); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/items/IModule.java b/src/main/java/de/ellpeck/prettypipes/items/IModule.java index 21a4ecf..2aea475 100644 --- a/src/main/java/de/ellpeck/prettypipes/items/IModule.java +++ b/src/main/java/de/ellpeck/prettypipes/items/IModule.java @@ -1,9 +1,11 @@ package de.ellpeck.prettypipes.items; +import de.ellpeck.prettypipes.misc.DirectionSelector; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -17,9 +19,9 @@ public interface IModule { void tick(ItemStack module, PipeBlockEntity tile); - boolean canNetworkSee(ItemStack module, PipeBlockEntity tile); + boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler); - boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack); + boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination); int getMaxInsertionAmount(ItemStack module, PipeBlockEntity tile, ItemStack stack, IItemHandler destination); @@ -44,4 +46,6 @@ public interface IModule { Integer getCustomNextNode(ItemStack module, PipeBlockEntity tile, List nodes, int index); ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile); + + DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile); } diff --git a/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java b/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java index 3f30c9d..b2d0bec 100644 --- a/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/items/ModuleItem.java @@ -2,10 +2,12 @@ package de.ellpeck.prettypipes.items; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Utility; +import de.ellpeck.prettypipes.misc.DirectionSelector; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -45,12 +47,12 @@ public abstract class ModuleItem extends Item implements IModule { } @Override - public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) { + public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { return true; } @Override - public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) { + public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { return true; } @@ -103,4 +105,9 @@ public abstract class ModuleItem extends Item implements IModule { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) { return null; } + + @Override + public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) { + return null; + } } diff --git a/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java new file mode 100644 index 0000000..82e3dcd --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java @@ -0,0 +1,104 @@ +package de.ellpeck.prettypipes.misc; + +import de.ellpeck.prettypipes.PrettyPipes; +import de.ellpeck.prettypipes.packets.PacketButton; +import de.ellpeck.prettypipes.pipe.PipeBlockEntity; +import net.minecraft.client.gui.components.AbstractWidget; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.client.gui.widget.ExtendedButton; + +public class DirectionSelector { + + public Direction direction; + private boolean modified; + + private final ItemStack stack; + private final PipeBlockEntity pipe; + + public DirectionSelector(ItemStack stack, PipeBlockEntity pipe) { + this.stack = stack; + this.pipe = pipe; + this.load(); + } + + @OnlyIn(Dist.CLIENT) + public AbstractWidget getButton(int x, int y) { + return new ExtendedButton(x, y, 100, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> + PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.DIRECTION_SELECTOR)) { + @Override + public Component getMessage() { + var pipe = DirectionSelector.this.pipe; + var dir = DirectionSelector.this.direction; + MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "none")); + if (dir != null) { + var blockName = pipe.getItemHandler(dir) != null ? pipe.getLevel().getBlockState(pipe.getBlockPos().relative(dir)).getBlock().getName() : null; + if (blockName != null) + msg = msg.append(" (").append(blockName).append(")"); + } + return msg; + } + }; + } + + public void onButtonPacket() { + var dir = this.getValidDirection(this.direction != null ? this.direction : Direction.UP); + if (this.direction != dir) { + this.direction = dir; + this.modified = true; + } + } + + public void save() { + if (!this.modified) + return; + this.modified = false; + + var tag = new CompoundTag(); + if (this.direction != null) + tag.putString("direction", this.direction.getName()); + this.stack.getOrCreateTag().put("direction_selector", tag); + } + + public void load() { + if (this.stack.hasTag()) { + var tag = this.stack.getTag().getCompound("direction_selector"); + this.direction = Direction.byName(tag.getString("direction")); + } + + // default to the first direction with a container + // don't mark as modified here because we don't want to save this automatic direction + if (this.direction == null || !this.isDirectionValid(this.direction)) + this.direction = this.getValidDirection(Direction.UP); + } + + private boolean isDirectionValid(Direction dir) { + if (this.pipe.getItemHandler(dir) == null) + return false; + return this.pipe.streamModules() + .filter(p -> p.getLeft() != this.stack) + .map(p -> p.getRight().getDirectionSelector(p.getLeft(), this.pipe)) + .noneMatch(p -> p != null && p.direction == dir); + } + + private Direction getValidDirection(Direction dir) { + for (var i = 0; i < 6; i++) { + dir = Direction.from3DDataValue(dir.get3DDataValue() + 1); + if (this.isDirectionValid(dir)) + return dir; + } + return null; + } + + public interface IDirectionContainer { + + DirectionSelector getSelector(); + + } +} diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java index ee5f585..fb80392 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java @@ -48,17 +48,22 @@ public class ItemFilter extends ItemStackHandler { } @OnlyIn(Dist.CLIENT) - public List getButtons(Screen gui, int x, int y) { + public List getButtons(Screen gui, int x, int y, boolean rightAligned) { List buttons = new ArrayList<>(); if (this.canModifyWhitelist) { - var whitelistText = (Supplier) () -> new TranslatableComponent("info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist")); - buttons.add(new Button(x, y, 70, 20, whitelistText.get(), button -> { + var whitelistText = (Supplier) () -> "info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist"); + buttons.add(new Button(x - 20 * (rightAligned ? 1 : 0), y, 20, 20, new TranslatableComponent(whitelistText.get()), button -> { PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 0); - button.setMessage(whitelistText.get()); - })); + button.setMessage(new TranslatableComponent(whitelistText.get())); + }) { + @Override + public void renderToolTip(PoseStack matrix, int x, int y) { + gui.renderTooltip(matrix, new TranslatableComponent(whitelistText.get() + ".description").withStyle(ChatFormatting.GRAY), x, y); + } + }); } if (this.canPopulateFromInventories) { - buttons.add(new Button(x + 72, y, 70, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) { + buttons.add(new Button(x + 22 * (rightAligned ? -1 : 1), y, 20, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) { @Override public void renderToolTip(PoseStack matrix, int x, int y) { gui.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".populate.description").withStyle(ChatFormatting.GRAY), x, y); @@ -107,7 +112,7 @@ public class ItemFilter extends ItemStackHandler { } private boolean isFiltered(ItemStack stack) { - var types = getEqualityTypes(this.pipe); + var types = ItemFilter.getEqualityTypes(this.pipe); // also check if any filter increase modules have the item we need for (ItemStackHandler handler : this.pipe.getFilters()) { for (var i = 0; i < handler.getSlots(); i++) { @@ -164,6 +169,7 @@ public class ItemFilter extends ItemStackHandler { ItemFilter getFilter(); - default void onFilterPopulated() {} + default void onFilterPopulated() { + } } } diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemOrder.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemOrder.java index bf40ba1..8da32ce 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemOrder.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemOrder.java @@ -16,6 +16,6 @@ public enum ItemOrder { } public ItemOrder next() { - return values()[(this.ordinal() + 1) % values().length]; + return ItemOrder.values()[(this.ordinal() + 1) % ItemOrder.values().length]; } } diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemTerminalWidget.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemTerminalWidget.java index 890c5d8..7f3ad18 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemTerminalWidget.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemTerminalWidget.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.GuiComponent; import net.minecraft.client.gui.components.AbstractWidget; import net.minecraft.client.gui.narration.NarrationElementOutput; import net.minecraft.network.chat.TextComponent; @@ -41,7 +42,7 @@ public class ItemTerminalWidget extends AbstractWidget { this.setBlitOffset(100); renderer.blitOffset = 100; if (this.selected) - fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433); + GuiComponent.fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433); RenderSystem.enableDepthTest(); renderer.renderGuiItem(this.stack, this.x, this.y); var amount = !this.craftable ? this.stack.getCount() : 0; diff --git a/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java b/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java index 202140b..6181a7a 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java @@ -20,30 +20,30 @@ public class PlayerPrefs { public boolean syncJei = true; public void save() { - var file = getFile(); + var file = PlayerPrefs.getFile(); if (file.exists()) file.delete(); try (var writer = new FileWriter(file)) { - GSON.toJson(this, writer); + PlayerPrefs.GSON.toJson(this, writer); } catch (IOException e) { e.printStackTrace(); } } public static PlayerPrefs get() { - if (instance == null) { - var file = getFile(); + if (PlayerPrefs.instance == null) { + var file = PlayerPrefs.getFile(); if (file.exists()) { try (var reader = new FileReader(file)) { - instance = GSON.fromJson(reader, PlayerPrefs.class); - return instance; + PlayerPrefs.instance = PlayerPrefs.GSON.fromJson(reader, PlayerPrefs.class); + return PlayerPrefs.instance; } catch (IOException e) { e.printStackTrace(); } } - instance = new PlayerPrefs(); + PlayerPrefs.instance = new PlayerPrefs(); } - return instance; + return PlayerPrefs.instance; } private static File getFile() { diff --git a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java index c0caae2..269a453 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java +++ b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java @@ -102,6 +102,6 @@ public class NetworkLocation implements INBTSerializable { @Override public void deserializeNBT(CompoundTag nbt) { this.pipePos = NbtUtils.readBlockPos(nbt.getCompound("pipe_pos")); - this.direction = Direction.values()[(nbt.getInt("direction"))]; + this.direction = Direction.values()[nbt.getInt("direction")]; } } diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java b/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java index c15b38f..5ee484f 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeItem.java @@ -60,7 +60,7 @@ public class PipeItem implements IPipeItem { } public PipeItem(ItemStack stack, float speed) { - this(TYPE, stack, speed); + this(PipeItem.TYPE, stack, speed); } public PipeItem(ResourceLocation type, CompoundTag nbt) { @@ -78,7 +78,7 @@ public class PipeItem implements IPipeItem { public void setDestination(BlockPos startInventory, BlockPos destInventory, GraphPath path) { this.startInventory = startInventory; this.destInventory = destInventory; - this.path = compilePath(path); + this.path = PipeItem.compilePath(path); this.currGoalPos = this.getStartPipe(); this.currentTile = 0; @@ -126,7 +126,7 @@ public class PipeItem implements IPipeItem { currPipe = next; } } else { - var dist = (this.currGoalPos).distToLowCornerSqr(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F); + var dist = this.currGoalPos.distToLowCornerSqr(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F); if (dist < currSpeed * currSpeed) { // we're past the start of the pipe, so move to the center of the next pipe BlockPos nextPos; diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index 5510c9e..8558593 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -324,11 +324,9 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL if (!this.world.isLoaded(dest)) continue; var pipe = this.getPipe(dest); - if (!pipe.canNetworkSee()) - continue; for (var dir : Direction.values()) { var handler = pipe.getItemHandler(dir); - if (handler == null) + if (handler == null || !pipe.canNetworkSee(dir, handler)) continue; // check if this handler already exists (double-connected pipes, double chests etc.) if (info.stream().anyMatch(l -> handler.equals(l.getItemHandler(this.world)))) diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java index 9dcce0b..7da5ad7 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java @@ -26,6 +26,8 @@ import org.apache.logging.log4j.util.TriConsumer; import javax.annotation.Nullable; import java.util.function.Supplier; +import static de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer; + public class PacketButton { private BlockPos pos; @@ -121,6 +123,10 @@ public class PacketButton { TAG_FILTER((pos, data, player) -> { var container = (FilterModifierModuleContainer) player.containerMenu; FilterModifierModuleItem.setFilterTag(container.moduleStack, container.getTags().get(data[0])); + }), + DIRECTION_SELECTOR((pos, data, player) -> { + if (player.containerMenu instanceof IDirectionContainer filtered) + filtered.getSelector().onButtonPacket(); }); public final TriConsumer action; diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketCraftingModuleTransfer.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketCraftingModuleTransfer.java index a055d2e..9b32538 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketCraftingModuleTransfer.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketCraftingModuleTransfer.java @@ -53,8 +53,8 @@ public class PacketCraftingModuleTransfer { public void run() { Player player = ctx.get().getSender(); if (player.containerMenu instanceof CraftingModuleContainer container) { - copy(container.input, message.inputs); - copy(container.output, message.outputs); + PacketCraftingModuleTransfer.copy(container.input, message.inputs); + PacketCraftingModuleTransfer.copy(container.output, message.outputs); container.modified = true; container.broadcastChanges(); } diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java index 38ad98e..2411b72 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java @@ -93,7 +93,7 @@ public class PacketGhostSlot { private final TagKey tag; public Entry(List stacks) { - var tag = getTagForStacks(stacks); + var tag = Entry.getTagForStacks(stacks); if (tag != null) { this.stacks = null; this.tag = tag; diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketHandler.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketHandler.java index 35d3ed4..733f0cc 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketHandler.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketHandler.java @@ -16,24 +16,24 @@ public final class PacketHandler { private static SimpleChannel network; public static void setup() { - network = NetworkRegistry.newSimpleChannel(new ResourceLocation(PrettyPipes.ID, "network"), () -> VERSION, VERSION::equals, VERSION::equals); - network.registerMessage(0, PacketItemEnterPipe.class, PacketItemEnterPipe::toBytes, PacketItemEnterPipe::fromBytes, PacketItemEnterPipe::onMessage); - network.registerMessage(1, PacketButton.class, PacketButton::toBytes, PacketButton::fromBytes, PacketButton::onMessage); - network.registerMessage(2, PacketNetworkItems.class, PacketNetworkItems::toBytes, PacketNetworkItems::fromBytes, PacketNetworkItems::onMessage); - network.registerMessage(3, PacketRequest.class, PacketRequest::toBytes, PacketRequest::fromBytes, PacketRequest::onMessage); - network.registerMessage(4, PacketGhostSlot.class, PacketGhostSlot::toBytes, PacketGhostSlot::fromBytes, PacketGhostSlot::onMessage); - network.registerMessage(5, PacketCraftingModuleTransfer.class, PacketCraftingModuleTransfer::toBytes, PacketCraftingModuleTransfer::fromBytes, PacketCraftingModuleTransfer::onMessage); + PacketHandler.network = NetworkRegistry.newSimpleChannel(new ResourceLocation(PrettyPipes.ID, "network"), () -> PacketHandler.VERSION, PacketHandler.VERSION::equals, PacketHandler.VERSION::equals); + PacketHandler.network.registerMessage(0, PacketItemEnterPipe.class, PacketItemEnterPipe::toBytes, PacketItemEnterPipe::fromBytes, PacketItemEnterPipe::onMessage); + PacketHandler.network.registerMessage(1, PacketButton.class, PacketButton::toBytes, PacketButton::fromBytes, PacketButton::onMessage); + PacketHandler.network.registerMessage(2, PacketNetworkItems.class, PacketNetworkItems::toBytes, PacketNetworkItems::fromBytes, PacketNetworkItems::onMessage); + PacketHandler.network.registerMessage(3, PacketRequest.class, PacketRequest::toBytes, PacketRequest::fromBytes, PacketRequest::onMessage); + PacketHandler.network.registerMessage(4, PacketGhostSlot.class, PacketGhostSlot::toBytes, PacketGhostSlot::fromBytes, PacketGhostSlot::onMessage); + PacketHandler.network.registerMessage(5, PacketCraftingModuleTransfer.class, PacketCraftingModuleTransfer::toBytes, PacketCraftingModuleTransfer::fromBytes, PacketCraftingModuleTransfer::onMessage); } public static void sendToAllLoaded(Level world, BlockPos pos, Object message) { - network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message); + PacketHandler.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message); } public static void sendTo(Player player, Object message) { - network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message); + PacketHandler.network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message); } public static void sendToServer(Object message) { - network.send(PacketDistributor.SERVER.noArg(), message); + PacketHandler.network.send(PacketDistributor.SERVER.noArg(), message); } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java index a64b8ab..77483b1 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeItem.java @@ -46,7 +46,7 @@ public interface IPipeItem extends INBTSerializable { static IPipeItem load(CompoundTag nbt) { var type = new ResourceLocation(nbt.getString("type")); - var func = TYPES.get(type); + var func = IPipeItem.TYPES.get(type); return func != null ? func.apply(type, nbt) : null; } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index bd5b2a2..14b9373 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -50,26 +50,26 @@ public class PipeBlock extends BaseEntityBlock { public static final Map> DIRECTIONS = new HashMap<>(); private static final Map, VoxelShape> SHAPE_CACHE = new HashMap<>(); private static final Map, VoxelShape> COLL_SHAPE_CACHE = new HashMap<>(); - private static final VoxelShape CENTER_SHAPE = box(5, 5, 5, 11, 11, 11); + private static final VoxelShape CENTER_SHAPE = Block.box(5, 5, 5, 11, 11, 11); public static final Map DIR_SHAPES = ImmutableMap.builder() - .put(Direction.UP, box(5, 10, 5, 11, 16, 11)) - .put(Direction.DOWN, box(5, 0, 5, 11, 6, 11)) - .put(Direction.NORTH, box(5, 5, 0, 11, 11, 6)) - .put(Direction.SOUTH, box(5, 5, 10, 11, 11, 16)) - .put(Direction.EAST, box(10, 5, 5, 16, 11, 11)) - .put(Direction.WEST, box(0, 5, 5, 6, 11, 11)) + .put(Direction.UP, Block.box(5, 10, 5, 11, 16, 11)) + .put(Direction.DOWN, Block.box(5, 0, 5, 11, 6, 11)) + .put(Direction.NORTH, Block.box(5, 5, 0, 11, 11, 6)) + .put(Direction.SOUTH, Block.box(5, 5, 10, 11, 11, 16)) + .put(Direction.EAST, Block.box(10, 5, 5, 16, 11, 11)) + .put(Direction.WEST, Block.box(0, 5, 5, 6, 11, 11)) .build(); static { for (var dir : Direction.values()) - DIRECTIONS.put(dir, EnumProperty.create(dir.getName(), ConnectionType.class)); + PipeBlock.DIRECTIONS.put(dir, EnumProperty.create(dir.getName(), ConnectionType.class)); } public PipeBlock() { super(Block.Properties.of(Material.STONE).strength(2).sound(SoundType.STONE).noOcclusion()); var state = this.defaultBlockState().setValue(BlockStateProperties.WATERLOGGED, false); - for (var prop : DIRECTIONS.values()) + for (var prop : PipeBlock.DIRECTIONS.values()) state = state.setValue(prop, ConnectionType.DISCONNECTED); this.registerDefaultState(state); } @@ -100,7 +100,7 @@ public class PipeBlock extends BaseEntityBlock { @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { - builder.add(DIRECTIONS.values().toArray(new EnumProperty[0])); + builder.add(PipeBlock.DIRECTIONS.values().toArray(new EnumProperty[0])); builder.add(BlockStateProperties.WATERLOGGED); } @@ -114,7 +114,7 @@ public class PipeBlock extends BaseEntityBlock { var newState = this.createState(worldIn, pos, state); if (newState != state) { worldIn.setBlockAndUpdate(pos, newState); - onStateChanged(worldIn, pos, newState); + PipeBlock.onStateChanged(worldIn, pos, newState); } } @@ -133,17 +133,17 @@ public class PipeBlock extends BaseEntityBlock { @Override public void setPlacedBy(Level worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { - onStateChanged(worldIn, pos, state); + PipeBlock.onStateChanged(worldIn, pos, state); } @Override public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) { - return this.cacheAndGetShape(state, worldIn, pos, s -> s.getShape(worldIn, pos, context), SHAPE_CACHE, null); + return this.cacheAndGetShape(state, worldIn, pos, s -> s.getShape(worldIn, pos, context), PipeBlock.SHAPE_CACHE, null); } @Override public VoxelShape getCollisionShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) { - return this.cacheAndGetShape(state, worldIn, pos, s -> s.getCollisionShape(worldIn, pos, context), COLL_SHAPE_CACHE, s -> { + return this.cacheAndGetShape(state, worldIn, pos, s -> s.getCollisionShape(worldIn, pos, context), PipeBlock.COLL_SHAPE_CACHE, s -> { // make the shape a bit higher so we can jump up onto a higher block var newShape = new MutableObject(Shapes.empty()); s.forAllBoxes((x1, y1, z1, x2, y2, z2) -> newShape.setValue(Shapes.join(Shapes.create(x1, y1, z1, x2, y2 + 3 / 16F, z2), newShape.getValue(), BooleanOp.OR))); @@ -166,10 +166,10 @@ public class PipeBlock extends BaseEntityBlock { var key = Pair.of(state, cover); var shape = cache.get(key); if (shape == null) { - shape = CENTER_SHAPE; - for (var entry : DIRECTIONS.entrySet()) { + shape = PipeBlock.CENTER_SHAPE; + for (var entry : PipeBlock.DIRECTIONS.entrySet()) { if (state.getValue(entry.getValue()).isConnected()) - shape = Shapes.or(shape, DIR_SHAPES.get(entry.getKey())); + shape = Shapes.or(shape, PipeBlock.DIR_SHAPES.get(entry.getKey())); } if (shapeModifier != null) shape = shapeModifier.apply(shape); @@ -187,7 +187,7 @@ public class PipeBlock extends BaseEntityBlock { state = state.setValue(BlockStateProperties.WATERLOGGED, true); for (var dir : Direction.values()) { - var prop = DIRECTIONS.get(dir); + var prop = PipeBlock.DIRECTIONS.get(dir); var type = this.getConnectionType(world, pos, dir, state); // don't reconnect on blocked faces if (type.isConnected() && curr.getValue(prop) == ConnectionType.BLOCKED) @@ -215,8 +215,8 @@ public class PipeBlock extends BaseEntityBlock { if (blockHandler != null) return ConnectionType.CONNECTED; var offState = world.getBlockState(offset); - if (hasLegsTo(world, offState, offset, direction)) { - if (DIRECTIONS.values().stream().noneMatch(d -> state.getValue(d) == ConnectionType.LEGS)) + if (PipeBlock.hasLegsTo(world, offState, offset, direction)) { + if (PipeBlock.DIRECTIONS.values().stream().noneMatch(d -> state.getValue(d) == ConnectionType.LEGS)) return ConnectionType.LEGS; } return ConnectionType.DISCONNECTED; @@ -226,7 +226,7 @@ public class PipeBlock extends BaseEntityBlock { if (state.getBlock() instanceof WallBlock || state.getBlock() instanceof FenceBlock) return direction == Direction.DOWN; if (state.getMaterial() == Material.STONE || state.getMaterial() == Material.METAL) - return canSupportCenter(world, pos, direction.getOpposite()); + return Block.canSupportCenter(world, pos, direction.getOpposite()); return false; } @@ -240,7 +240,7 @@ public class PipeBlock extends BaseEntityBlock { var connections = 0; var force = false; for (var dir : Direction.values()) { - var value = newState.getValue(DIRECTIONS.get(dir)); + var value = newState.getValue(PipeBlock.DIRECTIONS.get(dir)); if (!value.isConnected()) continue; connections++; @@ -271,7 +271,7 @@ public class PipeBlock extends BaseEntityBlock { @Override public void playerWillDestroy(Level worldIn, BlockPos pos, BlockState state, Player player) { - dropItems(worldIn, pos, player); + PipeBlock.dropItems(worldIn, pos, player); super.playerWillDestroy(worldIn, pos, state, player); } @@ -302,7 +302,7 @@ public class PipeBlock extends BaseEntityBlock { @org.jetbrains.annotations.Nullable @Override public BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) { - return createTickerHelper(type, Registry.pipeBlockEntity, PipeBlockEntity::tick); + return BaseEntityBlock.createTickerHelper(type, Registry.pipeBlockEntity, PipeBlockEntity::tick); } public static void dropItems(Level worldIn, BlockPos pos, Player player) { diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java index dab0372..5ffe857 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java @@ -188,11 +188,9 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC public Pair getAvailableDestination(ItemStack stack, boolean force, boolean preventOversending) { if (!this.canWork()) return null; - if (!force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack))) - return null; for (var dir : Direction.values()) { var handler = this.getItemHandler(dir); - if (handler == null) + if (handler == null || !force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, dir, handler))) continue; var remain = ItemHandlerHelper.insertItem(handler, stack, true); // did we insert anything? @@ -315,13 +313,9 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC return null; } - public boolean isConnectedInventory(Direction dir) { - return this.getItemHandler(dir) != null; - } - public boolean canHaveModules() { for (var dir : Direction.values()) { - if (this.isConnectedInventory(dir)) + if (this.getItemHandler(dir) != null) return true; var connectable = this.getPipeConnectable(dir); if (connectable != null && connectable.allowsModules(this.worldPosition, dir)) @@ -330,8 +324,8 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC return false; } - public boolean canNetworkSee() { - return this.streamModules().allMatch(m -> m.getRight().canNetworkSee(m.getLeft(), this)); + public boolean canNetworkSee(Direction direction, IItemHandler handler) { + return this.streamModules().allMatch(m -> m.getRight().canNetworkSee(m.getLeft(), this, direction, handler)); } public Stream> streamModules() { diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/containers/AbstractPipeGui.java b/src/main/java/de/ellpeck/prettypipes/pipe/containers/AbstractPipeGui.java index 08f5826..21f1058 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/containers/AbstractPipeGui.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/containers/AbstractPipeGui.java @@ -79,7 +79,7 @@ public abstract class AbstractPipeGui> extend @Override protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) { RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderTexture(0, AbstractPipeGui.TEXTURE); this.blit(matrix, this.leftPos, this.topPos + 32, 0, 0, 176, 171); for (var tab : this.tabs) @@ -143,7 +143,7 @@ public abstract class AbstractPipeGui> extend AbstractPipeGui.this.itemRenderer.renderGuiItem(this.moduleStack, this.x + 6, this.y + itemOffset); RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderTexture(0, AbstractPipeGui.TEXTURE); } private void drawForeground(PoseStack matrix, int mouseX, int mouseY) { diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleGui.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleGui.java index 126f6f5..a373f5f 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleGui.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleGui.java @@ -17,7 +17,7 @@ public class CraftingModuleGui extends AbstractPipeGui protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) { super.renderBg(matrix, partialTicks, mouseX, mouseY); RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderTexture(0, TEXTURE); + RenderSystem.setShaderTexture(0, AbstractPipeGui.TEXTURE); this.blit(matrix, this.leftPos + 176 / 2 - 16 / 2, this.topPos + 32 + 18 * 2, 176, 80, 16, 16); } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java index 4bc9d90..a34bba5 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java @@ -13,10 +13,12 @@ import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.util.Mth; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemStackHandler; import org.apache.commons.lang3.tuple.Pair; @@ -54,12 +56,12 @@ public class CraftingModuleItem extends ModuleItem { } @Override - public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) { + public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { return false; } @Override - public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) { + public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { return false; } @@ -147,7 +149,7 @@ public class CraftingModuleItem extends ModuleItem { var out = output.getStackInSlot(i); if (!out.isEmpty() && ItemEquality.compareItems(out, stack, equalityTypes)) { // figure out how many crafting operations we can actually do with the input items we have in the network - var availableCrafts = CraftingTerminalBlockEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, addDependency(dependencyChain, module), equalityTypes); + var availableCrafts = CraftingTerminalBlockEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes); if (availableCrafts > 0) craftable += out.getCount() * availableCrafts; } @@ -179,7 +181,7 @@ public class CraftingModuleItem extends ModuleItem { continue; var copy = in.copy(); copy.setCount(in.getCount() * toCraft); - var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, addDependency(dependencyChain, module), equalityTypes); + var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes); tile.craftIngredientRequests.addAll(ret.getLeft()); } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleContainer.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleContainer.java index 38bc2c9..0b6fa7a 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleContainer.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleContainer.java @@ -1,5 +1,7 @@ package de.ellpeck.prettypipes.pipe.modules.extraction; +import de.ellpeck.prettypipes.misc.DirectionSelector; +import de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; @@ -9,9 +11,10 @@ import net.minecraft.world.inventory.MenuType; import javax.annotation.Nullable; -public class ExtractionModuleContainer extends AbstractPipeContainer implements IFilteredContainer { +public class ExtractionModuleContainer extends AbstractPipeContainer implements IFilteredContainer, IDirectionContainer { public ItemFilter filter; + public DirectionSelector directionSelector; public ExtractionModuleContainer(@Nullable MenuType type, int id, Player player, BlockPos pos, int moduleIndex) { super(type, id, player, pos, moduleIndex); @@ -20,6 +23,8 @@ public class ExtractionModuleContainer extends AbstractPipeContainer { @Override protected void init() { super.init(); - for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 18 * Mth.ceil(this.menu.filter.getSlots() / 9F) + 2)) + for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 18 * Mth.ceil(this.menu.filter.getSlots() / 9F) + 2, false)) this.addRenderableWidget(widget); } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java index 56084e9..768fdf0 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java @@ -7,9 +7,11 @@ import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; +import net.minecraft.core.Direction; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraftforge.items.IItemHandler; public class FilterModuleItem extends ModuleItem { @@ -23,7 +25,7 @@ public class FilterModuleItem extends ModuleItem { } @Override - public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) { + public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { var filter = this.getItemFilter(module, tile); return filter.isAllowed(stack); } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/modifier/FilterModifierModuleGui.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/modifier/FilterModifierModuleGui.java index 19204bc..185a2f9 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/modifier/FilterModifierModuleGui.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/modifier/FilterModifierModuleGui.java @@ -126,7 +126,7 @@ public class FilterModifierModuleGui extends AbstractPipeGui BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) { - return level.isClientSide ? null : createTickerHelper(type, Registry.pressurizerBlockEntity, PressurizerBlockEntity::tick); + return level.isClientSide ? null : BaseEntityBlock.createTickerHelper(type, Registry.pressurizerBlockEntity, PressurizerBlockEntity::tick); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerGui.java b/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerGui.java index c43d571..d817667 100644 --- a/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerGui.java +++ b/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerGui.java @@ -38,7 +38,7 @@ public class PressurizerGui extends AbstractContainerScreen BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) { - return createTickerHelper(type, Registry.craftingTerminalBlockEntity, ItemTerminalBlockEntity::tick); + return BaseEntityBlock.createTickerHelper(type, Registry.craftingTerminalBlockEntity, ItemTerminalBlockEntity::tick); } } diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java index be91b82..3361d82 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java @@ -111,10 +111,10 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity { network.startProfile("terminal_request_crafting"); this.updateItems(); // get the amount of crafts that we can do - var lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> { + var lowestAvailable = CraftingTerminalBlockEntity.getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> { var item = this.networkItems.get(s); return item != null ? item.getLocations() : Collections.emptyList(); - }, onItemUnavailable(player, force), new Stack<>(), ItemEquality.NBT); + }, ItemTerminalBlockEntity.onItemUnavailable(player, force), new Stack<>(), ItemEquality.NBT); // if we're forcing, just pretend we have one available if (lowestAvailable <= 0 && force) lowestAvailable = maxAmount; @@ -128,7 +128,7 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity { continue; requested = requested.copy(); requested.setCount(lowestAvailable); - this.requestItemImpl(requested, onItemUnavailable(player, force)); + this.requestItemImpl(requested, ItemTerminalBlockEntity.onItemUnavailable(player, force)); } player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".sending_ingredients", lowestAvailable).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GREEN)), UUID.randomUUID()); } else { diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlock.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlock.java index d286158..b77b361 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlock.java @@ -82,7 +82,7 @@ public class ItemTerminalBlock extends BaseEntityBlock { @org.jetbrains.annotations.Nullable @Override public BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType type) { - return createTickerHelper(type, Registry.itemTerminalBlockEntity, ItemTerminalBlockEntity::tick); + return BaseEntityBlock.createTickerHelper(type, Registry.itemTerminalBlockEntity, ItemTerminalBlockEntity::tick); } } diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java index 76f7bf9..c41e128 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java @@ -163,11 +163,11 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect .filter(s -> ItemEquality.compareItems(s, filter) && s.hasTag() && s.getTag().hashCode() == nbtHash) .findFirst().orElse(filter); } - var requested = this.requestItemImpl(stack, onItemUnavailable(player, false)); + var requested = this.requestItemImpl(stack, ItemTerminalBlockEntity.onItemUnavailable(player, false)); if (requested > 0) { player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getHoverName()).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GREEN)), UUID.randomUUID()); } else { - onItemUnavailable(player, false).accept(stack); + ItemTerminalBlockEntity.onItemUnavailable(player, false).accept(stack); } network.endProfile(); } @@ -175,7 +175,7 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect public int requestItemImpl(ItemStack stack, Consumer unavailableConsumer) { var item = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT)); Collection locations = item == null ? Collections.emptyList() : item.getLocations(); - var ret = requestItemLater(this.level, this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT); + var ret = ItemTerminalBlockEntity.requestItemLater(this.level, this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT); this.existingRequests.addAll(ret.getLeft()); return stack.getCount() - ret.getRight().getCount(); } diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java index b6fc5ff..dd0190d 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java @@ -6,6 +6,7 @@ import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketHandler; import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.resources.ResourceLocation; @@ -25,9 +26,9 @@ public class CraftingTerminalGui extends ItemTerminalGui { protected void init() { super.init(); this.requestButton = this.addRenderableWidget(new Button(this.leftPos + 8, this.topPos + 100, 50, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".request"), button -> { - var amount = requestModifier(); + var amount = ItemTerminalGui.requestModifier(); // also allow holding backspace instead of alt for people whose alt key is inaccessible (linux?) - var force = hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0; + var force = Screen.hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0; PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount, force)); })); this.tick(); @@ -70,7 +71,7 @@ public class CraftingTerminalGui extends ItemTerminalGui { @Override protected ResourceLocation getTexture() { - return TEXTURE; + return CraftingTerminalGui.TEXTURE; } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java index 2427719..0073649 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java @@ -15,6 +15,7 @@ import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.components.Widget; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.client.gui.narration.NarratableEntry; +import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.resources.language.I18n; @@ -72,7 +73,7 @@ public class ItemTerminalGui extends AbstractContainerScreen { - var modifier = requestModifier(); + var modifier = ItemTerminalGui.requestModifier(); if (modifier > 1 && this.requestAmount == 1) { this.requestAmount = modifier; } else { @@ -83,7 +84,7 @@ public class ItemTerminalGui extends AbstractContainerScreen { - this.requestAmount -= requestModifier(); + this.requestAmount -= ItemTerminalGui.requestModifier(); if (this.requestAmount < 1) this.requestAmount = 1; })); @@ -325,7 +326,7 @@ public class ItemTerminalGui extends AbstractContainerScreen Date: Thu, 30 Jun 2022 16:35:01 +0200 Subject: [PATCH 2/5] also added direction selector to the other modules --- .../prettypipes/misc/DirectionSelector.java | 22 ++-- .../ellpeck/prettypipes/misc/ItemFilter.java | 4 +- .../prettypipes/pipe/PipeBlockEntity.java | 109 +++++++++--------- .../extraction/ExtractionModuleItem.java | 12 +- .../insertion/FilterModuleContainer.java | 14 ++- .../modules/insertion/FilterModuleGui.java | 4 +- .../modules/insertion/FilterModuleItem.java | 9 +- .../retrieval/RetrievalModuleContainer.java | 14 ++- .../modules/retrieval/RetrievalModuleGui.java | 3 +- .../retrieval/RetrievalModuleItem.java | 23 ++-- 10 files changed, 132 insertions(+), 82 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java index 82e3dcd..cebaadc 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java @@ -16,7 +16,7 @@ import net.minecraftforge.client.gui.widget.ExtendedButton; public class DirectionSelector { - public Direction direction; + private Direction direction; private boolean modified; private final ItemStack stack; @@ -35,7 +35,7 @@ public class DirectionSelector { @Override public Component getMessage() { var pipe = DirectionSelector.this.pipe; - var dir = DirectionSelector.this.direction; + var dir = DirectionSelector.this.getDirection(); MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "none")); if (dir != null) { var blockName = pipe.getItemHandler(dir) != null ? pipe.getLevel().getBlockState(pipe.getBlockPos().relative(dir)).getBlock().getName() : null; @@ -48,7 +48,7 @@ public class DirectionSelector { } public void onButtonPacket() { - var dir = this.getValidDirection(this.direction != null ? this.direction : Direction.UP); + var dir = this.getValidDirection(this.getDirection()); if (this.direction != dir) { this.direction = dir; this.modified = true; @@ -61,8 +61,9 @@ public class DirectionSelector { this.modified = false; var tag = new CompoundTag(); - if (this.direction != null) - tag.putString("direction", this.direction.getName()); + var dir = this.getDirection(); + if (dir != null) + tag.putString("direction", dir.getName()); this.stack.getOrCreateTag().put("direction_selector", tag); } @@ -71,11 +72,13 @@ public class DirectionSelector { var tag = this.stack.getTag().getCompound("direction_selector"); this.direction = Direction.byName(tag.getString("direction")); } + } - // default to the first direction with a container - // don't mark as modified here because we don't want to save this automatic direction + public Direction getDirection() { + // default to the first direction with a container if ours is invalid or unset if (this.direction == null || !this.isDirectionValid(this.direction)) - this.direction = this.getValidDirection(Direction.UP); + return this.getValidDirection(this.direction); + return this.direction; } private boolean isDirectionValid(Direction dir) { @@ -84,10 +87,13 @@ public class DirectionSelector { return this.pipe.streamModules() .filter(p -> p.getLeft() != this.stack) .map(p -> p.getRight().getDirectionSelector(p.getLeft(), this.pipe)) + // don't use getDirection here because we don't want a stack overflow .noneMatch(p -> p != null && p.direction == dir); } private Direction getValidDirection(Direction dir) { + if (dir == null) + dir = Direction.UP; for (var i = 0; i < 6; i++) { dir = Direction.from3DDataValue(dir.get3DDataValue() + 1); if (this.isDirectionValid(dir)) diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java index fb80392..ff90ac7 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java @@ -52,7 +52,7 @@ public class ItemFilter extends ItemStackHandler { List buttons = new ArrayList<>(); if (this.canModifyWhitelist) { var whitelistText = (Supplier) () -> "info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist"); - buttons.add(new Button(x - 20 * (rightAligned ? 1 : 0), y, 20, 20, new TranslatableComponent(whitelistText.get()), button -> { + buttons.add(new Button(x - 20, y, 20, 20, new TranslatableComponent(whitelistText.get()), button -> { PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 0); button.setMessage(new TranslatableComponent(whitelistText.get())); }) { @@ -63,7 +63,7 @@ public class ItemFilter extends ItemStackHandler { }); } if (this.canPopulateFromInventories) { - buttons.add(new Button(x + 22 * (rightAligned ? -1 : 1), y, 20, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) { + buttons.add(new Button(x - 42, y, 20, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) { @Override public void renderToolTip(PoseStack matrix, int x, int y) { gui.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".populate.description").withStyle(ChatFormatting.GRAY), x, y); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java index 5ffe857..0316b62 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java @@ -186,63 +186,68 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC } public Pair getAvailableDestination(ItemStack stack, boolean force, boolean preventOversending) { - if (!this.canWork()) - return null; for (var dir : Direction.values()) { - var handler = this.getItemHandler(dir); - if (handler == null || !force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, dir, handler))) - continue; - var remain = ItemHandlerHelper.insertItem(handler, stack, true); - // did we insert anything? - if (remain.getCount() == stack.getCount()) - continue; - var toInsert = stack.copy(); - toInsert.shrink(remain.getCount()); - // limit to the max amount that modules allow us to insert - var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); - if (maxAmount < toInsert.getCount()) - toInsert.setCount(maxAmount); - var offset = this.worldPosition.relative(dir); - if (preventOversending || maxAmount < Integer.MAX_VALUE) { - var network = PipeNetwork.get(this.level); - // these are the items that are currently in the pipes, going to this inventory - var onTheWay = network.getItemsOnTheWay(offset, null); - if (onTheWay > 0) { - if (maxAmount < Integer.MAX_VALUE) { - // these are the items on the way, limited to items of the same type as stack - var onTheWaySame = network.getItemsOnTheWay(offset, stack); - // check if any modules are limiting us - if (toInsert.getCount() + onTheWaySame > maxAmount) - toInsert.setCount(maxAmount - onTheWaySame); - } - // totalSpace will be the amount of items that fit into the attached container - var totalSpace = 0; - for (var i = 0; i < handler.getSlots(); i++) { - var copy = stack.copy(); - var maxStackSize = copy.getMaxStackSize(); - // if the container can store more than 64 items in this slot, then it's likely - // a barrel or similar, meaning that the slot limit matters more than the max stack size - var limit = handler.getSlotLimit(i); - if (limit > 64) - maxStackSize = limit; - copy.setCount(maxStackSize); - // this is an inaccurate check since it ignores the fact that some slots might - // have space for items of other types, but it'll be good enough for us - var left = handler.insertItem(i, copy, true); - totalSpace += maxStackSize - left.getCount(); - } - // if the items on the way plus the items we're trying to move are too much, reduce - if (onTheWay + toInsert.getCount() > totalSpace) - toInsert.setCount(totalSpace - onTheWay); - } - } - // we return the item that can actually be inserted, NOT the remainder! - if (!toInsert.isEmpty()) - return Pair.of(offset, toInsert); + var dest = this.getAvailableDestination(dir, stack, force, preventOversending); + if (!dest.isEmpty()) + return Pair.of(this.worldPosition.relative(dir), dest); } return null; } + public ItemStack getAvailableDestination(Direction direction, ItemStack stack, boolean force, boolean preventOversending) { + if (!this.canWork()) + return ItemStack.EMPTY; + var handler = this.getItemHandler(direction); + if (handler == null || !force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, direction, handler))) + return ItemStack.EMPTY; + var remain = ItemHandlerHelper.insertItem(handler, stack, true); + // did we insert anything? + if (remain.getCount() == stack.getCount()) + return ItemStack.EMPTY; + var toInsert = stack.copy(); + toInsert.shrink(remain.getCount()); + // limit to the max amount that modules allow us to insert + var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); + if (maxAmount < toInsert.getCount()) + toInsert.setCount(maxAmount); + var offset = this.worldPosition.relative(direction); + if (preventOversending || maxAmount < Integer.MAX_VALUE) { + var network = PipeNetwork.get(this.level); + // these are the items that are currently in the pipes, going to this inventory + var onTheWay = network.getItemsOnTheWay(offset, null); + if (onTheWay > 0) { + if (maxAmount < Integer.MAX_VALUE) { + // these are the items on the way, limited to items of the same type as stack + var onTheWaySame = network.getItemsOnTheWay(offset, stack); + // check if any modules are limiting us + if (toInsert.getCount() + onTheWaySame > maxAmount) + toInsert.setCount(maxAmount - onTheWaySame); + } + // totalSpace will be the amount of items that fit into the attached container + var totalSpace = 0; + for (var i = 0; i < handler.getSlots(); i++) { + var copy = stack.copy(); + var maxStackSize = copy.getMaxStackSize(); + // if the container can store more than 64 items in this slot, then it's likely + // a barrel or similar, meaning that the slot limit matters more than the max stack size + var limit = handler.getSlotLimit(i); + if (limit > 64) + maxStackSize = limit; + copy.setCount(maxStackSize); + // this is an inaccurate check since it ignores the fact that some slots might + // have space for items of other types, but it'll be good enough for us + var left = handler.insertItem(i, copy, true); + totalSpace += maxStackSize - left.getCount(); + } + // if the items on the way plus the items we're trying to move are too much, reduce + if (onTheWay + toInsert.getCount() > totalSpace) + toInsert.setCount(totalSpace - onTheWay); + } + } + // we return the item that can actually be inserted, NOT the remainder! + return toInsert; + } + public int getPriority() { return this.priority; } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java index 316fe47..26fa807 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java @@ -35,10 +35,10 @@ public class ExtractionModuleItem extends ModuleItem { if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) return; var filter = this.getItemFilter(module, tile); - var dirSelector = this.getDirectionSelector(module, tile); - if (dirSelector.direction == null) + var dir = this.getDirectionSelector(module, tile).getDirection(); + if (dir == null) return; - var handler = tile.getItemHandler(dirSelector.direction); + var handler = tile.getItemHandler(dir); if (handler == null) return; @@ -49,7 +49,7 @@ public class ExtractionModuleItem extends ModuleItem { continue; if (!filter.isAllowed(stack)) continue; - var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dirSelector.direction), stack, this.preventOversending); + var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), stack, this.preventOversending); if (remain.getCount() != stack.getCount()) { handler.extractItem(j, stack.getCount() - remain.getCount(), false); return; @@ -59,12 +59,12 @@ public class ExtractionModuleItem extends ModuleItem { @Override public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { - return direction != this.getDirectionSelector(module, tile).direction; + return direction != this.getDirectionSelector(module, tile).getDirection(); } @Override public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { - return direction != this.getDirectionSelector(module, tile).direction; + return direction != this.getDirectionSelector(module, tile).getDirection(); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java index 2b7d194..d512e02 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java @@ -1,5 +1,7 @@ package de.ellpeck.prettypipes.pipe.modules.insertion; +import de.ellpeck.prettypipes.misc.DirectionSelector; +import de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; @@ -9,9 +11,10 @@ import net.minecraft.world.inventory.MenuType; import javax.annotation.Nullable; -public class FilterModuleContainer extends AbstractPipeContainer implements IFilteredContainer { +public class FilterModuleContainer extends AbstractPipeContainer implements IFilteredContainer, IDirectionContainer { public ItemFilter filter; + public DirectionSelector directionSelector; public FilterModuleContainer(@Nullable MenuType type, int id, Player player, BlockPos pos, int moduleIndex) { super(type, id, player, pos, moduleIndex); @@ -26,6 +29,8 @@ public class FilterModuleContainer extends AbstractPipeContainer { @Override protected void init() { super.init(); - for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 18 * Mth.ceil(this.menu.filter.getSlots() / 9F) + 2, false)) + var buttonsY = this.topPos + 17 + 32 + 18 * Mth.ceil(this.menu.filter.getSlots() / 9F) + 2; + for (var widget : this.menu.filter.getButtons(this, this.leftPos + this.imageWidth - 7, buttonsY, true)) this.addRenderableWidget(widget); + this.addRenderableWidget(this.menu.directionSelector.getButton(this.leftPos + 7, buttonsY)); } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java index 768fdf0..bd4eb12 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java @@ -4,6 +4,7 @@ import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleTier; +import de.ellpeck.prettypipes.misc.DirectionSelector; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; @@ -26,8 +27,7 @@ public class FilterModuleItem extends ModuleItem { @Override public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { - var filter = this.getItemFilter(module, tile); - return filter.isAllowed(stack); + return this.getDirectionSelector(module, tile).getDirection() != direction || this.getItemFilter(module, tile).isAllowed(stack); } @Override @@ -51,4 +51,9 @@ public class FilterModuleItem extends ModuleItem { filter.canPopulateFromInventories = this.canPopulateFromInventories; return filter; } + + @Override + public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) { + return new DirectionSelector(module, tile); + } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleContainer.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleContainer.java index 0b04e1f..78d2022 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleContainer.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleContainer.java @@ -1,18 +1,20 @@ package de.ellpeck.prettypipes.pipe.modules.retrieval; +import de.ellpeck.prettypipes.misc.DirectionSelector; +import de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer; import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.MenuType; -import net.minecraft.world.inventory.Slot; import javax.annotation.Nullable; -public class RetrievalModuleContainer extends AbstractPipeContainer implements IFilteredContainer { +public class RetrievalModuleContainer extends AbstractPipeContainer implements IFilteredContainer, IDirectionContainer { public ItemFilter filter; + public DirectionSelector directionSelector; public RetrievalModuleContainer(@Nullable MenuType type, int id, Player player, BlockPos pos, int moduleIndex) { super(type, id, player, pos, moduleIndex); @@ -21,6 +23,8 @@ public class RetrievalModuleContainer extends AbstractPipeContainer Date: Thu, 30 Jun 2022 16:38:03 +0200 Subject: [PATCH 3/5] 1.12.4 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 1354b69..4b624c4 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '1.12.3' +version = '1.12.4' group = 'de.ellpeck.prettypipes' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'PrettyPipes' From 626d8efe2e316bd7079696969e7ba4763c5f4b03 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 1 Jul 2022 00:05:11 +0200 Subject: [PATCH 4/5] added default "all sides" option which restores the old behavior --- .../prettypipes/misc/DirectionSelector.java | 43 +++---- .../prettypipes/network/PipeNetwork.java | 2 +- .../prettypipes/pipe/PipeBlockEntity.java | 117 +++++++++--------- .../modules/craft/CraftingModuleItem.java | 2 +- .../extraction/ExtractionModuleItem.java | 37 +++--- .../modules/insertion/FilterModuleItem.java | 2 +- .../retrieval/RetrievalModuleItem.java | 17 ++- .../assets/prettypipes/lang/en_us.json | 2 +- 8 files changed, 106 insertions(+), 116 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java index cebaadc..0af8a19 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/DirectionSelector.java @@ -13,9 +13,13 @@ import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.gui.widget.ExtendedButton; +import org.apache.commons.lang3.ArrayUtils; public class DirectionSelector { + private static final Direction[] ALL = ArrayUtils.addAll(Direction.values(), (Direction) null); + + // null means old behavior, which is all directions private Direction direction; private boolean modified; @@ -35,8 +39,8 @@ public class DirectionSelector { @Override public Component getMessage() { var pipe = DirectionSelector.this.pipe; - var dir = DirectionSelector.this.getDirection(); - MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "none")); + var dir = DirectionSelector.this.direction; + MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "all")); if (dir != null) { var blockName = pipe.getItemHandler(dir) != null ? pipe.getLevel().getBlockState(pipe.getBlockPos().relative(dir)).getBlock().getName() : null; if (blockName != null) @@ -48,7 +52,10 @@ public class DirectionSelector { } public void onButtonPacket() { - var dir = this.getValidDirection(this.getDirection()); + var dir = this.direction; + do { + dir = DirectionSelector.ALL[(ArrayUtils.indexOf(DirectionSelector.ALL, dir) + 1) % DirectionSelector.ALL.length]; + } while (!this.isDirectionValid(dir)); if (this.direction != dir) { this.direction = dir; this.modified = true; @@ -61,9 +68,8 @@ public class DirectionSelector { this.modified = false; var tag = new CompoundTag(); - var dir = this.getDirection(); - if (dir != null) - tag.putString("direction", dir.getName()); + if (this.direction != null) + tag.putString("direction", this.direction.getName()); this.stack.getOrCreateTag().put("direction_selector", tag); } @@ -74,34 +80,25 @@ public class DirectionSelector { } } - public Direction getDirection() { - // default to the first direction with a container if ours is invalid or unset - if (this.direction == null || !this.isDirectionValid(this.direction)) - return this.getValidDirection(this.direction); - return this.direction; + public Direction[] directions() { + return this.direction != null ? new Direction[]{this.direction} : Direction.values(); + } + + public boolean has(Direction dir) { + return this.direction == null || this.direction == dir; } private boolean isDirectionValid(Direction dir) { + if (dir == null) + return true; if (this.pipe.getItemHandler(dir) == null) return false; return this.pipe.streamModules() .filter(p -> p.getLeft() != this.stack) .map(p -> p.getRight().getDirectionSelector(p.getLeft(), this.pipe)) - // don't use getDirection here because we don't want a stack overflow .noneMatch(p -> p != null && p.direction == dir); } - private Direction getValidDirection(Direction dir) { - if (dir == null) - dir = Direction.UP; - for (var i = 0; i < 6; i++) { - dir = Direction.from3DDataValue(dir.get3DDataValue() + 1); - if (this.isDirectionValid(dir)) - return dir; - } - return null; - } - public interface IDirectionContainer { DirectionSelector getSelector(); diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index 8558593..a7c30c8 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -147,7 +147,7 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL if (!this.world.isLoaded(pipePos)) continue; var pipe = this.getPipe(pipePos); - var dest = pipe.getAvailableDestination(stack, false, preventOversending); + var dest = pipe.getAvailableDestination(Direction.values(), stack, false, preventOversending); if (dest == null || dest.getLeft().equals(startInventory)) continue; var sup = (Function) speed -> itemSupplier.apply(dest.getRight(), speed); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java index 0316b62..cbfe68c 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java @@ -167,7 +167,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC } public Pair getAvailableDestinationOrConnectable(ItemStack stack, boolean force, boolean preventOversending) { - var dest = this.getAvailableDestination(stack, force, preventOversending); + var dest = this.getAvailableDestination(Direction.values(), stack, force, preventOversending); if (dest != null) return dest; // if there's no available destination, try inserting into terminals etc. @@ -185,69 +185,66 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC return null; } - public Pair getAvailableDestination(ItemStack stack, boolean force, boolean preventOversending) { - for (var dir : Direction.values()) { - var dest = this.getAvailableDestination(dir, stack, force, preventOversending); - if (!dest.isEmpty()) - return Pair.of(this.worldPosition.relative(dir), dest); + public Pair getAvailableDestination(Direction[] directions, ItemStack stack, boolean force, boolean preventOversending) { + if (!this.canWork()) + return null; + for (var dir : directions) { + var handler = this.getItemHandler(dir); + if (handler == null) + continue; + if (!force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, dir, handler))) + continue; + var remain = ItemHandlerHelper.insertItem(handler, stack, true); + // did we insert anything? + if (remain.getCount() == stack.getCount()) + continue; + var toInsert = stack.copy(); + toInsert.shrink(remain.getCount()); + // limit to the max amount that modules allow us to insert + var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); + if (maxAmount < toInsert.getCount()) + toInsert.setCount(maxAmount); + var offset = this.worldPosition.relative(dir); + if (preventOversending || maxAmount < Integer.MAX_VALUE) { + var network = PipeNetwork.get(this.level); + // these are the items that are currently in the pipes, going to this inventory + var onTheWay = network.getItemsOnTheWay(offset, null); + if (onTheWay > 0) { + if (maxAmount < Integer.MAX_VALUE) { + // these are the items on the way, limited to items of the same type as stack + var onTheWaySame = network.getItemsOnTheWay(offset, stack); + // check if any modules are limiting us + if (toInsert.getCount() + onTheWaySame > maxAmount) + toInsert.setCount(maxAmount - onTheWaySame); + } + // totalSpace will be the amount of items that fit into the attached container + var totalSpace = 0; + for (var i = 0; i < handler.getSlots(); i++) { + var copy = stack.copy(); + var maxStackSize = copy.getMaxStackSize(); + // if the container can store more than 64 items in this slot, then it's likely + // a barrel or similar, meaning that the slot limit matters more than the max stack size + var limit = handler.getSlotLimit(i); + if (limit > 64) + maxStackSize = limit; + copy.setCount(maxStackSize); + // this is an inaccurate check since it ignores the fact that some slots might + // have space for items of other types, but it'll be good enough for us + var left = handler.insertItem(i, copy, true); + totalSpace += maxStackSize - left.getCount(); + } + // if the items on the way plus the items we're trying to move are too much, reduce + if (onTheWay + toInsert.getCount() > totalSpace) + toInsert.setCount(totalSpace - onTheWay); + } + } + // we return the item that can actually be inserted, NOT the remainder! + if (!toInsert.isEmpty()) + return Pair.of(offset, toInsert); } return null; } - public ItemStack getAvailableDestination(Direction direction, ItemStack stack, boolean force, boolean preventOversending) { - if (!this.canWork()) - return ItemStack.EMPTY; - var handler = this.getItemHandler(direction); - if (handler == null || !force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, direction, handler))) - return ItemStack.EMPTY; - var remain = ItemHandlerHelper.insertItem(handler, stack, true); - // did we insert anything? - if (remain.getCount() == stack.getCount()) - return ItemStack.EMPTY; - var toInsert = stack.copy(); - toInsert.shrink(remain.getCount()); - // limit to the max amount that modules allow us to insert - var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); - if (maxAmount < toInsert.getCount()) - toInsert.setCount(maxAmount); - var offset = this.worldPosition.relative(direction); - if (preventOversending || maxAmount < Integer.MAX_VALUE) { - var network = PipeNetwork.get(this.level); - // these are the items that are currently in the pipes, going to this inventory - var onTheWay = network.getItemsOnTheWay(offset, null); - if (onTheWay > 0) { - if (maxAmount < Integer.MAX_VALUE) { - // these are the items on the way, limited to items of the same type as stack - var onTheWaySame = network.getItemsOnTheWay(offset, stack); - // check if any modules are limiting us - if (toInsert.getCount() + onTheWaySame > maxAmount) - toInsert.setCount(maxAmount - onTheWaySame); - } - // totalSpace will be the amount of items that fit into the attached container - var totalSpace = 0; - for (var i = 0; i < handler.getSlots(); i++) { - var copy = stack.copy(); - var maxStackSize = copy.getMaxStackSize(); - // if the container can store more than 64 items in this slot, then it's likely - // a barrel or similar, meaning that the slot limit matters more than the max stack size - var limit = handler.getSlotLimit(i); - if (limit > 64) - maxStackSize = limit; - copy.setCount(maxStackSize); - // this is an inaccurate check since it ignores the fact that some slots might - // have space for items of other types, but it'll be good enough for us - var left = handler.insertItem(i, copy, true); - totalSpace += maxStackSize - left.getCount(); - } - // if the items on the way plus the items we're trying to move are too much, reduce - if (onTheWay + toInsert.getCount() > totalSpace) - toInsert.setCount(totalSpace - onTheWay); - } - } - // we return the item that can actually be inserted, NOT the remainder! - return toInsert; - } - public int getPriority() { return this.priority; } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java index a34bba5..f206d49 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java @@ -75,7 +75,7 @@ public class CraftingModuleItem extends ModuleItem { network.startProfile("crafting_ingredients"); var request = tile.craftIngredientRequests.peek(); var equalityTypes = ItemFilter.getEqualityTypes(tile); - var dest = tile.getAvailableDestination(request.stack, true, true); + var dest = tile.getAvailableDestination(Direction.values(), request.stack, true, true); if (dest != null) { var requestRemain = network.requestExistingItem(request.location, tile.getBlockPos(), dest.getLeft(), request, dest.getRight(), equalityTypes); network.resolveNetworkLock(request); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java index 26fa807..03d7088 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/extraction/ExtractionModuleItem.java @@ -35,36 +35,35 @@ public class ExtractionModuleItem extends ModuleItem { if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) return; var filter = this.getItemFilter(module, tile); - var dir = this.getDirectionSelector(module, tile).getDirection(); - if (dir == null) - return; - var handler = tile.getItemHandler(dir); - if (handler == null) - return; - - var network = PipeNetwork.get(tile.getLevel()); - for (var j = 0; j < handler.getSlots(); j++) { - var stack = handler.extractItem(j, this.maxExtraction, true); - if (stack.isEmpty()) + var dirSelector = this.getDirectionSelector(module, tile); + for (var dir : dirSelector.directions()) { + var handler = tile.getItemHandler(dir); + if (handler == null) continue; - if (!filter.isAllowed(stack)) - continue; - var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), stack, this.preventOversending); - if (remain.getCount() != stack.getCount()) { - handler.extractItem(j, stack.getCount() - remain.getCount(), false); - return; + var network = PipeNetwork.get(tile.getLevel()); + for (var j = 0; j < handler.getSlots(); j++) { + var stack = handler.extractItem(j, this.maxExtraction, true); + if (stack.isEmpty()) + continue; + if (!filter.isAllowed(stack)) + continue; + var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), stack, this.preventOversending); + if (remain.getCount() != stack.getCount()) { + handler.extractItem(j, stack.getCount() - remain.getCount(), false); + return; + } } } } @Override public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { - return direction != this.getDirectionSelector(module, tile).getDirection(); + return !this.getDirectionSelector(module, tile).has(direction); } @Override public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { - return direction != this.getDirectionSelector(module, tile).getDirection(); + return !this.getDirectionSelector(module, tile).has(direction); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java index bd4eb12..92e470f 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleItem.java @@ -27,7 +27,7 @@ public class FilterModuleItem extends ModuleItem { @Override public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { - return this.getDirectionSelector(module, tile).getDirection() != direction || this.getItemFilter(module, tile).isAllowed(stack); + return !this.getDirectionSelector(module, tile).has(direction) || this.getItemFilter(module, tile).isAllowed(stack); } @Override diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java index ea305e7..e2f440c 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java @@ -34,10 +34,7 @@ public class RetrievalModuleItem extends ModuleItem { public void tick(ItemStack module, PipeBlockEntity tile) { if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) return; - var dir = this.getDirectionSelector(module, tile).getDirection(); - if (dir == null) - return; - + var directions = this.getDirectionSelector(module, tile).directions(); var network = PipeNetwork.get(tile.getLevel()); var equalityTypes = ItemFilter.getEqualityTypes(tile); // loop through filters to see which items to pull @@ -48,13 +45,13 @@ public class RetrievalModuleItem extends ModuleItem { continue; var copy = filtered.copy(); copy.setCount(this.maxExtraction); - var dest = tile.getAvailableDestination(dir, copy, true, this.preventOversending); - if (dest.isEmpty()) + var dest = tile.getAvailableDestination(directions, copy, true, this.preventOversending); + if (dest.getRight().isEmpty()) continue; - var remain = dest.copy(); + var remain = dest.getRight().copy(); // are we already waiting for crafting results? If so, don't request those again remain.shrink(network.getCurrentlyCraftingAmount(tile.getBlockPos(), copy, equalityTypes)); - if (network.requestItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), remain, equalityTypes).isEmpty()) + if (network.requestItem(tile.getBlockPos(), dest.getLeft(), remain, equalityTypes).isEmpty()) break; } } @@ -62,12 +59,12 @@ public class RetrievalModuleItem extends ModuleItem { @Override public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { - return direction != this.getDirectionSelector(module, tile).getDirection(); + return !this.getDirectionSelector(module, tile).has(direction); } @Override public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { - return direction != this.getDirectionSelector(module, tile).getDirection(); + return !this.getDirectionSelector(module, tile).has(direction); } @Override diff --git a/src/main/resources/assets/prettypipes/lang/en_us.json b/src/main/resources/assets/prettypipes/lang/en_us.json index e1e7c4d..09ce44a 100644 --- a/src/main/resources/assets/prettypipes/lang/en_us.json +++ b/src/main/resources/assets/prettypipes/lang/en_us.json @@ -97,5 +97,5 @@ "dir.prettypipes.east": "East", "dir.prettypipes.south": "South", "dir.prettypipes.west": "West", - "dir.prettypipes.none": "No Side Valid" + "dir.prettypipes.all": "All Sides" } \ No newline at end of file From c1ccdd848604c288868488a0f8348098f8f487d6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 1 Jul 2022 00:08:38 +0200 Subject: [PATCH 5/5] 1.12.5 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 4b624c4..840b45e 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '1.12.4' +version = '1.12.5' group = 'de.ellpeck.prettypipes' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'PrettyPipes'