This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_wow
K_wow wrote ...

2014/11/14

Problems with MC modding!

K_wow K_wow

2014/11/14

#
I'm trying to make an Australian Animals mod for Minecraft, and in this mod the wombat digs burrows. This works by obtaining coordinates in an array and setting the block with those coordinates to air. The problem is, the wombat keeps trying to obtain the coordinates from the next array item even after it has finished digging the burrow. Here is my code:
	public void onLivingUpdate()
	{
		if (!this.worldObj.isClient && this.worldObj.getGameRules().getGameRuleBooleanValue("mobGriefing") && !this.getClipped())
        {
			Block var4;
            if (this.rand.nextInt(1000) < 100)
            {
                if (digProgress <= -1)
                {
                	burrowX = MathHelper.floor_double(this.posX);
                	burrowY = MathHelper.floor_double(this.posY);
                	burrowZ = MathHelper.floor_double(this.posZ);
                	
                	digProgress = 0;
                }
            }
            if (digProgress >= 0)
            {
            	int digProgressActual = digProgress / 20;
            	if (digProgressActual < 23);
            	{
					var4 = this.worldObj.getBlock(burrowX + digPositionsX[digProgressActual], burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual]);
					if (carriableBlocks[Block.getIdFromBlock(var4)])
            		{
            			if (digFailures < 2)
            			{
            				if (digProgressActual == 2)
            				{
            					if (!firstFailure)
            					{
            						worldObj.setBlock(burrowX + digPositionsX[digProgressActual], burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual], Blocks.air);
            						setPositionAndUpdate(burrowX + digPositionsX[digProgressActual] + 0.5D, burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual] + 0.5D);
            					}
                    			else
                    			{
                    				digFailures = 0;
                    				firstFailure = false;
                    				secondFailure = false;
                    				digProgress = -1;
									burrowX = 0;
									burrowY = 0;
									burrowZ = 0;
                    			}
            				}
            				else if (digProgressActual == 4)
            				{
            					if (!secondFailure)
            					{
            						worldObj.setBlock(burrowX + digPositionsX[digProgressActual], burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual], Blocks.air);
            						setPositionAndUpdate(burrowX + digPositionsX[digProgressActual] + 0.5D, burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual] + 0.5D);
            					}
                    			else
                    			{
                    				digFailures = 0;
                    				firstFailure = false;
                    				secondFailure = false;
                    				digProgress = -1;
									burrowX = 0;
									burrowY = 0;
									burrowZ = 0;
                    			}
            				}
            				else
            				{
            					worldObj.setBlock(burrowX + digPositionsX[digProgressActual], burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual], Blocks.air);
        						setPositionAndUpdate(burrowX + digPositionsX[digProgressActual] + 0.5D, burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual] + 0.5D);
            				}
            			}
            			else
            			{
            				digFailures = 0;
            				firstFailure = false;
            				secondFailure = false;
            				digProgress = -1;
							burrowX = 0;
							burrowY = 0;
							burrowZ = 0;
            			}
            		}
            		else if (var4 != Blocks.air)
            		{
            			if (digProgressActual == 1 && !firstFailure)
            			{
            				digFailures++;
            				firstFailure = true;
            			}
            			if (digProgressActual == 3 && !secondFailure)
            			{
            				digFailures++;
            				firstFailure = true;
            			}
            		}
            		digProgress++;
            	}
            }
		}
		if (this.nailTimer > 0)
		{
			--this.nailTimer;
			setClipped(true);
		}
		else
		{
			setClipped(false);
		}
		super.onLivingUpdate();
	}
The problem lies with this line:
var4 = this.worldObj.getBlock(burrowX + digPositionsX[digProgressActual], burrowY + digPositionsY[digProgressActual], burrowZ + digPositionsZ[digProgressActual]);
The wombat tries to obtain a value that is outside the bounds of the coordinate arrays, despite the fact that I have code to ensure that the wombat stops digging before that happens. The arrays hold 23 items, and the wombat is supposed to only obtain the "var4" block as long as digProgressActual (the variable used to obtain the array value) is under 23. I have tried lowering the digProgressActual limit, but the wombat just keeps on digging until it crashes the game. Can someone please tell me what I'm doing wrong?
danpost danpost

2014/11/14

#
Please clear your terminal window, recreate the error and copy/paste the terminal error listing here. Oh, and if you have modified the code, re-post it also.
davmac davmac

2014/11/14

#
Your problem is on line 20:
            if (digProgressActual < 23);  
That semi-colon at the end of the line shouldn't be there. It effectively makes the 'if' statement do nothing.
K_wow K_wow

2014/11/14

#
Thankyou so much davmac! The burrow digging works great now! :) It seems I need to observe my code more closely in future.
danpost danpost

2014/11/14

#
@davmac, good catch (things like that are difficult to spot, especially if you are not looking for it specifically). I should try to remember to look for things like that in the code of others.
You need to login to post a reply.