Skip to content

Commit

Permalink
Fix read/write in NEISetCraftingRecipe
Browse files Browse the repository at this point in the history
  • Loading branch information
bziemons committed Jun 18, 2020
1 parent ea46dde commit 950b305
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
6 changes: 3 additions & 3 deletions common/logisticspipes/gui/popup/GuiRecipeImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ protected void renderGuiBackground(int mouseX, int mouseY) {
protected void actionPerformed(GuiButton button) {
int id = button.id;
if (id == 0) {
NonNullList<ItemStack> stackList = NonNullList.withSize(9, ItemStack.EMPTY);
NEISetCraftingRecipe packet = PacketHandler.getPacket(NEISetCraftingRecipe.class);
NonNullList<ItemStack> stackList = packet.getStackList();
int i = 0;
for (Canidates canidate : grid) {
if (canidate == null) {
Expand All @@ -188,8 +189,7 @@ protected void actionPerformed(GuiButton button) {
}
stackList.set(i++, canidate.order.get(canidate.pos).makeNormalStack());
}
NEISetCraftingRecipe packet = PacketHandler.getPacket(NEISetCraftingRecipe.class);
MainProxy.sendPacketToServer(packet.setStackList(stackList).setBlockPos(tile.getPos()));
MainProxy.sendPacketToServer(packet.setBlockPos(tile.getPos()));
exitGui();
} else if (id == 1) {
MainProxy.sendPacketToServer(PacketHandler.getPacket(FindMostLikelyRecipeComponents.class).setContent(list).setTilePos(tile));
Expand Down
44 changes: 11 additions & 33 deletions common/logisticspipes/network/packets/NEISetCraftingRecipe.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package logisticspipes.network.packets;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;

import lombok.Getter;
import lombok.Setter;

import logisticspipes.blocks.crafting.LogisticsCraftingTableTileEntity;
import logisticspipes.network.abstractpackets.CoordinatesPacket;
import logisticspipes.network.abstractpackets.ModernPacket;
Expand All @@ -21,14 +18,16 @@
@StaticResolve
public class NEISetCraftingRecipe extends CoordinatesPacket {

@Getter
@Setter
private NonNullList<ItemStack> stackList = NonNullList.withSize(9, ItemStack.EMPTY);

public NEISetCraftingRecipe(int id) {
super(id);
}

public NonNullList<ItemStack> getStackList() {
return this.stackList;
}

@Override
public void processPacket(EntityPlayer player) {
TileEntity tile = getTileAs(player.world, TileEntity.class);
Expand All @@ -47,37 +46,16 @@ public ModernPacket template() {
@Override
public void writeData(LPDataOutput output) {
super.writeData(output);

output.writeInt(stackList.size());

for (int i = 0; i < stackList.size(); i++) {
final ItemStack stack = stackList.get(i);

if (!stack.isEmpty()) {
output.writeByte(i);
output.writeInt(Item.getIdFromItem(stack.getItem()));
output.writeInt(stack.getCount());
output.writeInt(stack.getItemDamage());
output.writeNBTTagCompound(stack.getTagCompound());
}
}
output.writeByte(-1); // mark packet end
output.writeCollection(stackList, (out, stack) -> out.writeNBTTagCompound(stack.isEmpty() ? null : stack.writeToNBT(new NBTTagCompound())));
}

@Override
public void readData(LPDataInput input) {
super.readData(input);

byte index = input.readByte();

while (index != -1) { // read until the end
final int itemID = input.readInt();
int stackSize = input.readInt();
int damage = input.readInt();
ItemStack stack = new ItemStack(Item.getItemById(itemID), stackSize, damage);
stack.setTagCompound(input.readNBTTagCompound());
stackList.set(index, stack);
index = input.readByte(); // read the next slot
}
NonNullList<ItemStack> readList = input.readNonNullList(inp -> {
NBTTagCompound tag = inp.readNBTTagCompound();
return tag == null ? null : new ItemStack(tag);
}, ItemStack.EMPTY);
if (readList != null) stackList = readList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,8 @@ public <T> NonNullList<T> readNonNullList(IReadListObject<T> reader, @Nonnull T

NonNullList<T> list = NonNullList.withSize(size, fillItem);
for (int i = 0; i < size; i++) {
list.set(i, reader.readObject(this));
T obj = reader.readObject(this);
if (obj != null) list.set(i, obj);
}
return list;
}
Expand Down

0 comments on commit 950b305

Please sign in to comment.