Help with Dropped Projectile Item

Discussion in 'Plugin Development' started by moodoggy, Jan 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    moodoggy

    Hello!

    I will really appreciate assistance!

    Basically what I am wanting to do is have a projectile item, for example a Slimeball. I have the code for this!

    Code:java
    1. @EventHandler
    2. public void onInteractST(PlayerInteractEvent event) {
    3. final Player player = event.getPlayer();
    4. if (event.getAction() == Action.LEFT_CLICK_AIR) {
    5. if (player.getItemInHand().getTypeId() == 341) {
    6. if (stcooldown.get(player.getName()) != 0) {
    7. player.sendMessage(ChatColor.BLUE + "Skill> " + ChatColor.GRAY + "You can't use " + ChatColor.LIGHT_PURPLE + "Slime Ball " + ChatColor.GRAY + "for another " + ChatColor.GREEN + stcooldown.get(player.getName()) + " seconds.");
    8. } else {
    9. stcooldown.put(player.getName(), 0);
    10. repeatSchedulerST(player);
    11. player.getWorld().dropItem(player.getLocation().add(new Vector(0, 2, 0)), new ItemStack(Material.SLIME_BALL, 1)).setVelocity(player.getEyeLocation().getDirection().multiply(2));
    12. int amount = player.getItemInHand().getAmount();
    13. player.getItemInHand().setAmount(amount-1);
    14. if (amount == 1) {
    15. player.getInventory().removeItem(player.getItemInHand());
    16. player.sendMessage(ChatColor.BLUE + "Skill> " + ChatColor.GRAY + "You used " + ChatColor.LIGHT_PURPLE + "Slime Ball");
    17. }
    18. }
    19. }
    20. }
    21. }


    What I want to do now is make it so if this item that is thrown hits a player, the player receives a slowness effect etc. Would I use a DamageEvent or what because I would need a custom projectile class for that.

    If someone can explain each step please, and or show me exactly what I have to do and how I can change that up with other skills with how I want it I will pay $5 to your Paypal.
     
  2. Offline

    skipperguy12

    Don't think you're allowed to pay money to people...
    What exactly do you want?
    What does this do right now? Just drop a slimeball? Throw it? (I'm not the best with velocities).

    If it actually throws, and hits them, you should use onEntityDamageByEntity event.

    I'm not sure what the projectile really is considered though...
     
    moodoggy likes this.
  3. Offline

    stirante

    Save entityId of projectile. Add EntityDamageByEntityEvent listener and if event.getDamager().getEntityId() == savedId then apply event.getEntity().addPotionEffect().

    EDIT: Now I realized that you are using dropped items :D.Save entity id of dropped item, add PlayerPickupItemEvent listener and in this listener if event.getItem().getEntityId() == savedId then apply potion effect to event.getPlayer().
     
  4. Offline

    moodoggy

    I will try get this. Is anyone able to give me an example code. By pay I meant donate

    Thanks for the help

    stirante

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  5. Offline

    stirante

    Code:
        @EventHandler
        public void onPickup(PlayerPickupItemEvent e){
            if (e.getItem().getEntityId() == savedId){
                e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 1));
            }
        }
    And instead of:
    Code:
    player.getWorld().dropItem(player.getLocation().add(new Vector(0, 2, 0)), new ItemStack(Material.SLIME_BALL, 1)).setVelocity(player.getEyeLocation().getDirection().multiply(2));
    
    use this:
    Code:
                Item item = player.getWorld().dropItem(player.getLocation().add(new Vector(0, 2, 0)), new ItemStack(Material.SLIME_BALL, 1));
                savedId = item.getEntityId();
                item.setVelocity(player.getEyeLocation().getDirection().multiply(2));
     
  6. Offline

    moodoggy

    Thanks lots for that. It works when the player picks the item up. Although an issue is that the Item if thrown at the Entity/Player goes straight past and the player does not pick it up. For them to pick it up and receive the slowness the thrower has to chuck it at their feet.

    I need it so if the Slimeball hits them, then they receive it, or they pickup straight away as it hits them.

    If ANYONE could help please!

    stirante
    microgeek
     
  7. Offline

    microgeek

    When the item is 'thrown' add a repeating event getting the closest player, and checking if they are > 1 block away, then remove the item, stop the repeating event, and add the item the the players inventory.
     
  8. Offline

    stirante

    Try this:
    Code:
    package com.stirante.Testificate;
    
    import java.util.List;
    
    import net.minecraft.server.v1_4_6.AxisAlignedBB;
    import net.minecraft.server.v1_4_6.Entity;
    import net.minecraft.server.v1_4_6.EntityItem;
    import net.minecraft.server.v1_4_6.EntityLiving;
    import net.minecraft.server.v1_4_6.EnumMovingObjectType;
    import net.minecraft.server.v1_4_6.ItemStack;
    import net.minecraft.server.v1_4_6.MathHelper;
    import net.minecraft.server.v1_4_6.MovingObjectPosition;
    import net.minecraft.server.v1_4_6.Vec3D;
    import net.minecraft.server.v1_4_6.World;
    
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_4_6.CraftWorld;
    import org.bukkit.craftbukkit.v1_4_6.entity.CraftPlayer;
    import org.bukkit.craftbukkit.v1_4_6.inventory.CraftItemStack;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class CustomItem extends EntityItem {
        
        private EntityLiving    shooter;
        
        public CustomItem(World world, double d0, double d1, double d2,
                ItemStack itemstack, EntityLiving entity) {
            super(world, d0, d1, d2, itemstack);
            shooter = entity;
        }
        
        public CustomItem(World world, EntityLiving entityliving, ItemStack item) {
            super(world);
            setItemStack(item);
            shooter = entityliving;
            this.a(0.25F, 0.25F);
            setPositionRotation(entityliving.locX,
                    entityliving.locY + entityliving.getHeadHeight(),
                    entityliving.locZ, entityliving.yaw, entityliving.pitch);
            locX -= (MathHelper.cos(yaw / 180.0F * 3.1415927F) * 0.16F);
            locY -= 0.10000000149011612D;
            locZ -= (MathHelper.sin(yaw / 180.0F * 3.1415927F) * 0.16F);
            setPosition(locX, locY, locZ);
            height = 0.0F;
            float f = 0.4F;
            
            motX = (-MathHelper.sin(yaw / 180.0F * 3.1415927F)
                    * MathHelper.cos(pitch / 180.0F * 3.1415927F) * f);
            motZ = (MathHelper.cos(yaw / 180.0F * 3.1415927F)
                    * MathHelper.cos(pitch / 180.0F * 3.1415927F) * f);
            motY = (-MathHelper.sin(pitch / 180.0F * 3.1415927F) * f);
            shoot(motX, motY, motZ, 1.5F, 1.0F);
        }
        
        @SuppressWarnings("rawtypes")
        @Override
        public void j_() {
            super.j_();
            Vec3D vec3d = world.getVec3DPool().create(locX, locY, locZ);
            Vec3D vec3d1 = world.getVec3DPool().create(locX + motX, locY + motY,
                    locZ + motZ);
            MovingObjectPosition movingobjectposition = world.a(vec3d, vec3d1);
            
            vec3d = world.getVec3DPool().create(locX, locY, locZ);
            vec3d1 = world.getVec3DPool().create(locX + motX, locY + motY,
                    locZ + motZ);
            if (movingobjectposition != null)
                vec3d1 = world.getVec3DPool().create(movingobjectposition.pos.c,
                        movingobjectposition.pos.d, movingobjectposition.pos.e);
            
            if (!world.isStatic) {
                Entity entity = null;
                List list = world.getEntities(this, boundingBox.a(motX, motY, motZ)
                        .grow(1.0D, 1.0D, 1.0D));
                double d0 = 0.0D;
                EntityLiving entityliving = shooter;
                
                for (int j = 0; j < list.size(); ++j) {
                    Entity entity1 = (Entity) list.get(j);
                    
                    if (entity1.L() && entity1 != entityliving) {
                        float f = 0.3F;
                        AxisAlignedBB axisalignedbb = entity1.boundingBox.grow(f,
                                f, f);
                        MovingObjectPosition movingobjectposition1 = axisalignedbb
                                .a(vec3d, vec3d1);
                        
                        if (movingobjectposition1 != null) {
                            double d1 = vec3d
                                    .distanceSquared(movingobjectposition1.pos); // CraftBukkit
                                                                                    // -
                                                                                    // distance
                                                                                    // efficiency
                            
                            if (d1 < d0 || d0 == 0.0D) {
                                entity = entity1;
                                d0 = d1;
                            }
                        }
                    }
                }
                
                if (entity != null)
                    movingobjectposition = new MovingObjectPosition(entity);
            }
            
            LivingEntity living = null;
            if (movingobjectposition != null)
                if (movingobjectposition.type == EnumMovingObjectType.TILE) {
                }
                else if (movingobjectposition.entity != null
                        && movingobjectposition.entity instanceof EntityLiving)
                    living = (LivingEntity) movingobjectposition.entity
                            .getBukkitEntity();
            // HERE WRITE CODE ON ENTITY HIT---------------------------------------------------------------
            living.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 1));
        }
        
        public void shoot(double d0, double d1, double d2, float f, float f1) {
            float f2 = MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
            
            d0 /= f2;
            d1 /= f2;
            d2 /= f2;
            d0 += random.nextGaussian() * 0.007499999832361937D * f1;
            d1 += random.nextGaussian() * 0.007499999832361937D * f1;
            d2 += random.nextGaussian() * 0.007499999832361937D * f1;
            d0 *= f;
            d1 *= f;
            d2 *= f;
            motX = d0;
            motY = d1;
            motZ = d2;
            float f3 = MathHelper.sqrt(d0 * d0 + d2 * d2);
            
            lastYaw = yaw = (float) (Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D);
            lastPitch = pitch = (float) (Math.atan2(d1, f3) * 180.0D / 3.1415927410125732D);
        }
        
        public static Item launch(Player player,
                org.bukkit.inventory.ItemStack item1) {
            Location location = player.getLocation();
            CustomItem item = new CustomItem(
                    ((CraftWorld) player.getWorld()).getHandle(),
                    ((CraftPlayer) player).getHandle(),
                    CraftItemStack.asNMSCopy(item1));
            ((CraftWorld) location.getWorld()).getHandle().addEntity(item,
                    SpawnReason.CUSTOM);
            return (Item) item.getBukkitEntity();
        }
        
    }
    
    Do it as separate class.
     
  9. Offline

    moodoggy

    Thanks a lot. If this works ill send $5 your way as a donation
     
  10. Offline

    stirante

    You mean my class or his solution?
     
  11. Offline

    moodoggy

    @gomeow
    Could you briefly explain to me what I do with Stirante's class when he says " // HERE WRITE CODE ON ENTITY HIT---------------------------------------------------------------"

    I would really appreciate it!
     
  12. Offline

    stirante

    I wrote sample code. I mean when Item hits player code below this is executed. I already wrote adding potion effect.
     
Thread Status:
Not open for further replies.

Share This Page