almost finished module tabs!

This commit is contained in:
Ellpeck 2020-04-16 04:42:42 +02:00
parent 3646fc4611
commit 0cc6dfc3b7
26 changed files with 453 additions and 243 deletions

View file

@ -1,6 +1,8 @@
package de.ellpeck.prettypipes;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@ -12,6 +14,6 @@ public final class PrettyPipes {
public PrettyPipes() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(Registry::setup);
bus.addListener(Registry::setupClient);
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> bus.addListener(Registry.Client::setup));
}
}

View file

@ -1,13 +1,14 @@
package de.ellpeck.prettypipes;
import com.mojang.datafixers.types.Func;
import de.ellpeck.prettypipes.blocks.pipe.*;
import de.ellpeck.prettypipes.items.ExtractionModuleItem;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.pipe.extraction.ExtractionModuleContainer;
import de.ellpeck.prettypipes.pipe.extraction.ExtractionModuleGui;
import de.ellpeck.prettypipes.pipe.extraction.ExtractionModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.items.WrenchItem;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.*;
import de.ellpeck.prettypipes.pipe.containers.*;
import net.minecraft.block.Block;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.client.renderer.RenderType;
@ -39,7 +40,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
@Mod.EventBusSubscriber(bus = Bus.MOD)
public final class Registry {
@ -58,7 +58,9 @@ public final class Registry {
public static Block pipeBlock;
public static TileEntityType<PipeTileEntity> pipeTileEntity;
public static ContainerType<PipeContainer> pipeContainer;
public static ContainerType<MainPipeContainer> pipeContainer;
public static ContainerType<ExtractionModuleContainer> extractionModuleContainer;
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event) {
@ -90,20 +92,8 @@ public final class Registry {
@SubscribeEvent
public static void registerContainer(RegistryEvent.Register<ContainerType<?>> event) {
event.getRegistry().registerAll(
pipeContainer = (ContainerType<PipeContainer>) IForgeContainerType.create((windowId, inv, data) -> {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, inv.player.world, data.readBlockPos());
int openModule = data.readInt();
IModule module = openModule < 0 ? null : (IModule) tile.modules.getStackInSlot(openModule).getItem();
return tile != null ? new PipeContainer(pipeContainer, windowId, inv.player, tile, module) : null;
}).setRegistryName("pipe")
);
}
private static Item[] createTieredModule(String name, Function<ModuleTier, Item> item) {
List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values())
items.add(item.apply(tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name));
return items.toArray(new Item[0]);
pipeContainer = (ContainerType<MainPipeContainer>) IForgeContainerType.create((windowId, inv, data) -> new MainPipeContainer(pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"),
extractionModuleContainer = (ContainerType<ExtractionModuleContainer>) IForgeContainerType.create((windowId, inv, data) -> new ExtractionModuleContainer(extractionModuleContainer, windowId, inv.player, data.readBlockPos(), data.readInt())).setRegistryName("extraction_module"));
}
public static void setup(FMLCommonSetupEvent event) {
@ -122,9 +112,20 @@ public final class Registry {
PacketHandler.setup();
}
public static void setupClient(FMLClientSetupEvent event) {
RenderTypeLookup.setRenderLayer(pipeBlock, RenderType.cutout());
ClientRegistry.bindTileEntityRenderer(pipeTileEntity, PipeRenderer::new);
ScreenManager.registerFactory(pipeContainer, PipeGui::new);
private static Item[] createTieredModule(String name, Function<ModuleTier, Item> item) {
List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values())
items.add(item.apply(tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name));
return items.toArray(new Item[0]);
}
public static final class Client {
public static void setup(FMLClientSetupEvent event) {
RenderTypeLookup.setRenderLayer(pipeBlock, RenderType.cutout());
ClientRegistry.bindTileEntityRenderer(pipeTileEntity, PipeRenderer::new);
ScreenManager.registerFactory(pipeContainer, MainPipeGui::new);
ScreenManager.registerFactory(extractionModuleContainer, ExtractionModuleGui::new);
}
}
}

View file

@ -1,53 +0,0 @@
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.blocks.pipe.PipeContainer;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraftforge.items.IItemHandler;
public class ExtractionModuleItem extends ModuleItem {
private final int maxExtraction;
private final int speed;
public ExtractionModuleItem(ModuleTier tier) {
this.maxExtraction = tier.forTier(1, 8, 64);
this.speed = tier.forTier(20, 15, 10);
}
@Override
public void tick(PipeTileEntity tile) {
if (tile.getWorld().getGameTime() % this.speed != 0)
return;
PipeNetwork network = PipeNetwork.get(tile.getWorld());
for (Direction dir : Direction.values()) {
IItemHandler handler = tile.getItemHandler(dir);
if (handler == null)
continue;
for (int j = 0; j < handler.getSlots(); j++) {
ItemStack stack = handler.extractItem(j, this.maxExtraction, true);
if (!stack.isEmpty() && network.tryInsertItem(tile.getPos(), tile.getPos().offset(dir), stack)) {
handler.extractItem(j, this.maxExtraction, false);
return;
}
}
}
}
@Override
public boolean canAcceptItem(PipeTileEntity tile, ItemStack stack) {
return false;
}
@Override
public boolean isCompatible(PipeTileEntity tile, IModule other) {
return !(other instanceof ExtractionModuleItem);
}
@Override
public boolean hasContainerTab(PipeTileEntity tile, PipeContainer container) {
return true;
}
}

View file

@ -1,46 +1,25 @@
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.blocks.pipe.PipeContainer;
import de.ellpeck.prettypipes.blocks.pipe.PipeGui;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import net.minecraft.inventory.container.Slot;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.Range;
import java.util.Collections;
import java.util.List;
public interface IModule {
void tick(PipeTileEntity tile);
void tick(ItemStack module, PipeTileEntity tile);
boolean canAcceptItem(PipeTileEntity tile, ItemStack stack);
boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack);
boolean isAvailableDestination(PipeTileEntity tile, ItemStack stack, IItemHandler destination);
boolean isAvailableDestination(ItemStack module, PipeTileEntity tile, ItemStack stack, IItemHandler destination);
int getPriority(PipeTileEntity tile);
int getPriority(ItemStack module, PipeTileEntity tile);
boolean hasContainerTab(PipeTileEntity tile, PipeContainer container);
boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other);
boolean isCompatible(PipeTileEntity tile, IModule other);
boolean hasContainer(ItemStack module, PipeTileEntity tile);
default List<Slot> getContainerSlots(PipeTileEntity tile, PipeContainer container) {
return Collections.emptyList();
}
default Range<Integer> getShiftClickSlots(PipeTileEntity tile, PipeContainer container, ItemStack newStack) {
return null;
}
@OnlyIn(Dist.CLIENT)
default void drawContainerGuiBackground(PipeTileEntity tile, PipeContainer container, PipeGui gui, int mouseX, int mouseY) {
}
@OnlyIn(Dist.CLIENT)
default void drawContainerGuiForeground(PipeTileEntity tile, PipeContainer container, PipeGui gui, int mouseX, int mouseY) {
}
AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex);
}

View file

@ -1,8 +1,10 @@
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.blocks.pipe.PipeContainer;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
@ -13,27 +15,27 @@ public abstract class ModuleItem extends Item implements IModule {
}
@Override
public void tick(PipeTileEntity tile) {
public void tick(ItemStack module, PipeTileEntity tile) {
}
@Override
public boolean canAcceptItem(PipeTileEntity tile, ItemStack stack) {
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) {
return true;
}
@Override
public boolean isAvailableDestination(PipeTileEntity tile, ItemStack stack, IItemHandler destination) {
public boolean isAvailableDestination(ItemStack module, PipeTileEntity tile, ItemStack stack, IItemHandler destination) {
return true;
}
@Override
public int getPriority(PipeTileEntity tile) {
public int getPriority(ItemStack module, PipeTileEntity tile) {
return 0;
}
@Override
public boolean hasContainerTab(PipeTileEntity tile, PipeContainer container) {
return false;
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return null;
}
}

View file

@ -1,8 +1,8 @@
package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.blocks.pipe.PipeBlock;
import de.ellpeck.prettypipes.blocks.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.ConnectionType;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;

View file

@ -1,4 +1,4 @@
package de.ellpeck.prettypipes.events;
package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.network.PipeNetwork;

View file

@ -0,0 +1,86 @@
package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.resources.I18n;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
public class ItemFilter extends ItemStackHandler {
private final ItemStack stack;
private boolean isWhitelist;
public ItemFilter(int size, ItemStack stack) {
super(size);
this.stack = stack;
if (this.stack != null && this.stack.hasTag())
this.deserializeNBT(this.stack.getTag().getCompound("filter"));
}
public List<Slot> getSlots(int x, int y) {
List<Slot> slots = new ArrayList<>();
for (int i = 0; i < this.getSlots(); i++) {
slots.add(new SlotFilter(this, i, x, y));
x += 18;
}
return slots;
}
@OnlyIn(Dist.CLIENT)
public List<Widget> getButtons(int x, int y) {
Supplier<String> whitelistText = () -> I18n.format("info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist"));
return Collections.singletonList(
new Button(x, y, 80, 20, whitelistText.get(), button -> {
// TODO actually make whitelist button work
}));
}
public boolean isAllowed(ItemStack stack) {
for (int i = 0; i < this.getSlots(); i++) {
ItemStack other = this.getStackInSlot(i);
if (ItemHandlerHelper.canItemStacksStack(stack, other)) {
// if we're whitelist, then this is true -> item is allowed
return this.isWhitelist;
}
}
// if we're whitelist, then this is false -> item is disallowed
return !this.isWhitelist;
}
public void save() {
if (this.stack != null)
this.stack.getOrCreateTag().put("filter", this.serializeNBT());
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = super.serializeNBT();
nbt.putBoolean("whitelist", this.isWhitelist);
return nbt;
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
super.deserializeNBT(nbt);
this.isWhitelist = nbt.getBoolean("whitelist");
}
}

View file

@ -0,0 +1,53 @@
package de.ellpeck.prettypipes.misc;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
public class SlotFilter extends SlotItemHandler {
public SlotFilter(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
super(itemHandler, index, xPosition, yPosition);
}
public static boolean checkFilter(Container container, int slotId, PlayerEntity player) {
if (slotId >= 0 && slotId < container.inventorySlots.size()) {
Slot slot = container.getSlot(slotId);
if (slot instanceof SlotFilter) {
((SlotFilter) slot).slotClick(player);
return true;
}
}
return false;
}
private void slotClick(PlayerEntity player) {
ItemStack heldStack = player.inventory.getItemStack();
ItemStack stackInSlot = this.getStack();
if (!stackInSlot.isEmpty() && heldStack.isEmpty()) {
this.putStack(ItemStack.EMPTY);
} else if (!heldStack.isEmpty()) {
ItemStack s = heldStack.copy();
s.setCount(1);
this.putStack(s);
}
}
@Override
public boolean isItemValid(ItemStack stack) {
return false;
}
@Override
public void putStack(ItemStack stack) {
super.putStack(stack.copy());
}
@Override
public boolean canTakeStack(PlayerEntity playerIn) {
return false;
}
}

View file

@ -1,21 +1,15 @@
package de.ellpeck.prettypipes.network;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.INBTSerializable;
import org.jgrapht.graph.DefaultWeightedEdge;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class NetworkEdge extends DefaultWeightedEdge implements INBTSerializable<CompoundNBT> {

View file

@ -1,7 +1,7 @@
package de.ellpeck.prettypipes.network;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;

View file

@ -4,8 +4,8 @@ import com.google.common.collect.Streams;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.blocks.pipe.PipeBlock;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
import net.minecraft.block.BlockState;
@ -20,10 +20,8 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.LazyOptional;
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.ListenableGraph;
import org.jgrapht.alg.connectivity.ConnectivityInspector;
import org.jgrapht.alg.interfaces.ShortestPathAlgorithm;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.event.GraphEdgeChangeEvent;
@ -32,8 +30,6 @@ import org.jgrapht.event.GraphVertexChangeEvent;
import org.jgrapht.graph.DefaultListenableGraph;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.jgrapht.traverse.ClosestFirstIterator;
import org.jgrapht.traverse.DepthFirstIterator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

View file

@ -1,18 +1,22 @@
package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkHooks;
import org.apache.logging.log4j.util.TriConsumer;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;
import java.util.function.Supplier;
public class PacketButton {
@ -60,10 +64,26 @@ public class PacketButton {
public enum ButtonResult {
PIPE_TAB((pos, data, player) -> {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, player.world, pos);
NetworkHooks.openGui((ServerPlayerEntity) player, tile.createContainer(data[0]), buf -> {
buf.writeBlockPos(pos);
buf.writeInt(data[0]);
});
if (data[0] < 0) {
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
} else {
ItemStack stack = tile.modules.getStackInSlot(data[0]);
NetworkHooks.openGui((ServerPlayerEntity) player, new INamedContainerProvider() {
@Override
public ITextComponent getDisplayName() {
return stack.getDisplayName();
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory inv, PlayerEntity player) {
return ((IModule) stack.getItem()).getContainer(stack, tile, windowId, inv, player, data[0]);
}
}, buf -> {
buf.writeBlockPos(pos);
buf.writeInt(data[0]);
});
}
});
public final TriConsumer<BlockPos, int[], PlayerEntity> action;

View file

@ -1,11 +1,9 @@
package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;

View file

@ -1,4 +1,4 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe;
import net.minecraft.util.IStringSerializable;

View file

@ -1,4 +1,4 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe;
import com.google.common.collect.ImmutableMap;
import de.ellpeck.prettypipes.Utility;
@ -61,7 +61,7 @@ public class PipeBlock extends ContainerBlock {
}
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) {
if (!player.getHeldItem(handIn).isEmpty())
return ActionResultType.PASS;
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
@ -70,10 +70,7 @@ public class PipeBlock extends ContainerBlock {
if (!tile.isConnectedInventory())
return ActionResultType.PASS;
if (!worldIn.isRemote)
NetworkHooks.openGui((ServerPlayerEntity) player, tile.createContainer(-1), buf -> {
buf.writeBlockPos(pos);
buf.writeInt(-1);
});
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
return ActionResultType.SUCCESS;
}

View file

@ -1,4 +1,4 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe;
import com.mojang.blaze3d.matrix.MatrixStack;
import de.ellpeck.prettypipes.network.PipeItem;

View file

@ -1,9 +1,10 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
@ -24,15 +25,14 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Stream;
public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity {
public final ItemStackHandler modules = new ItemStackHandler(3) {
@Override
@ -41,7 +41,7 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
if (!(item instanceof IModule))
return false;
IModule module = (IModule) item;
return PipeTileEntity.this.streamModules().allMatch(m -> module.isCompatible(PipeTileEntity.this, m) && m.isCompatible(PipeTileEntity.this, module));
return PipeTileEntity.this.streamModules().allMatch(m -> module.isCompatible(stack, PipeTileEntity.this, m.getRight()) && m.getRight().isCompatible(m.getLeft(), PipeTileEntity.this, module));
}
@Override
@ -89,7 +89,7 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
IProfiler profiler = this.world.getProfiler();
profiler.startSection("ticking_modules");
this.streamModules().forEach(m -> m.tick(this));
this.streamModules().forEach(m -> m.getRight().tick(m.getLeft(), this));
profiler.endSection();
profiler.startSection("ticking_items");
@ -103,7 +103,7 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
}
public BlockPos getAvailableDestination(ItemStack stack) {
if (this.streamModules().anyMatch(u -> !u.canAcceptItem(this, stack)))
if (this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack)))
return null;
for (Direction dir : Direction.values()) {
IItemHandler handler = this.getItemHandler(dir);
@ -111,7 +111,7 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
continue;
if (!ItemHandlerHelper.insertItem(handler, stack, true).isEmpty())
continue;
if (this.streamModules().anyMatch(u -> !u.isAvailableDestination(this, stack, handler)))
if (this.streamModules().anyMatch(m -> !m.getRight().isAvailableDestination(m.getLeft(), this, stack, handler)))
continue;
return this.pos.offset(dir);
}
@ -119,7 +119,7 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
}
public int getPriority() {
return this.streamModules().mapToInt(u -> u.getPriority(this)).max().orElse(0);
return this.streamModules().mapToInt(m -> m.getRight().getPriority(m.getLeft(), this)).max().orElse(0);
}
public IItemHandler getItemHandler(Direction dir) {
@ -139,35 +139,25 @@ public class PipeTileEntity extends TileEntity implements ITickableTileEntity {
return Arrays.stream(Direction.values()).anyMatch(this::isConnectedInventory);
}
public Stream<IModule> streamModules() {
Stream.Builder<IModule> builder = Stream.builder();
public Stream<Pair<ItemStack, IModule>> streamModules() {
Stream.Builder<Pair<ItemStack, IModule>> builder = Stream.builder();
for (int i = 0; i < this.modules.getSlots(); i++) {
ItemStack stack = this.modules.getStackInSlot(i);
if (stack.isEmpty())
continue;
builder.accept((IModule) stack.getItem());
builder.accept(Pair.of(stack, (IModule) stack.getItem()));
}
return builder.build();
}
public INamedContainerProvider createContainer(int openModule) {
ItemStack moduleStack = openModule < 0 ? null : this.modules.getStackInSlot(openModule);
return new INamedContainerProvider() {
@Override
public ITextComponent getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".pipe");
}
@Override
public ITextComponent getDisplayName() {
if (moduleStack != null)
return moduleStack.getDisplayName();
return new TranslationTextComponent("container." + PrettyPipes.ID + ".pipe");
}
@Nullable
@Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) {
IModule module = moduleStack == null ? null : (IModule) moduleStack.getItem();
return new PipeContainer(Registry.pipeContainer, window, player, PipeTileEntity.this, module);
}
};
@Nullable
@Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) {
return new MainPipeContainer(Registry.pipeContainer, window, player, PipeTileEntity.this.pos);
}
}

View file

@ -1,33 +1,33 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe.containers;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.SlotFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.SlotItemHandler;
import org.apache.commons.lang3.Range;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nullable;
public class PipeContainer extends Container {
public abstract class AbstractPipeContainer<T extends IModule> extends Container {
public final PipeTileEntity tile;
public final IModule openModule;
public final T module;
public final ItemStack moduleStack;
public PipeContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, PipeTileEntity tile, IModule openModule) {
public AbstractPipeContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) {
super(type, id);
this.tile = tile;
this.openModule = openModule;
this.tile = Utility.getTileEntity(PipeTileEntity.class, player.world, pos);
this.moduleStack = moduleIndex < 0 ? null : this.tile.modules.getStackInSlot(moduleIndex);
this.module = moduleIndex < 0 ? null : (T) this.moduleStack.getItem();
if (openModule == null) {
for (int i = 0; i < 3; i++)
this.addSlot(new SlotItemHandler(tile.modules, i, 62 + i * 18, 17 + 32));
} else {
for (Slot slot : openModule.getContainerSlots(tile, this))
this.addSlot(slot);
}
// needs to be done here so transferStackInSlot works correctly, bleh
this.addSlots();
for (int l = 0; l < 3; ++l)
for (int j1 = 0; j1 < 9; ++j1)
@ -36,6 +36,8 @@ public class PipeContainer extends Container {
this.addSlot(new Slot(player.inventory, i1, 8 + i1 * 18, 147 + 32));
}
protected abstract void addSlots();
@Override
public boolean canInteractWith(PlayerEntity playerIn) {
return true;
@ -53,29 +55,16 @@ public class PipeContainer extends Container {
ItemStack newStack = slot.getStack();
ItemStack currentStack = newStack.copy();
inv:
if (slotIndex >= inventoryStart) {
// shift into this container here
// mergeItemStack with the slots that newStack should go into
// return an empty stack if mergeItemStack fails
if (this.openModule == null) {
if (newStack.getItem() instanceof IModule) {
if (!this.mergeItemStack(newStack, 0, 3, false))
return ItemStack.EMPTY;
break inv;
}
} else {
// bleh
Range<Integer> range = this.openModule.getShiftClickSlots(this.tile, this, newStack);
if (range != null) {
if (!this.mergeItemStack(newStack, range.getMinimum(), range.getMaximum(), false))
return ItemStack.EMPTY;
break inv;
}
if (newStack.getItem() instanceof IModule) {
if (!this.mergeItemStack(newStack, 0, 3, false))
return ItemStack.EMPTY;
}
// end custom code
if (slotIndex >= inventoryStart && slotIndex <= inventoryEnd) {
else if (slotIndex >= inventoryStart && slotIndex <= inventoryEnd) {
if (!this.mergeItemStack(newStack, hotbarStart, hotbarEnd + 1, false))
return ItemStack.EMPTY;
} else if (slotIndex >= inventoryEnd + 1 && slotIndex < hotbarEnd + 1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd + 1, false)) {
@ -96,4 +85,11 @@ public class PipeContainer extends Container {
}
return ItemStack.EMPTY;
}
@Override
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
if (SlotFilter.checkFilter(this, slotId, player))
return ItemStack.EMPTY;
return super.slotClick(slotId, dragType, clickTypeIn, player);
}
}

View file

@ -1,34 +1,29 @@
package de.ellpeck.prettypipes.blocks.pipe;
package de.ellpeck.prettypipes.pipe.containers;
import com.mojang.blaze3d.systems.RenderSystem;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.audio.SimpleSound;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.text.ITextComponent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PipeGui extends ContainerScreen<PipeContainer> {
public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extends ContainerScreen<T> {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/pipe.png");
private final List<Tab> tabs = new ArrayList<>();
private int lastTabAmount;
public PipeGui(PipeContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public AbstractPipeGui(T screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn);
this.xSize = 176;
this.ySize = 171 + 32;
@ -51,9 +46,6 @@ public class PipeGui extends ContainerScreen<PipeContainer> {
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
this.font.drawString(this.playerInventory.getDisplayName().getFormattedText(), 8, this.ySize - 96 + 2, 4210752);
this.font.drawString(this.title.getFormattedText(), 8, 6 + 32, 4210752);
if (this.container.openModule != null)
this.container.openModule.drawContainerGuiForeground(this.container.tile, this.container, this, mouseX, mouseY);
for (Tab tab : this.tabs)
tab.drawForeground(mouseX, mouseY);
}
@ -66,11 +58,11 @@ public class PipeGui extends ContainerScreen<PipeContainer> {
for (Tab tab : this.tabs)
tab.draw();
if (this.container.openModule == null) {
for (int i = 0; i < 3; i++)
this.blit(this.guiLeft + 61 + i * 18, this.guiTop + 32 + 16, 176, 62, 18, 18);
} else {
this.container.openModule.drawContainerGuiBackground(this.container.tile, this.container, this, mouseX, mouseY);
// draw the slots since we're using a blank ui
for (Slot slot : this.container.inventorySlots) {
if (slot.inventory == this.playerInventory)
continue;
this.blit(this.guiLeft + slot.xPos - 1, this.guiTop + slot.yPos - 1, 176, 62, 18, 18);
}
}
@ -100,7 +92,7 @@ public class PipeGui extends ContainerScreen<PipeContainer> {
if (stack.isEmpty())
continue;
IModule module = (IModule) stack.getItem();
if (module.hasContainerTab(this.container.tile, this.container))
if (module.hasContainer(stack, this.container.tile))
this.tabs.add(new Tab(stack, module, this.tabs.size(), i));
}
}
@ -116,8 +108,8 @@ public class PipeGui extends ContainerScreen<PipeContainer> {
this.moduleStack = moduleStack;
this.module = module;
this.index = index;
this.x = PipeGui.this.guiLeft + 5 + tabIndex * 28;
this.y = PipeGui.this.guiTop;
this.x = AbstractPipeGui.this.guiLeft + 5 + tabIndex * 28;
this.y = AbstractPipeGui.this.guiTop;
}
private void draw() {
@ -125,33 +117,33 @@ public class PipeGui extends ContainerScreen<PipeContainer> {
int v = 0;
int height = 30;
int itemOffset = 9;
if (this.module == PipeGui.this.container.openModule) {
if (this.module == AbstractPipeGui.this.container.module) {
y = 0;
v = 30;
height = 32;
itemOffset = 7;
}
PipeGui.this.blit(this.x, this.y + y, 176, v, 28, height);
AbstractPipeGui.this.blit(this.x, this.y + y, 176, v, 28, height);
PipeGui.this.itemRenderer.renderItemIntoGUI(this.moduleStack, this.x + 6, this.y + itemOffset);
PipeGui.this.getMinecraft().getTextureManager().bindTexture(TEXTURE);
AbstractPipeGui.this.itemRenderer.renderItemIntoGUI(this.moduleStack, this.x + 6, this.y + itemOffset);
AbstractPipeGui.this.getMinecraft().getTextureManager().bindTexture(TEXTURE);
}
private void drawForeground(int mouseX, int mouseY) {
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
return;
PipeGui.this.renderTooltip(this.moduleStack.getDisplayName().getFormattedText(), mouseX - PipeGui.this.guiLeft, mouseY - PipeGui.this.guiTop);
AbstractPipeGui.this.renderTooltip(this.moduleStack.getDisplayName().getFormattedText(), mouseX - AbstractPipeGui.this.guiLeft, mouseY - AbstractPipeGui.this.guiTop);
}
private boolean onClicked(double mouseX, double mouseY, int button) {
if (this.module == PipeGui.this.container.openModule)
if (this.module == AbstractPipeGui.this.container.module)
return false;
if (button != 0)
return false;
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
return false;
PacketHandler.sendToServer(new PacketButton(PipeGui.this.container.tile.getPos(), PacketButton.ButtonResult.PIPE_TAB, this.index));
PipeGui.this.getMinecraft().getSoundHandler().play(SimpleSound.master(SoundEvents.UI_BUTTON_CLICK, 1));
PacketHandler.sendToServer(new PacketButton(AbstractPipeGui.this.container.tile.getPos(), PacketButton.ButtonResult.PIPE_TAB, this.index));
AbstractPipeGui.this.getMinecraft().getSoundHandler().play(SimpleSound.master(SoundEvents.UI_BUTTON_CLICK, 1));
return true;
}
}

View file

@ -0,0 +1,21 @@
package de.ellpeck.prettypipes.pipe.containers;
import de.ellpeck.prettypipes.items.IModule;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.SlotItemHandler;
import javax.annotation.Nullable;
public class MainPipeContainer extends AbstractPipeContainer<IModule> {
public MainPipeContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos) {
super(type, id, player, pos, -1);
}
@Override
protected void addSlots() {
for (int i = 0; i < 3; i++)
this.addSlot(new SlotItemHandler(this.tile.modules, i, 62 + i * 18, 17 + 32));
}
}

View file

@ -0,0 +1,10 @@
package de.ellpeck.prettypipes.pipe.containers;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.text.ITextComponent;
public class MainPipeGui extends AbstractPipeGui<MainPipeContainer> {
public MainPipeGui(MainPipeContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn);
}
}

View file

@ -0,0 +1,32 @@
package de.ellpeck.prettypipes.pipe.extraction;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nullable;
public class ExtractionModuleContainer extends AbstractPipeContainer<ExtractionModuleItem> {
public ItemFilter filter;
public ExtractionModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex);
}
@Override
protected void addSlots() {
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack);
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
this.addSlot(slot);
}
@Override
public void onContainerClosed(PlayerEntity playerIn) {
super.onContainerClosed(playerIn);
this.filter.save();
}
}

View file

@ -0,0 +1,19 @@
package de.ellpeck.prettypipes.pipe.extraction;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.text.ITextComponent;
public class ExtractionModuleGui extends AbstractPipeGui<ExtractionModuleContainer> {
public ExtractionModuleGui(ExtractionModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn);
}
@Override
protected void init() {
super.init();
for (Widget widget : this.container.filter.getButtons(this.guiLeft + 7, this.guiTop + 17 + 32 + 20))
this.addButton(widget);
}
}

View file

@ -0,0 +1,73 @@
package de.ellpeck.prettypipes.pipe.extraction;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraftforge.items.IItemHandler;
public class ExtractionModuleItem extends ModuleItem {
private final int maxExtraction;
private final int speed;
public final int filterSlots;
public ExtractionModuleItem(ModuleTier tier) {
this.maxExtraction = tier.forTier(1, 8, 64);
this.speed = tier.forTier(20, 15, 10);
this.filterSlots = tier.forTier(3, 6, 9);
}
@Override
public void tick(ItemStack module, PipeTileEntity tile) {
if (tile.getWorld().getGameTime() % this.speed != 0)
return;
ItemFilter filter = new ItemFilter(this.filterSlots, module);
PipeNetwork network = PipeNetwork.get(tile.getWorld());
for (Direction dir : Direction.values()) {
IItemHandler handler = tile.getItemHandler(dir);
if (handler == null)
continue;
for (int j = 0; j < handler.getSlots(); j++) {
ItemStack stack = handler.extractItem(j, this.maxExtraction, true);
if (stack.isEmpty())
continue;
if (!filter.isAllowed(stack))
continue;
if (network.tryInsertItem(tile.getPos(), tile.getPos().offset(dir), stack)) {
handler.extractItem(j, this.maxExtraction, false);
return;
}
}
}
}
@Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) {
return false;
}
@Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) {
return !(other instanceof ExtractionModuleItem);
}
@Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) {
return true;
}
@Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return new ExtractionModuleContainer(Registry.extractionModuleContainer, windowId, player, tile.getPos(), moduleIndex);
}
}

View file

@ -5,5 +5,7 @@
"item.prettypipes.high_extraction_module": "High Extraction Module",
"block.prettypipes.pipe": "Pipe",
"itemGroup.prettypipes": "Pretty Pipes",
"container.prettypipes.pipe": "Pipe"
"container.prettypipes.pipe": "Pipe",
"info.prettypipes.whitelist": "Allowed",
"info.prettypipes.blacklist": "Disallowed"
}