Implemented transfer of stacks requirements from client to server, close #1487
This commit is contained in:
parent
0db2a98e05
commit
970425f9bf
4 changed files with 131 additions and 6 deletions
|
@ -40,6 +40,9 @@ import buildcraft.core.blueprints.BptBuilderBase;
|
|||
import buildcraft.core.blueprints.BptBuilderBlueprint;
|
||||
import buildcraft.core.blueprints.BptContext;
|
||||
import buildcraft.core.network.NetworkData;
|
||||
import buildcraft.core.network.RPC;
|
||||
import buildcraft.core.network.RPCHandler;
|
||||
import buildcraft.core.network.RPCSide;
|
||||
import buildcraft.core.robots.EntityRobot;
|
||||
import buildcraft.core.utils.Utils;
|
||||
|
||||
|
@ -61,6 +64,8 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
|
|||
|
||||
private EntityRobot builderRobot;
|
||||
|
||||
private LinkedList <ItemStack> requiredToBuild;
|
||||
|
||||
private class PathIterator {
|
||||
|
||||
public Iterator<BlockIndex> currentIterator;
|
||||
|
@ -593,10 +598,33 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
|
|||
}
|
||||
|
||||
public Collection<ItemStack> getNeededItems() {
|
||||
if (bluePrintBuilder instanceof BptBuilderBlueprint) {
|
||||
return ((BptBuilderBlueprint) bluePrintBuilder).neededItems;
|
||||
} else {
|
||||
return null;
|
||||
return requiredToBuild;
|
||||
}
|
||||
|
||||
@RPC (RPCSide.CLIENT)
|
||||
public void setItemRequirements (LinkedList <ItemStack> rq, LinkedList <Integer> realSizes) {
|
||||
// Item stack serialized are represented through bytes, so 0-255. In
|
||||
// order to get the real amounts, we need to pass the real sizes of the
|
||||
// stacks as a separate list.
|
||||
|
||||
requiredToBuild = rq;
|
||||
|
||||
if (rq.size() > 0) {
|
||||
Iterator <ItemStack> itStack = rq.iterator();
|
||||
Iterator <Integer> size = realSizes.iterator();
|
||||
|
||||
while (true) {
|
||||
ItemStack stack = itStack.next();
|
||||
stack.stackSize = size.next();
|
||||
|
||||
if (stack.stackSize > 999) {
|
||||
stack.stackSize = 999;
|
||||
}
|
||||
|
||||
if (!itStack.hasNext()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -646,6 +674,16 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory,
|
|||
|
||||
if (bluePrintBuilder instanceof BptBuilderBlueprint) {
|
||||
((BptBuilderBlueprint) bluePrintBuilder).recomputeNeededItems();
|
||||
|
||||
LinkedList <Integer> realSize = new LinkedList<Integer>();
|
||||
|
||||
for (ItemStack stack : ((BptBuilderBlueprint) bluePrintBuilder).neededItems) {
|
||||
realSize.add(stack.stackSize);
|
||||
stack.stackSize = 0;
|
||||
}
|
||||
|
||||
RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements",
|
||||
((BptBuilderBlueprint) bluePrintBuilder).neededItems, realSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import java.util.ListIterator;
|
|||
import java.util.Map.Entry;
|
||||
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldSettings.GameType;
|
||||
|
@ -336,7 +337,6 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
|||
HashMap <StackKey, Integer> computeStacks = new HashMap <StackKey, Integer> ();
|
||||
|
||||
for (SchematicToBuild slot : primaryList) {
|
||||
|
||||
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
|
||||
|
||||
try {
|
||||
|
@ -394,6 +394,29 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
|||
neededItems.add(newStack);
|
||||
}
|
||||
|
||||
|
||||
LinkedList <ItemStack> sortedList = new LinkedList <ItemStack> ();
|
||||
|
||||
for (ItemStack toInsert : neededItems) {
|
||||
int index = 0;
|
||||
boolean didInsert = false;
|
||||
|
||||
for (ItemStack inserted : sortedList) {
|
||||
if (inserted.stackSize < toInsert.stackSize) {
|
||||
sortedList.add(index, toInsert);
|
||||
didInsert = true;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (!didInsert) {
|
||||
sortedList.addLast(toInsert);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Collections.sort (neededItems, new Comparator<ItemStack>() {
|
||||
@Override
|
||||
public int compare(ItemStack o1, ItemStack o2) {
|
||||
|
@ -401,7 +424,11 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
|||
return -1;
|
||||
} else if (o1.stackSize < o2.stackSize) {
|
||||
return 1;
|
||||
} else if (o1.getItemDamage() > o2.getItemDamage()) {
|
||||
} else if (Item.getIdFromItem(o1.getItem()) > Item.getIdFromItem(o2.getItem())) {
|
||||
return -1;
|
||||
} else if (Item.getIdFromItem(o1.getItem()) < Item.getIdFromItem(o2.getItem())) {
|
||||
return 1;
|
||||
} else if (o1.getItemDamage() > o2.getItemDamage()) {
|
||||
return -1;
|
||||
} else if (o1.getItemDamage() < o2.getItemDamage()) {
|
||||
return 1;
|
||||
|
|
|
@ -633,6 +633,7 @@ public class ClassMapping extends ClassSerializer {
|
|||
static {
|
||||
registerSerializer(String.class, new SerializerString());
|
||||
registerSerializer(HashMap.class, new SerializerHashMap());
|
||||
registerSerializer(LinkedList.class, new SerializerLinkedList());
|
||||
registerSerializer(Block.class, new SerializerBlock());
|
||||
registerSerializer(Item.class, new SerializerItem());
|
||||
registerSerializer(NBTTagCompound.class, new SerializerNBT());
|
||||
|
|
59
common/buildcraft/core/network/serializers/SerializerLinkedList.java
Executable file
59
common/buildcraft/core/network/serializers/SerializerLinkedList.java
Executable file
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
|
||||
* http://www.mod-buildcraft.com
|
||||
*
|
||||
* BuildCraft is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://www.mod-buildcraft.com/MMPL-1.0.txt
|
||||
*/
|
||||
package buildcraft.core.network.serializers;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class SerializerLinkedList extends ClassSerializer {
|
||||
|
||||
private static SerializerObject anonymousSerializer = new SerializerObject();
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf data, Object o, SerializationContext context)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
LinkedList list = (LinkedList) o;
|
||||
|
||||
if (o == null) {
|
||||
data.writeBoolean(false);
|
||||
} else {
|
||||
data.writeBoolean(true);
|
||||
data.writeShort(list.size());
|
||||
|
||||
for (Object val : list) {
|
||||
anonymousSerializer.write(data, val, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(ByteBuf data, Object o, SerializationContext context)
|
||||
throws IllegalArgumentException, IllegalAccessException,
|
||||
InstantiationException, ClassNotFoundException {
|
||||
|
||||
if (!data.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
int size = data.readShort();
|
||||
|
||||
LinkedList list = new LinkedList ();
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
Object val = anonymousSerializer.read(data, null, context);
|
||||
|
||||
list.add(val);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue