CommandEnchant.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package me.km.commands;
  2. import me.kt.api.GlobalText;
  3. import me.kt.api.Module;
  4. import me.kt.api.ModuleCommand;
  5. import me.kt.api.SimpleTabCompleter;
  6. import java.util.Arrays;
  7. import java.util.stream.Collectors;
  8. import org.bukkit.Material;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.enchantments.Enchantment;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.inventory.ItemStack;
  13. public class CommandEnchant extends ModuleCommand
  14. {
  15. public CommandEnchant(Module m)
  16. {
  17. super("enchant", m);
  18. this.setDescription("Erzeugt Custom-Enchants");
  19. this.setUsage("/enchant <enchant> <level>");
  20. this.setPermission("kt.enchant");
  21. m.registerTabCompleter("enchant", new SimpleTabCompleter(
  22. Arrays.asList(Enchantment.values()).stream().map(n -> n.getName()).collect(Collectors.toList()), 1));
  23. }
  24. @Override
  25. public boolean execute(CommandSender cs, String string, String[] arg)
  26. {
  27. if(!(cs instanceof Player))
  28. {
  29. this.getModule().send(cs, GlobalText.onlyPlayer());
  30. return true;
  31. }
  32. Player p = (Player) cs;
  33. ItemStack hand = p.getInventory().getItemInMainHand();
  34. if(hand.getType() == Material.AIR)
  35. {
  36. this.getModule().send(cs, "Du musst ein Item in der Hand halten.");
  37. return true;
  38. }
  39. if(arg.length < 2)
  40. {
  41. return false;
  42. }
  43. Enchantment ench = Enchantment.getByName(arg[0]);
  44. if(ench == null)
  45. {
  46. this.getModule().send(cs, "Das gegebene Enchantment existiert nicht.");
  47. return true;
  48. }
  49. try
  50. {
  51. int i = Integer.parseInt(arg[1]);
  52. if(i < 1)
  53. {
  54. throw new NumberFormatException();
  55. }
  56. hand.addUnsafeEnchantment(ench, i);
  57. p.getInventory().setItemInMainHand(hand);
  58. this.getModule().send(cs, "Das Enchantment wurde hinzugefügt.");
  59. return true;
  60. }
  61. catch(NumberFormatException ex)
  62. {
  63. this.getModule().send(cs, GlobalText.noPositiveNaturalNumber());
  64. }
  65. return true;
  66. }
  67. }