Advertisement
Guest User

XPSet

a guest
Dec 21st, 2012
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package com.gmail.mirelatrue.xpset;
  2.  
  3. import org.bukkit.command.Command;
  4. import org.bukkit.command.CommandExecutor;
  5. import org.bukkit.command.CommandSender;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.plugin.java.JavaPlugin;
  8.  
  9. /**
  10. * XPSet
  11. *
  12. * @author Marenwynn <[email protected]>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27.  
  28. public class XPSet extends JavaPlugin implements CommandExecutor {
  29.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  30.         if (cmd.getName().toLowerCase().contentEquals("xp")) {
  31.             if (args.length > 2) {             
  32.                 if (checkValid(args[1], args[2]) != true) { return true; }
  33.                
  34.                 String param = args[0];
  35.                 Player p = this.getServer().getPlayer(args[1]);
  36.                 int value = Integer.parseInt(args[2]);
  37.                
  38.                 if (checkPermission(sender, "set")) {
  39.                     if (param.equalsIgnoreCase("set")) {
  40.                         setXP(p, value);
  41.                         return true;
  42.                     } else if (param.equalsIgnoreCase("setlevel")) {
  43.                         setXP(p, calcXPLevels(value));
  44.                         return true;
  45.                     }
  46.                 }
  47.                
  48.                 if (checkPermission(sender, "take")) {
  49.                     if (param.equalsIgnoreCase("take")) {
  50.                         setXP(p, p.getTotalExperience() - value);
  51.                         return true;
  52.                     } else if (param.equalsIgnoreCase("takelevels")) {
  53.                         setXP(p, calcXPLevels(p.getLevel() - value));
  54.                         return true;
  55.                     }
  56.                 }
  57.                
  58.                 if (checkPermission(sender, "give")) {
  59.                     if (param.equalsIgnoreCase("give")) {
  60.                         setXP(p, p.getTotalExperience() + value);
  61.                         return true;
  62.                     } else if (param.equalsIgnoreCase("givelevels")) {
  63.                         int xpToAdd;
  64.                         int xpNextLevel = calcXPLevels(p.getLevel() + 1);
  65.                         int xpTNL = xpNextLevel - p.getTotalExperience();
  66.                         int futureLevel = p.getLevel() + value;
  67.                        
  68.                         if (xpTNL == 0) {
  69.                             // If TNL is zero, we calculate xpToAdd from current level
  70.                             xpToAdd = calcXPLevels(futureLevel) - calcXPLevels(p.getLevel());
  71.                         } else {
  72.                             // If TNL is greater than zero, we need to calculate xpToAdd from the next level
  73.                             xpToAdd = calcXPLevels(futureLevel) - xpNextLevel;
  74.                         }
  75.                        
  76.                         xpToAdd += xpTNL;      
  77.                         setXP(p, p.getTotalExperience() + xpToAdd);
  78.                        
  79.                         return true;
  80.                     }
  81.                 }
  82.             }
  83.         }      
  84.         return true;
  85.     }
  86.    
  87.     protected Boolean checkPermission (CommandSender p, String perm) {     
  88.         if (p.hasPermission("XPSet.*") || p.hasPermission("XPSet." + perm) || p.isOp()) {
  89.             return true;
  90.         }
  91.        
  92.         return false;
  93.     }
  94.    
  95.     // Sets given player's XP
  96.     protected void setXP (Player p, int amount) {      
  97.         p.setExp(0);
  98.         p.setLevel(0);
  99.         p.setTotalExperience(0);
  100.         p.giveExp(amount);
  101.        
  102.         // Bukkit sometimes screws up the level, even though total experience is at the threshold, thus this hack...
  103.         if (calcXPLevels(p.getLevel() + 1) == p.getTotalExperience()) {
  104.             p.setLevel(p.getLevel() + 1);
  105.             p.setExp(0);
  106.         }
  107.     }
  108.    
  109.     // Counts how much XP is in a given number of levels...
  110.     protected int calcXPLevels (int levels) {
  111.         int xp = 0;
  112.        
  113.         for (int i = 1; i <= levels; i++) {
  114.             if (i <= 16) {
  115.                 xp += 17;
  116.             } else if (i > 16 && i <= 31) {
  117.                 xp += (i - 16) * 3 + 17;
  118.             } else if (i > 31) {
  119.                 xp += (i - 31) * 7 + 62;
  120.             }
  121.         }
  122.        
  123.         return xp;
  124.     }
  125.    
  126.     // Make sure player name entered is an online player
  127.     // Make sure amount entered is a valid Integer and greater than zero.
  128.     protected Boolean checkValid (String player, String value) {
  129.         if (!this.getServer().getOfflinePlayer(player).isOnline()) { return false; }
  130.        
  131.         try {
  132.             Integer.parseInt(value);
  133.         } catch (NumberFormatException nfe) {
  134.             return false;
  135.         }      
  136.        
  137.         return true;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement