Jump to content

Just keeps eating and eating


tmetcalfe89

Recommended Posts

I have a Tile Entity with a slot (we'll call it "fuel slot") and an integer (we'll call it "fuel level"). When items are placed in fuel slot, the integer is incremented by the number of items in fuel slot and its items are removed. This happens until fuel level reaches a cap ("we'll call it fuel max"), then the process stops "eating" the items in fuel slot and stops incrementing fuel level. If you've played with Steve's Carts, the fertilizer module works in a similar manner.

 

The problem is that in my current figuration, when fuel level hits fuel max, the integer stops incrementing but the items are still being removed from fuel slot. All of them.

 

Here's the code:

 

 

AETTileEntity x TileEntity i IInventory

    private static final int MAX_FUEL_LEVEL = 128;
    private ItemStack[] inventory;
    private int fuelLevel;

    public void onInventoryChanged() {
        if (inventory[0] != null) {
            int fuelToAdd = inventory[0].stackSize;
            if (getFuelLevelRoom() < fuelToAdd) {
                decrStackSize(0, getFuelLevelRoom());
                fuelLevel = MAX_FUEL_LEVEL;
            } else {
                decrStackSize(0, fuelToAdd);
                fuelLevel += fuelToAdd;
            }
        }
    }


    private int getFuelLevelRoom() {
        return MAX_FUEL_LEVEL - fuelLevel;
    }

    public ItemStack decrStackSize(int slot, int amount) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
            if (stack.stackSize <= amount) {
                setInventorySlotContents(slot, null);
            } else {
                stack = stack.splitStack(amount);
                if (stack.stackSize == 0) {
                    setInventorySlotContents(slot, null);
                }
            }
        }
        return stack;
    }

    public void setInventorySlotContents(int slot, ItemStack stack) {
        inventory[slot] = stack;
        if (stack != null && stack.stackSize > getInventoryStackLimit()) {
            stack.stackSize = getInventoryStackLimit();
        }
        onInventoryChanged();
    }

 

 

 

Another quirk I'm noticing is that AETTileEntity.onInventoryChange() is firing close to two bazillion times per time I put something in the slot.

 

Full source is available on https://github.com/tmetcalfe89/Elements/

If I helped, show me some love and hit that thanks button!

Link to comment
Share on other sites

if (getFuelLevelRoom() < fuelToAdd)

 

What happens here if getFuelLevelRoom() returns zero?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

if (getFuelLevelRoom() < fuelToAdd)

 

What happens here if getFuelLevelRoom() returns zero?

 

Let's say I put a stack in...

 

if (0 < 64) {

    decrease fuel slot stack size by 0

    set fuel level to fuel max

}

 

Seems a little redundant. I could add a check for 0, but it wouldn't solve my problem.

If I helped, show me some love and hit that thanks button!

Link to comment
Share on other sites

Does it subtract a stack of size zero...or is it subtracting a stack of (size minus 0)?

 

I'm not familiar with the functions there, but it would seem to me that adding a check for 'no fuel needed' would be best.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Well, here's the code for decrStackSize as it was. It was decreasing by 0, so that wasn't the issue. I think it was that onInventoryChange was being called by way too many sources and that there wasn't a distinction between client and server calls so there was a lack of synchronization there.

 

 

 

    public ItemStack decrStackSize(int slot, int amount) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
            if (stack.stackSize <= amount) {
                setInventorySlotContents(slot, null);
            } else {
                stack = stack.splitStack(amount);
                if (stack.stackSize == 0) {
                    setInventorySlotContents(slot, null);
                }
            }
        }
        return stack;
    }

 

 

 

I fixed it though! I deleted the overridden onInventoryChange() method and used updateEntity() instead with the following. (Note: It's now consuming 1 at a time rather than as much as it can and I divvied up some code into private methods.):

 

 

    public void updateEntity() {
        if (!worldObj.isRemote) {
            if (canAddFuel()) {
                decrStackSize(0, 1);
                ++fuelLevel;
                onInventoryChanged();
            }
        }
    }

 

 

If I helped, show me some love and hit that thanks button!

Link to comment
Share on other sites

One at a time should be sufficient in most cases.  I don't think anyone will really notice.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.