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.ReflectionUtils;
  9. import me.km.utils.Utils;
  10. import net.minecraft.enchantment.EnchantmentHelper;
  11. import net.minecraft.entity.*;
  12. import net.minecraft.entity.effect.LightningBoltEntity;
  13. import net.minecraft.entity.item.*;
  14. import net.minecraft.entity.monster.CreeperEntity;
  15. import net.minecraft.entity.passive.SheepEntity;
  16. import net.minecraft.entity.player.ServerPlayerEntity;
  17. import net.minecraft.entity.projectile.*;
  18. import net.minecraft.inventory.EquipmentSlotType;
  19. import net.minecraft.item.ItemStack;
  20. import net.minecraft.nbt.CompoundNBT;
  21. import net.minecraft.nbt.JsonToNBT;
  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.Vec3d;
  31. import net.minecraft.util.text.StringTextComponent;
  32. import net.minecraft.world.World;
  33. import net.minecraft.world.server.ServerWorld;
  34. public class EntityCommands {
  35. public static void registerFunctions(ScriptManager sm, SnuviScheduler scheduler) {
  36. sm.registerConsumer("entity.setnopickup", (sc, in) -> {
  37. ((AbstractArrowEntity) in[0].get(sc)).pickupStatus = AbstractArrowEntity.PickupStatus.DISALLOWED;
  38. });
  39. sm.registerFunction("entity.shootprojectile", (sc, in) -> launchProjectile((LivingEntity) in[0].get(sc),
  40. getNamedClass(in[1].getString(sc)), in[2].getDouble(sc), in.length >= 4 ? in[3].get(sc) : null));
  41. sm.registerFunction("entity.isblocking", (sc, in) -> ((LivingEntity) in[0].get(sc)).isActiveItemStackBlocking());
  42. sm.registerFunction("entity.getarmorthoughness", (sc, in) -> ((LivingEntity) in[0].get(sc)).getAttribute(SharedMonsterAttributes.ARMOR_TOUGHNESS).getValue());
  43. sm.registerFunction("entity.getarmor", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getTotalArmorValue());
  44. sm.registerFunction("entity.getmagicarmor", (sc, in) -> {
  45. int level = 0;
  46. for(ItemStack stack : ((LivingEntity) in[0].get(sc)).getArmorInventoryList()) {
  47. CompoundNBT com = stack.getTag();
  48. if(com != null && com.contains("magic")) {
  49. level += com.getInt("magic");
  50. }
  51. }
  52. return level;
  53. });
  54. sm.registerFunction("entity.getenchantmentmodifier", (sc, in) -> EnchantmentHelper.getEnchantmentModifierDamage(((LivingEntity) in[0].get(sc)).getArmorInventoryList(), (DamageSource) in[1].get(sc)));
  55. sm.registerConsumer("entity.setburning", (sc, in) -> ((Entity) in[0].get(sc)).setFire(in[1].getInt(sc)));
  56. sm.registerFunction("entity.isburning", (sc, in) -> ((Entity) in[0].get(sc)).isBurning());
  57. sm.registerFunction("entity.getlook", (sc, in) -> {
  58. Object[] o = new Object[3];
  59. Vec3d v = ((Entity) in[0].get(sc)).getLookVec();
  60. o[0] = v.x;
  61. o[1] = v.y;
  62. o[2] = v.z;
  63. return o;
  64. });
  65. sm.registerFunction("entity.getlocation", (sc, in) -> new Location((Entity) in[0].get(sc)));
  66. sm.registerConsumer("entity.heal", (sc, in) -> {
  67. LivingEntity liv = (LivingEntity) in[0].get(sc);
  68. float heal = in[1].getFloat(sc);
  69. scheduler.scheduleTask(() -> liv.heal(heal));
  70. });
  71. sm.registerConsumer("entity.damage", (sc, in) -> {
  72. LivingEntity liv = (LivingEntity) in[0].get(sc);
  73. float damage = in[1].getFloat(sc);
  74. DamageSource damageSource = (in.length >= 3) ? (DamageSource) in[2].get(sc) : DamageSource.GENERIC;
  75. scheduler.scheduleTask(() -> liv.attackEntityFrom(damageSource, damage));
  76. });
  77. sm.registerConsumer("entity.damagedirect", (sc, in) -> {
  78. LivingEntity liv = (LivingEntity) in[0].get(sc);
  79. float damageAmount = in[1].getFloat(sc);
  80. DamageSource ds = (DamageSource) in[2].get(sc);
  81. if(!liv.isInvulnerableTo(ds)) {
  82. float f2 = Math.max(damageAmount - liv.getAbsorptionAmount(), 0.0F);
  83. liv.setAbsorptionAmount(liv.getAbsorptionAmount() - (damageAmount - f2));
  84. float absorbedDamage = damageAmount - f2;
  85. if(absorbedDamage > 0.0f && absorbedDamage < 3.4028235E37f && ds.getTrueSource() instanceof ServerPlayerEntity) {
  86. ((ServerPlayerEntity) ds.getTrueSource()).addStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(absorbedDamage * 10.0f));
  87. }
  88. if(f2 > 0.0f) {
  89. float f1 = liv.getHealth();
  90. liv.getCombatTracker().trackDamage(ds, f1, f2);
  91. liv.setHealth(f1 - f2);
  92. liv.setAbsorptionAmount(liv.getAbsorptionAmount() - f2);
  93. }
  94. }
  95. });
  96. sm.registerFunction("entity.fromsource", (sc, in) -> {
  97. DamageSource ds = (DamageSource) in[0].get(sc);
  98. Entity ent = ds.getTrueSource();
  99. if(ent == null) {
  100. return ds.getImmediateSource();
  101. }
  102. return ent;
  103. });
  104. sm.registerAlias("damage.get", "entity.getdamagesource");
  105. sm.registerFunction("entity.getmaxhealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getMaxHealth());
  106. sm.registerFunction("entity.gethealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getHealth());
  107. sm.registerConsumer("entity.sethealth", (sc, in) -> ((LivingEntity) in[0].get(sc)).setHealth(in[1].getFloat(sc)));
  108. sm.registerConsumer("entity.setname", (sc, in) -> {
  109. Entity ent = (Entity) in[0].get(sc);
  110. ent.setCustomName(new StringTextComponent(in[1].getString(sc)));
  111. if(in.length >= 3) {
  112. ent.setCustomNameVisible(in[2].getBoolean(sc));
  113. return;
  114. }
  115. ent.setCustomNameVisible(false);
  116. });
  117. sm.registerFunction("entity.getname", (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getFormattedText());
  118. sm.registerConsumer("entity.throw", (sc, in) -> {
  119. Entity ent = (Entity) in[0].get(sc);
  120. ent.setMotion(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc));
  121. ent.velocityChanged = true;
  122. });
  123. sm.registerConsumer("entity.teleport", (sc, in) -> {
  124. Entity ent = (Entity) in[0].get(sc);
  125. Location l = (Location) in[1].get(sc);
  126. if(l.getWorld() == null) {
  127. throw new IllegalArgumentException("world must not be null");
  128. }
  129. if(ent instanceof ServerPlayerEntity) {
  130. ServerPlayerEntity p = (ServerPlayerEntity) ent;
  131. p.stopRiding();
  132. if(p.isSleeping()) {
  133. p.func_225652_a_(true, true);
  134. }
  135. float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
  136. float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
  137. p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
  138. } else {
  139. if(ent.world != l.getWorld()) {
  140. ServerWorld ws = (ServerWorld) l.getWorld();
  141. ent.changeDimension(ws.getDimension().getType());
  142. }
  143. if(l.getYaw() != 0 && l.getPitch() != 0) {
  144. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  145. } else {
  146. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  147. }
  148. }
  149. });
  150. sm.registerConsumer("entity.setequip", (sc, in) -> {
  151. LivingEntity liv = (LivingEntity) in[0].get(sc);
  152. ItemStack stack = ((ItemStack) in[2].get(sc)).copy();
  153. switch(in[1].getString(sc)) {
  154. case "hand":
  155. liv.setItemStackToSlot(EquipmentSlotType.MAINHAND, stack);
  156. return;
  157. case "head":
  158. liv.setItemStackToSlot(EquipmentSlotType.HEAD, stack);
  159. return;
  160. case "chest":
  161. liv.setItemStackToSlot(EquipmentSlotType.CHEST, stack);
  162. return;
  163. case "legs":
  164. liv.setItemStackToSlot(EquipmentSlotType.LEGS, stack);
  165. return;
  166. case "feet":
  167. liv.setItemStackToSlot(EquipmentSlotType.FEET, stack);
  168. return;
  169. case "offhand":
  170. liv.setItemStackToSlot(EquipmentSlotType.OFFHAND, stack);
  171. }
  172. });
  173. sm.registerFunction("entity.getequip", (sc, in) -> {
  174. LivingEntity liv = (LivingEntity) in[0].get(sc);
  175. switch(in[1].getString(sc)) {
  176. case "hand":
  177. return liv.getItemStackFromSlot(EquipmentSlotType.MAINHAND);
  178. case "head":
  179. return liv.getItemStackFromSlot(EquipmentSlotType.HEAD);
  180. case "chest":
  181. return liv.getItemStackFromSlot(EquipmentSlotType.CHEST);
  182. case "legs":
  183. return liv.getItemStackFromSlot(EquipmentSlotType.LEGS);
  184. case "feet":
  185. return liv.getItemStackFromSlot(EquipmentSlotType.FEET);
  186. case "offhand":
  187. return liv.getItemStackFromSlot(EquipmentSlotType.OFFHAND);
  188. }
  189. return ItemStack.EMPTY;
  190. });
  191. sm.registerConsumer("entity.removeall", (sc, in) -> {
  192. Class<? extends Entity> c = (Class<? extends Entity>) getNamedClass(in[0].getString(sc));
  193. if(c == Entity.class) {
  194. return;
  195. }
  196. Location l = (Location) in[1].get(sc);
  197. Utils.getEntities(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[2].getDouble(sc), c).stream().forEach(ent -> {
  198. ent.remove();
  199. });
  200. });
  201. sm.registerConsumer("entity.remove", (sc, in) -> ((Entity) in[0].get(sc)).remove());
  202. sm.registerConsumer("entity.setinvulnerable", (sc, in) -> {
  203. ((Entity) in[0].get(sc)).setInvulnerable(in[1].getBoolean(sc));
  204. });
  205. sm.registerConsumer("entity.setsilent", (sc, in) -> {
  206. ((Entity) in[0].get(sc)).setSilent(in[1].getBoolean(sc));
  207. });
  208. sm.registerConsumer("entity.setinvisible", (sc, in) -> {
  209. ((Entity) in[0].get(sc)).setInvisible(in[1].getBoolean(sc));
  210. });
  211. sm.registerConsumer("entity.ride", (sc, in) -> {
  212. ((Entity) in[0].get(sc)).startRiding(((Entity) in[1].get(sc)));
  213. });
  214. sm.registerConsumer("entity.unmount", (sc, in) -> {
  215. ((Entity) in[0].get(sc)).stopRiding();
  216. });
  217. sm.registerConsumer("entity.addeffect", (sc, in) -> {
  218. LivingEntity base = (LivingEntity) in[0].get(sc);
  219. Effect potion = Mapper.getPotion(in[1].getString(sc));
  220. if(potion == null) { // doing this only to prevent EffectInstance doing shit
  221. throw new IllegalArgumentException("potion does not exist");
  222. }
  223. if(base.isPotionActive(potion)) {
  224. base.removePotionEffect(potion);
  225. }
  226. boolean showParticles = in.length >= 5 ? in[4].getBoolean(sc) : true;
  227. base.addPotionEffect(new EffectInstance(potion, in[2].getInt(sc), in[3].getInt(sc), false, showParticles));
  228. });
  229. sm.registerConsumer("entity.cleareffects", (sc, in) -> {
  230. ((LivingEntity) in[0].get(sc)).clearActivePotions();
  231. });
  232. sm.registerFunction("entity.geteffectamplifier", (sc, in) -> {
  233. EffectInstance effect = ((LivingEntity) in[0].get(sc)).getActivePotionEffect(Mapper.getPotion(in[1].getString(sc)));
  234. return effect == null ? 0 : effect.getAmplifier() + 1;
  235. });
  236. sm.registerConsumer("entity.spawnitemframe", (sc, in) -> {
  237. Location l = ((Location) in[0].get(sc));
  238. ItemFrameEntity frame = new ItemFrameEntity(l.getWorld().getWorld(), l.getBlockPos(), Direction.byName(in[1].getString(sc)));
  239. frame.setDisplayedItem(((ItemStack) in[2].get(sc))); // copy happens in internals
  240. l.getWorld().addEntity(frame);
  241. });
  242. sm.registerFunction("entity.getitemfromframe", (sc, in) -> ((ItemFrameEntity) in[0].get(sc)).getDisplayedItem());
  243. sm.registerFunction("entity.get", (sc, in) -> {
  244. Location l = (Location) in[0].get(sc);
  245. return Utils.getEntity(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[1].getDouble(sc), getNamedClass(in[2].getString(sc)));
  246. });
  247. sm.registerFunction("entity.getpotiontype", (sc, in) -> PotionUtils.getPotionFromItem(((PotionEntity) in[0].get(sc)).getItem()).getRegistryName().toString());
  248. sm.registerConsumer("entity.setgravity", (sc, in) -> {
  249. ((Entity) in[0].get(sc)).setNoGravity(!in[1].getBoolean(sc));
  250. });
  251. sm.registerFunction("entity.iswet", (sc, in) -> ((Entity) in[0].get(sc)).isWet());
  252. sm.registerConsumer("entity.setpickupdelay", (sc, in) -> {
  253. ((ItemEntity) in[0].get(sc)).setPickupDelay(in[1].getInt(sc));
  254. });
  255. sm.registerConsumer("entity.setage", (sc, in) -> {
  256. ReflectionUtils.setAge((ItemEntity) in[0].get(sc), in[1].getInt(sc));
  257. });
  258. sm.registerFunction("entity.spawn", (sc, in) -> {
  259. ResourceLocation rl = new ResourceLocation(in[0].getString(sc));
  260. Location l = (Location) in[1].get(sc);
  261. ServerWorld sw = (ServerWorld) l.getWorld();
  262. CompoundNBT compoundnbt = in.length >= 3 ? JsonToNBT.getTagFromJson(in[2].getString(sc)) : new CompoundNBT();
  263. compoundnbt.putString("id", rl.toString());
  264. if(EntityType.getKey(EntityType.LIGHTNING_BOLT).equals(rl)) {
  265. LightningBoltEntity ent = new LightningBoltEntity(sw, l.getX(), l.getY(), l.getZ(), false);
  266. sw.addLightningBolt(ent);
  267. return ent;
  268. }
  269. Entity entity = EntityType.func_220335_a(compoundnbt, sw, (ent) -> {
  270. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  271. return sw.summonEntity(ent) ? ent : null;
  272. });
  273. if(entity != null && entity instanceof MobEntity) {
  274. ((MobEntity) entity).onInitialSpawn(sw, sw.getDifficultyForLocation(new BlockPos(entity)), SpawnReason.COMMAND, null, null);
  275. }
  276. return entity;
  277. });
  278. sm.registerFunction("entity.near", (sc, in) -> Utils.getLiving((Entity) in[0].get(sc), in[1].getDouble(sc)));
  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. }