added a display that shows items that are currently being crafted

This commit is contained in:
Ell 2020-10-20 00:52:56 +02:00
parent 3dcda2f69a
commit 76f332c03e
9 changed files with 123 additions and 19 deletions

View file

@ -30,6 +30,7 @@ import net.minecraftforge.event.TickEvent.ClientTickEvent;
import net.minecraftforge.event.world.PistonEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -67,7 +68,13 @@ public class JEIPrettyPipesPlugin implements IModPlugin {
registration.addGuiContainerHandler(ItemTerminalGui.class, new IGuiContainerHandler<ItemTerminalGui>() {
@Override
public List<Rectangle2d> getGuiExtraAreas(ItemTerminalGui containerScreen) {
return Collections.singletonList(new Rectangle2d(containerScreen.getGuiLeft() - 22, containerScreen.getGuiTop(), 22, 64));
List<Rectangle2d> ret = new ArrayList<>();
// sorting buttons
ret.add(new Rectangle2d(containerScreen.getGuiLeft() - 22, containerScreen.getGuiTop(), 22, 64));
// crafting hud
if (containerScreen.currentlyCrafting != null && !containerScreen.currentlyCrafting.isEmpty())
ret.add(new Rectangle2d(containerScreen.getGuiLeft() + containerScreen.getXSize(), containerScreen.getGuiTop() + 4, 65, 89));
return ret;
}
});
}

View file

@ -22,7 +22,6 @@ import java.util.List;
public class ItemTerminalWidget extends Widget {
private static final ResourceLocation FONT = new ResourceLocation(PrettyPipes.ID, "unicode");
private final ItemTerminalGui screen;
public final int gridX;
public final int gridY;

View file

@ -114,6 +114,10 @@ public class PacketButton {
CRAFT_TERMINAL_REQUEST((pos, data, player) -> {
CraftingTerminalTileEntity tile = Utility.getTileEntity(CraftingTerminalTileEntity.class, player.world, pos);
tile.requestCraftingItems(player, data[0]);
}),
CANCEL_CRAFTING((pos, data, player) -> {
ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, pos);
tile.cancelCrafting();
});
public final TriConsumer<BlockPos, int[], PlayerEntity> action;

View file

@ -23,10 +23,12 @@ public class PacketNetworkItems {
private List<ItemStack> items;
private List<ItemStack> craftables;
private List<ItemStack> currentlyCrafting;
public PacketNetworkItems(List<ItemStack> items, List<ItemStack> craftables) {
public PacketNetworkItems(List<ItemStack> items, List<ItemStack> craftables, List<ItemStack> currentlyCrafting) {
this.items = items;
this.craftables = craftables;
this.currentlyCrafting = currentlyCrafting;
}
private PacketNetworkItems() {
@ -44,6 +46,9 @@ public class PacketNetworkItems {
client.craftables = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--)
client.craftables.add(buf.readItemStack());
client.currentlyCrafting = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--)
client.currentlyCrafting.add(buf.readItemStack());
return client;
}
@ -58,6 +63,9 @@ public class PacketNetworkItems {
buf.writeVarInt(packet.craftables.size());
for (ItemStack stack : packet.craftables)
buf.writeItemStack(stack);
buf.writeVarInt(packet.currentlyCrafting.size());
for (ItemStack stack : packet.currentlyCrafting)
buf.writeItemStack(stack);
}
@SuppressWarnings("Convert2Lambda")
@ -67,7 +75,7 @@ public class PacketNetworkItems {
public void run() {
Minecraft mc = Minecraft.getInstance();
if (mc.currentScreen instanceof ItemTerminalGui)
((ItemTerminalGui) mc.currentScreen).updateItemList(message.items, message.craftables);
((ItemTerminalGui) mc.currentScreen).updateItemList(message.items, message.craftables, message.currentlyCrafting);
}
});
ctx.get().setPacketHandled(true);

View file

@ -133,15 +133,17 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return;
this.networkItems = this.collectItems();
if (playersToSync.length > 0) {
List<Pair<BlockPos, ItemStack>> craftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos());
List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList());
List<ItemStack> clientCraftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos()).stream().map(Pair::getRight).collect(Collectors.toList());
List<ItemStack> clientCraftables = craftables.stream().map(Pair::getRight).collect(Collectors.toList());
List<ItemStack> currentlyCrafting = this.getCurrentlyCrafting(craftables).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList());
for (PlayerEntity player : playersToSync) {
if (!(player.openContainer instanceof ItemTerminalContainer))
continue;
ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile;
if (tile != this)
continue;
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables));
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables, currentlyCrafting));
}
}
}
@ -190,6 +192,47 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return items;
}
private List<ItemStack> getCurrentlyCrafting(List<Pair<BlockPos, ItemStack>> craftables) {
PipeNetwork network = PipeNetwork.get(this.world);
List<Pair<BlockPos, ItemStack>> items = new ArrayList<>();
for (Pair<BlockPos, ItemStack> craftable : craftables) {
PipeTileEntity pipe = network.getPipe(craftable.getLeft());
if (pipe == null)
continue;
for (Pair<BlockPos, ItemStack> request : pipe.craftResultRequests) {
BlockPos dest = request.getLeft();
ItemStack stack = request.getRight();
// add up all the items that should go to the same location
Optional<Pair<BlockPos, ItemStack>> existing = items.stream()
.filter(s -> s.getLeft() == dest && ItemEqualityType.compareItems(s.getRight(), stack, ItemEqualityType.NBT))
.findFirst();
if (existing.isPresent()) {
existing.get().getRight().grow(stack.getCount());
} else {
items.add(Pair.of(dest, stack.copy()));
}
}
}
return items.stream().map(Pair::getRight).collect(Collectors.toList());
}
public void cancelCrafting() {
PipeNetwork network = PipeNetwork.get(this.world);
PipeTileEntity pipe = this.getConnectedPipe();
if (pipe == null)
return;
for (Pair<BlockPos, ItemStack> craftable : network.getAllCraftables(pipe.getPos())) {
PipeTileEntity otherPipe = network.getPipe(craftable.getLeft());
if (otherPipe != null) {
otherPipe.craftIngredientRequests.clear();
otherPipe.craftResultRequests.clear();
}
}
PlayerEntity[] lookingPlayers = this.getLookingPlayers();
if (lookingPlayers.length > 0)
this.updateItems(lookingPlayers);
}
@Override
public CompoundNBT write(CompoundNBT compound) {
compound.put("items", this.items.serializeNBT());

View file

@ -8,6 +8,7 @@ import de.ellpeck.prettypipes.misc.PlayerPrefs;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import joptsimple.internal.Strings;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.TextFieldWidget;
@ -32,6 +33,9 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png");
public List<ItemStack> currentlyCrafting;
public TextFieldWidget search;
// craftables have the second parameter set to true
private final List<Pair<ItemStack, Boolean>> sortedItems = new ArrayList<>();
private List<ItemStack> items;
@ -41,10 +45,11 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private Button requestButton;
private Button orderButton;
private Button ascendingButton;
private Button cancelCraftingButton;
private String lastSearchText;
private int requestAmount = 1;
private int scrollOffset;
public TextFieldWidget search;
private ItemStack hoveredCrafting;
public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn);
@ -98,6 +103,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
prefs.save();
this.updateWidgets();
}));
this.cancelCraftingButton = this.addButton(new Button(this.guiLeft + this.xSize + 4, this.guiTop + 4 + 64, 54, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".cancel_all"), button -> {
if (this.currentlyCrafting == null || this.currentlyCrafting.isEmpty())
return;
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.CANCEL_CRAFTING));
}));
this.cancelCraftingButton.visible = false;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 9; x++)
this.addButton(new ItemTerminalWidget(this.guiLeft + this.getXOffset() + 8 + x * 18, this.guiTop + 18 + y * 18, x, y, this));
@ -141,9 +152,10 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
return super.keyPressed(x, y, z);
}
public void updateItemList(List<ItemStack> items, List<ItemStack> craftables) {
public void updateItemList(List<ItemStack> items, List<ItemStack> craftables, List<ItemStack> currentlyCrafting) {
this.items = items;
this.craftables = craftables;
this.currentlyCrafting = currentlyCrafting;
this.updateWidgets();
}
@ -151,6 +163,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
PlayerPrefs prefs = PlayerPrefs.get();
this.ascendingButton.setMessage(new StringTextComponent(prefs.terminalAscending ? "^" : "v"));
this.orderButton.setMessage(new StringTextComponent(prefs.terminalItemOrder.name().substring(0, 1)));
this.cancelCraftingButton.visible = this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty();
Comparator<ItemStack> comparator = prefs.terminalItemOrder.comparator;
if (!prefs.terminalAscending)
@ -218,6 +231,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
if (this.ascendingButton.isHovered())
this.renderTooltip(matrix, new TranslationTextComponent("info." + PrettyPipes.ID + "." + (prefs.terminalAscending ? "ascending" : "descending")), mouseX, mouseY);
}
if (this.cancelCraftingButton.isHovered()) {
String[] tooltip = I18n.format("info." + PrettyPipes.ID + ".cancel_all.desc").split("\n");
this.func_243308_b(matrix, Arrays.stream(tooltip).map(StringTextComponent::new).collect(Collectors.toList()), mouseX, mouseY);
}
if (!this.hoveredCrafting.isEmpty())
this.renderTooltip(matrix, this.hoveredCrafting, mouseX, mouseY);
this.func_230459_a_(matrix, mouseX, mouseY);
}
@ -228,6 +247,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
String amount = String.valueOf(this.requestAmount);
this.font.drawString(matrix, amount, (176 + 15 - this.font.getStringWidth(amount)) / 2F - 7 + this.getXOffset(), 106, 4210752);
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
this.font.drawString(matrix, I18n.format("info." + PrettyPipes.ID + ".crafting"), this.xSize + 4, 4 + 6, 4210752);
if (this.currentlyCrafting.size() > 6)
this.font.drawString(matrix, ". . .", this.xSize + 24, 4 + 51, 4210752);
}
}
@Override
@ -241,6 +266,31 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
} else {
this.blit(matrix, this.guiLeft + this.getXOffset() + 172, this.guiTop + 18, 244, 241, 12, 15);
}
// draw the items that are currently crafting
this.hoveredCrafting = ItemStack.EMPTY;
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
this.getMinecraft().getTextureManager().bindTexture(TEXTURE);
this.blit(matrix, this.guiLeft + this.xSize, this.guiTop + 4, 191, 0, 65, 89);
int x = 0;
int y = 0;
for (ItemStack stack : this.currentlyCrafting) {
int itemX = this.guiLeft + this.xSize + 4 + x * 18;
int itemY = this.guiTop + 4 + 16 + y * 18;
this.itemRenderer.renderItemAndEffectIntoGUI(stack, itemX, itemY);
this.itemRenderer.renderItemOverlayIntoGUI(this.font, stack, itemX, itemY, String.valueOf(stack.getCount()));
if (mouseX >= itemX && mouseY >= itemY && mouseX < itemX + 16 && mouseY < itemY + 18)
this.hoveredCrafting = stack;
x++;
if (x >= 3) {
x = 0;
y++;
if (y >= 2)
break;
}
}
}
}
protected ResourceLocation getTexture() {

View file

@ -1,10 +0,0 @@
{
"_comment": "Unicode fonts only",
"providers": [
{
"type": "legacy_unicode",
"sizes": "minecraft:font/glyph_sizes.bin",
"template": "minecraft:font/unicode_page_%s.png"
}
]
}

View file

@ -74,5 +74,8 @@
"info.prettypipes.descending": "Descending",
"info.prettypipes.sync_jei.on": "Sync JEI Search",
"info.prettypipes.sync_jei.off": "Don't Sync JEI Search",
"info.prettypipes.energy": "%s / %s FE"
"info.prettypipes.energy": "%s / %s FE",
"info.prettypipes.crafting": "Crafting",
"info.prettypipes.cancel_all": "Cancel",
"info.prettypipes.cancel_all.desc": "Cancels all ongoing crafting operations\nDoesn't remove inputs from blocks"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB