Snowball replace Block.

Discussion in 'Plugin Development' started by BrainyXS, Jan 22, 2018.

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

    BrainyXS

    I will have a Stick, who can shoot a Snowball, and replace the Block the Snowball hit to Air. I can shoot, but the Block wont be replaced.

    Code:
    Code:
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    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.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.projectiles.ProjectileSource;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.Material;
    import org.bukkit.Location;
    import org.bukkit.entity.Snowball;
    import org.bukkit.util.Vector;
    import org.bukkit.entity.Entity;
    
    public class mine extends JavaPlugin implements Listener {
            @EventHandler
            public void onEvent(PlayerInteractEvent event){
                Player p = event.getPlayer();
               
                ItemStack gegenstand = event.getItem();
                ItemMeta metaData = gegenstand.getItemMeta();
               
                if(metaData.getDisplayName().equals("MineStuff")){
                    p.playSound(p.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 2F, 1F);
                    p.launchProjectile(Snowball.class);
                    
                }
            }
           
           
            public void onEvent(ProjectileHitEvent e){
                Projectile geschoss = e.getEntity();
                ProjectileSource schuetze = geschoss.getShooter();
               
                    if(schuetze instanceof Player){
                        Player spieler = (Player) schuetze;
                        ItemStack gegenstand = spieler.getInventory().getItemInMainHand();
                        ItemMeta metaData = gegenstand.getItemMeta();
    
                        if(metaData.getDisplayName().equals("MineStuff")){
                           
                            World w = spieler.getWorld();
                            Entity entity = e.getEntity();
                            Location loc = entity.getLocation();
                            Vector vec = entity.getVelocity();
                            Location loc2 = new Location(loc.getWorld(), loc.getX()+vec.getX(), loc.getY()+vec.getY(), loc.getZ()+vec.getZ());
                            System.out.println(loc2.getBlock().getTypeId());
                            w.getBlockAt(loc2).setType(Material.AIR);
                           
                        }
                    }
               
            }
           
            public void onEnable(){
                this.getLogger().info("Mining pluggin wird aufgesetzt");
                this.getLogger().info("es kann nun schneller geminet werden.");
               
                ItemStack MineStuff = new ItemStack(Material.STICK);
                ItemMeta metaData = MineStuff.getItemMeta();
                metaData.setDisplayName("MineStuff");
                MineStuff.setItemMeta(metaData);
                MineStuff.addUnsafeEnchantment(Enchantment.LUCK, 1);
    
                ShapedRecipe MineStuffRezept = new ShapedRecipe(MineStuff);
                MineStuffRezept.shape("DDD", "PSP", "PSP");
                MineStuffRezept.setIngredient('S', Material.STICK);
                MineStuffRezept.setIngredient('D', Material.TNT);
                MineStuffRezept.setIngredient('P', Material.ENDER_PEARL);
                this.getServer().addRecipe(MineStuffRezept);
    
                PluginManager pluginManager = this.getServer().getPluginManager();
                pluginManager.registerEvents(this, this);
            }
    }
    
    
     
  2. Offline

    Machine Maker

    Is it printing out the block ID of the block it hit?
     
  3. Offline

    BrainyXS

    Yes, and it should replace it.
     
  4. Offline

    Machine Maker

    Try printing out the location of the block it hits. Aim for a block and check to see if the coords match.
     
  5. Offline

    MattTheBeast

    Hi BrainyXS,
    from what I understand your trying to launch a snowball projectile if conditions are met and on block hit you want to set the target block to air.

    Here are some problems I have noticed:

    1)
    On the projectileHitEvent you are checking if they player is STILL meeting your launching conditions. Since the projectileHitEvent and PlayerInteract event are not called at the same time, any modification to the player object can cause your conditions to be false. This means that if the player changes the item in his hand before the projectileHitEvent is called the block the snowball has hit wont become air.

    I recommend you store all launched projectiles in a ArrayList and on the projectileHitEvent check to see if the ArrayList contains the shot projectile, then remove it from the ArrayList. This will also prevent random launched snowballs to set blocks to air.

    2)
    The way your getting the target block can be done much better and clean, I normally dont like to spoon feed, but here is how your onEvent should look like

    Code:
        public void onEvent(ProjectileHitEvent event) {
            Projectile check = event.getEntity();
           
            //check if the list contains the projectile
            if (YourProjectileList.contains(check)) {
                //remove the projectile from the list
                YourProjectileList.remove(check);
               
                Projectile projectile = event.getEntity();
                //getting the target block
                Block block = projectile.getLocation().getBlock();
                int id = block.getTypeId();
               
                //this is a good way to get the block correctly
                for (double i = 0.2D; i < 4.0D; i += 0.2D) {
                    if (id == 0) {
                        block = projectile.getLocation().add(projectile.getVelocity().normalize().multiply(i)).getBlock();
                        id = block.getTypeId();
                    }
                }
               
                if (id > 0) {
                    //set the block to air
                    block.setType(Material.AIR);
                   
                    //make it look like the block broke
                    projectile.getLocation().getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, id);
                }
            }
        }
    
     
Thread Status:
Not open for further replies.

Share This Page