Search in sources :

Example 1 with IPartyPreExperienceDropEvent

use of com.alessiodp.parties.api.events.common.party.IPartyPreExperienceDropEvent in project Parties by AlessioDP.

the class PartyImpl method giveExperience.

/**
 * Give party experience
 *
 * @param exp the experience number to give
 * @param killer the killer of the entity
 * @param killedEntity the killed entity object
 * @param gainMessage shows the gain message
 */
public void giveExperience(double exp, @Nullable PartyPlayer killer, @Nullable Object killedEntity, boolean gainMessage) {
    if (exp != 0) {
        plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_EXP_RECEIVED, getId(), exp), true);
        IPartyPreExperienceDropEvent eventPre = plugin.getEventManager().preparePreExperienceDropEvent(this, killer, killedEntity, experience);
        plugin.getEventManager().callEvent(eventPre);
        double newExperience = eventPre.getExperience();
        if (!eventPre.isCancelled() && newExperience != 0) {
            if (plugin.isBungeeCordEnabled()) {
                // Bukkit with BC enabled: just send the event to BC
                sendPacketExperience(newExperience, killer, gainMessage);
            } else {
                if (gainMessage) {
                    User killerUser = killer != null ? plugin.getPlayer(killer.getPlayerUUID()) : null;
                    if (killerUser != null)
                        killerUser.sendMessage(plugin.getMessageUtils().convertPlaceholders(Messages.ADDCMD_EXP_PARTY_GAINED_EXPERIENCE.replace("%exp%", CommonUtils.formatDouble(newExperience)), (PartyPlayerImpl) killer, this), true);
                }
                updateValue(() -> {
                    if (expResult == null)
                        calculateLevels();
                    int currentLevel = expResult.getLevel();
                    experience = experience + newExperience;
                    // Update party level directly after got experience
                    calculateLevels();
                    plugin.getScheduler().runAsync(() -> {
                        // Experience event
                        if (plugin.getPlatform() == PluginPlatform.BUNGEECORD) {
                            // Send the experience event to bukkit too
                            sendPacketExperience(newExperience, killer, gainMessage);
                        }
                        IPartyGetExperienceEvent partiesGetExperienceEvent = plugin.getEventManager().preparePartyGetExperienceEvent(this, newExperience, killer);
                        plugin.getEventManager().callEvent(partiesGetExperienceEvent);
                        // Level up event
                        if (expResult.getLevel() > currentLevel) {
                            // Send level up message
                            broadcastMessage(Messages.ADDCMD_EXP_PARTY_LEVEL_UP, null);
                            if (plugin.getPlatform() == PluginPlatform.BUNGEECORD) {
                                // Send the event to bukkit too
                                sendPacketLevelUp(expResult.getLevel());
                            }
                            IPartyLevelUpEvent partiesLevelUpEvent = plugin.getEventManager().prepareLevelUpEvent(this, expResult.getLevel());
                            plugin.getEventManager().callEvent(partiesLevelUpEvent);
                        }
                    });
                });
            }
        } else {
            plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_EXP_CANCELLED, getId(), exp), true);
        }
    }
}
Also used : User(com.alessiodp.core.common.user.User) IPartyLevelUpEvent(com.alessiodp.parties.api.events.common.party.IPartyLevelUpEvent) IPartyGetExperienceEvent(com.alessiodp.parties.api.events.common.party.IPartyGetExperienceEvent) IPartyPreExperienceDropEvent(com.alessiodp.parties.api.events.common.party.IPartyPreExperienceDropEvent)

Aggregations

User (com.alessiodp.core.common.user.User)1 IPartyGetExperienceEvent (com.alessiodp.parties.api.events.common.party.IPartyGetExperienceEvent)1 IPartyLevelUpEvent (com.alessiodp.parties.api.events.common.party.IPartyLevelUpEvent)1 IPartyPreExperienceDropEvent (com.alessiodp.parties.api.events.common.party.IPartyPreExperienceDropEvent)1