Trying to make a Plugin with a shooting tool

Discussion in 'Plugin Development' started by XPlliarmus, Oct 16, 2018.

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

    XPlliarmus

    Hay Guys,

    for a few weeks now, im trying to get a Plugin to work, which allows a player to shoot with a pickaxe which has a special name.

    I already have a solution to get a Pickaxe with a command, which is able to shoot eggs.
    But i really struggle with getting the block, where the egg lands, to disappear and land in the players inventory.

    Maybe someone can help me.

    Here is my code: (if you want to criticize, please don't be too mean, cause im trying to code for about 3 weeks.)

    Code:
    package de.xplliarmus.server.listener;
    
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Egg;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.projectiles.ProjectileSource;
    
    public class GunListener implements Listener {
    
        @EventHandler
    
        public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            // ItemStack is = p.getInventory().getItemInHand();
    
            if (p.getItemInHand().getType() == Material.GOLD_PICKAXE) {
    
                if (p.getInventory().getItemInHand().getItemMeta().getDisplayName().contains("§4§lGun Pickaxe")) {
    
                    Egg egg = p.launchProjectile(Egg.class);
                    egg.setVelocity(p.getEyeLocation().getDirection().multiply(3));
    
                }
            }
    
        }
    
    
        @EventHandler
        public void onEggHit(ProjectileHitEvent event) {
    
            ProjectileSource p = event.getEntity().getShooter();
            Projectile pj = event.getEntity();
    
            if (p instanceof Player) {
    
                if (pj instanceof Egg) {
    
    // here i want to make the block disappear and give it to the Player, who is shooting
    
                            }
                        }
                    }
                }
            }
        }
    }
     
  2. Offline

    Reflxction

    Get the block that the projectile landed on, and call block#setType(air);

    Sent from my TRT-L21A using Tapatalk
     
  3. @XPlliarmus

    event.getHitBlock() should give you the block where the egg landed or null if the egg didn't hit a block (but something else like an entity). So check if the block is null, add the right item to the players inventory and set the type of the block to Material.AIR.
     
  4. Offline

    XPlliarmus

    Thanks for your answer,

    i already tried to use the event.getHitBlock(), but it doesn't work.
    Error: "the method getHitBlock() is undefined for the type PrejectileHitEvent"

    xD "get the block that the prejectile landed on" funny, cause thats my only issue right now ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    This Worked, Thank you a lot.
    now my only problem is the spawning of all those chickens.
     
    Last edited: Oct 18, 2018
  5. Offline

    Tango_

    Code:
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (p.getItemInHand().getType() == Material.GOLD_PICKAXE) {
                if (p.getInventory().getItemInHand().getItemMeta().getDisplayName().contains("§4§lGun Pickaxe")) {
                Egg egg = p.launchProjectile(Egg.class);
                egg.setVelocity(p.getEyeLocation().getDirection().multiply(3));
                }
            }
        }
    
        @EventHandler
        public void onEggHit(ProjectileHitEvent event) {
            Player shooter = (Player) event.getEntity().getShooter();
            ProjectileSource p = event.getEntity().getShooter();
            Projectile pj = event.getEntity();
           
            if (p instanceof Player && pj instanceof Egg) {
                BlockIterator iterator = new BlockIterator(pj.getWorld(), pj.getLocation().toVector(), pj.getVelocity().normalize(), 0.0D, 4);
                Block hitBlock = null;
               
                while (iterator.hasNext()) {
                    hitBlock = iterator.next();
                    if (hitBlock.getType() != Material.AIR) break;
                }
               
                shooter.getInventory().addItem(new ItemStack(hitBlock.getType()));
                hitBlock.setType(Material.AIR);
            }
        }
    I found some code from a previous plugin I made, not sure if this still works, but give it a go.
     
    XPlliarmus likes this.
  6. Offline

    XPlliarmus




    Omg, Thanks :D

    Finaly it works!!
    Thanks a lot!
     
Thread Status:
Not open for further replies.

Share This Page