EntityCommands.java 19 KB

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