This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sirdabalot's profile picture

Mega newb help with returning things [SOLVED]

Started by sirdabalot, 10 November 2012 - 11:49 PM
sirdabalot #1
Posted 11 November 2012 - 12:49 AM
Right, so I have this:


public Object[] callMethod(IComputerAccess computer, int method,
   Object[] arguments) throws Exception {
  switch(method) {
  case 0:
   return new Object[] World.getBlockId();
  }
}

But I have no idea how to put in the arguments received from the computer into the x, y, z arguments of the World.getBlockId() part, can anyone help me?

Yes, I am literally just starting to learn java.
Orwell #2
Posted 11 November 2012 - 04:15 AM
First of, to return an array of Objects, you should do it like so:

return new Object[] {1,2,3};
With the accolades.

Also, you're using World.getBlockId() wrong. 'World' is a class, but you need an instance of the class. You're probably using this in an extended class of TileEntity, so you could get the world Object like so:

World world = this.worldObj;
The x,y and z coordinates are also stored in the TileEntity class, so what you want would be:

public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception {
  switch( method ) {
	case 0:
	  World world = this.worldObj;
	  return new Object[] {world.getBlockId(this.xCoord, this.yCoord, this.zCoord) };
	  break;
	default:
	  break;
  }
  return new Object[] {};  // if the method wasn't 0, return an empty array to avoid null values.
}

In the switch statement, it's best to break after the specific case and have a default statement to catch everything that isn't any of the cases.
billysback #3
Posted 11 November 2012 - 04:44 AM
you could also do:

public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception {
  Object[] obs = {}
  switch( method ) {
		case 0:
		  World world = this.worldObj;
		  obs = {world.getBlockId(this.xCoord, this.yCoord, this.zCoord) };
		  break;
		default:
		  break;
  }
  return obs;
}
In my experience switch has never worked for me, at least not how I wanted, so I never use it, so I wouldn't do it this way personally, but if I did this is the way I would have done it, little difference but just my preference.
sirdabalot #4
Posted 11 November 2012 - 05:18 AM
First of, to return an array of Objects, you should do it like so:

return new Object[] {1,2,3};
With the accolades.

Also, you're using World.getBlockId() wrong. 'World' is a class, but you need an instance of the class. You're probably using this in an extended class of TileEntity, so you could get the world Object like so:

World world = this.worldObj;
The x,y and z coordinates are also stored in the TileEntity class, so what you want would be:

public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception {
  switch( method ) {
	case 0:
	  World world = this.worldObj;
	  return new Object[] {world.getBlockId(this.xCoord, this.yCoord, this.zCoord) };
	  break;
	default:
	  break;
  }
  return new Object[] {};  // if the method wasn't 0, return an empty array to avoid null values.
}

In the switch statement, it's best to break after the specific case and have a default statement to catch everything that isn't any of the cases.

Some of that makes sense, maybe I should learn more of the basics and revisit this.
Oh, and also where you put this.xCoord, this.yCoord, this.zCoord, how would I make the computer input the coordinates? Because I don't want it to get the position of the block itself but I want to make a function for the computer so I can use it like this:


mapper = peripheral.wrap(side)
mapper.getBlockId(X, Y, Z)
Orwell #5
Posted 11 November 2012 - 05:54 AM
Well, it's in the arguments array, so you'd do this:

if (arguments.length > 2) {
  int x = (int) arguments[0];
  int y = (int) arguments[1];
  int z = (int) arguments[2];
  World world = this.worldObj;
  return new Object[] { world.getBlockId(x,y,z) };
}
break;
Under 'case 0:'.

I also use the way to define the array as empty at the beginning, then I assign an array to it if needed. The switch statement is probably the most efficient ways to do this though and is also used in the ComputerCraft code in the callMethod function if I'm not mistaken.
sirdabalot #6
Posted 11 November 2012 - 06:01 AM
Well, it's in the arguments array, so you'd do this:

if (arguments.length > 2) {
  int x = (int) arguments[0];
  int y = (int) arguments[1];
  int z = (int) arguments[2];
  World world = this.worldObj;
  return new Object[] { world.getBlockId(x,y,z) };
}
break;
Under 'case 0:'.

I also use the way to define the array as empty at the beginning, then I assign an array to it if needed. The switch statement is probably the most efficient ways to do this though and is also used in the ComputerCraft code in the callMethod function if I'm not mistaken.

Thanks
sirdabalot #7
Posted 11 November 2012 - 08:06 AM
Kay, if I wanted to use a normal Block instead of a TileEntity, how would I go about it?

EDIT: Never mind, SUPAH DERP!
sirdabalot #8
Posted 16 November 2012 - 05:57 AM
Well, it's in the arguments array, so you'd do this:

if (arguments.length > 2) {
  int x = (int) arguments[0];
  int y = (int) arguments[1];
  int z = (int) arguments[2];
  World world = this.worldObj;
  return new Object[] { world.getBlockId(x,y,z) };
}
break;
Under 'case 0:'.

I also use the way to define the array as empty at the beginning, then I assign an array to it if needed. The switch statement is probably the most efficient ways to do this though and is also used in the ComputerCraft code in the callMethod function if I'm not mistaken.

One last question; the line int x = (int) arguments[0] tells me that I "Cannot cast from Object to int". So… what, do I use some sort of conversion function or something?
Orwell #9
Posted 16 November 2012 - 12:02 PM
Well, it's in the arguments array, so you'd do this:

if (arguments.length > 2) {
  int x = (int) arguments[0];
  int y = (int) arguments[1];
  int z = (int) arguments[2];
  World world = this.worldObj;
  return new Object[] { world.getBlockId(x,y,z) };
}
break;
Under 'case 0:'.

I also use the way to define the array as empty at the beginning, then I assign an array to it if needed. The switch statement is probably the most efficient ways to do this though and is also used in the ComputerCraft code in the callMethod function if I'm not mistaken.

One last question; the line int x = (int) arguments[0] tells me that I "Cannot cast from Object to int". So… what, do I use some sort of conversion function or something?

Oh yea, forgot, you need to cast the object to the Integer wrapper and then get the inner int value. Like this:

int x = ((Integer) arguments[0]).intValue();
sirdabalot #10
Posted 16 November 2012 - 11:27 PM
Oh yea, forgot, you need to cast the object to the Integer wrapper and then get the inner int value. Like this:

int x = ((Integer) arguments[0]).intValue();

Thanks for your help and for putting up with me. :)/>/>
immibis #11
Posted 18 November 2012 - 01:57 AM
Well, it's in the arguments array, so you'd do this:

if (arguments.length > 2) {
  int x = (int) arguments[0];
  int y = (int) arguments[1];
  int z = (int) arguments[2];
  World world = this.worldObj;
  return new Object[] { world.getBlockId(x,y,z) };
}
break;
Under 'case 0:'.

I also use the way to define the array as empty at the beginning, then I assign an array to it if needed. The switch statement is probably the most efficient ways to do this though and is also used in the ComputerCraft code in the callMethod function if I'm not mistaken.

One last question; the line int x = (int) arguments[0] tells me that I "Cannot cast from Object to int". So… what, do I use some sort of conversion function or something?

Oh yea, forgot, you need to cast the object to the Integer wrapper and then get the inner int value. Like this:

int x = ((Integer) arguments[0]).intValue();
Actually, Java does the second part automatically. And the argument is always a Double, never an Integer, so that will probably crash when you call the function. One of these works:

int x = (int)(Double)arguments[0];
int x = (int)(double)(Double)arguments[0];
I'm not sure if you need the middle (double) or not.
Orwell #12
Posted 18 November 2012 - 02:30 AM
You are right, didn't think that through. But still, I don't think you can cast Double to int.. Not sure though. Couldn't you do this instead?

int x = ((Double)arguments[0]).intValue();
It's what I often use.

Edit: sirdabalot didn't comment on this anymore though. So maybe it did work after all? I wonder. I guess not, casting an object to Integer while it's actually a Double, how could Java handle that..
Espen #13
Posted 18 November 2012 - 03:58 AM
What you did with casting Object to Double and then call intValue on that will work, as all the converison work is done by the Double.intValue() method.
But you're right in that you cannot cast Double to Integer (i.e. (Integer)DoubleVariable ), as that would be a loss of precision which isn't automatically handled by casts. So the way you did it with ((Double)arguments[0]).intValue() is definitely the way to do it.
Edited on 18 November 2012 - 03:02 AM
sirdabalot #14
Posted 18 November 2012 - 04:05 AM
Oops, sorry didn't recognise that more people replied… but, yeah I figured it out with trial and error when I got the crash.