Posted December 14, 20159 yr 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.
December 14, 20159 yr Make an array of the blockpos and iterate through it I think its my java of the variables.
December 14, 20159 yr 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.
December 14, 20159 yr 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.
December 14, 20159 yr 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.
December 14, 20159 yr 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.
December 14, 20159 yr 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.
December 14, 20159 yr 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.