Jump to content

[1.8] Help me simplify this method?


AnZaNaMa

Recommended Posts

I feel like there is a way to simplify this method, but I can't figure out how. Any help is appreciated.

 

public PowerConnectable[] getConnectedMachines(){
        PowerConnectable[] machines = new PowerConnectable[]{null};
        if(worldObj.getTileEntity(pos.east()) != null && worldObj.getTileEntity(pos.east()) instanceof PowerConnectable){
            machines[0] = (PowerConnectable)worldObj.getTileEntity(pos.east());
        }
        if(worldObj.getTileEntity(pos.west()) != null && worldObj.getTileEntity(pos.west()) instanceof PowerConnectable){
            machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.west());
        }
        if(worldObj.getTileEntity(pos.north()) != null && worldObj.getTileEntity(pos.north()) instanceof PowerConnectable){
            machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.north());
        }
        if(worldObj.getTileEntity(pos.south()) != null && worldObj.getTileEntity(pos.south()) instanceof PowerConnectable){
            machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.south());
        }
        if(worldObj.getTileEntity(pos.up()) != null && worldObj.getTileEntity(pos.up()) instanceof PowerConnectable){
            machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.up());
        }
        if(worldObj.getTileEntity(pos.down()) != null && worldObj.getTileEntity(pos.down()) instanceof PowerConnectable){
            machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.down());
        }
        return machines;
    }

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

good idea! thanks! I was trying to figure out how I could make it into a for loop, but with needing east, west, north, south, up, down, I couldn't figure it out.

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

Also store the TE in a variable:

 

TileEntity te = world.getTileEntity (pos.Noth ());
If (te != null && te instanceof PowerConnectable) { }

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

Also why are you using an array when you clearly want a list?

 

PowerConnectable[] machines = new PowerConnectable[]{null};

machines[machines.length] = (PowerConnectable)worldObj.getTileEntity(pos.west());

You're going to crash since the max index of that array is 0 and then length is always going to be 1

I think its my java of the variables.

Link to comment
Share on other sites

BlockPos.offset(EnumFacing e) can be used

That's what I'd do. I also recommend walking through the entire EnumFacing class from top to bottom to learn what's available for use. Don't reinvent any wheels.

 

And I agree that a list would be better than an array.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

So most space & process efficient is:

List<MyTileEntity> found = Lists.<MyTileEntity>newArrayList();
TileEntity t;
for(Enumfacing e : EnumFacing.values())
    if((t = World.getTileEntity(BlockPos.offset(e , 1))) instanceof MyTileEntity)
        found.add((MyTileEntity)t);

 

If you don't need to hold onto the list you may as well remove it and directly invoke whatever methods you need where the add is.

I think its my java of the variables.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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