Java tutorial
/* * LabPlugin * Copyright (C) 2013 beaucoralk <http://www.liveatblocks.fr> and contributors * * 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 3 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/>. */ package com.beaucoralk.bukkit.labplugin.commands; import com.beaucoralk.bukkit.labplugin.LabPlugin; import com.beaucoralk.bukkit.labplugin.utils.VariablesUtils; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.WordUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; /** * This is the classe who are instancied for /cpenchant commands. * /cpenchant, it's a command who enchant an item before to be given to player. * * @author beaucoralk */ public class CpEnchant extends JavaPlugin implements CommandExecutor { private LabPlugin plugin; public static final String usageMessage = "/cpenchant <item>[:data] <player> <enchantment>:<level>,<enchantment>:<level>,... [amount]"; public CpEnchant(LabPlugin _plugin) { plugin = _plugin; } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (sender instanceof Player) { if (sender.hasPermission("cp.enchant") && !sender.isOp()) { sender.sendMessage(ChatColor.RED + "Don't have permission."); return true; } } if ((args.length < 3) || (args.length > 4)) { sender.sendMessage(ChatColor.RED + "'/cpenchant' command must have between 3 and 4 argument."); sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); return true; } //Dfinition de l'item vis par la commande String[] _material_data = args[0].split(":"); Material _material = Material.matchMaterial(_material_data[0]); if (_material == null) { sender.sendMessage(ChatColor.RED + "Item '" + _material_data[0] + "' not found."); sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); return true; } Integer _amount = 1; if (args.length == 4) { _amount = VariablesUtils.getInteger(args[3]); } Short _data = 0; if (_material_data.length == 2) { _data = Short.valueOf(_material_data[1]); } ItemStack _item = new ItemStack(_material, _amount, _data); if (_material == null) { sender.sendMessage(ChatColor.RED + "Item '" + _material_data[0] + "' not found."); sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); return true; } //Dfinition du joueur vis par la commande Player _player = Bukkit.getPlayerExact(args[1]); if (_player == null) { sender.sendMessage(ChatColor.RED + "Player '" + args[1] + "' not found."); sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); return true; } //Dfinition des diffrents enchantement Map<Enchantment, String> listEnchantments = new HashMap<>(); if (args.length >= 3) { for (String _enchant_level : args[2].split(",")) { String _enchant = _enchant_level.split(":")[0]; String _level = _enchant_level.split(":")[1]; Enchantment _enchantment = getEnchantment(_enchant.toUpperCase()); if (_enchantment != null) { listEnchantments.put(_enchantment, _level); } } } if (listEnchantments.isEmpty()) { sender.sendMessage(ChatColor.RED + "Enchantments list error retry."); sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); return true; } StringBuilder _nameitem = new StringBuilder(); //Dfinition du nom de l'item if (args.length >= 5) { for (int i = 4; i < args.length; i++) { _nameitem.append(args[i]); if (i < (args.length - 1)) { _nameitem.append(" "); } } } //Ajout des enchantement l'item for (Map.Entry<Enchantment, String> entry : listEnchantments.entrySet()) { Enchantment enchantment = entry.getKey(); Integer level = -1; int minLevel = enchantment.getStartLevel(); int maxLevel = enchantment.getMaxLevel(); Boolean conflicts = false; if ("max".equals(entry.getValue())) { level = maxLevel; } else { level = VariablesUtils.getInteger(entry.getValue()); } if (level != null) { if (level < minLevel || level > maxLevel) { sender.sendMessage(String.format("Level for enchantment %s must be between %d and %d", WordUtils.capitalizeFully(enchantment.getName().replaceAll("_", " ")), minLevel, maxLevel)); sender.sendMessage("Specify 0 for level to remove an enchantment"); return true; } } Map<Enchantment, Integer> enchantments = _item.getEnchantments(); for (Map.Entry<Enchantment, Integer> _entry : enchantments.entrySet()) { Enchantment _enchant = _entry.getKey(); if (_enchant.equals(enchantment)) { continue; } if (_enchant.conflictsWith(enchantment)) { sender.sendMessage( String.format("Can't apply the enchantment %s on an item with the enchantment %s", WordUtils.capitalizeFully(enchantment.getName().replaceAll("_", " ")), WordUtils.capitalizeFully(_enchant.getName().replaceAll("_", " ")))); conflicts = true; break; } } if (!conflicts && enchantment.canEnchantItem(_item)) { _item.addUnsafeEnchantment(enchantment, level); _item.getItemMeta().setDisplayName(_nameitem.toString()); } } _player.getInventory().addItem(_item); return true; } private Enchantment getEnchantment(String lookup) { Enchantment enchantment = Enchantment.getByName(lookup); if (enchantment == null) { Integer id = VariablesUtils.getInteger(lookup); if (id != null) { enchantment = Enchantment.getById(id); } } return enchantment; } }