Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

  • Author

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.

Well BlockPos.offset(EnumFacing e) can be used

 

Parse through EnumFacing.values() to create the array and then parse through the positions, this way you're not instantiating up to 3 new BlockPos per step

I think its my java of the variables.

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.

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.

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.

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.