Help with Particles?

Discussion in 'Spigot Plugin Development' started by VivianMusic, Aug 7, 2018.

  1. Alright, I am using this code to spawn my particles

    Code (Text):
            if(location.getWorld().getName().equalsIgnoreCase("flatroom")) {
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    public void run() {
                        if(exists) {
                            createHelix(location);
                        }
                    }
                }, 5, 5);
            }
    And the method being called..
    Code (Text):
        public void createHelix(Location loc) {
            double radius = 0.5;
            for(double y = 0; y <= 4; y+=0.05) {
                double x = radius * Math.cos(y);
                double z = radius * Math.sin(y);
                PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.PORTAL, true, (float) (loc.getX() + x), (float) (loc.getY() + y -2), (float) (loc.getZ() + z), 0, 0, 0, 0, 1);
                for(Player online : Bukkit.getOnlinePlayers()) {
                    ((CraftPlayer)online).getHandle().playerConnection.sendPacket(packet);
                }
            }
           
            double radius2 = -0.5;
            for(double y = 0; y <= 4; y+=0.05) {
                double x = radius2 * Math.cos(y);
                double z = radius2 * Math.sin(y);
                PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.PORTAL, true, (float) (loc.getX() + x), (float) (loc.getY() + y -2), (float) (loc.getZ() + z), 0, 0, 0, 0, 1);
                for(Player online : Bukkit.getOnlinePlayers()) {
                    ((CraftPlayer)online).getHandle().playerConnection.sendPacket(packet);
                }
            }
        }
    Alright, a few things:
    1. For some WEIRD reason, the particles spawn in ALL worlds even though I clearly have a check for the world "flatroom" or "world". This code creates this cool effect, which I like: https://i.gyazo.com/a8ab247f669c7cf0cfce0afd798271b4.mp4 but that leads to #2.
    2. How can I make the particles go from the starting position, then once they reach max y, they go back down and keep repeating?
     
  2. 1. So although yeah you did the check which will work, in the method createHelix you iterate through every player online and send them the particle packets. Instead of this though you may just want to spawn normal Particles using the Spigot API (World.spawnParticle) rather than iterating through players and sending packets.

    2. What so the helix goes up and then down or something?
     
  3. But why is it spawning in every world...?

    and yes, that is what I want
     
  4. Alright, adding this fixed it:

    Code (Text):
                for(Player online : Bukkit.getOnlinePlayers()) {
                    if(online.getWorld().getName().equalsIgnoreCase("flatroom")) {
                        ((CraftPlayer)online).getHandle().playerConnection.sendPacket(packet);
                    }
                }
    but what about the up/down particles?
     
  5. I'm not 100% sure, I just do a lot of trial and error when it comes to that. I think though you need to change the for loop so that instead of iterating from top to bottom, its iterating from bottom to top?
     
  6. It would need to like..flip
     
  7. #7 PanteLegacy, Aug 8, 2018
    Last edited: Aug 8, 2018
    • Agree Agree x 1
  8. Please use the normal particle API. This is a pain to work with.
     
    • Agree Agree x 2
  9. What is actually happening atm, and how is this different from what you are trying to achieve? Perhaps a video might help.
     
  10. If you insist on sending every player separately just use World#getPlayers...
    Code (Text):
    for (Player player: world.getPlayers()) { ... }
    And like others have said before, use the builtin API functions World#spawnParticle as they do all the work for you. I use them all the time; they're really great.
     
  11. Running 1.8.8

    I posted a gif in my op.
     
    #11 VivianMusic, Aug 8, 2018
    Last edited by a moderator: Aug 8, 2018
  12. The client don't care which world they are in, if they receive the particle packet they will display those particle at that specific coordinates.

    Instead of using Bukkit.getOnlinePlayers() use what our friend StarTux suggested.
     
  13. I already made a post saying I solved that issue, i just need to get the fancier particles