finished the item terminal, yay

This commit is contained in:
Ellpeck 2020-05-07 23:06:35 +02:00
parent 01dda04701
commit 5541d5d1a4
14 changed files with 164 additions and 11 deletions

View file

@ -0,0 +1,17 @@
package de.ellpeck.prettypipes.misc;
import net.minecraft.item.ItemStack;
import java.util.Comparator;
public enum ItemOrder {
AMOUNT(Comparator.comparingInt(ItemStack::getCount)),
NAME(Comparator.comparing(s -> s.getDisplayName().getFormattedText())),
MOD(Comparator.comparing(s -> s.getItem().getRegistryName().getNamespace()));
public final Comparator<ItemStack> comparator;
ItemOrder(Comparator<ItemStack> comparator) {
this.comparator = comparator;
}
}

View file

@ -29,6 +29,7 @@ public class ItemTerminalWidget extends Widget {
this.gridX = gridX; this.gridX = gridX;
this.gridY = gridY; this.gridY = gridY;
this.screen = screen; this.screen = screen;
this.visible = false;
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
@ -7,6 +8,7 @@ import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem; import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
@ -107,6 +109,14 @@ public class PacketButton {
STACK_SIZE_AMOUNT((pos, data, player) -> { STACK_SIZE_AMOUNT((pos, data, player) -> {
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer; AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer;
StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]); StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]);
}),
TERMINAL_ORDER((pos, data, player) -> {
player.getPersistentData().putInt(PrettyPipes.ID + ":item_order", data[0]);
Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, pos).updateItems(player);
}),
TERMINAL_ASCENDING((pos, data, player) -> {
player.getPersistentData().putBoolean(PrettyPipes.ID + ":ascending", data[0] > 0);
Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, pos).updateItems(player);
}); });
public final TriConsumer<BlockPos, int[], PlayerEntity> action; public final TriConsumer<BlockPos, int[], PlayerEntity> action;

View file

@ -1,6 +1,7 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemOrder;
import de.ellpeck.prettypipes.network.PipeItem; import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer;
@ -21,9 +22,13 @@ import java.util.function.Supplier;
public class PacketNetworkItems { public class PacketNetworkItems {
private List<ItemStack> items; private List<ItemStack> items;
private ItemOrder order;
private boolean ascending;
public PacketNetworkItems(List<ItemStack> items) { public PacketNetworkItems(List<ItemStack> items, ItemOrder order, boolean ascending) {
this.items = items; this.items = items;
this.order = order;
this.ascending = ascending;
} }
private PacketNetworkItems() { private PacketNetworkItems() {
@ -38,6 +43,8 @@ public class PacketNetworkItems {
stack.setCount(buf.readVarInt()); stack.setCount(buf.readVarInt());
client.items.add(stack); client.items.add(stack);
} }
client.order = ItemOrder.values()[buf.readByte()];
client.ascending = buf.readBoolean();
return client; return client;
} }
@ -49,6 +56,8 @@ public class PacketNetworkItems {
buf.writeItemStack(copy); buf.writeItemStack(copy);
buf.writeVarInt(stack.getCount()); buf.writeVarInt(stack.getCount());
} }
buf.writeByte(packet.order.ordinal());
buf.writeBoolean(packet.ascending);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -58,7 +67,7 @@ public class PacketNetworkItems {
public void run() { public void run() {
Minecraft mc = Minecraft.getInstance(); Minecraft mc = Minecraft.getInstance();
if (mc.currentScreen instanceof ItemTerminalGui) if (mc.currentScreen instanceof ItemTerminalGui)
((ItemTerminalGui) mc.currentScreen).updateItemList(message.items); ((ItemTerminalGui) mc.currentScreen).updateItemList(message.items, message.order, message.ascending);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -1,6 +1,7 @@
package de.ellpeck.prettypipes.terminal; package de.ellpeck.prettypipes.terminal;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.network.PipeItem;
import de.ellpeck.prettypipes.pipe.ConnectionType; import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
@ -9,20 +10,24 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.ContainerBlock; import net.minecraft.block.ContainerBlock;
import net.minecraft.block.SoundType; import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.IBlockReader; import net.minecraft.world.IBlockReader;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks; import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List;
public class ItemTerminalBlock extends ContainerBlock implements IPipeConnectable { public class ItemTerminalBlock extends ContainerBlock implements IPipeConnectable {
public ItemTerminalBlock() { public ItemTerminalBlock() {
@ -32,7 +37,7 @@ public class ItemTerminalBlock extends ContainerBlock implements IPipeConnectabl
@Override @Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) { public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) {
ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, worldIn, pos); ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, worldIn, pos);
if (tile == null) if (tile == null || tile.getConnectedPipe() == null)
return ActionResultType.PASS; return ActionResultType.PASS;
if (!worldIn.isRemote) { if (!worldIn.isRemote) {
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
@ -41,6 +46,16 @@ public class ItemTerminalBlock extends ContainerBlock implements IPipeConnectabl
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, worldIn, pos);
if (tile != null)
Utility.dropInventory(tile, tile.items);
super.onReplaced(state, worldIn, pos, newState, isMoving);
}
}
@Nullable @Nullable
@Override @Override
public TileEntity createNewTileEntity(IBlockReader worldIn) { public TileEntity createNewTileEntity(IBlockReader worldIn) {
@ -66,4 +81,9 @@ public class ItemTerminalBlock extends ContainerBlock implements IPipeConnectabl
public BlockRenderType getRenderType(BlockState state) { public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL; return BlockRenderType.MODEL;
} }
@Override
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
}
} }

View file

@ -4,6 +4,7 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.EquatableItemStack; import de.ellpeck.prettypipes.misc.EquatableItemStack;
import de.ellpeck.prettypipes.misc.ItemOrder;
import de.ellpeck.prettypipes.network.NetworkItem; import de.ellpeck.prettypipes.network.NetworkItem;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.PipeItem; import de.ellpeck.prettypipes.network.PipeItem;
@ -48,7 +49,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
}; };
public Map<EquatableItemStack, NetworkItem> networkItems; public Map<EquatableItemStack, NetworkItem> networkItems;
private Queue<Triple<NetworkLocation, Integer, Integer>> pendingRequests = new ArrayDeque<>(); private final Queue<Triple<NetworkLocation, Integer, Integer>> pendingRequests = new ArrayDeque<>();
public ItemTerminalTileEntity() { public ItemTerminalTileEntity() {
super(Registry.itemTerminalTileEntity); super(Registry.itemTerminalTileEntity);
@ -95,7 +96,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
} }
private PipeTileEntity getConnectedPipe() { public PipeTileEntity getConnectedPipe() {
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
for (Direction dir : Direction.values()) { for (Direction dir : Direction.values()) {
PipeTileEntity pipe = network.getPipe(this.pos.offset(dir)); PipeTileEntity pipe = network.getPipe(this.pos.offset(dir));
@ -106,6 +107,8 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
public void updateItems(PlayerEntity... playersToSync) { public void updateItems(PlayerEntity... playersToSync) {
if (this.getConnectedPipe() == null)
return;
this.networkItems = this.collectItems(); this.networkItems = this.collectItems();
if (playersToSync.length > 0) { if (playersToSync.length > 0) {
List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList()); List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList());
@ -115,7 +118,11 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile; ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile;
if (tile != this) if (tile != this)
continue; continue;
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems));
CompoundNBT nbt = player.getPersistentData();
ItemOrder order = ItemOrder.values()[nbt.getInt(PrettyPipes.ID + ":item_order")];
boolean ascending = nbt.getBoolean(PrettyPipes.ID + ":ascending");
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, order, ascending));
} }
} }
} }

View file

@ -1,7 +1,9 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.misc.ItemOrder;
import de.ellpeck.prettypipes.misc.ItemTerminalWidget; import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest; import de.ellpeck.prettypipes.packets.PacketRequest;
import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.gui.screen.inventory.ContainerScreen;
@ -14,8 +16,7 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import java.util.List; import java.util.*;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -25,8 +26,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private Button minusButton; private Button minusButton;
private Button plusButton; private Button plusButton;
private Button requestButton; private Button requestButton;
private Button orderButton;
private Button ascendingButton;
private int requestAmount = 1; private int requestAmount = 1;
private int scrollOffset; private int scrollOffset;
private ItemOrder order;
private boolean ascending;
public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
@ -63,6 +68,18 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
this.requestAmount = 1; this.requestAmount = 1;
})); }));
this.requestButton.active = false; this.requestButton.active = false;
this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> {
if (this.items == null)
return;
int order = (this.order.ordinal() + 1) % ItemOrder.values().length;
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ORDER, order));
}));
this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> {
if (this.items == null)
return;
int asc = !this.ascending ? 1 : 0;
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc));
}));
for (int y = 0; y < 4; y++) { for (int y = 0; y < 4; y++) {
for (int x = 0; x < 9; x++) for (int x = 0; x < 9; x++)
this.addButton(new ItemTerminalWidget(this.guiLeft + 8 + x * 18, this.guiTop + 18 + y * 18, x, y, this)); this.addButton(new ItemTerminalWidget(this.guiLeft + 8 + x * 18, this.guiTop + 18 + y * 18, x, y, this));
@ -77,12 +94,22 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
this.minusButton.active = this.requestAmount > 1; this.minusButton.active = this.requestAmount > 1;
} }
public void updateItemList(List<ItemStack> items) { public void updateItemList(List<ItemStack> items, ItemOrder order, boolean ascending) {
this.items = items; this.order = order;
this.ascending = ascending;
this.items = new ArrayList<>(items);
this.updateWidgets(); this.updateWidgets();
this.ascendingButton.setMessage(this.ascending ? "^" : "v");
this.orderButton.setMessage(this.order.name().substring(0, 1));
} }
private void updateWidgets() { private void updateWidgets() {
Comparator<ItemStack> comparator = this.order.comparator;
if (!this.ascending)
comparator = comparator.reversed();
this.items.sort(comparator);
List<ItemTerminalWidget> widgets = this.streamWidgets().collect(Collectors.toList()); List<ItemTerminalWidget> widgets = this.streamWidgets().collect(Collectors.toList());
for (int i = 0; i < widgets.size(); i++) { for (int i = 0; i < widgets.size(); i++) {
ItemTerminalWidget widget = widgets.get(i); ItemTerminalWidget widget = widgets.get(i);
@ -105,6 +132,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
if (widget instanceof ItemTerminalWidget) if (widget instanceof ItemTerminalWidget)
widget.renderToolTip(mouseX, mouseY); widget.renderToolTip(mouseX, mouseY);
} }
if (this.items != null) {
if (this.orderButton.isHovered())
this.renderTooltip(I18n.format("info." + PrettyPipes.ID + ".order", I18n.format("info." + PrettyPipes.ID + ".order." + this.order.name().toLowerCase(Locale.ROOT))), mouseX, mouseY);
if (this.ascendingButton.isHovered())
this.renderTooltip(I18n.format("info." + PrettyPipes.ID + "." + (this.ascending ? "ascending" : "descending")), mouseX, mouseY);
}
this.renderHoveredToolTip(mouseX, mouseY); this.renderHoveredToolTip(mouseX, mouseY);
} }

View file

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "prettypipes:block/item_terminal"
}
}
}

View file

@ -33,7 +33,9 @@
"info.prettypipes.nbt_filter_modifier": "Causes any filter slots to filter by item data (NBT)", "info.prettypipes.nbt_filter_modifier": "Causes any filter slots to filter by item data (NBT)",
"info.prettypipes.tag_filter_modifier": "Causes any filter slots to filter by tags\n(Modern equivalent of the Ore Dictionary)", "info.prettypipes.tag_filter_modifier": "Causes any filter slots to filter by tags\n(Modern equivalent of the Ore Dictionary)",
"info.prettypipes.redstone_module": "Allows disabling the pipe with a redstone signal\nWorks for both extraction and retrieval", "info.prettypipes.redstone_module": "Allows disabling the pipe with a redstone signal\nWorks for both extraction and retrieval",
"info.prettypipes.item_terminal": "Allows viewing and requesting all items in a pipe network\nAlso has slots for putting items into the network",
"block.prettypipes.pipe": "Pipe", "block.prettypipes.pipe": "Pipe",
"block.prettypipes.item_terminal": "Item Terminal",
"itemGroup.prettypipes": "Pretty Pipes", "itemGroup.prettypipes": "Pretty Pipes",
"container.prettypipes.pipe": "Pipe", "container.prettypipes.pipe": "Pipe",
"container.prettypipes.item_terminal": "Item Terminal", "container.prettypipes.item_terminal": "Item Terminal",
@ -47,5 +49,11 @@
"info.prettypipes.limit_to_max_off": "Don't limit to one stack", "info.prettypipes.limit_to_max_off": "Don't limit to one stack",
"info.prettypipes.request": "Request", "info.prettypipes.request": "Request",
"info.prettypipes.not_found": "%s not found", "info.prettypipes.not_found": "%s not found",
"info.prettypipes.sending": "Sending %s %s" "info.prettypipes.sending": "Sending %s %s",
"info.prettypipes.order": "Order by %s",
"info.prettypipes.order.amount": "Amount",
"info.prettypipes.order.name": "Name",
"info.prettypipes.order.mod": "Mod",
"info.prettypipes.ascending": "Ascending",
"info.prettypipes.descending": "Descending"
} }

View file

@ -0,0 +1,7 @@
{
"parent": "block/cube_column",
"textures": {
"end": "prettypipes:block/item_terminal_top",
"side": "prettypipes:block/item_terminal"
}
}

View file

@ -0,0 +1,3 @@
{
"parent": "prettypipes:block/item_terminal"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 486 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 B

View file

@ -0,0 +1,31 @@
{
"type": "minecraft:crafting_shaped",
"pattern": [
"DPI",
"RCE",
"IPD"
],
"key": {
"E": {
"item": "prettypipes:high_extraction_module"
},
"R": {
"item": "prettypipes:high_retrieval_module"
},
"P": {
"item": "minecraft:ender_pearl"
},
"I": {
"item": "minecraft:iron_block"
},
"C": {
"item": "minecraft:chest"
},
"D": {
"item": "minecraft:diamond_block"
}
},
"result": {
"item": "prettypipes:item_terminal"
}
}