CommandEnchant.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package me.km.commands;
  2. import me.km.api.GlobalText;
  3. import me.km.api.Module;
  4. import java.util.Arrays;
  5. import java.util.stream.Collectors;
  6. import me.km.api.ModuleTabCommand;
  7. import me.km.permissions.Permissions;
  8. import net.minecraft.command.ICommandSender;
  9. import net.minecraft.enchantment.Enchantment;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.item.ItemStack;
  12. public class CommandEnchant extends ModuleTabCommand
  13. {
  14. public CommandEnchant(Module m)
  15. {
  16. super("enchant", m, Enchantment.REGISTRY.getKeys().stream().map(r -> r.getResourcePath()).collect(Collectors.toList()), 0);
  17. super.setDescription("Erzeugt Custom-Enchants");
  18. super.setUsage("/enchant <enchant> <level>");
  19. super.setPermission(Permissions.ENCHANT);
  20. }
  21. @Override
  22. public boolean execute(ICommandSender cs, String[] arg)
  23. {
  24. if(!(cs instanceof EntityPlayer))
  25. {
  26. this.getModule().send(cs, GlobalText.onlyPlayer());
  27. return true;
  28. }
  29. EntityPlayer p = (EntityPlayer) cs;
  30. ItemStack hand = p.getHeldItemMainhand();
  31. if(hand == ItemStack.EMPTY)
  32. {
  33. this.getModule().send(cs, "Du musst ein Item in der Hand halten.");
  34. return true;
  35. }
  36. if(arg.length < 2)
  37. {
  38. return false;
  39. }
  40. Enchantment ench = Enchantment.getEnchantmentByLocation(arg[0]);
  41. if(ench == null)
  42. {
  43. this.getModule().send(cs, "Das gegebene Enchantment existiert nicht.");
  44. return true;
  45. }
  46. try
  47. {
  48. int i = Integer.parseInt(arg[1]);
  49. if(i < 1)
  50. {
  51. throw new NumberFormatException();
  52. }
  53. hand.addEnchantment(ench, i);
  54. this.getModule().send(cs, "Das Enchantment wurde hinzugefügt.");
  55. return true;
  56. }
  57. catch(NumberFormatException ex)
  58. {
  59. this.getModule().send(cs, GlobalText.noPositiveNaturalNumber());
  60. }
  61. return true;
  62. }
  63. }