Checking if a player is near another player in a radius.

Discussion in 'Plugin Development' started by VinexAx789, Jun 22, 2016.

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

    VinexAx789

    Issue: None of the code is running. I believe it's because of my counter variable.

    What am I trying to accomplish?: I want to make it so if the player is on the same team as the player that is a medic and is in a block radius of 8 blocks it will give them a potion effect of regeneration, now if they stay in the radius for 20 seconds it will give them more potion effects.

    My Code:
    Code:
        int counter = 0;
    
        @SuppressWarnings("deprecation")
        public void onMedicNearby(Player p) {
            double radius = 8D;
            for (String pl : medicPlayer.keySet()) {
                if (!(plugin.getPlayersTeam(p).equals(plugin.getPlayersTeam(Bukkit.getPlayer(pl))))) {
                    return;
                }
                if (plugin.getPlayersTeam(p).equals(plugin.getPlayersTeam(Bukkit.getPlayer(pl)))) {
                    if (p.getLocation().distanceSquared(Bukkit.getPlayer(pl).getLocation()) <= radius * radius) {
                        p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1),
                                true);
                        p.getPlayer().playEffect(p.getLocation(), Effect.HEART, 5);
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                counter++;
                                if (counter == 20) {
                                    p.getPlayer().sendMessage(
                                            C.red + "You are within the radius of a medic you're currently being healed!");
                                    p.getPlayer().addPotionEffect(
                                            new PotionEffect(PotionEffectType.ABSORPTION, Integer.MAX_VALUE, 1), true);
                                    p.getPlayer().playEffect(p.getLocation(), Effect.HEART, 5);
                                    counter = 0;
                                }
                            }
                        }.runTaskTimer(plugin, 20L, 20L);
                    }
    
                }
            }
        }
    If you can please help I would be very thankful.

    Thanks again,

    - Vinex_
     
  2. Offline

    MCMastery

    I haven't looked through your code yet really, but for the BukkitRunnable, the 20L is ticks, not seconds. So if you want 20 seconds, you would use 20 * 20 (since there are 20 ticks in a second).
    just something i noticed... haven't looked at it yet
     
  3. Offline

    VinexAx789

    @MCMastery I made it runTaskTimer(plugin, 20L, 20L) so It would repeat continuously I thought it does that then I used the counter variable to count up to 20 then after that it resets. I want this to count for each player near them.
     
  4. Offline

    Zombie_Striker

    @VinexAx789
    Are you sure you have called that method? Have you tried debugging? At what line exactly does it stop working?
     
  5. Offline

    VinexAx789

    @Zombie_Striker Yes I've called it, and I'm actually currently debugging it right now I'll post my results after.
     
  6. Offline

    Lordloss

    Code:
    (p.getLocation().distanceSquared(Bukkit.getPlayer(pl).getLocation()) <= radius * radius)
    This will get all players within 64 Blocks..
     
  7. Offline

    VinexAx789

    @Lordloss True but at this point I don't care just want to get it working that is all.
     
  8. Offline

    Lordloss

    well than show your updated code, and where it stops working.
     
  9. Offline

    VinexAx789

    @Lordloss I'll have to wait until I can get another tester online which will be sometime today so.

    @Lordloss It's actually running it's just giving the wrong player the effects.

    Code:
        int counter = 0;
    
        @SuppressWarnings("deprecation")
        public void onMedicNearby(Player p) {
            double radius = 8D;
            for (String pl : medicPlayer.keySet()) {
                Bukkit.getPlayer("Vinex_").sendMessage(C.debug + pl);
                if (!(plugin.getPlayersTeam(p).equals(plugin.getPlayersTeam(Bukkit.getPlayer(pl))))) {
                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " does not = to " + pl + "'s team.");
                    return;
                }
                if (plugin.getPlayersTeam(p).equals(plugin.getPlayersTeam(Bukkit.getPlayer(pl)))) {
                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " is = to " + pl + "'s team.");
                    if (p.getLocation().distanceSquared(Bukkit.getPlayer(pl).getLocation()) <= radius * radius) {
                        Bukkit.getPlayer("Vinex_").sendMessage(C.debug + pl + " is in range of " + pl);
                        p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1),
                                true);
                        Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " has been given the effect of regen.");
                        p.getPlayer().playEffect(p.getLocation(), Effect.HEART, 5);
                        Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " has played the effect Heart");
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                counter++;
                                Bukkit.getPlayer("Vinex_").sendMessage(C.debug + "counter adding up");
                                if (counter == 20) {
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + "counter is = to 20");
                                    p.getPlayer().sendMessage(
                                            C.red + "You are within the radius of a medic you're currently being healed!");
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + "Message sent!");
                                    p.getPlayer().addPotionEffect(
                                            new PotionEffect(PotionEffectType.ABSORPTION, Integer.MAX_VALUE, 1), true);
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " has been given the effect of absorption.");
                                    p.getPlayer().playEffect(p.getLocation(), Effect.HEART, 5);
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + p + " has played the effect Heart again!");
                                    counter = 0;
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + "The counter has been reset back to zero!");
                                    Bukkit.getPlayer("Vinex_").sendMessage(C.debug + "End of debugging.");
                                }
                            }
                        }.runTaskTimer(plugin, 20L, 20L);
                    }
    
                }
            }
        }
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 23, 2016
Thread Status:
Not open for further replies.

Share This Page