# [1.7.10] particle problem

## Recommended Posts

hello guys,

I am making a method to spawn particles on every point of a line.

It makes the particles succesfully, but I found a really big and strange problem:

If I use that method, all entities in the world stop moving and are immume to communications with player,

if I close the world, minecraft freezes...

The problem disappeares if I remove the while statement in the method.

Here is the method:

```public void spawnParticles(World world, double red, double green, double blue, double dbp){
double distanceXT = position2.x - position1.x;
double distanceYT = position2.y - position1.y;
double distanceZT = position2.z - position1.z;
double speedX = distanceXT * dbp;
double speedY = distanceYT * dbp;
double speedZ = distanceZT * dbp;
double x = position1.x;
double y = position1.y;
double z = position1.z;
double minX = position1.x;
double maxX = position2.x;
if(minX > position2.x){
minX = position2.x;
maxX = position1.x;
}
while(x <= maxX && x >= minX){
world.spawnParticle("reddust", x, y, z, red, green, blue);
x += speedX;
y += speedY;
z += speedZ;
}//TODO big problem

}```

Here is the whole class:

```public class Line {
public Line(Position pos1, Position pos2){
position1 = pos1;
position2 = pos2;
refreshDistance();
}
private Position position1;
private Position position2;

public double distanceX;
public double distanceY;
public double distanceZ;
public double distance;

public void refreshDistance(){
distanceX = position2.x - position1.x;
distanceY = position2.y - position1.y;
distanceZ = position2.z - position1.z;
if(distanceX < 0){
distanceX *= -1;
}
if(distanceY < 0){
distanceY *= -1;
}
if(distanceZ < 0){
distanceZ *= -1;
}
double distanceXZ = Math.hypot(distanceX, distanceZ);
distance = Math.hypot(distanceXZ, distanceY);
}
public Position getPosition(int i){
if(i <= 1){
return position1;
}
else {
return position2;
}
}

public void setPosition(int i, Position pos){
if(i <= 1){
position1 = pos;
}
else {
position2 = pos;
}
refreshDistance();
}

/**
*
* @param world the world to spawn the particles.
* @param red how red the particles will be.
* @param green how green the particles will be.
* @param blue how blue the particles will be.
* @param dbp distance between particles.
*/
public void spawnParticles(World world, double red, double green, double blue, double dbp){
double distanceXT = position2.x - position1.x;
double distanceYT = position2.y - position1.y;
double distanceZT = position2.z - position1.z;
double speedX = distanceXT * dbp;
double speedY = distanceYT * dbp;
double speedZ = distanceZT * dbp;
double x = position1.x;
double y = position1.y;
double z = position1.z;
double minX = position1.x;
double maxX = position2.x;
if(minX > position2.x){
minX = position2.x;
maxX = position1.x;
}
while(x <= maxX && x >= minX){
world.spawnParticle("reddust", x, y, z, red, green, blue);
x += speedX;
y += speedY;
z += speedZ;
}//TODO big problem

}
public List getEntities(World world, Class entityClass){
double x1 = position1.x;
double x2 = position2.x;
if(x1 > position2.x){
x1 = position2.x;
x2 = position1.x;
}
double y1 = position1.y;
double y2 = position2.y;
if(y1 > position2.y){
y1 = position2.y;
y2 = position1.y;
}
double z1 = position1.z;
double z2 = position2.z;
if(z1 > position2.z){
z1 = position2.z;
z2 = position1.z;
}
List entities2 = new ArrayList();
List entities = world.getEntitiesWithinAABB(entityClass, AxisAlignedBB.getBoundingBox(x1, y1, z1, x2, y2, z2));
int times = 0;
double factor = ExtraFeatures.divineAccurate(position2.y - position1.y, position2.x - position1.x);
System.out.println(factor);
while(times < entities.size()){
Entity entity = (Entity) entities.get(times);
AxisAlignedBB aabb = entity.boundingBox;
if(aabb != null){
double a = aabb.minX - x1;
double b = aabb.maxX - x1;
double c = aabb.minY - y1;
double d = aabb.maxY - y1;
if(a <= d && b >= c){
}
else {
System.out.println("id: " + entity.getUniqueID() + "    xMin: " + aabb.minX + "   xMax: " + aabb.maxX + "    yMin: " + aabb.minY + "   yMax: " + aabb.maxY + " a,b,c,d   "  + a + "   " + b + "   " + c + "   " + d);
}
}
else {
System.out.println(entity + "   " + aabb);
}
++times;
}
//TODO work in progress
return entities2;
}
}
```

And here is position.java if you need it:

```public class Position {
public double x;
public double y;
public double z;
/**
* Creates a new position on the given location.
* @param posX The x location of the position.
* @param posY The y location of the position.
* @param posZ The z location of the given position.
*/
public Position(double posX, double posY, double posZ){
x = posX;
y = posY;
z = posZ;
}
/**
* Creates a new position on the given location, this constructor works with integers.
* @param posX The x location of the position.
* @param posY The y location of the position.
* @param posZ The z location of the given position.
*/
public Position(int posX, int posY, int posZ){
x = posX;
y = posY;
z = posZ;
}
/**
* Creates a new position on the given location, this constructor works with floats.
* @param posX The x location of the position.
* @param posY The y location of the position.
* @param posZ The z location of the given position.
*/
public Position(float posX, float posY, float posZ){
x = posX;
y = posY;
z = posZ;
}
/**
* Creates a new position at the given entity.
* @param entity The entity where the position has to be created.
*/
public Position(Entity entity){
x = entity.posX;
y = entity.posY;
z = entity.posZ;
}

public Position(MovingObjectPosition mop){
x = mop.hitVec.xCoord;
y = mop.hitVec.yCoord;
z = mop.hitVec.zCoord;
}
/**
* Returns this position as string.
*/
public String toString(){
String string = x + "," + y + "," + z;
return string;
}
/**
* Makes a string for the given position.
* @param p
* @return the string.
*/
public static String makeString(Position p){
return p.toString();
}
/**
* Gives the x of this position as integer.
* @return The integer the double is the closest to.
*/
public int intX(){
double t = x - (int)x;
if(t >= 0.5){
t = 1;
}
else {
t = 0;
}
return (int) ((int)x + t);
}
/**
* Gives the y of this position as integer.
* @return The integer the double is the closest to.
*/
public int intY(){
double t = y - (int)y;
if(t >= 0.5){
t = 1;
}
else {
t = 0;
}
return (int) ((int)y + t);
}
/**
* Gives the z of this position as integer.
* @return The integer the double is the closest to.
*/
public int intZ(){
double t = z - (int)z;
if(t >= 0.5){
t = 1;
}
else {
t = 0;
}
return (int) ((int)z + t);
}
/**
* this method gives the x of the position as float.
* @return The x of the position as float.
*/
public float floatX(){
return (float) x;
}
/**
* this method gives the y of the position as float.
* @return The y of the position as float.
*/
public float floatY(){
return (float)y;
}
/**
* this method gives the z of the position as float.
* @return The z of the position as float.
*/
public float floatZ(){
return (float)z;
}
/**
* Gives the squared distance to another position. It will use Math.hypot for getting it.
* @param p The other position.
* @return The squared distance from this position to the given position.
*/
public double getSquaredDistance(Position p){
double distanceX;
double distanceY;
double distanceZ;
if(x >= p.x){
distanceX = x - p.x;
}
else {
distanceX = p.x - x;
}
if(y >= p.y){
distanceY = y - p.y;
}
else {
distanceY = p.y - y;
}
if(z >= p.z){
distanceZ = z - p.z;
}
else {
distanceZ = p.z - z;
}
double distanceXZ = Math.hypot(distanceX, distanceZ);
return Math.hypot(distanceXZ, distanceY);
}
/**
* Gives the distance to the given position. This will just use distanceX + distanceY + distanceZ.
* @param p The other position.
* @return The distance to the given position.
*/
public double getIndirectDistance(Position p){
double distanceX;
double distanceY;
double distanceZ;
if(x >= p.x){
distanceX = x - p.x;
}
else {
distanceX = p.x - x;
}
if(y >= p.y){
distanceY = y - p.y;
}
else {
distanceY = p.y - y;
}
if(z >= p.z){
distanceZ = z - p.z;
}
else {
distanceZ = p.z - z;
}
return distanceX + distanceY + distanceZ;
}
/**
* Gives the squared distance between two positions. It will use Math.hypot.
* @param a The first position.
* @param p The second position.
* @return The squared distance between the two positions.
*/
public static double getSquaredDistance(Position a, Position p){
double distanceX;
double distanceY;
double distanceZ;
if(a.x >= p.x){
distanceX = a.x - p.x;
}
else {
distanceX = p.x - a.x;
}
if(a.y >= p.y){
distanceY = a.y - p.y;
}
else {
distanceY = p.y - a.y;
}
if(a.z >= p.z){
distanceZ = a.z - p.z;
}
else {
distanceZ = p.z - a.z;
}
double distanceXZ = Math.hypot(distanceX, distanceZ);
return Math.hypot(distanceXZ, distanceY);
}
/**
* Gives the distance between the given positions. This will just use distanceX + distanceY + distanceZ.
* @param p The first position.
* @param a The second position.
* @return The distance between the given positions.
*/
public static double getIndirectDistance(Position a, Position p){
double distanceX;
double distanceY;
double distanceZ;
if(a.x >= p.x){
distanceX = a.x - p.x;
}
else {
distanceX = p.x - a.x;
}
if(a.y >= p.y){
distanceY = a.y - p.y;
}
else {
distanceY = p.y - a.y;
}
if(a.z >= p.z){
distanceZ = a.z - p.z;
}
else {
distanceZ = p.z - a.z;
}
return distanceX + distanceY + distanceZ;
}
/**
* This method will write itself in the given NBTTagCompound.
* It will set a tag with the given key, and set the doubles there.
* This method is not called automatically, so save it where you need it.
* Be sure to use the same key at readFromNBt and to use another key for every position.
* @param nbt The NBTTagCompound where it will save its position.
* @param key The key it will use to create a new tag.
*/
public void writeToNBT(NBTTagCompound nbt, String key){
NBTTagCompound a = new NBTTagCompound();
a.setDouble("x", x);
a.setDouble("y", y);
a.setDouble("z", z);
nbt.setTag(key, a);
}
/**
* This method is made to read the position from the given NBTTagCompound.
* Be sure to use the same NBTTagCompound and string as writeToNBT.
* @param nbt The NBTTagCompound it will use to read the position.
* @param key The tag of the NBTTagCompound it will check.
*/
public void readFromNBT(NBTTagCompound nbt, String key){
x = nbt.getCompoundTag(key).getDouble("x");
y = nbt.getCompoundTag(key).getDouble("y");
z = nbt.getCompoundTag(key).getDouble("z");
}
/**
* Spawns an entity at the given position.
* @param p The position the entity has to spawn.
* @param entity The entity to spawn.
* @param world The world to spawn the entity.
*/
public static void spawnEntity(Position p, Entity entity, World world){
entity.posX = p.x;
entity.posY = p.y;
entity.posZ = p.z;
world.spawnEntityInWorld(entity);
}
/**
* Spawns an entity at this position.
* @param entity The entity to spawn.
* @param world The world to spawn the entity.
*/
public void spawnEntity(Entity entity, World world){
entity.posX = x;
entity.posY = y;
entity.posZ = z;
world.spawnEntityInWorld(entity);
}
/**
* Sets a block at the given position.
* @param p The position to place the block.
* @param world The world to set the block.
* @param block The block to set.
*/
public static void setBlock(Position p, World world, Block block){
world.setBlock(p.intX(), p.intY(), p.intZ(), block);
}
/**
* Sets a block at this position.
* @param world The world to place the block.
* @param block The block to place.
*/
public void setBlock(World world, Block block){
world.setBlock(intX(), intY(), intZ(), block);
}
/**
* Spawns a particle at the given position with the given colors.
* @param world The world to spawn the particle.
* @param p The position to spawn the particle.
* @param color1 The amount red.
* @param color2 The amount green.
* @param color3 The amount purple/blue.
*/
public static void spawnParticle(World world, Position p, double color1, double color2, double color3){
world.spawnParticle("reddust", p.x, p.y, p.z, color1, color2, color3);
}
/**
* Spawn a particle at this position with the given colors.
* @param world The world to spawn the particle.
* @param color1 The amount red.
* @param color2 The amount green.
* @param color3 The amount purple/blue.
*/
public void spawnParticle(World world, double color1, double color2, double color3){
world.spawnParticle("reddust", x, y, z, color1, color2, color3);
}
}```

##### Share on other sites

You have an infinite loop where the condition in the for loop is always true this cocks the came at that line. If you are still able to move around in the world it must be server side. I havent had a good look at your code but is shouldnt be to hard to find.

Pro tip i like to avoid while loops for this reason if there is another way to do it but in a case like yours i always at a break condition to exit the loop if it gets stuck. Usually i have an int that increments each loop and if it exceeds a certain value it breaks the loop.

I am the author of Draconic Evolution

##### Share on other sites

Hi

Like Brandon says, you probably have an infinite loop in that while statement if minX and maxX are equal.  There are also a number of mistakes in the distance logic I think.

What I suggest:

1) calculate the distance between the two particles using the square root formula for distance

2) divide that distance by dbp (first check dbp is something reasonable (say) >= 0.1 ); this gives the number of particles to draw.  Check it is reasonable (say < 100)

3) calculate the stepDeltaX = (finishX - startX) / number of particles.  Repeat for stepDeltaY and stepDeltaZ

4) run a for loop from 1 to number of particles.  Starting from x = startX, each loop calculate x += stepDeltaX.  Do the same for stepDeltaY and stepDeltaZ.

No chance of infinite loop that way, and it will handle all cases of start and end points properly.

-TGG

##### Share on other sites

The idea of calculating the number of particles to spawn worked.

But I still want to know why only the serverside freezed, it was called on client and server. (otherwise I couldn't see the particles.)

Can someone find out why?

##### Share on other sites

I dont want to spent any time debugging your code for you but i will tell you that the client and server handle things differently especially when it comes to rendering so some of the variables you were using probably have slightly different values in the client and server. But that is just an guess.

I am the author of Draconic Evolution

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

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

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

• ### Posts

• I am currently attempting to make a custom mob that will break blocks if they are in the way of attacking the player. I am very new to how goals work in general. Instead of actually writing code for the goal, I have been looking at all of the vanilla mob goals and I am so lost. I really just need a step in the right direction. I already know a decent amount of Java. If anyone could help I would really appreciate it.
• we tried that at first, didnt work... I noticed that other worlds (in this modpack) do load. Maybe u can tell me if this world is lost or somthing? (would be bad bc I have much playtime in it
• I can work with any version. I'm not super familiar with 1.10, but I'll figure it out.
• Nice if it's ok could we do 1.10.2 it's ok if you need a tutorial I know more 1.10.2 that most people nowadays.   The reason why that version is because I want to bring back old forge versions.   If you don't want to I totally understand!
• https://paste.gg/p/anonymous/593e95e3c9e14ab6806b8c03922a2872/files/a567f393ea55432790a967c79fa4b7fc/raw

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...