I have a small problem where the elements from my arraylist are not removing. This is an ArrayList. Here's my code:
package net.lucrecious.armorconstruct.helpers;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
public class WorldHelper {
public static void replace_coord_with_block(World world, ArrayList<int[]> coords, Block result_block){
for (int[] c : coords){
world.setBlock(c[0], c[1], c[2], result_block);
}
}
public static ArrayList<int[]> get_blocks_in_sphere(EntityPlayer player, World world, int radius, Class<?>...block_types){
ArrayList<int[]> coord_list = new ArrayList<int[]>();
int r = radius;
int i = MathHelper.floor_double(player.posX);
int j = MathHelper.floor_double(player.posY);
int k = MathHelper.floor_double(player.posZ);
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance
if(dist > r)
continue;
Block block = world.getBlock(i+x, j+y, k+z);
for (Class<?> cls : block_types){
if (cls.isInstance(block)){
coord_list.add(new int[]{i+x, j+y, k+z});
}
}
}
}
}
return coord_list;
}
public static ArrayList<int[]> get_blocks_in_sphere_holo(EntityPlayer player, World world, int radius, Class<?>...block_types){
ArrayList<int[]> sphere = get_blocks_in_sphere(player, world, radius, block_types);
ArrayList<int[]> inner_sphere = get_blocks_in_sphere(player, world, radius-1, block_types);
sphere.removeAll(inner_sphere);
return sphere;
}
public static ArrayList<int[]> get_blocks_in_sphere_filled(EntityPlayer player, World world, int radius, Class<?>...block_types){
return get_blocks_in_sphere(player, world, radius, block_types);
}
}
The idea is that getting the coordinates for a filled sphere, then grabbing the coordinates for a slightly smaller filled sphere and removing similar coordinates, effectively making an unfilled sphere.
I know there's some minecraft code in there - but it should still be understandable. I'm also sure that there would be similar coordinates. I even tried this with no decrease in radius, and this still doesn't work. So even if all the coordinates are same, this doesn't work.
Any ideas on what's causing this or how to make it work?
When you call removeAll()
java will remove entries from the list that are equal to any entry in the list you pass as an argument. The trouble is, two arrays are never equal unless they are exactly the same array instance, which in your code they are not.
You could use a wrapper class to contain your int arrays, which overrides the equals()
method and uses Arrays.equals(array1, array2)
to compare two arrays for equality.