Solved Get position of particles

Discussion in 'Plugin Development' started by loman, Apr 5, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    loman

    Hello, I am making a system which spawns particles and detects if the player touches the particles and give effect if does.
    But now I get sucks on how to get the position of the particles
    Any ideas plz?

    Here is how I spawn particles:
    Code:
    PacketPlayOutWorldParticles packet1 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, true, locationx, locationy, locationz, 1, 0, 1, 0, 5);
                    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet1);
    or maybe there are other ways to spawn particles and detect if player touches them?
     
    Last edited: Apr 5, 2020
  2. Offline

    KarimAKL

    @loman Why do you use packets?

    You have the location from "locationx", "locationy", and "locationz", from there you can check if the player is near that location using:
    Code:Java
    1. // Create a location object from the x, y, and z.
    2. Location particleLocation = new Location(player.getWorld(), locationx, locationy, locationz);
    3.  
    4. // Check if the player's location is near that location
    5. double range = 1.0; // 1 block
    6.  
    7. if (player.getLocation().distanceSquared(particleLocation) <= range * range) {
    8. // Do something if they're within the specified range of the particle
    9. }
     
  3. Offline

    loman

    ummm @KarimAKL
    "locationx", "locationy", and "locationz" are actually the x , y , z location of the player
    And the reason I use packets is that I use it randomly spawn particles around the player
    So any method to get the location of the particles and detect if any player touches them?
    Thx

    Code:
    Float locationx = (float)player.getLocation().getX();
                    Float locationy = (float)player.getLocation().getY()+1;
                    Float locationz = (float)player.getLocation().getZ();
                    PacketPlayOutWorldParticles packet1 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, true, locationx, locationy, locationz, 1, 0, 1, 0, 5);
                    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet1);
     
  4. Offline

    KarimAKL

    Then just use that Location instead of creating a new Location object.

    You can do that with the Bukkit API, you don't need to use packets.

    I gave you the code with an explanation, what more do you need?
     
  5. Offline

    loman

    @KarimAKL
    I think the main problem is the method u suggest can't generate a random position of particles?
    It's because I want the particles generated randomly around the player randomly within a 2*2 range :)
    Maybe can u show me an example of how to spawn particles with a bukkit API and don't need to use packet?
    I'm sorry that I seem to make u angry .....
     
    Last edited: Apr 5, 2020
  6. Offline

    KarimAKL

    @loman Just use Player#spawnParticle() like you use the packet method.

    Also, i'm not angry. :p
     
  7. Offline

    loman

    Oh
    I will try that
    Thx for you generous help :) :p
     
  8. Offline

    KarimAKL

    @loman Let me know how it goes.
     
  9. Offline

    loman

    Code:
    getServer().getWorld().spawnParticle(Particle.FLAME,particleLocation,5);
    double range = 0.25;
    if (player.getLocation().distanceSquared(particleLocation) <= range * range) {
    player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 1,1));
    }
    
    @KarimAKL I do it like this
    hope it works


    btw why I only can insert general code but u can insert java code? How to do that
     
  10. Offline

    KarimAKL

    @loman It should work.

    Also, use [Syntax=Java]Code here[/Syntax] for the Java syntax highlighting.
     
    loman likes this.
  11. Offline

    loman

    @KarimAKL why my plugin can't detect the nearby players :thinking:
    is that I make some mistakes of the plugin?
    thx~

    Code:Java
    1. for(Entity entity : player.getNearbyEntities(3,3,3)){
    2. if (entity instanceof LivingEntity) {
    3. double range = 1;
    4. for (int a = 1; a <= 5; a++) {
    5. if (entity.getLocation().distanceSquared(this.position.get("flame" + a)) <= range * range) {
    6. getLogger().info("potion");
    7. ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 5, 4));
    8. }}}}
     
    Last edited: Apr 5, 2020
  12. Offline

    KarimAKL

    @loman What happens when you run this code? Did you make sure any entities were present when testing? What is your situation when testing? Also, why do you have a loop that runs 5 times in there?
     
  13. Offline

    loman

    @KarimAKL
    when i test this code, I build a 5*5 platform with some zombie and another player on that platform
    The reason I loop 5 times is that i spawn 5 particles randomly and store the location in a hashmap
    The particles are successfully spawned but not potion effect is given by the plugin ._.
    And I have tried to debug
    The plugin can detect that there are zombies and the player next to me
    But just don't give any potion effects even the particles are just next to him
     
  14. Offline

    KarimAKL

    @loman So you're telling me that:
    Code:Java
    1. // This runs
    2. for (Entity entity : player.getNearbyEntities(3, 3, 3)) {
    3. // This runs
    4. if (entity instanceof LivingEntity) {
    5. // This runs
    6. double range = 1;
    7. for (int a = 1; a <= 5; a++) {
    8. // This doesn't run
    9. getLogger().info("potion");
    10. ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 5, 4));
    11. }
    12. }
    13. }

    Because that doesn't make any sense. What is the results of your debugging?
     
  15. Offline

    loman

    @KarimAKL yes u are right
    result is : the cmd doesn't show "potion" but it can detect all the entity in nearby 3*3*3
    so conclusion is : I don't know where is wrong :p

    edit: I think maybe I make another way to detect if the player is near the particles and try if it works, thx for your help~
     
    Last edited: Apr 5, 2020
Thread Status:
Not open for further replies.

Share This Page