Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.mirelatrue.xpset;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- /**
- * XPSet
- *
- * @author Marenwynn <[email protected]>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- public class XPSet extends JavaPlugin implements CommandExecutor {
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (cmd.getName().toLowerCase().contentEquals("xp")) {
- if (args.length > 2) {
- if (checkValid(args[1], args[2]) != true) { return true; }
- String param = args[0];
- Player p = this.getServer().getPlayer(args[1]);
- int value = Integer.parseInt(args[2]);
- if (checkPermission(sender, "set")) {
- if (param.equalsIgnoreCase("set")) {
- setXP(p, value);
- return true;
- } else if (param.equalsIgnoreCase("setlevel")) {
- setXP(p, calcXPLevels(value));
- return true;
- }
- }
- if (checkPermission(sender, "take")) {
- if (param.equalsIgnoreCase("take")) {
- setXP(p, p.getTotalExperience() - value);
- return true;
- } else if (param.equalsIgnoreCase("takelevels")) {
- setXP(p, calcXPLevels(p.getLevel() - value));
- return true;
- }
- }
- if (checkPermission(sender, "give")) {
- if (param.equalsIgnoreCase("give")) {
- setXP(p, p.getTotalExperience() + value);
- return true;
- } else if (param.equalsIgnoreCase("givelevels")) {
- int xpToAdd;
- int xpNextLevel = calcXPLevels(p.getLevel() + 1);
- int xpTNL = xpNextLevel - p.getTotalExperience();
- int futureLevel = p.getLevel() + value;
- if (xpTNL == 0) {
- // If TNL is zero, we calculate xpToAdd from current level
- xpToAdd = calcXPLevels(futureLevel) - calcXPLevels(p.getLevel());
- } else {
- // If TNL is greater than zero, we need to calculate xpToAdd from the next level
- xpToAdd = calcXPLevels(futureLevel) - xpNextLevel;
- }
- xpToAdd += xpTNL;
- setXP(p, p.getTotalExperience() + xpToAdd);
- return true;
- }
- }
- }
- }
- return true;
- }
- protected Boolean checkPermission (CommandSender p, String perm) {
- if (p.hasPermission("XPSet.*") || p.hasPermission("XPSet." + perm) || p.isOp()) {
- return true;
- }
- return false;
- }
- // Sets given player's XP
- protected void setXP (Player p, int amount) {
- p.setExp(0);
- p.setLevel(0);
- p.setTotalExperience(0);
- p.giveExp(amount);
- // Bukkit sometimes screws up the level, even though total experience is at the threshold, thus this hack...
- if (calcXPLevels(p.getLevel() + 1) == p.getTotalExperience()) {
- p.setLevel(p.getLevel() + 1);
- p.setExp(0);
- }
- }
- // Counts how much XP is in a given number of levels...
- protected int calcXPLevels (int levels) {
- int xp = 0;
- for (int i = 1; i <= levels; i++) {
- if (i <= 16) {
- xp += 17;
- } else if (i > 16 && i <= 31) {
- xp += (i - 16) * 3 + 17;
- } else if (i > 31) {
- xp += (i - 31) * 7 + 62;
- }
- }
- return xp;
- }
- // Make sure player name entered is an online player
- // Make sure amount entered is a valid Integer and greater than zero.
- protected Boolean checkValid (String player, String value) {
- if (!this.getServer().getOfflinePlayer(player).isOnline()) { return false; }
- try {
- Integer.parseInt(value);
- } catch (NumberFormatException nfe) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement