make builders display stack sizes up to 99k

This commit is contained in:
asiekierka 2015-06-15 16:53:07 +02:00
parent 0815e5f118
commit fc62748f63
6 changed files with 124 additions and 50 deletions

View file

@ -16,6 +16,7 @@ import java.util.List;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
@ -33,6 +34,7 @@ import net.minecraftforge.fluids.IFluidHandler;
import buildcraft.BuildCraftBuilders;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.core.Position;
@ -52,6 +54,7 @@ import buildcraft.core.blueprints.BlueprintBase;
import buildcraft.core.blueprints.BptBuilderBase;
import buildcraft.core.blueprints.BptBuilderBlueprint;
import buildcraft.core.blueprints.BptBuilderTemplate;
import buildcraft.core.blueprints.RequirementItemStack;
import buildcraft.core.builders.TileAbstractBuilder;
import buildcraft.core.lib.fluids.Tank;
import buildcraft.core.lib.fluids.TankManager;
@ -84,7 +87,7 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
private BptBuilderBase currentBuilder;
private RecursiveBlueprintBuilder recursiveBuilder;
private LinkedList<BlockIndex> path;
private ArrayList<ItemStack> requiredToBuild;
private ArrayList<RequirementItemStack> requiredToBuild;
private NBTTagCompound initNBT = null;
private boolean done = true;
private boolean isBuilding = false;
@ -623,7 +626,7 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
return getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof ItemBlueprint;
}
public List<ItemStack> getNeededItems() {
public List<RequirementItemStack> getNeededItems() {
return requiredToBuild;
}
@ -634,12 +637,24 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
if ("clearItemRequirements".equals(command)) {
requiredToBuild = null;
} else if ("setItemRequirements".equals(command)) {
int size = stream.readUnsignedShort();
requiredToBuild = new ArrayList<ItemStack>();
int size = stream.readUnsignedMedium();
requiredToBuild = new ArrayList<RequirementItemStack>();
for (int i = 0; i < size; i++) {
ItemStack stack = NetworkUtils.readStack(stream);
stack.stackSize = Math.min(999, stream.readUnsignedShort());
requiredToBuild.add(stack);
int itemId = stream.readUnsignedShort();
int itemDamage = stream.readShort();
int stackSize = stream.readUnsignedMedium();
boolean hasCompound = stackSize >= 0x800000;
ItemStack stack = new ItemStack(Item.getItemById(itemId), 1, itemDamage);
if (hasCompound) {
stack.setTagCompound(NetworkUtils.readNBT(stream));
}
if (stack != null && stack.getItem() != null){
requiredToBuild.add(new RequirementItemStack(stack, stackSize & 0x7FFFFF));
} else {
BCLog.logger.error("Corrupt ItemStack in TileBuilder.receiveCommand! This should not happen! (ID " + itemId + ", damage " + itemDamage + ")");
}
}
}
} else if (side.isServer()) {
@ -657,14 +672,18 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
}
}
private Packet getItemRequirementsPacket(final ArrayList<ItemStack> items) {
private Packet getItemRequirementsPacket(final List<RequirementItemStack> items) {
if (items != null) {
return new PacketCommand(this, "setItemRequirements", new CommandWriter() {
public void write(ByteBuf data) {
data.writeShort(items.size());
for (ItemStack rb : items) {
NetworkUtils.writeStack(data, rb);
data.writeShort(rb.stackSize);
data.writeMedium(items.size());
for (RequirementItemStack rb : items) {
data.writeShort(Item.getIdFromItem(rb.stack.getItem()));
data.writeShort(rb.stack.getItemDamage());
data.writeMedium((rb.stack.hasTagCompound() ? 0x800000 : 0x000000) | Math.min(0x7FFFFF, rb.size));
if (rb.stack.hasTagCompound()) {
NetworkUtils.writeNBT(data, rb.stack.getTagCompound());
}
}
}
});
@ -718,7 +737,7 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
}
public void updateRequirements() {
ArrayList<ItemStack> reqCopy = null;
List<RequirementItemStack> reqCopy = null;
if (currentBuilder instanceof BptBuilderBlueprint) {
currentBuilder.initialize();
reqCopy = ((BptBuilderBlueprint) currentBuilder).neededItems;
@ -730,7 +749,7 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
}
public void updateRequirements(EntityPlayer caller) {
ArrayList<ItemStack> reqCopy = null;
List<RequirementItemStack> reqCopy = null;
if (currentBuilder instanceof BptBuilderBlueprint) {
currentBuilder.initialize();
reqCopy = ((BptBuilderBlueprint) currentBuilder).neededItems;
@ -843,9 +862,9 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
return null;
}
ItemStack requirement = bpt.neededItems.get(i);
RequirementItemStack requirement = bpt.neededItems.get(i);
int qty = quantityMissing(requirement);
int qty = quantityMissing(requirement.stack, requirement.size);
if (qty <= 0) {
return null;
@ -855,7 +874,7 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
request.index = i;
request.requester = this;
request.stack = requirement;
request.stack = requirement.stack;
return request;
}
@ -885,9 +904,9 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
return stack;
}
ItemStack requirement = bpt.neededItems.get(i);
RequirementItemStack requirement = bpt.neededItems.get(i);
int qty = quantityMissing(requirement);
int qty = quantityMissing(requirement.stack, requirement.size);
if (qty <= 0) {
return stack;
@ -911,8 +930,8 @@ public class TileBuilder extends TileAbstractBuilder implements IHasWork, IFluid
}
}
private int quantityMissing(ItemStack requirement) {
int left = requirement.stackSize;
private int quantityMissing(ItemStack requirement, int amount) {
int left = amount <= 0 ? requirement.stackSize : amount;
for (IInvSlot slot : InventoryIterator.getIterable(this)) {
if (slot.getStackInSlot() != null) {

View file

@ -0,0 +1,40 @@
package buildcraft.builders.gui;
import org.lwjgl.opengl.GL11;
import net.minecraft.item.ItemStack;
import buildcraft.core.blueprints.RequirementItemStack;
import buildcraft.core.lib.gui.AdvancedSlot;
import buildcraft.core.lib.gui.GuiAdvancedInterface;
public class BuilderRequirementSlot extends AdvancedSlot {
public RequirementItemStack stack;
public BuilderRequirementSlot(GuiAdvancedInterface gui, int x, int y) {
super(gui, x, y);
}
@Override
public ItemStack getItemStack() {
return stack != null ? stack.stack : null;
}
@Override
public void drawStack(ItemStack item) {
int cornerX = (gui.width - gui.getXSize()) / 2;
int cornerY = (gui.height - gui.getYSize()) / 2;
gui.drawStack(item, cornerX + x, cornerY + y);
if (stack != null) {
// Render real stack size
String s = String.valueOf(stack.size > 999 ? Math.min(99, stack.size / 1000) + "K" : stack.size);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_BLEND);
gui.getFontRenderer().drawStringWithShadow(s,
cornerX + x + 17 - gui.getFontRenderer().getStringWidth(s), cornerY + y + 9, 16777215);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
}
}

View file

@ -16,14 +16,13 @@ import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import buildcraft.BuildCraftCore;
import buildcraft.builders.TileBuilder;
import buildcraft.core.blueprints.RequirementItemStack;
import buildcraft.core.lib.fluids.Tank;
import buildcraft.core.lib.gui.GuiAdvancedInterface;
import buildcraft.core.lib.gui.ItemSlot;
import buildcraft.core.lib.network.command.CommandWriter;
import buildcraft.core.lib.network.command.PacketCommand;
import buildcraft.core.lib.utils.StringUtils;
@ -48,7 +47,7 @@ public class GuiBuilder extends GuiAdvancedInterface {
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 4; ++j) {
slots.set(i * 4 + j, new ItemSlot(this, 179 + j * 18, 18 + i * 18));
slots.set(i * 4 + j, new BuilderRequirementSlot(this, 179 + j * 18, 18 + i * 18));
}
}
}
@ -80,7 +79,7 @@ public class GuiBuilder extends GuiAdvancedInterface {
drawTexturedModalRect(guiLeft + 169, guiTop, 169, 0, 256 - 169, ySize);
}
List<ItemStack> needs = builder.getNeededItems();
List<RequirementItemStack> needs = builder.getNeededItems();
if (needs != null) {
if (needs.size() > slots.size()) {
@ -102,9 +101,9 @@ public class GuiBuilder extends GuiAdvancedInterface {
for (int s = 0; s < slots.size(); s++) {
int ts = offset + s;
if (ts >= needs.size()) {
((ItemSlot) slots.get(s)).stack = null;
((BuilderRequirementSlot) slots.get(s)).stack = null;
} else {
((ItemSlot) slots.get(s)).stack = needs.get(ts).copy();
((BuilderRequirementSlot) slots.get(s)).stack = needs.get(ts);
}
}
@ -115,7 +114,7 @@ public class GuiBuilder extends GuiAdvancedInterface {
sbPosition = 0;
sbLength = 0;
for (int s = 0; s < slots.size(); s++) {
((ItemSlot) slots.get(s)).stack = null;
((BuilderRequirementSlot) slots.get(s)).stack = null;
}
for (GuiButton b : (List<GuiButton>) buttonList) {
b.visible = false;

View file

@ -59,7 +59,7 @@ import buildcraft.core.lib.utils.BlockUtils;
public class BptBuilderBlueprint extends BptBuilderBase {
public ArrayList<ItemStack> neededItems = new ArrayList<ItemStack>();
public ArrayList<RequirementItemStack> neededItems = new ArrayList<RequirementItemStack>();
protected HashSet<Integer> builtEntities = new HashSet<Integer>();
@ -702,33 +702,31 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
for (Entry<StackKey, Integer> e : computeStacks.entrySet()) {
ItemStack newStack = e.getKey().stack.copy();
newStack.stackSize = e.getValue();
neededItems.add(newStack);
neededItems.add(new RequirementItemStack(e.getKey().stack.copy(), e.getValue()));
}
Collections.sort (neededItems, new Comparator<ItemStack>() {
Collections.sort(neededItems, new Comparator<RequirementItemStack>() {
@Override
public int compare(ItemStack o1, ItemStack o2) {
if (o1.stackSize > o2.stackSize) {
return -1;
} else if (o1.stackSize < o2.stackSize) {
return 1;
} 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;
public int compare(RequirementItemStack o1, RequirementItemStack o2) {
if (o1.size != o2.size) {
return o1.size < o2.size ? 1 : -1;
} else {
return 0;
ItemStack os1 = o1.stack;
ItemStack os2 = o2.stack;
if (Item.getIdFromItem(os1.getItem()) > Item.getIdFromItem(os2.getItem())) {
return -1;
} else if (Item.getIdFromItem(os1.getItem()) < Item.getIdFromItem(os2.getItem())) {
return 1;
} else if (os1.getItemDamage() > os2.getItemDamage()) {
return -1;
} else if (os1.getItemDamage() < os2.getItemDamage()) {
return 1;
} else {
return 0;
}
}
}
});
}
@Override

View file

@ -0,0 +1,19 @@
package buildcraft.core.blueprints;
import net.minecraft.item.ItemStack;
public class RequirementItemStack {
public final ItemStack stack;
public final int size;
public RequirementItemStack(ItemStack stack, int size) {
this.stack = stack;
this.size = size;
stack.stackSize = 1;
}
@Override
public int hashCode() {
return this.stack.hashCode() * 13 + this.size;
}
}

View file

@ -11,7 +11,6 @@ package buildcraft.core.lib.gui;
import net.minecraft.item.ItemStack;
public class ItemSlot extends AdvancedSlot {
public ItemStack stack;
public ItemSlot(GuiAdvancedInterface gui, int x, int y) {