Search in sources :

Example 76 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent 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 77 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project BluePower by Qmunity.

the class BPEventHandler method onEntityAttack.

@SubscribeEvent
public void onEntityAttack(LivingAttackEvent event) {
    if (!isAttacking && event.getSource() instanceof EntityDamageSource) {
        // this event will be trigger recursively by EntityLiving#hurt,
        // so we need to stop the loop.
        EntityDamageSource entitySource = (EntityDamageSource) event.getSource();
        if (entitySource.getEntity() instanceof PlayerEntity) {
            PlayerEntity killer = (PlayerEntity) entitySource.getEntity();
            if (!killer.inventory.getSelected().isEmpty()) {
                if (EnchantmentHelper.getEnchantments(killer.inventory.getSelected()).containsKey(BPEnchantments.disjunction)) {
                    if (event.getEntityLiving() instanceof EndermanEntity || event.getEntityLiving() instanceof EnderDragonEntity) {
                        int level = EnchantmentHelper.getItemEnchantmentLevel(BPEnchantments.disjunction, killer.inventory.getSelected());
                        isAttacking = true;
                        event.getEntityLiving().hurt(event.getSource(), event.getAmount() * (level * 0.5F + 1));
                        isAttacking = false;
                    }
                }
            }
        }
    }
}
Also used : EnderDragonEntity(net.minecraft.entity.boss.dragon.EnderDragonEntity) EntityDamageSource(net.minecraft.util.EntityDamageSource) PlayerEntity(net.minecraft.entity.player.PlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 78 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project BluePower by Qmunity.

the class BPEventHandler method onAnvilEvent.

@SubscribeEvent
public void onAnvilEvent(AnvilUpdateEvent event) {
    if (!event.getLeft().isEmpty() && event.getLeft().getItem() == BPItems.screwdriver) {
        if (!event.getRight().isEmpty() && event.getRight().getItem() == Items.ENCHANTED_BOOK) {
            if (EnchantmentHelper.getEnchantments(event.getRight()).get(Enchantments.SILK_TOUCH) != null) {
                event.setOutput(new ItemStack(BPItems.silky_screwdriver, 1));
                event.setCost(20);
            }
        }
    }
}
Also used : ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 79 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project BluePower by Qmunity.

the class BPEventHandler method itemPickUp.

@SubscribeEvent
public void itemPickUp(EntityItemPickupEvent event) {
    PlayerEntity player = event.getPlayer();
    ItemStack pickUp = event.getItem().getItem();
    if (!(player.containerMenu instanceof ContainerSeedBag)) {
        for (ItemStack is : player.inventory.items) {
            if (!is.isEmpty() && is.getItem() instanceof ItemSeedBag) {
                ItemStack seedType = ItemSeedBag.getSeedType(is);
                if (!seedType.isEmpty() && seedType.sameItem(pickUp)) {
                    ItemStackHandler seedBagInvHandler = new ItemStackHandler(9);
                    // Get Items from the NBT Handler
                    if (is.hasTag())
                        seedBagInvHandler.deserializeNBT(is.getTag().getCompound("inv"));
                    // Attempt to insert items
                    for (int j = 0; j < 9 && !pickUp.isEmpty(); ++j) {
                        pickUp = seedBagInvHandler.insertItem(j, pickUp, false);
                    }
                    // Update items in the NBT
                    if (!is.hasTag())
                        is.setTag(new CompoundNBT());
                    if (is.getTag() != null) {
                        is.getTag().put("inv", seedBagInvHandler.serializeNBT());
                    }
                    // Pickup Leftovers
                    if (pickUp.isEmpty()) {
                        event.setResult(Event.Result.ALLOW);
                        event.getItem().remove();
                        return;
                    } else {
                        event.getItem().setItem(pickUp);
                    }
                }
            }
        }
    }
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) ItemSeedBag(com.bluepowermod.item.ItemSeedBag) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ContainerSeedBag(com.bluepowermod.container.ContainerSeedBag) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 80 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project Tropicraft by Tropicraft.

the class PlayerRotationHandler method onRenderPlayer.

@SubscribeEvent
public static void onRenderPlayer(RenderPlayerEvent.Pre event) {
    PoseStack stack = event.getMatrixStack();
    MultiBufferSource buffers = event.getBuffers();
    Player p = event.getPlayer();
    Entity riding = p.getVehicle();
    if (riding instanceof BeachFloatEntity) {
        FurnitureEntity floaty = (FurnitureEntity) riding;
        stack.pushPose();
        stack.mulPose(Vector3f.YP.rotationDegrees(-(floaty.yRotO + (event.getPartialRenderTick() * (floaty.getYRot() - floaty.yRotO)))));
        stack.translate(0, 1.55, 1.55);
        stack.mulPose(Vector3f.XN.rotationDegrees(90));
        // Cancel out player camera rotation
        float f = interpolateAndWrap(p.yBodyRot, p.yBodyRotO, event.getPartialRenderTick());
        stack.mulPose(Vector3f.YP.rotationDegrees(f));
        // Lock in head
        rotationYawHead = p.yHeadRot;
        prevRotationYawHead = p.yHeadRotO;
        p.yHeadRot = p.yBodyRot;
        p.yHeadRotO = p.yBodyRotO;
        rotationPitch = p.getXRot();
        prevRotationPitch = p.xRotO;
        p.setXRot(10f);
        p.xRotO = 10f;
        // Cancel limb swing
        p.animationPosition = 0;
        p.animationSpeed = 0;
        p.animationSpeedOld = 0;
    }
    if (riding instanceof SeaTurtleEntity) {
        SeaTurtleEntity turtle = (SeaTurtleEntity) riding;
        stack.pushPose();
        // Cancel out player camera rotation
        float pitch = interpolateAndWrap(turtle.getXRot(), turtle.xRotO, event.getPartialRenderTick());
        float yaw = interpolateAndWrap(turtle.yHeadRot, turtle.yHeadRotO, event.getPartialRenderTick());
        stack.translate(0, turtle.getPassengersRidingOffset() - p.getMyRidingOffset(), 0);
        stack.mulPose(Vector3f.YN.rotationDegrees(yaw));
        // TODO figure out why this budging is needed
        stack.translate(0, -0.1, 0);
        stack.mulPose(Vector3f.XP.rotationDegrees(pitch));
        stack.translate(0, 0.1, 0);
        stack.mulPose(Vector3f.YP.rotationDegrees(yaw));
        stack.translate(0, -turtle.getPassengersRidingOffset() + p.getMyRidingOffset(), 0);
        Vec3 passengerOffset = (new Vec3(-0.25f, 0.0D, 0.0D)).yRot((float) (-Math.toRadians(yaw) - (Math.PI / 2)));
        stack.translate(passengerOffset.x(), 0, passengerOffset.z());
        // Lock in head
        rotationPitch = p.getXRot();
        prevRotationPitch = p.xRotO;
        p.setXRot(10f);
        p.xRotO = 10f;
    }
}
Also used : BeachFloatEntity(net.tropicraft.core.common.entity.placeable.BeachFloatEntity) Entity(net.minecraft.world.entity.Entity) FurnitureEntity(net.tropicraft.core.common.entity.placeable.FurnitureEntity) SeaTurtleEntity(net.tropicraft.core.common.entity.SeaTurtleEntity) Player(net.minecraft.world.entity.player.Player) PoseStack(com.mojang.blaze3d.vertex.PoseStack) FurnitureEntity(net.tropicraft.core.common.entity.placeable.FurnitureEntity) Vec3(net.minecraft.world.phys.Vec3) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) BeachFloatEntity(net.tropicraft.core.common.entity.placeable.BeachFloatEntity) SeaTurtleEntity(net.tropicraft.core.common.entity.SeaTurtleEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)87 PlayerEntity (net.minecraft.entity.player.PlayerEntity)18 ItemStack (net.minecraft.item.ItemStack)17 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)11 ResourceLocation (net.minecraft.util.ResourceLocation)10 World (net.minecraft.world.World)8 Player (net.minecraft.world.entity.player.Player)8 BlockPos (net.minecraft.util.math.BlockPos)7 Entity (net.minecraft.world.entity.Entity)7 ItemStack (net.minecraft.world.item.ItemStack)7 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 Minecraft (net.minecraft.client.Minecraft)5 BlockPos (net.minecraft.core.BlockPos)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)5 StringTextComponent (net.minecraft.util.text.StringTextComponent)5 Item (net.minecraft.world.item.Item)5 BlockItem (net.minecraft.item.BlockItem)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 BlockState (net.minecraft.world.level.block.state.BlockState)4 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)4