[SOLVED] Getting Nearby Players

Discussion in 'Plugin Development' started by Hunt800, Apr 9, 2011.

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

    Hunt800

    Let me start off by saying I'm a total n00b. I've made some things in Java (a while ago) but never touched anything MineCraft related until like yesterday. So, I've started making a little Gambling plugin for commands like /roll or /card just because I figured it'd be easy to learn with (no real world manipulation). Everything started off nice, I got the hang of Maven and was able to get a simple plugin, with Permissions, that would take a command like "/roll <numDice> <sides>" and broadcast a result. But, then I realized it would be annoying to spam the whole server with those messages so I tried to make it only broadcast to nearby players, but now it's glitching.
    TL;DR: Meh, that's fine, you don't need to read that crappy background crap. Basically, below is some method that's meant to get nearby PLAYERS and put them into an Array
    Code:
        private ArrayList<Player> getNear(Player player) {
        	List<Entity> nearbyEntities = player.getNearbyEntities(25, 25, 25);
        	ArrayList<Player> nearbyPlayers = new ArrayList<Player>();
        	nearbyPlayers.add(player);
        	for(Integer i = 0; i<nearbyEntities.size(); i++) {
        		Entity curEnt = nearbyEntities.get(i);
        		if(!curEnt.isDead() && curEnt instanceof Player) {
        			if((Player)curEnt != player) {
        				nearbyPlayers.add((Player)curEnt);
        			}
        		}
        	}
        	return nearbyPlayers;
        }
    But... When I try to do anything with nearbyPlayers (ie in a for loop "nearbyPlayers.get(i).sendMessage("blah")") and nearbyPlayers has someone in it OTHER than the original player, I get an internal error. What dumb/stupid/n00by error am I looking right past?

    Thanks.
     
  2. Offline

    vildaberper

    ArrayList<Player> nearbyPlayers = new ArrayList<Player>();
    should be
    List<Player> nearbyPlayers = new ArrayList<Player>();

    Thats how I do it. :)
    Dont know if that is the problem though, but the rest looks fine to me.
     
  3. Offline

    Hunt800

    Ah, never mind. I changed how I went about it.
    Code:
    private void broadcastNear(Player player, String toBroadcast, Integer distance) {
            for (Player p : getServer().getOnlinePlayers()) {
                if (getDistance(p.getLocation(), player.getLocation()) <= distance) {
                    p.sendMessage(toBroadcast);
                }
            }
        }
        private float getDistance(Location p, Location q) {
            return (float) Math.sqrt(Math.pow(p.getBlockX() - q.getBlockX(), 2) + Math.pow(p.getBlockY() - q.getBlockY(), 2) + Math.pow(p.getBlockZ() - q.getBlockZ(), 2));
        }
    
    Works better
     
  4. Offline

    Plague

    Not that I know much about it, but there has been a method added that returns nearby players some time ago.
     
  5. Offline

    Hunt800

    Plague: You're thinking about getNearbyEntities(double x, double y, double z). It doesn't list Player objects, it just list all types of Entities for some reason my method for sorting through Entities wasn't working.
     
  6. Offline

    Plague

    Well then get entities and use
    if (entity instanceof Player)
     
  7. Offline

    Hunt800

    Did you look at the OP? I had that... But, it doesn't matter, my fix works nicely too and deals with a radius instead of nearbyentities' cube.
     
  8. Offline

    Plague

    I did but it has no real formatting so I missed it :)

    Anyway if instanceof is not working correctly I'D file a bug for it (unless it's because of something else in java), since this problem was here for damage events and then they fixed it.
     
  9. Offline

    Hunt800

    Yeah... I'm certain the rest of my code is crap too. :/ Heh...

    Though, I doubt I should file a bug, since if I actually examined the issue more I probably would've figured out that I just missed something stupid. So yeah, all good then :)
     
Thread Status:
Not open for further replies.

Share This Page