Java Code Examples for org.bukkit.World#getPlayers()

The following examples show how to use org.bukkit.World#getPlayers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorldPlayerCounterTask.java    From HolographicDisplays with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
	worlds.clear();
	
	for (World world : Bukkit.getWorlds()) {
		List<Player> players = world.getPlayers();
		int count = 0;
		
		for (Player player : players) {
			if (!player.hasMetadata("NPC")) {
				count++;
			}
		}
		worlds.put(world.getName(), count);
	}
}
 
Example 2
Source File: WorldGuardHandler.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
public static List<Player> getPlayersInRegion(World world, ProtectedRegion region) {
    // Note: This might be heavy - for large servers...
    List<Player> players = new ArrayList<>();
    if (region == null) {
        return players;
    }
    for (Player player : world.getPlayers()) {
        if (player != null && player.isOnline()) {
            Location p = player.getLocation();
            if (region.contains(p.getBlockX(), p.getBlockY(), p.getBlockZ())) {
                players.add(player);
            }
        }
    }
    return players;
}
 
Example 3
Source File: GriefDefenderPlugin.java    From GriefDefender with MIT License 5 votes vote down vote up
private void cleanup() {
    for (BukkitRunnable task : this.runningTasks) {
        task.cancel();
    }
    for (World world : Bukkit.getServer().getWorlds()) {
        for (Player player : world.getPlayers()) {
            if (player.isDead()) {
                continue;
            }
            final GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
            playerData.onDisconnect();
        }
    }
}
 
Example 4
Source File: PermanentSlowness.java    From GlobalWarming with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    for (World world : Bukkit.getWorlds()) {
        WorldClimateEngine climateEngine = ClimateEngine.getInstance().getClimateEngine(world.getUID());
        if (climateEngine != null && climateEngine.isEffectEnabled(ClimateEffectType.PERMANENT_SLOWNESS)) {
            for (Player player : world.getPlayers()) {
                updatePlayerSlowness(player, climateEngine.getTemperature());
            }
        }
    }
}
 
Example 5
Source File: Weather.java    From GlobalWarming with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get a random player's location (must be outdoors, specifically: no block overhead)
 */
private Location getOutdoorPlayerLocation(World world) {
    Location location = null;
    List<Player> players = world.getPlayers();
    if (players.size() > 0) {
        Player player = players.get(ThreadLocalRandom.current().nextInt(0, players.size()));
        if (world.getHighestBlockAt(player.getLocation()).getY() < player.getLocation().getY()) {
            location = player.getLocation();
        }
    }

    return location;
}
 
Example 6
Source File: PermadayCommand.java    From UHC with MIT License 5 votes vote down vote up
@Override
protected boolean runCommand(CommandSender sender, OptionSet options) {
    List<World> worlds = worldsSpec.values(options);

    if (worlds.size() == 0) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(messages.getRaw("provide world"));
            return true;
        }

        worlds = Lists.newArrayList(((Player) sender).getWorld());
    }

    final boolean on = !options.has(turnOff);

    for (final World world : worlds) {
        if (on) {
            world.setGameRuleValue(DO_DAYLIGHT_CYCLE_GAMERULE, "false");
            world.setTime(SUN_OVERHEAD_TIME);
        } else {
            world.setGameRuleValue(DO_DAYLIGHT_CYCLE_GAMERULE, "true");
        }

        final String message = messages.evalTemplate(
                on ? "on notification" : "off notification",
                ImmutableMap.of("world", world.getName())
        );

        for (final Player player : world.getPlayers()) {
            player.sendMessage(message);
        }
    }

    sender.sendMessage(messages.evalTemplate(
            on ? "on completed" : "off completed",
            ImmutableMap.of("count", worlds.size())
    ));

    return true;
}
 
Example 7
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, false, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 8
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 9
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 10
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, false, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 11
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 12
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 13
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 14
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	EnumParticle particle = EnumParticle.valueOf(type);
	PacketPlayOutWorldParticles particles = new PacketPlayOutWorldParticles(particle, true, x, y, z, offsetX, offsetY, offsetZ, data, amount, 1);
	for (Player player: world.getPlayers()) {
		CraftPlayer start = (CraftPlayer) player; //Replace player with your player.
		EntityPlayer target = start.getHandle();
		PlayerConnection connect = target.playerConnection;
		connect.sendPacket(particles);
	}
}
 
Example 15
Source File: UtilsMc.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
public static void broadcastToWorld(World world, String message) {
	for (Player player : world.getPlayers()) {
		player.sendMessage(message);
	}
}
 
Example 16
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 4 votes vote down vote up
public void sendParticles(World world, String type, float x, float y, float z, float offsetX, float offsetY, float offsetZ, float data, int amount) {
	Particle particle = Particle.valueOf(type);
    for (Player player: world.getPlayers()) {
	    player.spawnParticle(particle, x, y, z, amount, offsetX, offsetY, offsetZ, data);
	}
}
 
Example 17
Source File: CommandManager.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s,
						 @NotNull String[] args) {
	if (!(sender.hasPermission("minetinker.commands.main"))) {
		sendError(sender, LanguageManager.getString("Commands.Failure.Cause.NoPermission"));
		return true;
	}

	if (args.length <= 0) {
		sendError(sender, LanguageManager.getString("Commands.Failure.Cause.InvalidArguments"));
		sendHelp(sender, null);
		return true;
	}

	if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?")) {
		sendHelp(sender, args.length >= 2 ? map.get(args[1].toLowerCase()) : null);
		return true;
	}

	SubCommand sub = map.get(args[0].toLowerCase());
	if (sub == null) {
		sendError(sender, LanguageManager.getString("Commands.Failure.Cause.UnknownCommand"));
		sendHelp(sender, null);
		return true;
	}

	parseArguments(sender, sub, args);

	//@all / @allworld
	{
		int index = -1;
		boolean worldOnly = false;
		for (int i = 0; i < args.length; i++) {
			List<ArgumentType> types = sub.getArgumentsToParse().get(i);
			if (types != null && types.contains(ArgumentType.PLAYER)) {
				if (args[i].startsWith("@aw")) {
					index = i;
					worldOnly = true;
					break;
				} else if (args[i].startsWith("@a")) {
					index = i;
					break;
				}
			}
		}
		if (index != -1) {
			Collection<? extends Player> players;
			if (worldOnly) {
				World world = null;
				if (sender instanceof BlockCommandSender) {
					world = ((BlockCommandSender) sender).getBlock().getWorld();
				} else if (sender instanceof Entity) {
					world = ((Entity) sender).getWorld();
				}

				if (world == null) return true;
				players = world.getPlayers();
			} else {
				players = Bukkit.getOnlinePlayers();
			}

			boolean ret = true;
			for (Player player : players) {
				String[] arg = args.clone();
				arg[index] = player.getName();
				ret = ret && onCommand(sender, command, s, arg);
			}
		}
	}

	if (!sender.hasPermission(sub.getPermission())) {
		sendError(sender, LanguageManager.getString("Commands.Failure.Cause.NoPermission"));
		return true;
	}

	return sub.onCommand(sender, args);
}
 
Example 18
Source File: ClaimBlockTask.java    From GriefDefender with MIT License 4 votes vote down vote up
@Override
public void run() {
    for (World world : Bukkit.getServer().getWorlds()) {
        for (Player player : world.getPlayers()) {
            final GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
            final GDClaim claim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
            final GDPermissionUser holder = PermissionHolderCache.getInstance().getOrCreateUser(player);
            final int accrualPerHour = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Integer.class), holder, Options.BLOCKS_ACCRUED_PER_HOUR, claim).intValue();
            if (accrualPerHour > 0) {
                Location lastLocation = playerData.lastAfkCheckLocation;
                // if he's not in a vehicle and has moved at least three blocks since the last check and he's not being pushed around by fluids
                if (player.getVehicle() == null &&
                        (lastLocation == null || lastLocation.getWorld() != player.getWorld() || lastLocation.distanceSquared(player.getLocation()) >= 0) &&
                        !NMSUtil.getInstance().isBlockWater(player.getLocation().getBlock())) {
                    int accruedBlocks = playerData.getBlocksAccruedPerHour() / 12;
                    if (accruedBlocks < 0) {
                        accruedBlocks = 1;
                    }

                    if (GriefDefenderPlugin.getInstance().isEconomyModeEnabled()) {
                        final VaultProvider vaultProvider = GriefDefenderPlugin.getInstance().getVaultProvider();
                        if (!vaultProvider.hasAccount(player)) {
                            continue;
                        }
                        vaultProvider.depositPlayer(player, accruedBlocks);
                    } else {
                        int currentTotal = playerData.getAccruedClaimBlocks();
                        if ((currentTotal + accruedBlocks) > playerData.getMaxAccruedClaimBlocks()) {
                            playerData.setAccruedClaimBlocks(playerData.getMaxAccruedClaimBlocks());
                            playerData.lastAfkCheckLocation = player.getLocation();
                            return;
                        }

                        playerData.setAccruedClaimBlocks(playerData.getAccruedClaimBlocks() + accruedBlocks);
                    }
                }

                playerData.lastAfkCheckLocation = player.getLocation();
            }
        }
    }
}
 
Example 19
Source File: PlayerTickTask.java    From GriefDefender with MIT License 4 votes vote down vote up
@Override
public void run() {
    for (World world : Bukkit.getServer().getWorlds()) {
        for (Player player : world.getPlayers()) {
            if (player.isDead()) {
                continue;
            }
            final GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
            final GDClaim claim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
            // send queued visuals
            int count = 0;
            final Iterator<BlockSnapshot> iterator = playerData.queuedVisuals.iterator();
            while (iterator.hasNext()) {
                final BlockSnapshot snapshot = iterator.next();
                if (count > GriefDefenderPlugin.getGlobalConfig().getConfig().visual.clientVisualsPerTick) {
                    break;
                }
                NMSUtil.getInstance().sendBlockChange(player, snapshot);
                iterator.remove();
                count++;
            }

            // chat capture
            playerData.updateRecordChat();
            // health regen
            if (world.getFullTime() % 100 == 0L) {
                final GameMode gameMode = player.getGameMode();
                // Handle player health regen
                if (gameMode != GameMode.CREATIVE && gameMode != GameMode.SPECTATOR && GDOptions.isOptionEnabled(Options.PLAYER_HEALTH_REGEN)) {
                    final double maxHealth = player.getMaxHealth();
                    if (player.getHealth() < maxHealth) {
                        final double regenAmount = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Double.class), playerData.getSubject(), Options.PLAYER_HEALTH_REGEN, claim);
                        if (regenAmount > 0) {
                            final double newHealth = player.getHealth() + regenAmount;
                            if (newHealth > maxHealth) {
                                player.setHealth(maxHealth);
                            } else {
                                player.setHealth(newHealth);
                            }
                        }
                    }
                }
            }
            // teleport delay
            if (world.getFullTime() % 20 == 0L) {
                if (playerData.teleportDelay > 0) {
                    final int delay = playerData.teleportDelay - 1;
                    if (delay == 0) {
                        playerData.teleportDelay = 0;
                        player.teleport(playerData.teleportLocation);
                        playerData.teleportLocation = null;
                        playerData.teleportSourceLocation = null;
                        continue;
                    }
                    TextAdapter.sendComponent(player, MessageStorage.MESSAGE_DATA.getMessage(MessageStorage.TELEPORT_DELAY_NOTICE, 
                            ImmutableMap.of("delay", TextComponent.of(delay, TextColor.GOLD))));
                    playerData.teleportDelay = delay;
                }
            }
        }
    }
}