Java Code Examples for org.bukkit.event.player.PlayerItemConsumeEvent#getPlayer()

The following examples show how to use org.bukkit.event.player.PlayerItemConsumeEvent#getPlayer() . 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: LugolsIodinePotion.java    From CraftserveRadiation with Apache License 2.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
    ItemStack item = event.getItem();
    if (!this.test(item)) {
        return;
    }

    PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();

    int durationSeconds = 0;
    if (container.has(this.durationSecondsKey, PersistentDataType.INTEGER)) {
        durationSeconds = container.getOrDefault(this.durationSecondsKey, PersistentDataType.INTEGER, 0);
    } else if (container.has(this.durationKey, PersistentDataType.INTEGER)) {
        durationSeconds = (int) TimeUnit.MINUTES.toSeconds(container.getOrDefault(this.durationKey, PersistentDataType.INTEGER, 0)); // legacy
    }

    if (durationSeconds <= 0) {
        return;
    }

    Player player = event.getPlayer();
    this.effect.setEffect(player, durationSeconds);
    this.broadcastConsumption(player, durationSeconds);
}
 
Example 2
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerConsumeItem(PlayerItemConsumeEvent event) {
    final Player player = event.getPlayer();
    final ItemStack itemInUse = event.getItem();
    GDCauseStackManager.getInstance().pushCause(player);
    if (!GDFlags.ITEM_USE || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUID())) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_USE.toString(), itemInUse, player.getWorld().getUID())) {
        return;
    }

    GDTimings.PLAYER_USE_ITEM_EVENT.startTiming();
    Location location = player.getLocation();
    GDPlayerData playerData = this.dataStore.getOrCreatePlayerData(location.getWorld(), player.getUniqueId());
    GDClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);

    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.ITEM_USE, player, itemInUse, player, TrustTypes.ACCESSOR, true);
    if (result == Tristate.FALSE) {
        final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_ITEM_USE,
                ImmutableMap.of("item", ItemTypeRegistryModule.getInstance().getNMSKey(itemInUse)));
        GriefDefenderPlugin.sendClaimDenyMessage(claim, player,  message);
        event.setCancelled(true);
    }
    GDTimings.PLAYER_USE_ITEM_EVENT.stopTiming();
}
 
Example 3
Source File: PlayerListener.java    From WorldGuardExtraFlagsPlugin with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent event)
{
	Player player = event.getPlayer();
	
	ItemMeta itemMeta = event.getItem().getItemMeta();
	if (itemMeta instanceof PotionMeta)
	{
		this.plugin.getWorldGuardCommunicator().getSessionManager().get(player).getHandler(GiveEffectsFlagHandler.class).drinkPotion(player, Potion.fromItemStack(event.getItem()).getEffects());
	}
	else
	{
		Material material = event.getItem().getType();
		if (material == Material.MILK_BUCKET)
		{
			this.plugin.getWorldGuardCommunicator().getSessionManager().get(player).getHandler(GiveEffectsFlagHandler.class).drinkMilk(player);
		}
	}
}
 
Example 4
Source File: Compat19.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onConsume(PlayerItemConsumeEvent e) {
    if (e.getItem() == null) {
        return;
    }

    Player p = e.getPlayer();
    //deny potion
    if (p == null) {
        return;
    }

    Region r = RedProtect.get().rm.getTopRegion(p.getLocation());
    if (r != null && e.getItem().getType().equals(Material.CHORUS_FRUIT) && !r.canTeleport(p)) {
        RedProtect.get().lang.sendMessage(p, "playerlistener.region.cantuse");
        e.setCancelled(true);
    }
}
 
Example 5
Source File: ActionListener.java    From ActionHealth with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onConsume(PlayerItemConsumeEvent event) {
    if (!main.configStore.actionStore.enabled)
        return;

    ActionStore.ActionType actionType = ActionStore.ActionType.CONSUME;
    Player player = event.getPlayer();

    actionHelper.executeTriggers(actionType, player, event.getItem());
}
 
Example 6
Source File: SlimefunItemConsumeListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onConsume(PlayerItemConsumeEvent e) {
    Player p = e.getPlayer();
    ItemStack item = e.getItem();
    SlimefunItem sfItem = SlimefunItem.getByItem(item);

    if (sfItem != null) {
        if (Slimefun.hasUnlocked(p, sfItem, true)) {
            if (sfItem instanceof Juice) {
                // Fix for Saturation on potions is no longer working

                for (PotionEffect effect : ((PotionMeta) item.getItemMeta()).getCustomEffects()) {
                    if (effect.getType().equals(PotionEffectType.SATURATION)) {
                        p.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, effect.getDuration(), effect.getAmplifier()));
                        break;
                    }
                }

                removeGlassBottle(p, item);
            }
            else {
                sfItem.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(e, p, item));
            }
        }
        else {
            e.setCancelled(true);
        }
    }
}
 
Example 7
Source File: FoodEatenListener.java    From Statz with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEat(final PlayerItemConsumeEvent event) {

    final PlayerStat stat = PlayerStat.FOOD_EATEN;

    // Get player
    final Player player = event.getPlayer();

    // Do general check
    if (!plugin.doGeneralCheck(player, stat))
        return;

    PlayerStatSpecification specification = new FoodEatenSpecification(player.getUniqueId(), 1,
            player.getWorld().getName(), event.getItem().getType());

    // Update value to new stat.
    plugin.getDataManager().setPlayerInfo(player.getUniqueId(), stat, specification.constructQuery());

}