Solved could not pass on BlockBreak event

Discussion in 'Plugin Development' started by da_Do, Nov 10, 2018.

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

    da_Do

    Hello :)
    Can someone help me please ? Thanks everyone .
    CMD :

    CODE :
    Code:
    package me.Piggies.main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.PigZombie;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    
    
    public class Main extends JavaPlugin implements Listener {
       
       
       
        private Main plugin;
    
        public void onEnable() {
            getLogger().info("Plugin enabledd");
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        public void onDisable() {
            getLogger().info("Plugin Disabled");
        }
       
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            if(e.getBlock().getType().equals(Material.SPONGE)) {
                Pig pig = (Pig) e.getBlock().getWorld().spawnEntity(e.getBlock().getLocation(), EntityType.PIG);               
                pig.setCustomName(ChatColor.GOLD + "Hit me!");
                pig.setCustomNameVisible(true);
                pig.setMetadata("Angry", new FixedMetadataValue(plugin, "Angry"));
            }
        }
       
        public void onDamageEntity(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Pig && e.getDamager() instanceof Player) {
                if(e.getEntity().hasMetadata("Angry")) {
                    Location loc = e.getEntity().getLocation();
                    e.getEntity().remove();
                    PigZombie pg = (PigZombie) loc.getWorld().spawnEntity(loc, EntityType.PIG_ZOMBIE);
                    pg.getEquipment().setItemInMainHand(new ItemStack(Material.GOLD_SWORD));
                    pg.setCustomName(ChatColor.AQUA + "Jebal Som Ci Mater :)");
                    pg.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                    pg.setCustomNameVisible(true);
                    startMinionSpawn(pg, (Player)e.getDamager());
                }
            }
            if(e.getDamager() instanceof PigZombie && e.getEntity() instanceof Player) {
                if(e.getDamager().hasMetadata("ExploidingZombie")) {
                    e.getDamager().getWorld().createExplosion(e.getDamager().getLocation(), 3, true);
                }
            }
        }
       
        private void startMinionSpawn(PigZombie pg, Player damager) {
            new BukkitRunnable() {
               
                @Override
                public void run() {
                    if(pg.isDead()) {
                        cancel();
                        return;
                    }
                   
                    PigZombie bz = (PigZombie) pg.getLocation().getWorld().spawnEntity(pg.getLocation(), EntityType.PIG_ZOMBIE);
                    bz.setAngry(true);
                    bz.setBaby(true);
                    bz.setTarget(damager);
                    bz.getEquipment().setHelmet(new ItemStack(Material.TNT));
                    bz.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
                   
                   
                }
            }.runTaskTimer(plugin, 100, 100);
        }
    
    }
    
     
  2. Offline

    knokko

    @da_Do

    The plugin field of Main has a value of null during the BlockBreakEvent.

    At the line where you set the metadata of the pig, you should replace plugin with this.

    You could also set plugin to this in your onEnable method.
     
  3. Offline

    da_Do

    @knokko thanks for help :) working perfectly
     
Thread Status:
Not open for further replies.

Share This Page