Event Listener won't work

Discussion in 'Plugin Development' started by Pixelizedgaming, Feb 7, 2019.

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

    Pixelizedgaming

    another one of those stupid mistakes noobs like me do
    please help out

    Code:
    Main Plugin:
    package me.Pixel;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import net.milkbowl.vault.economy.Economy;
    
    public class PluginMain extends JavaPlugin{
        public static Economy econ = null;
       
       
    
    
        @Override
        public void onEnable(){
            new PlayerListener(this);
            if(!setupEco()){
                //getLogger().severe(String.format("[%s] You don't have Vault! Disabling plugin...", getDescription().getName()));
                //getServer().getPluginManager().disablePlugin(this);
            }
           
        }
        private boolean setupEco(){
            if(getServer().getPluginManager().getPlugin("Vault") == null){
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null){
                return false;
            }
            return econ != null;
        }
        @Override
        public void onDisable()
        {
           
           
        }
        public boolean onCommand(CommandSender sender,Command cmd,String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("vpunish")){
               
                int length = args.length;
                if(length == 0){
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "(!) No Player Specified!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if(target == null){
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "(!) Player " + args[0] + " not found!");
                    return true;
                }
                Bukkit.getServer().broadcastMessage(ChatColor.GREEN + target.getName() + " got punished by " + sender.getName());
               
                target.getInventory().setHelmet(new ItemStack(Material.BUCKET));
                target.getInventory().setChestplate(new ItemStack(Material.BUCKET));
                target.getInventory().setLeggings(new ItemStack(Material.BUCKET));
                target.getInventory().setBoots(new ItemStack(Material.BUCKET));
               
               
               
                target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 200, 1));
                target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 1));
                return true;
               
               
               
               
            }
            if (cmd.getName().equalsIgnoreCase("yeet"));
            for(Player p : Bukkit.getOnlinePlayers()){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                p.chat("yeet");
            }
            return true;
           
           
        }
       
    }
    Code:
    Event Handler Thing
    package me.Pixel;
    
    
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.*;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.projectiles.ProjectileSource;
    
    import net.milkbowl.vault.economy.EconomyResponse;
    
    
    
    
    
    
    
    
    
    
    
    
    
    public class PlayerListener implements Listener{
        public PlayerListener(PluginMain plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
           
           
        }
       
        @EventHandler
        public void placaClick(PlayerInteractEvent e){
            Sign sign = (Sign) e.getClickedBlock();
            Player p = e.getPlayer();
            Random rand = new Random();
           
           
    
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                p.sendMessage("Right clicked block");
                if (e.getClickedBlock().getType()==Material.SIGN || e.getClickedBlock().getType()==Material.WALL_SIGN || e.getClickedBlock().getType()==Material.SIGN_POST) {
                    p.sendMessage("Its a sign");
                    if (sign.getLine(2).equals("Click me")) {
                        int money = rand.nextInt(999) + 1;
                        EconomyResponse er = PluginMain.econ.bankDeposit(p.getName(), money);
                        if (er.transactionSuccess()){
                            p.sendMessage(ChatColor.GREEN + "You Have received " + ChatColor.GRAY + money + ChatColor.GREEN + "from the Server!");
                       
                    
                            p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 10F, 10F);
                        }
                    }
    
    
                }
            }
    
    
        }
        @EventHandler
        public void healEat(PlayerItemConsumeEvent event){
            Player player = event.getPlayer();
            Random random = new Random();
            int x = random.nextInt(11);
            double e = x;
            double g = player.getHealth();
            player.setHealth(g + e);
           
            player.sendMessage(ChatColor.GRAY + "Yum! You Gained " + ChatColor.AQUA + x + ChatColor.GRAY+ " Health!");
           
    } //
        @EventHandler
        public void antiVoid(PlayerEvent event){
            Player player = event.getPlayer();
            Location location = new Location(player.getWorld()  , player.getLocation().getX(), player.getLocation().getY()+60, player.getLocation().getZ());
            if (player.getLocation().getY()<0){
                player.teleport(location);//Teleports them up
                int y = (int) player.getLocation().getY(); //Gets Coords of player
                int x = (int) player.getLocation().getX();
                int z = (int) player.getLocation().getZ();
                Block Floor1 = player.getWorld().getBlockAt(x, y - 2, z);//Sets the block locations
                Block Air2 = player.getWorld().getBlockAt(x, y , z);
                Block Air1  = player.getWorld().getBlockAt(x, y -1, z);
                Block Air3  = player.getWorld().getBlockAt(x, y + 1, z);
                Floor1.setType(Material.GLASS);//sets the block types.
                Air1.setType(Material.AIR);
                Air2.setType(Material.AIR);
                Air3.setType(Material.AIR);
                player.sendMessage(ChatColor.RED + "Woah there buddy! Careful!");
        }
        }
    
        @EventHandler
        public void hpCounterBows(EntityDamageByEntityEvent event){
            if ( !(event.getEntity() instanceof Player) )
                return;
            Player player = (Player) event.getEntity();
           
            if(((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager() instanceof Arrow) {
                Arrow arrow = (Arrow) ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager();
                ProjectileSource shooter = arrow.getShooter();
    
                   if(shooter instanceof Player)
                   {
                      Player shooterPlayer = (Player) shooter;
                      long hp = Math.round( player.getHealth());
                      shooterPlayer.sendMessage(ChatColor.GRAY + player.getName() + ChatColor.GREEN + " has " + ChatColor.GRAY + hp + ChatColor.GREEN + " health left.");
                     
           
           
           
               
           
               
           
           
           
           
           
                   }   
        }
        }
                @EventHandler
                   public void hpCounterSwords(EntityDamageByEntityEvent event){
                       if ( !(event.getEntity() instanceof Player) )//checks if entity is a player
                           return;
                       Player player = (Player) event.getEntity();
                       if (((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager() instanceof Player) {
                           Player p2 = (Player) ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager();
                           int hp = (int) player.getHealth();
                        p2.sendMessage(ChatColor.GRAY + player.getName() + ChatColor.GREEN + " has " + ChatColor.GRAY + hp + ChatColor.GREEN + " health left.");
                       }
                   }
              
                   
              
                  
                  
              
               @EventHandler
                public void Ores(BlockBreakEvent event){
                    Player player = event.getPlayer();
                    Random random = new Random();
                    int rNumber = random.nextInt(120) + 1;
                    Block block = event.getBlock();
                    if (block.getType().equals( Material.COBBLESTONE)){
                        block.setType(Material.AIR);
                        if (rNumber >40){
                            return;
                        }
                        else if(rNumber <=80 && rNumber >105){
                            player.getInventory().addItem(new ItemStack(Material.IRON_INGOT));
                           
                        }
                        else if(rNumber <=105 && rNumber >115){
                            player.getInventory().addItem(new ItemStack(Material.GOLD_INGOT));
                           
                        }
                        else if(rNumber <=115 && rNumber >119){
                            player.getInventory().addItem(new ItemStack(Material.DIAMOND));
                       
                           
                        }
                        else if(rNumber <=119){
                            player.getInventory().addItem(new ItemStack(Material.EMERALD));
                        }
                        else if(rNumber <=40 && rNumber >=80){
                            player.getInventory().addItem(new ItemStack(Material.COAL));
                           
                        }
                    if (block.getType() == Material.IRON_ORE){
                        player.getInventory().remove(Material.IRON_ORE);
                        player.getInventory().addItem(new ItemStack(Material.IRON_INGOT));
                    }
                           
                    }
                   
                   
               }
                }
               
       
    
    
    Code:
    Plugin.yml
    
    
    
    name: PranksNUtils
    main: me.Pixel.PluginMain
    version: 1.1
    description: Antivoid, Health counter, plus many more!
    commands:
        vpunish:
          description: May the gods have mercy on this player
          usage: /<command>
        yeet:
          usage: /<command>
          description: DO NOT USE
    
    i'm sorry if i sound stupid, but I just started am noob please give me a break
     
  2. Offline

    Tango_

    @Pixelizedgaming
    You need to register your Listener class inside of the onEnable, put this: getServer().getPluginManager().registerEvents(new PlayerListener(), this);

    Also don't ever use Thread.sleep, it will halt your whole server
     
    Last edited: Feb 8, 2019
  3. Offline

    timtower Ninja on the waves Moderator

    @Tango_ plugin.getServer().getPluginManager().registerEvents(this, plugin);
    He did it in the constructor.
     
  4. Offline

    Tango_

    Ah, my bad.

    @Pixelizedgaming I took another look, it seems that you have used an invalid Event name 'PlayerEvent'.
    After you remove that or fix it, it should register your events properly.
     
  5. Online

    KarimAKL

    @Pixelizedgaming
    1. Instead of 'PlayerEvent' try the 'PlayerMoveEvent'.
    2. For the 'PlayerMoveEvent' you can use 'event.getFrom()', 'event.getTo()', 'event.setFrom()' and 'event.setTo()', with this you can check if 'event.getTo().getY() < 0' instead of 'player.getLocation().getY() < 0'
     
Thread Status:
Not open for further replies.

Share This Page