Cmd Entering Command

Discussion in 'Plugin Development' started by BladdonB, May 25, 2015.

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

    BladdonB

    I have a break block event and I don't know how to fix a problem. I want the cmd to send 10 bucks to a player for breaking a diamond block.

    Code:
                @EventHandler
                public void onBlockBreak(BlockBreakEvent e) {
                    Block b = e.getBlock();
                    Player p = e.getPlayer();
                    if (b.getType() == Material.DIAMOND_BLOCK) {
                        p.getInventory().addItem(new ItemStack(Material.AIR));
                        String cmd = "eco " + "give " + e.getPlayer() + " 10";
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
    How do I fix it? It will not send any money to the player.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @BladdonB Because currently you are sending {CraftPlayer:<username>} or something along those lines.
    Use getPlayer().getName() instead.
    Or hook into Vault
     
  3. Offline

    BladdonB

    Ok, so where do I replace it? I get confused fast :/
     
  4. Offline

    timtower Administrator Administrator Moderator

    @BladdonB You only have it once. So there is only 1 place to replace it.
     
  5. Offline

    BladdonB

    Code:
                @EventHandler
                public void onBlockBreak(BlockBreakEvent e) {
                    Block b = e.getBlock();
                    Player p = e.getPlayer();
                    if (b.getType() == Material.DIAMOND_BLOCK) {
                        p.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
                        String cmd = "eco " + "give " + getPlayer().getName() + " 10";
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
    So, I did what you said but it still wont work :( ,unless I put it in the wrong place. Can you show me how to do it? Maybe Fix it for me, you don't have to though.
     
  6. @BladdonB Why is Eco and give seperate strings?
     
  7. Offline

    BladdonB

    isnt the essentials command /eco give (player) ?
     
  8. Offline

    caderape

    @BladdonB he means you can write "eco give"
    getPlayer().getName() this is null dude
     
  9. Offline

    BladdonB

    so what do I do then cause that's what the guy in the 2nd post said.
     
  10. Offline

    caderape

    @BladdonB it's p.getname() or e.getplayer().getname();
     
  11. Offline

    BladdonB

    @caderape I tried it and it still doesn't work :(
     
  12. Offline

    caderape

    @BladdonB The event is called ? Do you have essentials on your server ?
    Show your code
     
  13. Offline

    Zombie_Striker

    @BladdonB
    Did you check if P is null? Did you check if the player Even exist? Did you register the Listener?Did you debug?

    Please do the following before you post again.
     
  14. Offline

    nverdier

    I don't see a way #getPlayer() could return null unless some plugin is calling a new BlockBreakEvent with a null Player...
     
    MaTaMoR_ likes this.
  15. Did you register events ?
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    
    Also is better if you use Vault for this like this.
     
  16. Offline

    BladdonB

    @caderape

    Code:
    package me.prison.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
       
        //When the plugin is enabled!
                @Override
                public void onEnable(){
                    getLogger().info("Prison autosell By: Bladdon is being enabled");
                }
               
                //when du plugin is disabledded
                public void OnDisable(){
                    getLogger().info("Prison autosell By: Bladdon is being disabled");
                }
               
                @EventHandler
                public void onBlockBreak(BlockBreakEvent e) {
                    Block b = e.getBlock();
                    Player p = e.getPlayer();
                    if (b.getType() == Material.DIAMOND_BLOCK) {
                        p.getInventory().addItem(new ItemStack(Material.AIR));
                        String cmd = "eco give " + e.getPlayer().getName() + " 10";
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
                        event.setCanceled(true);
                       
                    }
                   
                   
                }
       
    
    }
    I do have essentials on my server.
     
  17. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page