EntityCommands.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.entities.EntityItemProjectile;
  4. import me.km.scheduler.SnuviScheduler;
  5. import static me.km.snuviscript.commands.CommandUtils.getNamedClass;
  6. import me.km.utils.Location;
  7. import me.km.utils.Mapper;
  8. import me.km.utils.Utils;
  9. import net.minecraft.enchantment.EnchantmentHelper;
  10. import net.minecraft.entity.*;
  11. import net.minecraft.entity.effect.LightningBoltEntity;
  12. import net.minecraft.entity.item.*;
  13. import net.minecraft.entity.monster.CreeperEntity;
  14. import net.minecraft.entity.passive.SheepEntity;
  15. import net.minecraft.entity.player.ServerPlayerEntity;
  16. import net.minecraft.entity.projectile.*;
  17. import net.minecraft.inventory.EquipmentSlotType;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.nbt.CompoundNBT;
  20. import net.minecraft.nbt.JsonToNBT;
  21. import net.minecraft.network.play.server.SEntityPacket;
  22. import net.minecraft.potion.Effect;
  23. import net.minecraft.potion.EffectInstance;
  24. import net.minecraft.potion.PotionUtils;
  25. import net.minecraft.stats.Stats;
  26. import net.minecraft.util.DamageSource;
  27. import net.minecraft.util.Direction;
  28. import net.minecraft.util.ResourceLocation;
  29. import net.minecraft.util.math.BlockPos;
  30. import net.minecraft.util.math.MathHelper;
  31. import net.minecraft.util.math.Vec3d;
  32. import net.minecraft.util.text.StringTextComponent;
  33. import net.minecraft.world.World;
  34. import net.minecraft.world.server.ServerWorld;
  35. public class EntityCommands {
  36. public static void registerFunctions(ScriptManager sm, SnuviScheduler scheduler) {
  37. sm.registerConsumer("entity.setnopickup", (sc, in) -> {
  38. ((AbstractArrowEntity) in[0].get(sc)).pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
  39. });
  40. sm.registerFunction("entity.shootprojectile", (sc, in) -> launchProjectile((LivingEntity) in[0].get(sc),
  41. getNamedClass(in[1].getString(sc)), in[2].getDouble(sc), in.length >= 4 ? in[3].get(sc) : null));
  42. sm.registerFunction("entity.isblocking", (sc, in) -> ((LivingEntity) in[0].get(sc)).isActiveItemStackBlocking());
  43. sm.registerFunction("entity.getarmorthoughness", (sc, in) -> ((LivingEntity) in[0].get(sc)).getAttribute(SharedMonsterAttributes.ARMOR_TOUGHNESS).getValue());
  44. sm.registerFunction("entity.getarmor", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getTotalArmorValue());
  45. sm.registerFunction("entity.getmagicarmor", (sc, in) -> {
  46. int level = 0;
  47. for(ItemStack stack : ((LivingEntity) in[0].get(sc)).getArmorInventoryList()) {
  48. CompoundNBT com = stack.getTag();
  49. if(com != null && com.contains("magic")) {
  50. level += com.getInt("magic");
  51. }
  52. }
  53. return level;
  54. });
  55. sm.registerFunction("entity.getenchantmentmodifier", (sc, in) -> EnchantmentHelper.getEnchantmentModifierDamage(((LivingEntity) in[0].get(sc)).getArmorInventoryList(), (DamageSource) in[1].get(sc)));
  56. sm.registerConsumer("entity.setburning", (sc, in) -> ((Entity) in[0].get(sc)).setFire(in[1].getInt(sc)));
  57. sm.registerFunction("entity.isburning", (sc, in) -> ((Entity) in[0].get(sc)).isBurning());
  58. sm.registerFunction("entity.getlook", (sc, in) -> {
  59. Object[] o = new Object[3];
  60. Vec3d v = ((Entity) in[0].get(sc)).getLookVec();
  61. o[0] = v.x;
  62. o[1] = v.y;
  63. o[2] = v.z;
  64. return o;
  65. });
  66. sm.registerFunction("entity.getlocation", (sc, in) -> new Location((Entity) in[0].get(sc)));
  67. sm.registerConsumer("entity.heal", (sc, in) -> {
  68. LivingEntity liv = (LivingEntity) in[0].get(sc);
  69. float heal = in[1].getFloat(sc);
  70. scheduler.scheduleTask(() -> liv.heal(heal));
  71. });
  72. sm.registerConsumer("entity.damage", (sc, in) -> {
  73. LivingEntity liv = (LivingEntity) in[0].get(sc);
  74. float damage = in[1].getFloat(sc);
  75. DamageSource damageSource = (in.length >= 3) ? (DamageSource) in[2].get(sc) : DamageSource.GENERIC;
  76. scheduler.scheduleTask(() -> liv.attackEntityFrom(damageSource, damage));
  77. });
  78. sm.registerConsumer("entity.damagedirect", (sc, in) -> {
  79. LivingEntity liv = (LivingEntity) in[0].get(sc);
  80. float damageAmount = in[1].getFloat(sc);
  81. DamageSource ds = (DamageSource) in[2].get(sc);
  82. if(!liv.isInvulnerableTo(ds)) {
  83. float f2 = Math.max(damageAmount - liv.getAbsorptionAmount(), 0.0F);
  84. liv.setAbsorptionAmount(liv.getAbsorptionAmount() - (damageAmount - f2));
  85. float absorbedDamage = damageAmount - f2;
  86. if(absorbedDamage > 0.0f && absorbedDamage < 3.4028235E37f && ds.getTrueSource() instanceof ServerPlayerEntity) {
  87. ((ServerPlayerEntity) ds.getTrueSource()).addStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(absorbedDamage * 10.0f));
  88. }
  89. if(f2 > 0.0f) {
  90. float f1 = liv.getHealth();
  91. liv.getCombatTracker().trackDamage(ds, f1, f2);
  92. liv.setHealth(f1 - f2);
  93. liv.setAbsorptionAmount(liv.getAbsorptionAmount() - f2);
  94. }
  95. }
  96. });
  97. sm.registerFunction("entity.fromsource", (sc, in) -> {
  98. DamageSource ds = (DamageSource) in[0].get(sc);
  99. Entity ent = ds.getTrueSource();
  100. if(ent == null) {
  101. return ds.getImmediateSource();
  102. }
  103. return ent;
  104. });
  105. sm.registerAlias("damage.get", "entity.getdamagesource");
  106. sm.registerFunction("entity.getmaxhealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getMaxHealth());
  107. sm.registerFunction("entity.gethealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getHealth());
  108. sm.registerConsumer("entity.sethealth", (sc, in) -> ((LivingEntity) in[0].get(sc)).setHealth(in[1].getFloat(sc)));
  109. sm.registerConsumer("entity.setname", (sc, in) -> {
  110. Entity ent = (Entity) in[0].get(sc);
  111. ent.setCustomName(new StringTextComponent(in[1].getString(sc)));
  112. if(in.length >= 3) {
  113. ent.setCustomNameVisible(in[2].getBoolean(sc));
  114. return;
  115. }
  116. ent.setCustomNameVisible(false);
  117. });
  118. sm.registerFunction("entity.getname", (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getFormattedText());
  119. sm.registerConsumer("entity.throw", (sc, in) -> {
  120. Entity ent = (Entity) in[0].get(sc);
  121. ent.setMotion(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc));
  122. ent.velocityChanged = true;
  123. });
  124. sm.registerConsumer("entity.teleport", (sc, in) -> {
  125. Entity ent = (Entity) in[0].get(sc);
  126. Location l = (Location) in[1].get(sc);
  127. if(l.getWorld() == null) {
  128. throw new IllegalArgumentException("world must not be null");
  129. }
  130. if(ent instanceof ServerPlayerEntity) {
  131. ServerPlayerEntity p = (ServerPlayerEntity) ent;
  132. p.stopRiding();
  133. if(p.isSleeping()) {
  134. p.func_225652_a_(true, true);
  135. }
  136. float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
  137. float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
  138. p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
  139. } else {
  140. if(ent.world != l.getWorld()) {
  141. ServerWorld ws = (ServerWorld) l.getWorld();
  142. ent.changeDimension(ws.getDimension().getType());
  143. }
  144. float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
  145. float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
  146. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), yaw, pitch);
  147. }
  148. });
  149. sm.registerConsumer("entity.setequip", (sc, in) -> {
  150. LivingEntity liv = (LivingEntity) in[0].get(sc);
  151. ItemStack stack = ((ItemStack) in[2].get(sc)).copy();
  152. switch(in[1].getString(sc)) {
  153. case "hand":
  154. liv.setItemStackToSlot(EquipmentSlotType.MAINHAND, stack);
  155. return;
  156. case "head":
  157. liv.setItemStackToSlot(EquipmentSlotType.HEAD, stack);
  158. return;
  159. case "chest":
  160. liv.setItemStackToSlot(EquipmentSlotType.CHEST, stack);
  161. return;
  162. case "legs":
  163. liv.setItemStackToSlot(EquipmentSlotType.LEGS, stack);
  164. return;
  165. case "feet":
  166. liv.setItemStackToSlot(EquipmentSlotType.FEET, stack);
  167. return;
  168. case "offhand":
  169. liv.setItemStackToSlot(EquipmentSlotType.OFFHAND, stack);
  170. }
  171. });
  172. sm.registerFunction("entity.getequip", (sc, in) -> {
  173. LivingEntity liv = (LivingEntity) in[0].get(sc);
  174. switch(in[1].getString(sc)) {
  175. case "hand":
  176. return liv.getItemStackFromSlot(EquipmentSlotType.MAINHAND);
  177. case "head":
  178. return liv.getItemStackFromSlot(EquipmentSlotType.HEAD);
  179. case "chest":
  180. return liv.getItemStackFromSlot(EquipmentSlotType.CHEST);
  181. case "legs":
  182. return liv.getItemStackFromSlot(EquipmentSlotType.LEGS);
  183. case "feet":
  184. return liv.getItemStackFromSlot(EquipmentSlotType.FEET);
  185. case "offhand":
  186. return liv.getItemStackFromSlot(EquipmentSlotType.OFFHAND);
  187. }
  188. return ItemStack.EMPTY;
  189. });
  190. sm.registerConsumer("entity.removeall", (sc, in) -> {
  191. Class<? extends Entity> c = (Class<? extends Entity>) getNamedClass(in[0].getString(sc));
  192. if(c == Entity.class) {
  193. return;
  194. }
  195. Location l = (Location) in[1].get(sc);
  196. Utils.getEntities(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[2].getDouble(sc), c).stream().forEach(ent -> {
  197. ent.remove();
  198. });
  199. });
  200. sm.registerConsumer("entity.remove", (sc, in) -> ((Entity) in[0].get(sc)).remove());
  201. sm.registerConsumer("entity.setinvulnerable", (sc, in) -> {
  202. ((Entity) in[0].get(sc)).setInvulnerable(in[1].getBoolean(sc));
  203. });
  204. sm.registerConsumer("entity.setsilent", (sc, in) -> {
  205. ((Entity) in[0].get(sc)).setSilent(in[1].getBoolean(sc));
  206. });
  207. sm.registerConsumer("entity.setinvisible", (sc, in) -> {
  208. ((Entity) in[0].get(sc)).setInvisible(in[1].getBoolean(sc));
  209. });
  210. sm.registerConsumer("entity.mount", (sc, in) -> {
  211. ((Entity) in[0].get(sc)).startRiding(((Entity) in[1].get(sc)));
  212. });
  213. sm.registerConsumer("entity.unmount", (sc, in) -> {
  214. ((Entity) in[0].get(sc)).stopRiding();
  215. });
  216. sm.registerConsumer("entity.addeffect", (sc, in) -> {
  217. LivingEntity base = (LivingEntity) in[0].get(sc);
  218. Effect potion = Mapper.getPotion(in[1].getString(sc));
  219. if(potion == null) { // doing this only to prevent EffectInstance doing shit
  220. throw new IllegalArgumentException("potion does not exist");
  221. }
  222. if(base.isPotionActive(potion)) {
  223. base.removePotionEffect(potion);
  224. }
  225. boolean showParticles = in.length >= 5 ? in[4].getBoolean(sc) : true;
  226. base.addPotionEffect(new EffectInstance(potion, in[2].getInt(sc), in[3].getInt(sc), false, showParticles));
  227. });
  228. sm.registerConsumer("entity.cleareffects", (sc, in) -> {
  229. ((LivingEntity) in[0].get(sc)).clearActivePotions();
  230. });
  231. sm.registerFunction("entity.geteffectamplifier", (sc, in) -> {
  232. EffectInstance effect = ((LivingEntity) in[0].get(sc)).getActivePotionEffect(Mapper.getPotion(in[1].getString(sc)));
  233. return effect == null ? 0 : effect.getAmplifier() + 1;
  234. });
  235. sm.registerConsumer("entity.spawnitemframe", (sc, in) -> {
  236. Location l = ((Location) in[0].get(sc));
  237. ItemFrameEntity frame = new ItemFrameEntity(l.getWorld().getWorld(), l.getBlockPos(), Direction.byName(in[1].getString(sc)));
  238. frame.setDisplayedItem(((ItemStack) in[2].get(sc))); // copy happens in internals
  239. l.getWorld().addEntity(frame);
  240. });
  241. sm.registerFunction("entity.getitemfromframe", (sc, in) -> ((ItemFrameEntity) in[0].get(sc)).getDisplayedItem());
  242. sm.registerFunction("entity.get", (sc, in) -> {
  243. Location l = (Location) in[0].get(sc);
  244. return Utils.getEntity(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[1].getDouble(sc), getNamedClass(in[2].getString(sc)));
  245. });
  246. sm.registerFunction("entity.getpotiontype", (sc, in) -> PotionUtils.getPotionFromItem(((PotionEntity) in[0].get(sc)).getItem()).getRegistryName().toString());
  247. sm.registerConsumer("entity.setgravity", (sc, in) -> {
  248. ((Entity) in[0].get(sc)).setNoGravity(!in[1].getBoolean(sc));
  249. });
  250. sm.registerFunction("entity.iswet", (sc, in) -> ((Entity) in[0].get(sc)).isWet());
  251. sm.registerConsumer("entity.setpickupdelay", (sc, in) -> {
  252. ((ItemEntity) in[0].get(sc)).setPickupDelay(in[1].getInt(sc));
  253. });
  254. sm.registerFunction("entity.spawn", (sc, in) -> {
  255. ResourceLocation rl = new ResourceLocation(in[0].getString(sc));
  256. Location l = (Location) in[1].get(sc);
  257. ServerWorld sw = (ServerWorld) l.getWorld();
  258. CompoundNBT compoundnbt = in.length >= 3 ? JsonToNBT.getTagFromJson(in[2].getString(sc)) : new CompoundNBT();
  259. compoundnbt.putString("id", rl.toString());
  260. if(EntityType.getKey(EntityType.LIGHTNING_BOLT).equals(rl)) {
  261. LightningBoltEntity ent = new LightningBoltEntity(sw, l.getX(), l.getY(), l.getZ(), false);
  262. sw.addLightningBolt(ent);
  263. return ent;
  264. }
  265. Entity entity = EntityType.func_220335_a(compoundnbt, sw, (ent) -> {
  266. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  267. return sw.summonEntity(ent) ? ent : null;
  268. });
  269. if(entity != null && entity instanceof MobEntity) {
  270. ((MobEntity) entity).onInitialSpawn(sw, sw.getDifficultyForLocation(new BlockPos(entity)), SpawnReason.COMMAND, null, null);
  271. }
  272. return entity;
  273. });
  274. sm.registerFunction("entity.near", (sc, in) -> {
  275. Object o = in[0].get(sc);
  276. if(o instanceof Location) {
  277. return Utils.getEntities((Location) o, in[1].getDouble(sc));
  278. }
  279. return Utils.getEntities((Entity) o, in[1].getDouble(sc));
  280. });
  281. sm.registerConsumer("entity.setspeed", (sc, in) -> {
  282. ((LivingEntity) in[0].get(sc)).getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(in[1].getDouble(sc));
  283. });
  284. sm.registerConsumer("entity.setgrowingage", (sc, in) -> {
  285. ((AgeableEntity) in[0].get(sc)).setGrowingAge(in[1].getInt(sc));
  286. });
  287. sm.registerFunction("entity.gettype", (sc, in) -> ((Entity) in[0].get(sc)).getType().getRegistryName().getPath());
  288. sm.registerFunction("entity.issneaking", (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
  289. sm.registerFunction("entity.issneaking", (sc, in) -> ((Entity) in[0].get(sc)).isCrouching());
  290. sm.registerFunction("sheep.issheared", (sc, in) -> ((SheepEntity) in[0].get(sc)).getSheared());
  291. sm.registerFunction("sheep.getcolor", (sc, in) -> ((SheepEntity) in[0].get(sc)).getFleeceColor().getName());
  292. sm.registerConsumer("creeper.explode", (sc, in) -> ((CreeperEntity) in[0].get(sc)).ignite());
  293. }
  294. private static <T> T launchProjectile(LivingEntity liv, Class<? extends T> projectile, double scale, Object data) {
  295. World w = liv.world;
  296. Entity launch = null;
  297. if(EntityItemProjectile.class == projectile) {
  298. if(data == null) {
  299. throw new NullPointerException("Data musn't be null for EntityItemProjectile");
  300. }
  301. ItemStack stack = (ItemStack) data;
  302. if(stack.isEmpty()) {
  303. throw new IllegalArgumentException("Empty ItemStack not allowed here");
  304. }
  305. launch = new EntityItemProjectile(liv, stack.copy());
  306. ((EntityItemProjectile) launch).setHeadingFromThrower(liv, liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
  307. } else if(SnowballEntity.class == projectile) {
  308. launch = new SnowballEntity(w, liv);
  309. ((SnowballEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
  310. } else if(EggEntity.class == projectile) {
  311. launch = new EggEntity(w, liv);
  312. ((EggEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
  313. } else if(EnderPearlEntity.class == projectile) {
  314. launch = new EnderPearlEntity(w, liv);
  315. ((EnderPearlEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, 0.0f, 1.5f, 1.0f);
  316. } else if(PotionEntity.class == projectile) {
  317. launch = new PotionEntity(w, liv);
  318. ((PotionEntity) launch).setItem((ItemStack) data);
  319. ((PotionEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, -20.0f, 0.5f, 1.0f);
  320. } else if(ExperienceBottleEntity.class == projectile) {
  321. launch = new ExperienceBottleEntity(w, liv);
  322. ((ExperienceBottleEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, -20.0f, 0.7f, 1.0f);
  323. } else if(AbstractArrowEntity.class.isAssignableFrom(projectile)) {
  324. if(SpectralArrowEntity.class == projectile) {
  325. launch = new SpectralArrowEntity(w, liv);
  326. } else {
  327. launch = new ArrowEntity(w, liv);
  328. if(data != null) {
  329. ((ArrowEntity) launch).setPotionEffect((ItemStack) data);
  330. }
  331. }
  332. ((AbstractArrowEntity) launch).shoot(liv, liv.rotationPitch, liv.rotationYaw, 0.0F, 3.0F, 1.0F);
  333. } else if(DamagingProjectileEntity.class.isAssignableFrom(projectile)) {
  334. Vec3d v = liv.getLookVec().scale(10);
  335. if(SmallFireballEntity.class == projectile) {
  336. launch = new SmallFireballEntity(w, liv, v.x, v.y, v.z);
  337. } else if(WitherSkullEntity.class == projectile) {
  338. launch = new WitherSkullEntity(w, liv, v.x, v.y, v.z);
  339. } else if(DragonFireballEntity.class == projectile) {
  340. launch = new DragonFireballEntity(w, liv, v.x, v.y, v.z);
  341. } else {
  342. launch = new FireballEntity(w, liv, v.x, v.y, v.z);
  343. }
  344. } else {
  345. return null;
  346. }
  347. launch.setMotion(launch.getMotion().scale(scale));
  348. w.addEntity(launch);
  349. return (T) launch;
  350. }
  351. }