Solved Check chat for defined random integer, reward player

Discussion in 'Plugin Development' started by DinosaurKappa, Mar 3, 2017.

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

    DinosaurKappa

    I'm trying to check if the player says the random integer n, and if so, reward the player.

    Main class:
    Code:
    registerEvents(this, new Giveaway());
    getCommand("giveaway").setExecutor(new Giveaway());
    
    Giveaway class:

    Code:
    public class Giveaway implements CommandExecutor, Listener {
        Random r = new Random();
        int n = r.nextInt(10) + 1;
        String number = "" + n;
       
        public void onPlayerCommand(AsyncPlayerChatEvent e) {
            PlayerInventory pi = e.getPlayer().getInventory();
            System.out.println("debug");
            if(e.getMessage().equals(number)) {
                System.out.println("debug 2");
                e.getPlayer().sendMessage(ChatColor.RED + "You won the giveaway!");
                pi.addItem(new ItemStack(Material.DIAMOND_BLOCK, 3));
            }
        }
    
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player) sender;
    
            if(cmd.getName().equalsIgnoreCase("giveaway")) {
                if(!sender.hasPermission("outhost.giveaway")) {
                    p.sendMessage(ChatColor.RED + "You cannot start an activity!");
                   
                } else {
                    p.sendMessage(ChatColor.GREEN + "You have started a giveaway!");
                    p.sendMessage(ChatColor.RED + "Answer: " + ChatColor.GOLD + ChatColor.BOLD + n);
                    System.out.println("debug 3");
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + ChatColor.BOLD + "Outhost" + ChatColor.DARK_GRAY + "]" + ChatColor.GREEN + " A giveaway has started!");
                    Bukkit.broadcastMessage("              ");
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + ChatColor.BOLD + "Outhost" + ChatColor.DARK_GRAY + "]" + ChatColor.RED + " Range: 1-10");
                }
               
            }
           
            return false;
        }
    
    }
    
    You can see I tried debugging. "debug 3" works fine and the giveaway starts, but even if a player says something in chat nothing happens.

    Here's what happens when I run the code:
    [​IMG]


    inb4 i get roasted for not knowing how to parse :(
     
  2. @DinosaurKappa
    You forgot the @EventHandler tag for your AsyncPlayerChatEvent listener.

    Another thing I should mention is that AsyncPlayerChatEvent isn't fired on the main thread, so you should think about what's thread safe and not (namely PlayerInventory#addItem())
     
  3. Offline

    DinosaurKappa

    Yeah I knew I shouldn't have used PlayerInventory there but damn, definitely disappointed in myself for forgetting @EventHandler LOL. Thanks man.

    Edit: Now, it works, but it says the random number is 5 but it's actually 1.
     
    Last edited: Mar 3, 2017
  4. Offline

    DinosaurKappa

    Solution:
    Add @EventHandler (LOL)
    Use PlayerChatEvent, rather than AsyncPlayerChatEvent
    Parse int to string
     
Thread Status:
Not open for further replies.

Share This Page