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

The following examples show how to use org.bukkit.entity.Player#getFoodLevel() . 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: StaminaEffect.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply() {
    Object target = getTarget();
    if (!(target instanceof Player)) {
        return;
    }
    Player player = (Player) target;
    int newFoodValue = player.getFoodLevel();
    if (this.setFood) {
        newFoodValue = this.stamina;
    } else {
        newFoodValue += this.stamina;
    }
    newFoodValue = Math.min(20, newFoodValue);
    newFoodValue = Math.max(0, newFoodValue);
    player.setFoodLevel(newFoodValue);
}
 
Example 2
Source File: Vampire.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Player damager = (Player) event.getDamager();
	if (!getHasCooldown(damager)) {	
		double heal = (((Damageable) damager).getHealth() + (event.getDamage() / damageHealFraction));
		if ( heal < ((Damageable) damager).getMaxHealth()) 
			damager.setHealth(heal);
		 else 
			damager.setHealth(((Damageable) damager).getMaxHealth());
		int food = (int) (damager.getFoodLevel() + (event.getDamage() / damageHealFraction));
		if ( food < 20) 
			damager.setFoodLevel(food);
		 else 
			damager.setFoodLevel(20);
		EffectManager.playSound(damager.getLocation(), "ENTITY_PLAYER_BURP", 0.4f, 1f);
		generateCooldown(damager, cooldown);
	}
}
 
Example 3
Source File: WindStaff.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemUseHandler getItemHandler() {
    return e -> {
        Player p = e.getPlayer();

        if (p.getFoodLevel() >= 2) {
            if (p.getInventory().getItemInMainHand().getType() != Material.SHEARS && p.getGameMode() != GameMode.CREATIVE) {
                FoodLevelChangeEvent event = new FoodLevelChangeEvent(p, p.getFoodLevel() - 2);
                Bukkit.getPluginManager().callEvent(event);
                p.setFoodLevel(event.getFoodLevel());
            }

            p.setVelocity(p.getEyeLocation().getDirection().multiply(4));
            p.getWorld().playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1);
            p.getWorld().playEffect(p.getLocation(), Effect.SMOKE, 1);
            p.setFallDistance(0F);
        }
        else {
            SlimefunPlugin.getLocalization().sendMessage(p, "messages.hungry", true);
        }
    };
}
 
Example 4
Source File: StormStaff.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemUseHandler getItemHandler() {
    return e -> {
        Player p = e.getPlayer();
        ItemStack item = e.getItem();

        if (p.getFoodLevel() >= 4 || p.getGameMode() == GameMode.CREATIVE) {
            // Get a target block with max. 30 blocks of distance
            Location loc = p.getTargetBlock(null, 30).getLocation();

            if (loc.getWorld() != null && loc.getChunk().isLoaded()) {
                if (loc.getWorld().getPVP() && SlimefunPlugin.getProtectionManager().hasPermission(p, loc, ProtectableAction.PVP)) {
                    e.cancel();
                    useItem(p, item, loc);
                }
                else {
                    SlimefunPlugin.getLocalization().sendMessage(p, "messages.no-pvp", true);
                }
            }
        }
        else {
            SlimefunPlugin.getLocalization().sendMessage(p, "messages.hungry", true);
        }
    };
}
 
Example 5
Source File: CoolerListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onHungerLoss(FoodLevelChangeEvent e) {
    if (cooler == null || cooler.isDisabled()) {
        return;
    }

    Player p = (Player) e.getEntity();

    if (e.getFoodLevel() < p.getFoodLevel()) {
        for (ItemStack item : p.getInventory().getContents()) {
            if (cooler.isItem(item)) {
                if (Slimefun.hasUnlocked(p, cooler, true)) {
                    takeJuiceFromCooler(p, item);
                }
                else {
                    return;
                }
            }
        }
    }
}
 
Example 6
Source File: PlayerData.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
public PlayerData(final Player p) {
  	if (SkyWarsReloaded.getCfg().debugEnabled()) {
      	Util.get().logToFile(ChatColor.RED + "[skywars] " + ChatColor.YELLOW + "Creating " + p.getName() + "'s Datafile");
  	}
  	this.beingRestored = false;
      this.uuid = p.getUniqueId();
      this.sb = p.getScoreboard();
      this.health = p.getHealth();
      this.food = p.getFoodLevel();
      this.sat = p.getSaturation();
if (!SkyWarsReloaded.getCfg().displayPlayerExeperience()) {
	xp = p.getExp();
}
      inv = Bukkit.createInventory(null, InventoryType.PLAYER, p.getName());
      inv.setContents(p.getInventory().getContents());
  	if (SkyWarsReloaded.getCfg().debugEnabled()) {
  		Util.get().logToFile(ChatColor.RED + "[skywars] " + ChatColor.YELLOW + p.getName() + "'s Datafile has been created");
  	}
  }
 
Example 7
Source File: PlayerUtils.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
public static boolean canEat(Player p, Material food) {
	GameMode gm = p.getGameMode();
	if (gm == GameMode.CREATIVE || gm == GameMode.SPECTATOR)
		return false; // Can't eat anything in those gamemodes
	
	boolean edible = food.isEdible();
	if (!edible)
		return false;
	boolean special;
	switch (food) {
		case GOLDEN_APPLE:
		case CHORUS_FRUIT:
			special = true;
			break;
			//$CASES-OMITTED$
		default:
			special = false;
	}
	if (p.getFoodLevel() < 20 || special)
		return true;
	
	return false;
}
 
Example 8
Source File: ExamineGui.java    From StaffPlus with GNU General Public License v3.0 6 votes vote down vote up
private ItemStack foodItem(Player player)
{
	int healthLevel = (int) player.getHealth();
	int foodLevel = player.getFoodLevel();
	List<String> lore = new ArrayList<String>();
	
	for(String string : messages.examineFood)
	{
		lore.add(string.replace("%health%", healthLevel + "/20").replace("%hunger%", foodLevel + "/20"));
	}
	
	ItemStack item = Items.builder()
			.setMaterial(Material.BREAD).setAmount(1)
			.setName("&bFood")
			.setLore(lore)
			.build();
	
	return item;
}
 
Example 9
Source File: StaminaEffect.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean meetsRequirement() {
    Object target = getTarget();
    Entity origin = getOrigin();
    if (!(target instanceof Player)) {
        if (!this.silent && origin instanceof Player) {
            ((Player) origin).sendMessage(ChatColor.RED + Civs.getPrefix() + " target doesn't have stamina.");
        }
        return false;
    }

    Player player = (Player) target;
    int newFoodValue = player.getFoodLevel();
    newFoodValue += stamina;

    if (newFoodValue < 0) {
        if (!this.silent && origin instanceof Player) {
            ((Player) origin).sendMessage(ChatColor.RED + Civs.getPrefix() + " target doesn't have " + stamina + "stamina.");
        }
        return false;
    }
    return true;
}
 
Example 10
Source File: PWIPlayer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
PWIPlayer(Player player, Group group, double bankBalance, double balance, boolean useAttributes) {
    this.uuid = player.getUniqueId();
    this.name = player.getName();
    this.location = player.getLocation();

    this.group = group;
    this.saved = false;

    this.armor = player.getInventory().getArmorContents();
    this.enderChest = player.getEnderChest().getContents();
    this.inventory = player.getInventory().getContents();

    this.canFly = player.getAllowFlight();
    this.displayName = player.getDisplayName();
    this.exhaustion = player.getExhaustion();
    this.experience = player.getExp();
    this.isFlying = player.isFlying();
    this.foodLevel = player.getFoodLevel();
    if (useAttributes) {
        this.maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue();
    } else {
        this.maxHealth = player.getMaxHealth();
    }
    this.health = player.getHealth();
    this.gamemode = player.getGameMode();
    this.level = player.getLevel();
    this.saturationLevel = player.getSaturation();
    this.potionEffects = player.getActivePotionEffects();
    this.fallDistance = player.getFallDistance();
    this.fireTicks = player.getFireTicks();
    this.maxAir = player.getMaximumAir();
    this.remainingAir = player.getRemainingAir();

    this.bankBalance = bankBalance;
    this.balance = balance;
}
 
Example 11
Source File: GeneralEventHandler.java    From SuperVanish with Mozilla Public License 2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onFoodLevelChange(FoodLevelChangeEvent e) {
    try {
        if (e.getEntity() instanceof Player && config.getBoolean("InvisibilityFeatures.DisableHunger")) {
            Player p = (Player) e.getEntity();
            if (plugin.getVanishStateMgr().isVanished(p.getUniqueId())
                    && e.getFoodLevel() <= p.getFoodLevel())
                e.setCancelled(true);
        }
    } catch (Exception er) {
        plugin.logException(er);
    }
}
 
Example 12
Source File: FullHungerAction.java    From UHC with MIT License 5 votes vote down vote up
@Override
protected void run(Player player) {
    // store old values
    level = player.getFoodLevel();
    saturation = player.getSaturation();
    exhaustion = player.getExhaustion();

    // max out values
    player.setFoodLevel(FULL_FOOD_LEVEL);
    player.setSaturation(FULL_SATURATION_LEVEL);
    player.setExhaustion(FULL_EXHAUSTION_LEVEL);
}
 
Example 13
Source File: PlayerManager.java    From CS-CoreLib with GNU General Public License v3.0 5 votes vote down vote up
public static void loseHunger(Player p, int amount) {
	if (p.getGameMode() != GameMode.CREATIVE) {
		int starve = p.getFoodLevel() - amount;
		if (starve < 0) starve = 0;
		p.setFoodLevel(starve);
	}
}
 
Example 14
Source File: PlayerEvents.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerFoodChange(final FoodLevelChangeEvent event) {
    if (event.getEntity() instanceof Player && plugin.getWorldManager().isSkyWorld(event.getEntity().getWorld())) {
        Player hungerman = (Player) event.getEntity();
        float randomNum = RANDOM.nextFloat();
        if (plugin.getWorldManager().isSkyWorld(hungerman.getWorld())
                && hungerman.getFoodLevel() > event.getFoodLevel()
                && plugin.playerIsOnIsland(hungerman)) {
            Perk perk = plugin.getPerkLogic().getPerk(hungerman);
            if (randomNum <= perk.getHungerReduction()) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 15
Source File: StormStaff.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void useItem(Player p, ItemStack item, Location loc) {
    loc.getWorld().strikeLightning(loc);

    if (p.getInventory().getItemInMainHand().getType() == Material.SHEARS) {
        return;
    }

    if (p.getGameMode() != GameMode.CREATIVE) {
        FoodLevelChangeEvent event = new FoodLevelChangeEvent(p, p.getFoodLevel() - 4);
        Bukkit.getPluginManager().callEvent(event);

        if (!event.isCancelled()) {
            p.setFoodLevel(event.getFoodLevel());
        }
    }

    ItemMeta meta = item.getItemMeta();
    int usesLeft = meta.getPersistentDataContainer().getOrDefault(usageKey, PersistentDataType.INTEGER, MAX_USES);

    if (usesLeft == 1) {
        p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
        item.setAmount(0);
    }
    else {
        usesLeft--;
        meta.getPersistentDataContainer().set(usageKey, PersistentDataType.INTEGER, usesLeft);

        List<String> lore = meta.getLore();
        lore.set(4, ChatColors.color("&e" + usesLeft + ' ' + (usesLeft > 1 ? "Uses" : "Use") + " &7left"));
        meta.setLore(lore);

        item.setItemMeta(meta);
    }
}
 
Example 16
Source File: PWIPlayer.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
PWIPlayer(Player player, Group group, double bankBalance, double balance, boolean useAttributes) {
    this.uuid = player.getUniqueId();
    this.name = player.getName();
    this.location = player.getLocation();

    this.group = group;
    this.saved = false;

    this.armor = player.getInventory().getArmorContents();
    this.enderChest = player.getEnderChest().getContents();
    this.inventory = player.getInventory().getContents();

    this.canFly = player.getAllowFlight();
    this.displayName = player.getDisplayName();
    this.exhaustion = player.getExhaustion();
    this.experience = player.getExp();
    this.isFlying = player.isFlying();
    this.foodLevel = player.getFoodLevel();
    if (useAttributes) {
        this.maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue();
    } else {
        this.maxHealth = player.getMaxHealth();
    }
    this.health = player.getHealth();
    this.gamemode = player.getGameMode();
    this.level = player.getLevel();
    this.saturationLevel = player.getSaturation();
    this.potionEffects = player.getActivePotionEffects();
    this.fallDistance = player.getFallDistance();
    this.fireTicks = player.getFireTicks();
    this.maxAir = player.getMaximumAir();
    this.remainingAir = player.getRemainingAir();

    this.bankBalance = bankBalance;
    this.balance = balance;
}
 
Example 17
Source File: TutorialPlayer.java    From ServerTutorial with MIT License 5 votes vote down vote up
public TutorialPlayer(Player player) {
    this.startLoc = player.getLocation();
    this.inventory = player.getInventory().getContents();
    this.flight = player.isFlying();
    this.allowFlight = player.getAllowFlight();
    this.exp = player.getExp();
    this.level = player.getLevel();
    this.hunger = player.getFoodLevel();
    this.health = player.getHealth();
    this.gameMode = player.getGameMode();
}
 
Example 18
Source File: DPlayerData.java    From DungeonsXL with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Saves the player's data to the file.
 *
 * @param player the Player to save
 */
public void savePlayerState(Player player) {
    oldGameMode = player.getGameMode();
    oldFireTicks = player.getFireTicks();
    oldFoodLevel = player.getFoodLevel();
    if (is1_9) {
        oldMaxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue();
    }
    oldHealth = player.getHealth();
    oldExp = player.getExp();
    oldLvl = player.getLevel();
    oldArmor = new ArrayList<>(Arrays.asList(player.getInventory().getArmorContents()));
    oldInventory = new ArrayList<>(Arrays.asList(player.getInventory().getContents()));
    if (is1_9) {
        oldOffHand = player.getInventory().getItemInOffHand();
    }
    oldLocation = player.getLocation();
    oldPotionEffects = player.getActivePotionEffects();
    if (is1_9) {
        oldCollidabilityState = player.isCollidable();
        oldInvulnerabilityState = player.isInvulnerable();
    }
    oldFlyingState = player.getAllowFlight();

    config.set(PREFIX_STATE_PERSISTENCE + "oldGameMode", oldGameMode.toString());
    config.set(PREFIX_STATE_PERSISTENCE + "oldFireTicks", oldFireTicks);
    config.set(PREFIX_STATE_PERSISTENCE + "oldFoodLevel", oldFoodLevel);
    config.set(PREFIX_STATE_PERSISTENCE + "oldMaxHealth", oldMaxHealth);
    config.set(PREFIX_STATE_PERSISTENCE + "oldHealth", oldHealth);
    config.set(PREFIX_STATE_PERSISTENCE + "oldExp", oldExp);
    config.set(PREFIX_STATE_PERSISTENCE + "oldLvl", oldLvl);
    config.set(PREFIX_STATE_PERSISTENCE + "oldArmor", oldArmor);
    config.set(PREFIX_STATE_PERSISTENCE + "oldInventory", oldInventory);
    config.set(PREFIX_STATE_PERSISTENCE + "oldOffHand", oldOffHand);
    config.set(PREFIX_STATE_PERSISTENCE + "oldLocation", oldLocation);
    config.set(PREFIX_STATE_PERSISTENCE + "oldPotionEffects", oldPotionEffects);
    config.set(PREFIX_STATE_PERSISTENCE + "oldCollidabilityState", oldCollidabilityState);
    config.set(PREFIX_STATE_PERSISTENCE + "oldFlyingState", oldFlyingState);
    config.set(PREFIX_STATE_PERSISTENCE + "oldInvulnerabilityState", oldInvulnerabilityState);

    save();
}
 
Example 19
Source File: ExoticGardenFruit.java    From ExoticGarden with GNU General Public License v3.0 4 votes vote down vote up
private void restoreHunger(Player p) {
    int level = p.getFoodLevel() + getFoodValue();
    p.playSound(p.getEyeLocation(), Sound.ENTITY_GENERIC_EAT, 1, 1);
    p.setFoodLevel(Math.min(level, 20));
    p.setSaturation(p.getSaturation() + getFoodValue());
}
 
Example 20
Source File: HealthKit.java    From CardinalPGM with MIT License 4 votes vote down vote up
@Override
public void apply(Player player, Boolean force) {
    if(health != -1 && (force || health > player.getHealth())) player.setHealth(health);
    if(hunger != -1 && (force || hunger > player.getFoodLevel())) player.setFoodLevel(hunger);
    if(saturation != 0 && (force || saturation > player.getSaturation())) player.setSaturation(saturation);
}