Search in sources :

Example 6 with AttributeInstance

use of net.minecraft.world.entity.ai.attributes.AttributeInstance in project MinecraftForge by MinecraftForge.

the class GravityAttributeTest method worldTick.

@SubscribeEvent
public void worldTick(TickEvent.WorldTickEvent event) {
    if (!event.world.isClientSide) {
        if (ticks++ > 60) {
            ticks = 0;
            Level w = event.world;
            List<LivingEntity> list;
            if (w.isClientSide) {
                ClientLevel cw = (ClientLevel) w;
                list = new ArrayList<>(100);
                for (Entity e : cw.entitiesForRendering()) {
                    if (e.isAlive() && e instanceof LivingEntity)
                        list.add((LivingEntity) e);
                }
            } else {
                ServerLevel sw = (ServerLevel) w;
                Stream<LivingEntity> s = StreamSupport.stream(sw.getEntities().getAll().spliterator(), false).filter(Entity::isAlive).filter(e -> e instanceof LivingEntity).map(e -> (LivingEntity) e);
                list = s.collect(Collectors.toList());
            }
            for (LivingEntity liv : list) {
                AttributeInstance grav = liv.getAttribute(ForgeMod.ENTITY_GRAVITY.get());
                boolean inPlains = liv.level.getBiome(liv.blockPosition()).getBiomeCategory() == BiomeCategory.PLAINS;
                if (inPlains && !grav.hasModifier(REDUCED_GRAVITY)) {
                    logger.info("Granted low gravity to Entity: {}", liv);
                    grav.addTransientModifier(REDUCED_GRAVITY);
                } else if (!inPlains && grav.hasModifier(REDUCED_GRAVITY)) {
                    logger.info("Removed low gravity from Entity: {}", liv);
                    grav.removeModifier(REDUCED_GRAVITY);
                }
            }
        }
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) CreativeModeTab(net.minecraft.world.item.CreativeModeTab) Item(net.minecraft.world.item.Item) ForgeMod(net.minecraftforge.common.ForgeMod) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) Multimap(com.google.common.collect.Multimap) ServerLevel(net.minecraft.server.level.ServerLevel) ArrayList(java.util.ArrayList) FMLJavaModLoadingContext(net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) AttributeModifier(net.minecraft.world.entity.ai.attributes.AttributeModifier) RegistryEvent(net.minecraftforge.event.RegistryEvent) StreamSupport(java.util.stream.StreamSupport) Mod(net.minecraftforge.fml.common.Mod) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Properties(net.minecraft.world.item.Item.Properties) BiomeCategory(net.minecraft.world.level.biome.Biome.BiomeCategory) UUID(java.util.UUID) TickEvent(net.minecraftforge.event.TickEvent) Collectors(java.util.stream.Collectors) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) List(java.util.List) MinecraftForge(net.minecraftforge.common.MinecraftForge) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Entity(net.minecraft.world.entity.Entity) Operation(net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Level(net.minecraft.world.level.Level) LogManager(org.apache.logging.log4j.LogManager) Rarity(net.minecraft.world.item.Rarity) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 7 with AttributeInstance

use of net.minecraft.world.entity.ai.attributes.AttributeInstance in project MyPet by xXKeyleXx.

the class FollowOwner method applyWalkSpeed.

private void applyWalkSpeed() {
    float walkSpeed = owner.getAbilities().walkingSpeed;
    if (owner.getAbilities().flying) {
        // make the pet faster when the player is flying
        walkSpeed += owner.getAbilities().flyingSpeed;
    } else if (owner.isSprinting()) {
        // make the pet faster when the player is sprinting
        if (owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED) != null) {
            walkSpeed += owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED).getValue();
        }
    } else if (owner.isPassenger() && owner.getVehicle() instanceof LivingEntity) {
        // adjust the speed to the pet can catch up with the vehicle the player is in
        AttributeInstance vehicleSpeedAttribute = ((LivingEntity) owner.getVehicle()).getAttributes().getInstance(Attributes.MOVEMENT_SPEED);
        if (vehicleSpeedAttribute != null) {
            walkSpeed = (float) vehicleSpeedAttribute.getValue();
        }
    } else if (owner.hasEffect(MobEffects.MOVEMENT_SPEED)) {
        // make the pet faster when the player is has the SPEED effect
        walkSpeed += owner.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() * 0.2 * walkSpeed;
    }
    // make aquatic pets faster - swimming is hard
    if (this.petEntity.isInWaterOrBubble() && this.petEntity.getNavigation() instanceof MyAquaticPetPathNavigation) {
        walkSpeed += 0.6f;
        if (owner.isSwimming()) {
            walkSpeed -= 0.035f;
        }
        if (owner.hasEffect(MobEffects.DOLPHINS_GRACE)) {
            walkSpeed += 0.08f;
        }
    }
    // make the pet a little bit faster than the player so it can catch up
    walkSpeed += 0.07f;
    nav.getParameters().addSpeedModifier("FollowOwner", walkSpeed);
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) MyAquaticPetPathNavigation(de.Keyle.MyPet.compat.v1_18_R1.entity.ai.navigation.MyAquaticPetPathNavigation) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance)

Example 8 with AttributeInstance

use of net.minecraft.world.entity.ai.attributes.AttributeInstance in project MyPet by xXKeyleXx.

the class FollowOwner method applyWalkSpeed.

private void applyWalkSpeed() {
    float walkSpeed = owner.getAbilities().walkingSpeed;
    if (owner.getAbilities().flying) {
        // make the pet faster when the player is flying
        walkSpeed += owner.getAbilities().flyingSpeed;
    } else if (owner.isSprinting()) {
        // make the pet faster when the player is sprinting
        if (owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED) != null) {
            walkSpeed += owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED).getValue();
        }
    } else if (owner.isPassenger() && owner.getVehicle() instanceof LivingEntity) {
        // adjust the speed to the pet can catch up with the vehicle the player is in
        AttributeInstance vehicleSpeedAttribute = ((LivingEntity) owner.getVehicle()).getAttributes().getInstance(Attributes.MOVEMENT_SPEED);
        if (vehicleSpeedAttribute != null) {
            walkSpeed = (float) vehicleSpeedAttribute.getValue();
        }
    } else if (owner.hasEffect(MobEffects.MOVEMENT_SPEED)) {
        // make the pet faster when the player is has the SPEED effect
        walkSpeed += owner.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() * 0.2 * walkSpeed;
    }
    // make aquatic pets faster - swimming is hard
    if (this.petEntity.isInWaterOrBubble() && this.petEntity.getNavigation() instanceof MyAquaticPetPathNavigation) {
        walkSpeed += 0.6f;
        if (owner.isSwimming()) {
            walkSpeed -= 0.035f;
        }
        if (owner.hasEffect(MobEffects.DOLPHINS_GRACE)) {
            walkSpeed += 0.08f;
        }
    }
    // make the pet a little bit faster than the player so it can catch up
    walkSpeed += 0.07f;
    nav.getParameters().addSpeedModifier("FollowOwner", walkSpeed);
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) MyAquaticPetPathNavigation(de.Keyle.MyPet.compat.v1_17_R1.entity.ai.navigation.MyAquaticPetPathNavigation) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance)

Aggregations

AttributeInstance (net.minecraft.world.entity.ai.attributes.AttributeInstance)8 LivingEntity (net.minecraft.world.entity.LivingEntity)3 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)2 AttributeMap (net.minecraft.world.entity.ai.attributes.AttributeMap)2 Multimap (com.google.common.collect.Multimap)1 MyAquaticPetPathNavigation (de.Keyle.MyPet.compat.v1_17_R1.entity.ai.navigation.MyAquaticPetPathNavigation)1 MyAquaticPetPathNavigation (de.Keyle.MyPet.compat.v1_18_R1.entity.ai.navigation.MyAquaticPetPathNavigation)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 ClientLevel (net.minecraft.client.multiplayer.ClientLevel)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 Entity (net.minecraft.world.entity.Entity)1 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)1 Attribute (net.minecraft.world.entity.ai.attributes.Attribute)1 AttributeModifier (net.minecraft.world.entity.ai.attributes.AttributeModifier)1 Operation (net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation)1