package me.km.snuviscript.commands; import java.util.UUID; import me.hammerle.snuviscript.code.ScriptManager; import me.km.utils.Location; import me.km.utils.ReflectionUtils; import me.km.utils.Utils; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.MobEntity; import net.minecraft.entity.ai.attributes.Attribute; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.ai.attributes.ModifiableAttributeInstance; import net.minecraftforge.registries.ForgeRegistries; public class LivingCommands { private static String getName(Attribute attribute) { String name = attribute.getAttributeName(); int index = name.indexOf("."); if(index != -1) { name = name.substring(index + 1); } name = name.replace("_", ""); name = name.toLowerCase(); return name; } public static void registerFunctions(ScriptManager sm) { for(Attribute attribute : ForgeRegistries.ATTRIBUTES.getValues()) { String name = getName(attribute); sm.registerConsumer("living.set" + name, (sc, in) -> { ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute); if(a == null) { return; } double amount = in[1].getDouble(sc) - a.getBaseValue(); UUID uuid = new UUID(name.length(), name.hashCode()); a.removeModifier(uuid); a.applyNonPersistentModifier(new AttributeModifier(uuid, name, amount, AttributeModifier.Operation.ADDITION)); }); sm.registerFunction("living.get" + name, (sc, in) -> { ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute); return a == null ? null : a.getValue(); }); sm.registerFunction("living.getbase" + name, (sc, in) -> { ModifiableAttributeInstance a = ((LivingEntity) in[0].get(sc)).getAttribute(attribute); return a == null ? null : a.getBaseValue(); }); } sm.registerConsumer("living.removeai", (sc, in) -> { LivingEntity ent = (LivingEntity) in[0].get(sc); if(ent instanceof MobEntity) { MobEntity mob = (MobEntity) in[0].get(sc); ReflectionUtils.getGoals(mob.goalSelector).clear(); ReflectionUtils.getGoals(mob.targetSelector).clear(); } }); sm.registerFunction("living.near", (sc, in) -> { Object o = in[0].get(sc); if(o instanceof Location) { return Utils.getLiving((Location) o, in[1].getDouble(sc)); } return Utils.getLiving((Entity) o, in[1].getDouble(sc)); }); } }