part 2: display craftables in the terminal

This commit is contained in:
Ell 2020-10-14 22:30:02 +02:00
parent eb8f2695de
commit 8029cc21a0
9 changed files with 93 additions and 12 deletions

View file

@ -7,6 +7,8 @@ import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.List;
public interface IModule { public interface IModule {
void tick(ItemStack module, PipeTileEntity tile); void tick(ItemStack module, PipeTileEntity tile);
@ -28,4 +30,6 @@ public interface IModule {
float getItemSpeedIncrease(ItemStack module, PipeTileEntity tile); float getItemSpeedIncrease(ItemStack module, PipeTileEntity tile);
boolean canPipeWork(ItemStack module, PipeTileEntity tile); boolean canPipeWork(ItemStack module, PipeTileEntity tile);
List<ItemStack> getCraftables(ItemStack module, PipeTileEntity tile);
} }

View file

@ -16,6 +16,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List; import java.util.List;
public abstract class ModuleItem extends Item implements IModule { public abstract class ModuleItem extends Item implements IModule {
@ -73,4 +74,9 @@ public abstract class ModuleItem extends Item implements IModule {
public boolean canPipeWork(ItemStack module, PipeTileEntity tile) { public boolean canPipeWork(ItemStack module, PipeTileEntity tile) {
return true; return true;
} }
@Override
public List<ItemStack> getCraftables(ItemStack module, PipeTileEntity tile) {
return Collections.emptyList();
}
} }

View file

@ -16,6 +16,7 @@ import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextComponent; import net.minecraft.util.text.TextComponent;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.client.gui.GuiUtils; import net.minecraftforge.fml.client.gui.GuiUtils;
import org.apache.commons.lang3.tuple.Pair;
import java.util.List; import java.util.List;
@ -27,6 +28,7 @@ public class ItemTerminalWidget extends Widget {
public final int gridY; public final int gridY;
public boolean selected; public boolean selected;
public ItemStack stack = ItemStack.EMPTY; public ItemStack stack = ItemStack.EMPTY;
public boolean craftable;
public ItemTerminalWidget(int xIn, int yIn, int gridX, int gridY, ItemTerminalGui screen) { public ItemTerminalWidget(int xIn, int yIn, int gridX, int gridY, ItemTerminalGui screen) {
super(xIn, yIn, 16, 16, new StringTextComponent("")); super(xIn, yIn, 16, 16, new StringTextComponent(""));
@ -52,7 +54,7 @@ public class ItemTerminalWidget extends Widget {
fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433); fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433);
RenderSystem.enableDepthTest(); RenderSystem.enableDepthTest();
renderer.renderItemAndEffectIntoGUI(mc.player, this.stack, this.x, this.y); renderer.renderItemAndEffectIntoGUI(mc.player, this.stack, this.x, this.y);
int amount = this.stack.getCount(); int amount = !this.craftable ? this.stack.getCount() : 0;
String amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount); String amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount);
RenderSystem.pushMatrix(); RenderSystem.pushMatrix();
RenderSystem.scalef(0.8F, 0.8F, 1); RenderSystem.scalef(0.8F, 0.8F, 1);

View file

@ -224,6 +224,24 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return tile; return tile;
} }
public List<Pair<BlockPos, ItemStack>> getOrderedCraftables(BlockPos node) {
if (!this.isNode(node))
return Collections.emptyList();
this.startProfile("get_craftables");
List<Pair<BlockPos, ItemStack>> craftables = new ArrayList<>();
for (BlockPos dest : this.getOrderedNetworkNodes(node)) {
if (!this.world.isBlockLoaded(dest))
continue;
PipeTileEntity pipe = this.getPipe(dest);
if (!pipe.canNetworkSee())
continue;
for (ItemStack stack : pipe.getCraftables())
craftables.add(Pair.of(pipe.getPos(), stack));
}
this.endProfile();
return craftables;
}
public List<NetworkLocation> getOrderedNetworkItems(BlockPos node) { public List<NetworkLocation> getOrderedNetworkItems(BlockPos node) {
if (!this.isNode(node)) if (!this.isNode(node))
return Collections.emptyList(); return Collections.emptyList();

View file

@ -22,9 +22,11 @@ import java.util.function.Supplier;
public class PacketNetworkItems { public class PacketNetworkItems {
private List<ItemStack> items; private List<ItemStack> items;
private List<ItemStack> craftables;
public PacketNetworkItems(List<ItemStack> items) { public PacketNetworkItems(List<ItemStack> items, List<ItemStack> craftables) {
this.items = items; this.items = items;
this.craftables = craftables;
} }
private PacketNetworkItems() { private PacketNetworkItems() {
@ -39,6 +41,9 @@ public class PacketNetworkItems {
stack.setCount(buf.readVarInt()); stack.setCount(buf.readVarInt());
client.items.add(stack); client.items.add(stack);
} }
client.craftables = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--)
client.craftables.add(buf.readItemStack());
return client; return client;
} }
@ -50,6 +55,9 @@ public class PacketNetworkItems {
buf.writeItemStack(copy); buf.writeItemStack(copy);
buf.writeVarInt(stack.getCount()); buf.writeVarInt(stack.getCount());
} }
buf.writeVarInt(packet.craftables.size());
for (ItemStack stack : packet.craftables)
buf.writeItemStack(stack);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -59,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.craftables);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -37,6 +37,7 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity { public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity {
@ -231,6 +232,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
return this.streamModules().allMatch(m -> m.getRight().canPipeWork(m.getLeft(), this)); return this.streamModules().allMatch(m -> m.getRight().canPipeWork(m.getLeft(), this));
} }
public List<ItemStack> getCraftables() {
return this.streamModules()
.flatMap(m -> m.getRight().getCraftables(m.getLeft(), this).stream())
.collect(Collectors.toList());
}
public IItemHandler getItemHandler(Direction dir, PipeItem item) { public IItemHandler getItemHandler(Direction dir, PipeItem item) {
if (!this.isConnected(dir)) if (!this.isConnected(dir))
return null; return null;

View file

@ -12,6 +12,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import java.util.ArrayList;
import java.util.List;
public class CraftingModuleItem extends ModuleItem { public class CraftingModuleItem extends ModuleItem {
public final int inputSlots; public final int inputSlots;
@ -38,6 +41,18 @@ public class CraftingModuleItem extends ModuleItem {
return new CraftingModuleContainer(Registry.craftingModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new CraftingModuleContainer(Registry.craftingModuleContainer, windowId, player, tile.getPos(), moduleIndex);
} }
@Override
public List<ItemStack> getCraftables(ItemStack module, PipeTileEntity tile) {
ItemStackHandler output = this.getOutput(module);
List<ItemStack> items = new ArrayList<>();
for (int i = 0; i < output.getSlots(); i++) {
ItemStack stack = output.getStackInSlot(i);
if (!stack.isEmpty())
items.add(stack);
}
return items;
}
public ItemStackHandler getInput(ItemStack module) { public ItemStackHandler getInput(ItemStack module) {
ItemStackHandler handler = new ItemStackHandler(this.inputSlots); ItemStackHandler handler = new ItemStackHandler(this.inputSlots);
if (module.hasTag()) if (module.hasTag())

View file

@ -51,6 +51,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
}; };
public Map<EquatableItemStack, NetworkItem> networkItems; public Map<EquatableItemStack, NetworkItem> networkItems;
public List<Pair<BlockPos, ItemStack>> craftables;
private final Queue<NetworkLock> pendingRequests = new ArrayDeque<>(); private final Queue<NetworkLock> pendingRequests = new ArrayDeque<>();
protected ItemTerminalTileEntity(TileEntityType<?> tileEntityTypeIn) { protected ItemTerminalTileEntity(TileEntityType<?> tileEntityTypeIn) {
@ -118,18 +119,21 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
public void updateItems(PlayerEntity... playersToSync) { public void updateItems(PlayerEntity... playersToSync) {
if (this.getConnectedPipe() == null) PipeTileEntity pipe = this.getConnectedPipe();
if (pipe == null)
return; return;
this.networkItems = this.collectItems(); this.networkItems = this.collectItems();
this.craftables = PipeNetwork.get(this.world).getOrderedCraftables(pipe.getPos());
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());
List<ItemStack> clientCraftables = this.craftables.stream().map(Pair::getRight).collect(Collectors.toList());
for (PlayerEntity player : playersToSync) { for (PlayerEntity player : playersToSync) {
if (!(player.openContainer instanceof ItemTerminalContainer)) if (!(player.openContainer instanceof ItemTerminalContainer))
continue; continue;
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)); PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables));
} }
} }
} }

View file

@ -22,15 +22,20 @@ import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> { public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png"); private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png");
// craftables have the second parameter set to true
private final List<Pair<ItemStack, Boolean>> sortedItems = new ArrayList<>();
private List<ItemStack> items; private List<ItemStack> items;
private List<ItemStack> sortedItems; private List<ItemStack> craftables;
private Button minusButton; private Button minusButton;
private Button plusButton; private Button plusButton;
private Button requestButton; private Button requestButton;
@ -136,8 +141,9 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
return super.keyPressed(x, y, z); return super.keyPressed(x, y, z);
} }
public void updateItemList(List<ItemStack> items) { public void updateItemList(List<ItemStack> items, List<ItemStack> craftables) {
this.items = items; this.items = items;
this.craftables = craftables;
this.updateWidgets(); this.updateWidgets();
} }
@ -150,8 +156,16 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
if (!prefs.terminalAscending) if (!prefs.terminalAscending)
comparator = comparator.reversed(); comparator = comparator.reversed();
this.sortedItems = new ArrayList<>(this.items); // add all items to the sorted items list
this.sortedItems.sort(comparator); this.sortedItems.clear();
for (ItemStack stack : this.items)
this.sortedItems.add(Pair.of(stack, false));
for (ItemStack stack : this.craftables)
this.sortedItems.add(Pair.of(stack, true));
// compare by craftability first, and then by the player's chosen order
Comparator<Pair<ItemStack, Boolean>> fullComparator = Comparator.comparing(Pair::getRight);
this.sortedItems.sort(fullComparator.thenComparing(Pair::getLeft, comparator));
String searchText = this.search.getText(); String searchText = this.search.getText();
if (!Strings.isNullOrEmpty(searchText)) { if (!Strings.isNullOrEmpty(searchText)) {
@ -159,11 +173,11 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
String search = searchText; String search = searchText;
String toCompare; String toCompare;
if (search.startsWith("@")) { if (search.startsWith("@")) {
toCompare = s.getItem().getRegistryName().getNamespace(); toCompare = s.getLeft().getItem().getRegistryName().getNamespace();
search = search.substring(1); search = search.substring(1);
} else { } else {
// don't use formatted text here since we want to search for name // don't use formatted text here since we want to search for name
toCompare = s.getDisplayName().getString(); toCompare = s.getLeft().getDisplayName().getString();
} }
return !toCompare.toLowerCase(Locale.ROOT).contains(search.toLowerCase(Locale.ROOT)); return !toCompare.toLowerCase(Locale.ROOT).contains(search.toLowerCase(Locale.ROOT));
}); });
@ -178,9 +192,12 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
int index = i + this.scrollOffset * 9; int index = i + this.scrollOffset * 9;
if (index >= this.sortedItems.size()) { if (index >= this.sortedItems.size()) {
widget.stack = ItemStack.EMPTY; widget.stack = ItemStack.EMPTY;
widget.craftable = false;
widget.visible = false; widget.visible = false;
} else { } else {
widget.stack = this.sortedItems.get(index); Pair<ItemStack, Boolean> stack = this.sortedItems.get(index);
widget.stack = stack.getLeft();
widget.craftable = stack.getRight();
widget.visible = true; widget.visible = true;
} }
} }