Java Code Examples for org.bukkit.entity.Player#isValid()

The following examples show how to use org.bukkit.entity.Player#isValid() . 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: ParticleData.java    From NyaaUtils with MIT License 6 votes vote down vote up
public void sendParticle(UUID uuid, Location loc, ParticleLimit limit, long time) {
    if (!lastSend.containsKey(uuid)) {
        lastSend.put(uuid, 0L);
    }
    if (time - lastSend.get(uuid) >= (freq < limit.getFreq() ? limit.getFreq() : freq) &&
            NyaaUtils.instance.cfg.particles_enabled.contains(particle.name())) {
        lastSend.put(uuid, time);
        double distance = Bukkit.getViewDistance() * 16;
        distance *= distance;
        for (Player player : loc.getWorld().getPlayers()) {
            if (player.isValid() && !NyaaUtils.instance.particleTask.bypassPlayers.contains(player.getUniqueId())
                    && loc.distanceSquared(player.getLocation()) <= distance) {
                player.spawnParticle(particle, loc,
                        count > limit.getAmount() ? limit.getAmount() : count,
                        offsetX > limit.getOffsetX() ? limit.getOffsetX() : offsetX,
                        offsetY > limit.getOffsetY() ? limit.getOffsetY() : offsetY,
                        offsetZ > limit.getOffsetZ() ? limit.getOffsetZ() : offsetZ,
                        extra > limit.getExtra() ? limit.getExtra() : extra,
                        getData());
            }
        }
    }
}
 
Example 2
Source File: ArmorTask.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    for (Player p : Bukkit.getOnlinePlayers()) {
        if (!p.isValid() || p.isDead()) {
            continue;
        }

        PlayerProfile.get(p, profile -> {
            ItemStack[] armor = p.getInventory().getArmorContents();
            HashedArmorpiece[] cachedArmor = profile.getArmor();

            handleSlimefunArmor(p, armor, cachedArmor);

            if (hasSunlight(p)) {
                checkForSolarHelmet(p);
            }

            checkForRadiation(p);
        });
    }
}
 
Example 3
Source File: CommandUtil.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public static void performCommand(OfflinePlayer offlinePlayer, String command) {
    String finalCommand = command;
    boolean runAsOp = false;
    boolean runFromConsole = false;
    for (;;) {
        if (finalCommand.startsWith("^")) {
            runAsOp = true;
            finalCommand = finalCommand.substring(1);
        } else if (finalCommand.startsWith("!")) {
            runFromConsole = true;
            finalCommand = finalCommand.substring(1);
        } else {
            break;
        }
    }
    if (offlinePlayer.isOnline()) {
        finalCommand = finalCommand.replace("$name$", ((Player) offlinePlayer).getName());
    } else {
        Player player1 = offlinePlayer.getPlayer();
        if (player1 != null && player1.isValid()) {
            finalCommand = finalCommand.replace("$name$", player1.getName());
        }
    }
    if (runFromConsole) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand);
    } else if (offlinePlayer.isOnline()) {
        Player player = (Player) offlinePlayer;
        boolean setOp = runAsOp && !player.isOp();
        if (setOp) {
            player.setOp(true);
        }
        player.performCommand(finalCommand);
        if (setOp) {
            player.setOp(false);
        }
    }
}
 
Example 4
Source File: Variable.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"deprecation"})
@Nullable Object convertIfOldPlayer(String key, Event event, @Nullable Object t){
	if(SkriptConfig.enablePlayerVariableFix.value() && t != null && t instanceof Player){
		Player p = (Player) t;
		if(!p.isValid() && p.isOnline()){
			Player player = uuidSupported ? Bukkit.getPlayer(p.getUniqueId()) : Bukkit.getPlayerExact(p.getName());
			Variables.setVariable(key, player, event, local);
			return player;
		}
	}
	return t;
}
 
Example 5
Source File: RemoveEffectPacketListener.java    From WorldGuardExtraFlagsPlugin with MIT License 5 votes vote down vote up
@Override
public void onPacketSending(PacketEvent event)
{
	if (!event.isCancelled())
	{
		Player player = event.getPlayer();
		if (!player.isValid()) //Work around, getIfPresent is broken inside WG due to using LocalPlayer as key instead of CacheKey
		{
			return;
		}

		try
		{
			Session session = WorldGuardExtraFlagsPlugin.getPlugin().getWorldGuardCommunicator().getSessionManager().get(player);
			
			GiveEffectsFlagHandler giveEffectsHandler = session.getHandler(GiveEffectsFlagHandler.class);
			if (giveEffectsHandler.isSupressRemovePotionPacket())
			{
				event.setCancelled(true);
			}
		}
		catch(IllegalStateException wgBug)
		{
			
		}
	}
}
 
Example 6
Source File: EntityPotionEffectEventListener.java    From WorldGuardExtraFlagsPlugin with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onEntityPotionEffectEvent(EntityPotionEffectEvent event)
{
	if (event.getAction() != EntityPotionEffectEvent.Action.REMOVED)
	{
		return;
	}
	
	if (event.getCause() != EntityPotionEffectEvent.Cause.PLUGIN)
	{
		return;
	}
	
	Entity entity = event.getEntity();
	if (!(entity instanceof Player))
	{
		return;
	}

	Player player = (Player)entity;
	if (!player.isValid()) //Work around, getIfPresent is broken inside WG due to using LocalPlayer as key instead of CacheKey
	{
		return;
	}

	try
	{
		Session session = WorldGuardExtraFlagsPlugin.getPlugin().getWorldGuardCommunicator().getSessionManager().get(player);
		
		GiveEffectsFlagHandler giveEffectsHandler = session.getHandler(GiveEffectsFlagHandler.class);
		if (giveEffectsHandler.isSupressRemovePotionPacket())
		{
			event.setCancelled(true);
		}
	}
	catch(IllegalStateException wgBug)
	{
		
	}
}
 
Example 7
Source File: TheHangingGardens.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onUpdate() {
	super.onUpdate();
	
	for (Town t : this.getTown().getCiv().getTowns()) {
		for (Resident res : t.getResidents()) {
			try {
				Player player = CivGlobal.getPlayer(res);
				
				if (player.isDead() || !player.isValid()) {
					continue;
				}
				
				if (player.getHealth() >= 20) {
					continue;
				}
				
				TownChunk tc = CivGlobal.getTownChunk(player.getLocation());
				if (tc == null || tc.getTown() != this.getTown()) {
					continue;
				}
				
				if (player.getHealth() >= 19.0) {
					player.setHealth(20);
				} else {
					player.setHealth(player.getHealth() + 1);
				}
			} catch (CivException e) {
				//Player not online;
			}
			
		}
	}
}
 
Example 8
Source File: TutorialAdvanceCommand.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean runCommand(CommandSender commandSender, Command command, String label, String[] args) {
    if (Civs.perm == null || !Civs.perm.has(commandSender, Constants.ADMIN_PERMISSION)) {
        sendMessage(commandSender, "no-permission", "You don't have permission to use /cv advancetut PlayerName");
        return true;
    }
    if (args.length < 2) {
        sendMessage(commandSender, "invalid-target", "Invalid command. Use /cv advancetut PlayerName");
        return true;
    }

    //0 advancetut
    //1 playerName
    String playerName = args[1];
    Player player = Bukkit.getPlayer(playerName);
    if (player == null || !player.isValid()) {
        sendMessage(commandSender, "invalid-target", "Invalid target player. Did you spell the name right?");
        return true;
    }

    Civilian civilian = CivilianManager.getInstance().getCivilian(player.getUniqueId());
    TutorialPath path = TutorialManager.getInstance().getPathByName(civilian.getTutorialPath());
    if (path == null) {
        Civs.logger.severe("Invalid path " + civilian.getTutorialPath() + " for " + player.getName() +
                " in plugins/Civs/players/" + player.getUniqueId() + ".yml");
        return true;
    }
    int tutorialIndex = civilian.getTutorialIndex();
    if (path.getSteps().size() <= tutorialIndex) {
        Civs.logger.warning("Can't advance " + player.getName() + "'s tutorial progress past the end of current path");
        return true;
    }
    TutorialStep step = path.getSteps().get(tutorialIndex);

    String param = null;
    if (step.getType().equals("build") || step.getType().equals("upkeep") ||
            step.getType().equals("buy")) {
        param = step.getRegion();
    } else if (step.getType().equals("kill")) {
        param = step.getKillType();
    }

    if (param == null) {
        Civs.logger.warning("Unable to find tutorial type param. Make sure you are trying to advance a valid tutorial step (example: not choose a path)");
        return true;
    }
    TutorialManager.getInstance().completeStep(civilian,
            TutorialManager.TutorialType.valueOf(step.getType().toUpperCase()), param);
    return true;
}
 
Example 9
Source File: GetLootCommandHandler.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
private static boolean getItem(ItemStack itemStack, String args1, Player player) {

        String itemRawName = itemStack.getItemMeta().getDisplayName();

        if (itemRawName != null) {

            Bukkit.getLogger().info(itemStack.getItemMeta().getDisplayName());

            String itemProcessedName = itemRawName.replaceAll(" ", "_").toLowerCase();
            itemProcessedName = ChatColor.stripColor(itemProcessedName);

            if (itemProcessedName.equalsIgnoreCase(args1) && player.isValid()) {

                player.getInventory().addItem(itemStack);

                return true;

            }

        }

        return false;

    }