Search in sources :

Example 26 with JobsPlayer

use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.

the class Jobs method action.

/**
     * Performed an action
     * 
     * Give correct experience and income
     * @param jPlayer - the player
     * @param action - the action
     * @param multiplier - the payment/xp multiplier
     */
public static void action(JobsPlayer jPlayer, ActionInfo info, double multiplier) {
    List<JobProgression> progression = jPlayer.getJobProgression();
    int numjobs = progression.size();
    // no job
    if (numjobs == 0) {
        Job jobNone = Jobs.getNoneJob();
        if (jobNone != null) {
            Double income = jobNone.getIncome(info, 1, numjobs);
            if (income != null)
                Jobs.getEconomy().pay(jPlayer, income * multiplier);
        }
    } else {
        for (JobProgression prog : progression) {
            int level = prog.getLevel();
            Double income = prog.getJob().getIncome(info, level, numjobs);
            if (income != null) {
                Double exp = prog.getJob().getExperience(info, level, numjobs);
                if (ConfigManager.getJobsConfiguration().addXpPlayer()) {
                    Player player = Bukkit.getServer().getPlayer(jPlayer.getPlayerUUID());
                    if (player != null) {
                        /*
                             * Minecraft experience is calculated in whole numbers only.
                             * Calculate the fraction of an experience point and perform a dice roll.
                             * That way jobs that give fractions of experience points will slowly give
                             * experience in the aggregate
                             */
                        int expInt = exp.intValue();
                        double remainder = exp.doubleValue() - expInt;
                        if (Math.abs(remainder) > Math.random()) {
                            if (exp.doubleValue() < 0) {
                                expInt--;
                            } else {
                                expInt++;
                            }
                        }
                        player.giveExp(expInt);
                    }
                }
                // give income
                Jobs.getEconomy().pay(jPlayer, income * multiplier);
                int oldLevel = prog.getLevel();
                if (prog.addExperience(exp * multiplier))
                    Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) JobProgression(com.gamingmesh.jobs.container.JobProgression) Job(com.gamingmesh.jobs.container.Job)

Example 27 with JobsPlayer

use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.

the class PermissionHandler method recalculatePermissions.

public void recalculatePermissions(JobsPlayer jPlayer) {
    Player player = plugin.getServer().getPlayer(jPlayer.getPlayerUUID());
    if (player == null)
        return;
    boolean changed = false;
    // remove old permissions
    String permName = "jobs.players." + player.getName();
    Permission permission = plugin.getServer().getPluginManager().getPermission(permName);
    if (permission != null) {
        plugin.getServer().getPluginManager().removePermission(permission);
        changed = true;
    }
    // Permissions should only apply if we have permission to use jobs in this world
    if (hasWorldPermission(player, player.getWorld().getName())) {
        List<JobProgression> progression = jPlayer.getJobProgression();
        // calculate new permissions
        HashMap<String, Boolean> permissions = new HashMap<String, Boolean>();
        if (progression.size() == 0) {
            Job job = Jobs.getNoneJob();
            if (job != null) {
                for (JobPermission perm : job.getPermissions()) {
                    if (perm.getLevelRequirement() <= 0) {
                        if (perm.getValue()) {
                            permissions.put(perm.getNode(), true);
                        } else {
                            /*
                                 * If the key exists, don't put a false node in
                                 * This is in case we already have a true node there
                                 */
                            if (!permissions.containsKey(perm.getNode())) {
                                permissions.put(perm.getNode(), false);
                            }
                        }
                    }
                }
            }
        } else {
            for (JobProgression prog : progression) {
                for (JobPermission perm : prog.getJob().getPermissions()) {
                    if (prog.getLevel() >= perm.getLevelRequirement()) {
                        /*
                             * If the key exists, don't put a false node in
                             * This is in case we already have a true node there
                             */
                        if (perm.getValue()) {
                            permissions.put(perm.getNode(), true);
                        } else {
                            if (!permissions.containsKey(perm.getNode())) {
                                permissions.put(perm.getNode(), false);
                            }
                        }
                    }
                }
            }
        }
        // add new permissions (if applicable)
        if (permissions.size() > 0) {
            plugin.getServer().getPluginManager().addPermission(new Permission(permName, PermissionDefault.FALSE, permissions));
            changed = true;
        }
    }
    // If the permissions changed, recalculate them
    if (!changed)
        return;
    // find old attachment
    PermissionAttachment attachment = null;
    for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
        if (pai.getAttachment() != null && pai.getAttachment().getPlugin() instanceof JobsPlugin) {
            attachment = pai.getAttachment();
        }
    }
    // create if attachment doesn't exist
    if (attachment == null) {
        attachment = player.addAttachment(plugin);
        attachment.setPermission(permName, true);
    }
    // recalculate!
    player.recalculatePermissions();
}
Also used : Player(org.bukkit.entity.Player) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) HashMap(java.util.HashMap) JobPermission(com.gamingmesh.jobs.container.JobPermission) JobProgression(com.gamingmesh.jobs.container.JobProgression) Permission(org.bukkit.permissions.Permission) JobPermission(com.gamingmesh.jobs.container.JobPermission) PermissionAttachmentInfo(org.bukkit.permissions.PermissionAttachmentInfo) Job(com.gamingmesh.jobs.container.Job) PermissionAttachment(org.bukkit.permissions.PermissionAttachment)

Example 28 with JobsPlayer

use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.

the class PlayerManager method saveAll.

/**
     * Save all the information of all of the players in the game
     */
public void saveAll() {
    JobsDAO dao = Jobs.getJobsDAO();
    /*
         * Saving is a three step process to minimize synchronization locks when called asynchronously.
         * 
         * 1) Safely copy list for saving.
         * 2) Perform save on all players on copied list.
         * 3) Garbage collect the real list to remove any offline players with saved data
         */
    ArrayList<JobsPlayer> list = null;
    synchronized (players) {
        list = new ArrayList<JobsPlayer>(players.values());
    }
    for (JobsPlayer jPlayer : list) {
        jPlayer.save(dao);
    }
    synchronized (players) {
        Iterator<JobsPlayer> iter = players.values().iterator();
        while (iter.hasNext()) {
            JobsPlayer jPlayer = iter.next();
            synchronized (jPlayer.saveLock) {
                if (!jPlayer.isOnline() && jPlayer.isSaved()) {
                    iter.remove();
                }
            }
        }
    }
}
Also used : JobsDAO(com.gamingmesh.jobs.dao.JobsDAO) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer)

Example 29 with JobsPlayer

use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.

the class PlayerManager method performLevelUp.

/**
     * Broadcasts level up about a player
     * @param jPlayer
     * @param job
     * @param oldLevel
     */
public void performLevelUp(JobsPlayer jPlayer, Job job, int oldLevel) {
    Player player = Bukkit.getServer().getPlayer(jPlayer.getPlayerUUID());
    JobProgression prog = jPlayer.getJobProgression(job);
    if (prog == null)
        return;
    String message;
    if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
        message = Language.getMessage("message.levelup.broadcast");
    } else {
        message = Language.getMessage("message.levelup.nobroadcast");
    }
    message = message.replace("%jobname%", job.getChatColor() + job.getName() + ChatColor.WHITE);
    Title oldTitle = ConfigManager.getJobsConfiguration().getTitleForLevel(oldLevel);
    if (oldTitle != null) {
        message = message.replace("%titlename%", oldTitle.getChatColor() + oldTitle.getName() + ChatColor.WHITE);
    }
    if (player != null) {
        message = message.replace("%playername%", player.getDisplayName());
    } else {
        message = message.replace("%playername%", jPlayer.getUserName());
    }
    message = message.replace("%joblevel%", "" + prog.getLevel());
    for (String line : message.split("\n")) {
        if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
            Bukkit.getServer().broadcastMessage(line);
        } else if (player != null) {
            player.sendMessage(line);
        }
    }
    Title newTitle = ConfigManager.getJobsConfiguration().getTitleForLevel(prog.getLevel());
    if (newTitle != null && !newTitle.equals(oldTitle)) {
        // user would skill up
        if (ConfigManager.getJobsConfiguration().isBroadcastingSkillups()) {
            message = Language.getMessage("message.skillup.broadcast");
        } else {
            message = Language.getMessage("message.skillup.nobroadcast");
        }
        if (player != null) {
            message = message.replace("%playername%", player.getDisplayName());
        } else {
            message = message.replace("%playername%", jPlayer.getUserName());
        }
        message = message.replace("%titlename%", newTitle.getChatColor() + newTitle.getName() + ChatColor.WHITE);
        message = message.replace("%jobname%", job.getChatColor() + job.getName() + ChatColor.WHITE);
        for (String line : message.split("\n")) {
            if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
                Bukkit.getServer().broadcastMessage(line);
            } else if (player != null) {
                player.sendMessage(line);
            }
        }
    }
    jPlayer.reloadHonorific();
    Jobs.getPermissionHandler().recalculatePermissions(jPlayer);
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) JobProgression(com.gamingmesh.jobs.container.JobProgression) Title(com.gamingmesh.jobs.container.Title)

Example 30 with JobsPlayer

use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.

the class PlayerManager method playerQuit.

/**
     * Handles player quit
     * @param playername
     */
public void playerQuit(Player player) {
    synchronized (players) {
        if (ConfigManager.getJobsConfiguration().saveOnDisconnect()) {
            JobsPlayer jPlayer = players.remove(player.getName().toLowerCase());
            if (jPlayer != null) {
                jPlayer.save(Jobs.getJobsDAO());
                jPlayer.onDisconnect();
            }
        } else {
            JobsPlayer jPlayer = players.get(player.getName().toLowerCase());
            if (jPlayer != null) {
                jPlayer.onDisconnect();
            }
        }
    }
}
Also used : JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer)

Aggregations

JobsPlayer (com.gamingmesh.jobs.container.JobsPlayer)33 Player (org.bukkit.entity.Player)26 OfflinePlayer (org.bukkit.OfflinePlayer)15 Job (com.gamingmesh.jobs.container.Job)14 EventHandler (org.bukkit.event.EventHandler)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 JobProgression (com.gamingmesh.jobs.container.JobProgression)7 ItemActionInfo (com.gamingmesh.jobs.actions.ItemActionInfo)6 Block (org.bukkit.block.Block)4 ItemStack (org.bukkit.inventory.ItemStack)4 CraftingInventory (org.bukkit.inventory.CraftingInventory)3 BlockActionInfo (com.gamingmesh.jobs.actions.BlockActionInfo)2 AnvilInventory (org.bukkit.inventory.AnvilInventory)2 EnchantingInventory (org.bukkit.inventory.EnchantingInventory)2 Inventory (org.bukkit.inventory.Inventory)2 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)2 MetadataValue (org.bukkit.metadata.MetadataValue)2 EntityActionInfo (com.gamingmesh.jobs.actions.EntityActionInfo)1 JobPermission (com.gamingmesh.jobs.container.JobPermission)1 Title (com.gamingmesh.jobs.container.Title)1