Jump to content

[1.6.4] Need Help with particles


Jacky2611

Recommended Posts

I'm just tying to add smoke that will appear if the player presses a button.

Right now i have this:

 

Packet Handler

 

 


package net.dimensionshift.mod;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.dimensionshift.mod.ParticleHandler;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;

public class PacketHandler implements IPacketHandler {

@Override
    public void onPacketData(INetworkManager manager,
                    Packet250CustomPayload packet, Player player) {
            
            if (packet.channel.equals("DimensionShift")) {
                    handlePacket(packet, player);
            }
    }
    
    private void handlePacket(Packet250CustomPayload packet, Player player) {
            DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
            String input;
		try {
			input = inputStream.readUTF();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			input=null;
			e.printStackTrace();
		}            
            EntityPlayer entityPlayer = (EntityPlayer) player;	
            
            	//Server receives command and sends it to the clients
            	if (input.equals("specialAbilityServer")) {

            		PacketHandler.sendPacket("specialAbilityClient", entityPlayer);

            		
            	//client receiving special ability command
            	} else if (input.equals("specialAbilityClient")) {

        			Double playerX =entityPlayer.posX;
        			Double playerY =entityPlayer.posY;
        			Double playerZ =entityPlayer.posZ;
        			World world = entityPlayer.getEntityWorld();
            		ParticleHandler.specialAbility(world, playerX, playerY, playerZ);

        	}


    }
    
    
    
    
    
    
    public static boolean sendPacket(String input, EntityPlayer playerEntity) {
        
    	String command = input;
    	
    	 ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length());
         DataOutputStream outputStream = new DataOutputStream(bos);
         try {
                 outputStream.writeUTF(command);
         } catch (Exception ex) {
                 ex.printStackTrace();
         }
         
         Packet250CustomPayload packet = new Packet250CustomPayload();
         packet.channel = "DimensionShift";
         packet.data = bos.toByteArray();
         packet.length = bos.size();
         
         Side side = FMLCommonHandler.instance().getEffectiveSide();
         if (side == Side.SERVER) {
                 //Server
                 EntityPlayerMP player = (EntityPlayerMP) playerEntity;
                 PacketDispatcher.sendPacketToPlayer(packet, (Player)player);
         } else if (side == Side.CLIENT) {
                 // Client
                 EntityClientPlayerMP player = (EntityClientPlayerMP) playerEntity;
                 player.sendQueue.addToSendQueue(packet);
         } else {
         }
         
         return false;
    	
    	
    	
    }

}

 

 

 

and my particle class:

 

 

 

package net.dimensionshift.mod;

import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import net.minecraft.world.World;

public class ParticleHandler {



public static void specialAbility(World world, Double x, Double y, Double z) {

for (int i=0; i<100000; i++) {

		//making smoke random:
	Random random=new Random();
	random.nextDouble();

	//x
	double rangeMinX=0;
	double rangeMaxX=10;
	double randomValueX = (rangeMinX + (rangeMaxX - rangeMinX) * random.nextDouble())-5;

	//Z
	double rangeMinZ=0;
	double rangeMaxZ=10;
	double randomValueZ = (rangeMinZ + (rangeMaxZ - rangeMinZ) * random.nextDouble())-5;

	//y (height)
	double rangeMinY=0;
	double rangeMaxY=2;
	double randomValueY = (rangeMinY + (rangeMaxY - rangeMinY) * random.nextDouble())-1.5;



	world.spawnParticle("smoke", x+randomValueX, y+randomValueY, z+randomValueZ, 0.0D, 0.0D, 0.0D);
	}
}

}

 

 

 

This code ..  works.

I have ~1 sec lag when i press the button and my smoke doesn't really look like a "cloud" (and it disappears after 2 secs, guess i have to make my own smoke...).

 

Can someone tell me if there is another way to spawn particles that is faster and doesn't produce this much lag?

 

Would really be great if someone could show me a better method to create my smoke.

 

Here could be your advertisement!

Link to comment
Share on other sites

1) Don't use string compare.  Use integers, they're faster.

2) Don't use a single packet handler.  Split it up into a server and a client half, this keeps your code clean.

3) Here's why it's slow:

for (int i=0; i<100000; i++) {

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

Hi

 

I think Draco is right, 100,000 particles is insane, vanilla uses 1 - 10.

 

have you considered largesmoke instead of smoke?

 

If it doesn't suit what you need, it's pretty easy to create your own EntityFX and render whatever you like.  A bit of googling will hopefully find you a suitable smoke-rendering algorithm which you can use.

 

-TGG

 

 

Link to comment
Share on other sites

I think Draco is right, 100,000 particles is insane, vanilla uses 1 - 10.

 

Enderman teleport I think is 64 or 128, but it's spread across a fairly wide area.  100k is orders of magnitude larger.

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

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.



×
×
  • Create New...

Important Information

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