2020-05-09 14:56:58 +02:00
|
|
|
package de.ellpeck.prettypipes.packets;
|
|
|
|
|
2022-05-20 10:31:00 +02:00
|
|
|
import com.google.common.collect.Streams;
|
|
|
|
import com.mojang.datafixers.util.Pair;
|
2024-03-07 16:00:49 +01:00
|
|
|
import de.ellpeck.prettypipes.PrettyPipes;
|
2020-05-09 14:56:58 +02:00
|
|
|
import de.ellpeck.prettypipes.Utility;
|
2021-12-02 14:44:26 +01:00
|
|
|
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
|
|
|
|
import net.minecraft.core.BlockPos;
|
2023-07-07 19:54:52 +02:00
|
|
|
import net.minecraft.core.registries.Registries;
|
2021-12-02 15:25:46 +01:00
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
2024-03-07 16:00:49 +01:00
|
|
|
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
2022-05-20 10:31:00 +02:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.tags.TagKey;
|
|
|
|
import net.minecraft.world.item.Item;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2022-05-20 10:31:00 +02:00
|
|
|
import net.minecraft.world.item.Items;
|
2023-07-07 19:54:52 +02:00
|
|
|
import net.minecraft.world.level.Level;
|
2024-03-07 16:00:49 +01:00
|
|
|
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
|
2020-05-09 14:56:58 +02:00
|
|
|
|
2022-05-20 10:31:00 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
2020-05-09 14:56:58 +02:00
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public class PacketGhostSlot implements CustomPacketPayload {
|
2020-05-09 14:56:58 +02:00
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "ghost_slot");
|
|
|
|
|
|
|
|
private final BlockPos pos;
|
|
|
|
private final List<Entry> stacks;
|
2020-05-09 14:56:58 +02:00
|
|
|
|
2022-06-27 14:19:51 +02:00
|
|
|
public PacketGhostSlot(BlockPos pos, List<Entry> stacks) {
|
2020-05-09 14:56:58 +02:00
|
|
|
this.pos = pos;
|
|
|
|
this.stacks = stacks;
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public PacketGhostSlot(FriendlyByteBuf buf) {
|
|
|
|
this.pos = buf.readBlockPos();
|
|
|
|
this.stacks = new ArrayList<>();
|
2021-12-02 17:46:56 +01:00
|
|
|
for (var i = buf.readInt(); i > 0; i--)
|
2024-03-07 16:00:49 +01:00
|
|
|
this.stacks.add(new Entry(buf));
|
2020-05-09 14:56:58 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
@Override
|
|
|
|
public void write(FriendlyByteBuf buf) {
|
|
|
|
buf.writeBlockPos(this.pos);
|
|
|
|
buf.writeInt(this.stacks.size());
|
|
|
|
for (var entry : this.stacks)
|
2022-06-27 14:19:51 +02:00
|
|
|
entry.write(buf);
|
2020-05-09 14:56:58 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
@Override
|
|
|
|
public ResourceLocation id() {
|
|
|
|
return PacketGhostSlot.ID;
|
|
|
|
}
|
2020-05-16 14:28:25 +02:00
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public static void onMessage(PacketGhostSlot message, PlayPayloadContext ctx) {
|
|
|
|
var player = ctx.player().orElseThrow();
|
|
|
|
var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level(), message.pos);
|
|
|
|
if (tile != null)
|
|
|
|
tile.setGhostItems(message.stacks);
|
2020-05-09 14:56:58 +02:00
|
|
|
}
|
2022-05-20 10:31:00 +02:00
|
|
|
|
|
|
|
public static class Entry {
|
|
|
|
|
|
|
|
private final List<ItemStack> stacks;
|
|
|
|
private final TagKey<Item> tag;
|
|
|
|
|
2023-07-07 19:54:52 +02:00
|
|
|
public Entry(Level level, List<ItemStack> stacks) {
|
|
|
|
var tag = Entry.getTagForStacks(level, stacks);
|
2022-05-20 10:31:00 +02:00
|
|
|
if (tag != null) {
|
|
|
|
this.stacks = null;
|
|
|
|
this.tag = tag;
|
|
|
|
} else {
|
|
|
|
this.stacks = stacks;
|
|
|
|
this.tag = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Entry(FriendlyByteBuf buf) {
|
|
|
|
if (buf.readBoolean()) {
|
|
|
|
this.tag = null;
|
|
|
|
this.stacks = new ArrayList<>();
|
|
|
|
for (var i = buf.readInt(); i > 0; i--)
|
|
|
|
this.stacks.add(buf.readItem());
|
|
|
|
} else {
|
|
|
|
this.stacks = null;
|
2023-07-07 19:54:52 +02:00
|
|
|
this.tag = TagKey.create(Registries.ITEM, new ResourceLocation(buf.readUtf()));
|
2022-05-20 10:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:54:52 +02:00
|
|
|
public List<ItemStack> getStacks(Level level) {
|
2022-05-20 10:31:00 +02:00
|
|
|
if (this.stacks != null)
|
|
|
|
return this.stacks;
|
2024-03-07 16:00:49 +01:00
|
|
|
return Streams.stream(level.registryAccess().registry(Registries.ITEM).orElseThrow().getTagOrEmpty(this.tag).iterator())
|
2022-05-20 10:31:00 +02:00
|
|
|
.filter(h -> h.value() != null & h.value() != Items.AIR)
|
|
|
|
.map(h -> new ItemStack(h.value())).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:00:49 +01:00
|
|
|
public void write(FriendlyByteBuf buf) {
|
2022-05-20 10:31:00 +02:00
|
|
|
if (this.stacks != null) {
|
|
|
|
buf.writeBoolean(true);
|
|
|
|
buf.writeInt(this.stacks.size());
|
|
|
|
for (var stack : this.stacks)
|
|
|
|
buf.writeItem(stack);
|
|
|
|
} else {
|
|
|
|
buf.writeBoolean(false);
|
|
|
|
buf.writeUtf(this.tag.location().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:54:52 +02:00
|
|
|
private static TagKey<Item> getTagForStacks(Level level, List<ItemStack> stacks) {
|
2024-03-07 16:00:49 +01:00
|
|
|
return level.registryAccess().registry(Registries.ITEM).orElseThrow().getTags().filter(e -> {
|
2022-05-20 10:31:00 +02:00
|
|
|
var tag = e.getSecond();
|
|
|
|
if (tag.size() != stacks.size())
|
|
|
|
return false;
|
|
|
|
for (var i = 0; i < tag.size(); i++) {
|
|
|
|
if (stacks.get(i).getItem() != tag.get(i).value())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}).map(Pair::getFirst).findFirst().orElse(null);
|
|
|
|
}
|
2024-03-07 16:00:49 +01:00
|
|
|
|
2022-05-20 10:31:00 +02:00
|
|
|
}
|
2024-03-07 16:00:49 +01:00
|
|
|
|
2020-05-09 14:56:58 +02:00
|
|
|
}
|