LivingCommands.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package me.km.snuviscript.commands;
  2. import java.util.UUID;
  3. import me.hammerle.snuviscript.code.ScriptManager;
  4. import me.km.utils.Location;
  5. import me.km.utils.ReflectionUtils;
  6. import me.km.utils.Utils;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.LivingEntity;
  9. import net.minecraft.entity.MobEntity;
  10. import net.minecraft.entity.SharedMonsterAttributes;
  11. import net.minecraft.entity.ai.attributes.AttributeModifier;
  12. import net.minecraft.entity.ai.attributes.IAttribute;
  13. public class LivingCommands {
  14. public static void registerFunctions(ScriptManager sm) {
  15. IAttribute[] attributes = new IAttribute[]{
  16. SharedMonsterAttributes.ARMOR, SharedMonsterAttributes.ARMOR_TOUGHNESS,
  17. SharedMonsterAttributes.ATTACK_DAMAGE, SharedMonsterAttributes.ATTACK_KNOCKBACK,
  18. SharedMonsterAttributes.ATTACK_SPEED, SharedMonsterAttributes.FLYING_SPEED,
  19. SharedMonsterAttributes.FOLLOW_RANGE, SharedMonsterAttributes.KNOCKBACK_RESISTANCE,
  20. SharedMonsterAttributes.LUCK, SharedMonsterAttributes.MAX_HEALTH,
  21. SharedMonsterAttributes.MOVEMENT_SPEED
  22. };
  23. for(IAttribute attribute : attributes) {
  24. String name = attribute.getName().substring(8).toLowerCase();
  25. sm.registerConsumer("living.set" + name, (sc, in) -> {
  26. LivingEntity liv = (LivingEntity) in[0].get(sc);
  27. double amount = in[1].getDouble(sc) - liv.getAttribute(attribute).getBaseValue();
  28. UUID uuid = new UUID(name.length(), name.hashCode());
  29. liv.getAttribute(attribute).removeModifier(uuid);
  30. liv.getAttribute(attribute).applyModifier(new AttributeModifier(uuid, name, amount, AttributeModifier.Operation.ADDITION));
  31. });
  32. sm.registerFunction("living.get" + name, (sc, in) -> {
  33. return ((LivingEntity) in[0].get(sc)).getAttribute(attribute).getValue();
  34. });
  35. sm.registerFunction("living.getbase" + name, (sc, in) -> {
  36. return ((LivingEntity) in[0].get(sc)).getAttribute(attribute).getBaseValue();
  37. });
  38. }
  39. sm.registerConsumer("living.removeai", (sc, in) -> {
  40. LivingEntity ent = (LivingEntity) in[0].get(sc);
  41. if(ent instanceof MobEntity) {
  42. MobEntity mob = (MobEntity) in[0].get(sc);
  43. ReflectionUtils.getGoals(mob.goalSelector).clear();
  44. ReflectionUtils.getGoals(mob.targetSelector).clear();
  45. }
  46. });
  47. sm.registerFunction("living.near", (sc, in) -> {
  48. Object o = in[0].get(sc);
  49. if(o instanceof Location) {
  50. return Utils.getLiving((Location) o, in[1].getDouble(sc));
  51. }
  52. return Utils.getLiving((Entity) o, in[1].getDouble(sc));
  53. });
  54. }
  55. }