fix interesting delivery robot bug, improve StatementParameterItemStackExact further

This commit is contained in:
Adrian Siekierka 2017-11-11 14:37:42 +01:00
parent 70cc38697e
commit 9bfc13031b
6 changed files with 100 additions and 20 deletions

View file

@ -2,4 +2,5 @@ Bugs fixed:
* [#3839] Attempt to work around potential world corruption?
* [#3837] Oil spawn in mid-air on superflat
* Potential direction statement parameter crash in gate GUI
* Delivery robots dropping requested item if occupies >1 slot
* Potential direction statement parameter crash in gate GUI

View file

@ -8,6 +8,8 @@
*/
package buildcraft.core.lib.inventory;
import java.util.Iterator;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
@ -238,4 +240,55 @@ public final class InvUtils {
return null;
}
public static Iterable<IInvSlot> getItems(final IInventory inv, final IStackFilter filter) {
return new Iterable<IInvSlot>() {
@Override
public Iterator<IInvSlot> iterator() {
return new Iterator<IInvSlot>() {
private final Iterator<IInvSlot> parent = InventoryIterator.getIterable(inv).iterator();
private boolean searched = false;
private IInvSlot next;
private void find() {
next = null;
searched = true;
while (parent.hasNext()) {
IInvSlot s = parent.next();
if (s.getStackInSlot() != null && filter.matches(s.getStackInSlot())) {
next = s;
return;
}
}
}
@Override
public boolean hasNext() {
if (!searched) {
find();
}
return next != null;
}
@Override
public IInvSlot next() {
if (!searched) {
find();
}
IInvSlot current = next;
find();
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported.");
}
};
}
};
}
}

View file

@ -11,8 +11,16 @@ import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.StatementMouseClick;
public class StatementParameterItemStackExact implements IStatementParameter {
protected ItemStack stack;
private int availableSlots;
public StatementParameterItemStackExact() {
this(-1);
}
public StatementParameterItemStackExact(int availableSlots) {
this.availableSlots = availableSlots;
}
@Override
public IIcon getIcon() {
@ -30,8 +38,10 @@ public class StatementParameterItemStackExact implements IStatementParameter {
if (areItemsEqual(this.stack, stack)) {
if (mouse.getButton() == 0) {
this.stack.stackSize += (mouse.isShift()) ? 16 : 1;
if (this.stack.stackSize > this.stack.getMaxStackSize()) {
this.stack.stackSize = this.stack.getMaxStackSize();
int maxSize = availableSlots < 0 ? 64 : Math.min(64, this.stack.getMaxStackSize() * availableSlots);
if (this.stack.stackSize > maxSize) {
this.stack.stackSize = maxSize;
}
} else {
this.stack.stackSize -= (mouse.isShift()) ? 16 : 1;
@ -46,8 +56,10 @@ public class StatementParameterItemStackExact implements IStatementParameter {
if (this.stack != null) {
if (mouse.getButton() == 0) {
this.stack.stackSize += (mouse.isShift()) ? 16 : 1;
if (this.stack.stackSize > this.stack.getMaxStackSize()) {
this.stack.stackSize = this.stack.getMaxStackSize();
int maxSize = availableSlots < 0 ? 64 : Math.min(64, this.stack.getMaxStackSize() * availableSlots);
if (this.stack.stackSize > maxSize) {
this.stack.stackSize = maxSize;
}
} else {
this.stack.stackSize -= (mouse.isShift()) ? 16 : 1;
@ -66,10 +78,15 @@ public class StatementParameterItemStackExact implements IStatementParameter {
stack.writeToNBT(tagCompound);
compound.setTag("stack", tagCompound);
}
compound.setInteger("availableSlots", availableSlots);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
if (compound.hasKey("availableSlots")) {
availableSlots = compound.getInteger("availableSlots");
}
stack = ItemStack.loadItemStackFromNBT(compound.getCompoundTag("stack"));
}

View file

@ -104,10 +104,11 @@ public class EntityRobot extends EntityRobotBase implements
public static final ResourceLocation ROBOT_BASE = new ResourceLocation(
DefaultProps.TEXTURE_PATH_ROBOTS + "/robot_base.png");
public static final int MAX_WEARABLES = 8;
public static final int TRANSFER_INV_SLOTS = 4;
private static Set<Integer> blacklistedItemsForUpdate = Sets.newHashSet();
public LaserData laser = new LaserData();
public LaserData laser = new LaserData();
public DockingStation linkedDockingStation;
public BlockIndex linkedDockingStationIndex;
public ForgeDirection linkedDockingStationSide;
@ -131,7 +132,7 @@ public class EntityRobot extends EntityRobotBase implements
private List<ItemStack> wearables = new ArrayList<ItemStack>();
private boolean needsUpdate = false;
private ItemStack[] inv = new ItemStack[4];
private ItemStack[] inv = new ItemStack[TRANSFER_INV_SLOTS];
private FluidStack tank;
private int maxFluid = FluidContainerRegistry.BUCKET_VOLUME * 4;
private ResourceLocation texture;

View file

@ -53,25 +53,32 @@ public class AIRobotDeliverRequested extends AIRobot {
return;
}
IInvSlot slot = InvUtils.getItem(robot, new ArrayStackOrListFilter(requested.getStack()));
if (slot == null) {
setSuccess(false);
terminate();
return;
}
IRequestProvider requester = requested.getRequester(robot.worldObj);
if (requester == null) {
setSuccess(false);
terminate();
return;
}
ItemStack newStack = requester.offerItem(requested.getSlot(), slot.getStackInSlot().copy());
if (newStack == null || newStack.stackSize != slot.getStackInSlot().stackSize) {
slot.setStackInSlot(newStack);
// TODO: Make this not exceed the requested amount of items.
int count = 0;
for (IInvSlot slot : InvUtils.getItems(robot, new ArrayStackOrListFilter(requested.getStack()))) {
int difference = slot.getStackInSlot().stackSize;
ItemStack newStack = requester.offerItem(requested.getSlot(), slot.getStackInSlot().copy());
if (newStack == null) {
slot.setStackInSlot(newStack);
} else if (newStack.stackSize != slot.getStackInSlot().stackSize) {
slot.setStackInSlot(newStack);
difference = newStack.stackSize - difference;
}
count += difference;
}
setSuccess(count > 0);
terminate();
}
}

View file

@ -13,6 +13,7 @@ import net.minecraft.client.renderer.texture.IIconRegister;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.core.lib.utils.StringUtils;
import buildcraft.core.statements.StatementParameterItemStackExact;
import buildcraft.robotics.EntityRobot;
public class ActionStationRequestItems extends ActionStationInputItems {
@ -42,6 +43,6 @@ public class ActionStationRequestItems extends ActionStationInputItems {
@Override
public IStatementParameter createParameter(int index) {
return new StatementParameterItemStackExact();
return new StatementParameterItemStackExact(EntityRobot.TRANSFER_INV_SLOTS);
}
}