LivingCommands.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.ai.attributes.Attribute;
  11. import net.minecraft.entity.ai.attributes.AttributeModifier;
  12. import net.minecraft.entity.ai.attributes.ModifiableAttributeInstance;
  13. import net.minecraftforge.registries.ForgeRegistries;
  14. public class LivingCommands {
  15. private static String getName(Attribute attribute) {
  16. String name = attribute.getAttributeName();
  17. int index = name.indexOf(".");
  18. if(index != -1) {
  19. name = name.substring(index + 1);
  20. }
  21. name = name.replace("_", "");
  22. name = name.toLowerCase();
  23. return name;
  24. }
  25. public static void registerFunctions(ScriptManager sm) {
  26. for(Attribute attribute : ForgeRegistries.ATTRIBUTES.getValues()) {
  27. String name = getName(attribute);
  28. sm.registerConsumer("living.set" + name, (sc, in) -> {
  29. ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
  30. if(a == null) {
  31. return;
  32. }
  33. double amount = in[1].getDouble(sc) - a.getBaseValue();
  34. UUID uuid = new UUID(name.length(), name.hashCode());
  35. a.removeModifier(uuid);
  36. a.applyNonPersistentModifier(new AttributeModifier(uuid, name, amount, AttributeModifier.Operation.ADDITION));
  37. });
  38. sm.registerFunction("living.get" + name, (sc, in) -> {
  39. ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
  40. return a == null ? null : a.getValue();
  41. });
  42. sm.registerFunction("living.getbase" + name, (sc, in) -> {
  43. ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute);
  44. return a == null ? null : a.getBaseValue();
  45. });
  46. }
  47. sm.registerConsumer("living.removeai", (sc, in) -> {
  48. LivingEntity ent = (LivingEntity) in[0].get(sc);
  49. if(ent instanceof MobEntity) {
  50. MobEntity mob = (MobEntity) in[0].get(sc);
  51. ReflectionUtils.getGoals(mob.goalSelector).clear();
  52. ReflectionUtils.getGoals(mob.targetSelector).clear();
  53. }
  54. });
  55. sm.registerFunction("living.near", (sc, in) -> {
  56. Object o = in[0].get(sc);
  57. if(o instanceof Location) {
  58. return Utils.getLiving((Location) o, in[1].getDouble(sc));
  59. }
  60. return Utils.getLiving((Entity) o, in[1].getDouble(sc));
  61. });
  62. }
  63. }