LivingCommands.java 2.4 KB

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