CommandSummon.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package me.km.commands;
  2. import me.km.api.Utils;
  3. import me.km.api.GlobalText;
  4. import me.km.api.Module;
  5. import me.km.api.ModuleCommand;
  6. import me.km.permissions.Permissions;
  7. import net.minecraft.command.CommandException;
  8. import net.minecraft.command.ICommandSender;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.EntityList;
  11. import net.minecraft.entity.EntityLiving;
  12. import net.minecraft.entity.effect.EntityLightningBolt;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.nbt.JsonToNBT;
  15. import net.minecraft.nbt.NBTException;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.util.ResourceLocation;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.util.text.ITextComponent;
  20. import net.minecraft.world.World;
  21. import net.minecraft.world.chunk.storage.AnvilChunkLoader;
  22. public class CommandSummon extends ModuleCommand
  23. {
  24. public CommandSummon(Module m)
  25. {
  26. super("summon", m);
  27. super.setDescription("Spawnt ein Mob / Mobs");
  28. super.setUsage("/summon <type> [amount] [data]");
  29. super.setPermission(Permissions.SUMMON);
  30. }
  31. @Override
  32. public boolean execute(ICommandSender cs, String[] arg)
  33. {
  34. if(!(cs instanceof EntityPlayer))
  35. {
  36. this.getModule().send(cs, GlobalText.onlyPlayer());
  37. return true;
  38. }
  39. if(arg.length < 1)
  40. {
  41. return false;
  42. }
  43. EntityPlayer p = (EntityPlayer) cs;
  44. int counter = 1;
  45. if(arg.length >= 2)
  46. {
  47. try
  48. {
  49. counter = Integer.parseInt(arg[1]);
  50. }
  51. catch(NumberFormatException ex)
  52. {
  53. }
  54. }
  55. BlockPos target = Utils.getPlayerTarget(p).add(0, 1, 0);
  56. String s = arg[0];
  57. double d0 = target.getX();
  58. double d1 = target.getY();
  59. double d2 = target.getZ();
  60. World world = p.getEntityWorld();
  61. try
  62. {
  63. if (!world.isBlockLoaded(target))
  64. {
  65. this.getModule().send(cs, "Der Spawnpunkt ist außerhalb der Welt.");
  66. return true;
  67. }
  68. else if (EntityList.LIGHTNING_BOLT.equals(new ResourceLocation(s)))
  69. {
  70. world.addWeatherEffect(new EntityLightningBolt(world, d0, d1, d2, false));
  71. }
  72. else
  73. {
  74. NBTTagCompound nbttagcompound = new NBTTagCompound();
  75. boolean flag = false;
  76. if (arg.length >= 3)
  77. {
  78. ITextComponent itextcomponent = getChatComponentFromNthArg(cs, arg, 2);
  79. try
  80. {
  81. nbttagcompound = JsonToNBT.getTagFromJson(itextcomponent.getUnformattedText());
  82. flag = true;
  83. }
  84. catch (NBTException nbtexception)
  85. {
  86. this.getModule().send(cs, "Der Tag ist ungültig.");
  87. return true;
  88. }
  89. }
  90. nbttagcompound.setString("id", s);
  91. for(int i = 0; i < counter; i++)
  92. {
  93. Entity entity = AnvilChunkLoader.readWorldEntityPos(nbttagcompound, world, d0, d1, d2, true);
  94. if (entity == null)
  95. {
  96. this.getModule().send(cs, "Die Entity lässt sich nicht spawnen.");
  97. return true;
  98. }
  99. else
  100. {
  101. entity.setLocationAndAngles(d0, d1, d2, entity.rotationYaw, entity.rotationPitch);
  102. if (!flag && entity instanceof EntityLiving)
  103. {
  104. ((EntityLiving) entity).onInitialSpawn(world.getDifficultyForLocation(new BlockPos(entity)), null);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. catch(CommandException ex)
  111. {
  112. this.getModule().send(cs, "Die Parameter konnten nicht gelesen werden.");
  113. }
  114. return true;
  115. }
  116. }