use of org.bukkit.event.entity.FoodLevelChangeEvent in project Glowstone by GlowstoneMC.
the class GlowPlayer method pulse.
@Override
public void pulse() {
super.pulse();
if (usageItem != null) {
if (usageItem == getItemInHand()) {
if (--usageTime == 0) {
ItemType item = ItemTable.instance().getItem(usageItem.getType());
if (item instanceof ItemFood) {
((ItemFood) item).eat(this, usageItem);
}
}
} else {
usageItem = null;
usageTime = 0;
}
}
if (digging != null) {
pulseDigging();
}
if (exhaustion > 4.0f) {
exhaustion -= 4.0f;
if (saturation > 0f) {
saturation = Math.max(saturation - 1f, 0f);
sendHealth();
} else if (world.getDifficulty() != Difficulty.PEACEFUL) {
FoodLevelChangeEvent event = EventFactory.callEvent(new FoodLevelChangeEvent(this, Math.max(food - 1, 0)));
if (!event.isCancelled()) {
food = event.getFoodLevel();
}
sendHealth();
}
}
if (getHealth() < getMaxHealth() && !isDead()) {
if (food > 18 && ticksLived % 80 == 0 || world.getDifficulty() == Difficulty.PEACEFUL) {
EntityRegainHealthEvent event1 = new EntityRegainHealthEvent(this, 1f, RegainReason.SATIATED);
EventFactory.callEvent(event1);
if (!event1.isCancelled()) {
setHealth(getHealth() + 1);
}
exhaustion = Math.min(exhaustion + 3.0f, 40.0f);
saturation -= 3;
}
}
if (food == 0 && getHealth() > 1 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
// stream world
streamBlocks();
processBlockChanges();
// add to playtime
incrementStatistic(Statistic.PLAY_ONE_TICK);
if (isSneaking()) {
incrementStatistic(Statistic.SNEAK_TIME);
}
// update inventory
for (InventoryMonitor.Entry entry : invMonitor.getChanges()) {
sendItemChange(entry.slot, entry.item);
}
// send changed metadata
List<MetadataMap.Entry> changes = metadata.getChanges();
if (!changes.isEmpty()) {
session.send(new EntityMetadataMessage(SELF_ID, changes));
}
// update or remove entities
List<Integer> destroyIds = new LinkedList<>();
for (Iterator<GlowEntity> it = knownEntities.iterator(); it.hasNext(); ) {
GlowEntity entity = it.next();
if (!isWithinDistance(entity) || entity.isRemoved()) {
destroyIds.add(entity.getEntityId());
it.remove();
} else {
entity.createUpdateMessage().forEach(session::send);
}
}
if (!destroyIds.isEmpty()) {
session.send(new DestroyEntitiesMessage(destroyIds));
}
// add entities
knownChunks.parallelStream().forEach(key -> {
GlowChunk chunk = world.getChunkAt(key.getX(), key.getZ());
chunk.getRawEntities().stream().filter(entity -> this != entity).filter(this::isWithinDistance).filter(entity -> !entity.isDead()).filter(entity -> !knownEntities.contains(entity)).filter(entity -> !hiddenEntities.contains(entity.getUniqueId())).forEach((entity) -> {
knownEntities.add(entity);
entity.createSpawnMessage().forEach(session::send);
});
});
if (passengerChanged) {
session.send(new SetPassengerMessage(SELF_ID, getPassengers().stream().mapToInt(Entity::getEntityId).toArray()));
}
getAttributeManager().sendMessages(session);
}
use of org.bukkit.event.entity.FoodLevelChangeEvent in project Glowstone by GlowstoneMC.
the class ItemFood method eat.
public boolean eat(GlowPlayer player, ItemStack item) {
PlayerItemConsumeEvent event1 = new PlayerItemConsumeEvent(player, item);
EventFactory.callEvent(event1);
if (event1.isCancelled())
return false;
FoodLevelChangeEvent event2 = new FoodLevelChangeEvent(player, getFoodLevel(item) + player.getFoodLevel());
EventFactory.callEvent(event2);
if (!event2.isCancelled()) {
player.setFoodLevelAndSaturation(event2.getFoodLevel(), getSaturation(item));
}
player.setUsageItem(null);
player.setUsageTime(0);
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
} else {
player.getInventory().clear(player.getInventory().getHeldItemSlot());
}
return true;
}
use of org.bukkit.event.entity.FoodLevelChangeEvent in project Essentials by drtshock.
the class Commandfeed method feedPlayer.
private void feedPlayer(final Player player) throws QuietAbortException {
final int amount = 30;
final FoodLevelChangeEvent flce = new FoodLevelChangeEvent(player, amount);
ess.getServer().getPluginManager().callEvent(flce);
if (flce.isCancelled()) {
throw new QuietAbortException();
}
player.setFoodLevel(flce.getFoodLevel() > 20 ? 20 : flce.getFoodLevel());
player.setSaturation(10);
player.setExhaustion(0F);
}
Aggregations