CommandPotion.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package me.km.commands;
  2. import java.util.ArrayList;
  3. import me.km.api.GlobalText;
  4. import me.km.api.Module;
  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.entity.player.EntityPlayer;
  10. import net.minecraft.init.Items;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.potion.Potion;
  13. import net.minecraft.potion.PotionEffect;
  14. import net.minecraft.potion.PotionUtils;
  15. public class CommandPotion extends ModuleTabCommand
  16. {
  17. public CommandPotion(Module m)
  18. {
  19. super("potion", m, Potion.REGISTRY.getKeys().stream().map(r -> r.getResourcePath()).collect(Collectors.toList()), 0);
  20. super.setDescription("Erzeugt Custom-Potions");
  21. super.setUsage("/potion [effect] [duration] [amplifier]");
  22. super.setPermission(Permissions.POTION);
  23. }
  24. @Override
  25. public boolean execute(ICommandSender cs, String[] arg)
  26. {
  27. if(!(cs instanceof EntityPlayer))
  28. {
  29. this.getModule().send(cs, GlobalText.onlyPlayer());
  30. return true;
  31. }
  32. EntityPlayer p = (EntityPlayer) cs;
  33. ItemStack hand = p.getHeldItemMainhand();
  34. if(hand.getItem() != Items.SPLASH_POTION &&
  35. hand.getItem() != Items.POTIONITEM &&
  36. hand.getItem() != Items.LINGERING_POTION)
  37. {
  38. this.getModule().send(cs, "Du musst einen Trank in der Hand halten.");
  39. return true;
  40. }
  41. if(arg.length < 1)
  42. {
  43. return false;
  44. }
  45. try
  46. {
  47. ArrayList<PotionEffect> list = new ArrayList<>();
  48. list.add(new PotionEffect(Potion.getPotionFromResourceLocation(arg[0]), Integer.parseInt(arg[1]), Integer.parseInt(arg[2])));
  49. PotionUtils.appendEffects(hand, list);
  50. this.getModule().send(cs, "Der Effekt wurde hinzugefügt.");
  51. }
  52. catch(NumberFormatException ex)
  53. {
  54. this.getModule().send(cs, GlobalText.noIntegerNumber());
  55. return true;
  56. }
  57. catch(NullPointerException ex)
  58. {
  59. this.getModule().send(cs, "Der gegebene Potion-Effect existiert nicht.");
  60. return true;
  61. }
  62. catch(ArrayIndexOutOfBoundsException ex)
  63. {
  64. return false;
  65. }
  66. return true;
  67. }
  68. }