Solved Making sign execute custom-made command?

Discussion in 'Plugin Development' started by SuippoKala, Jul 6, 2015.

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

    SuippoKala

    So i have made this command /sword which will give you a sword for 1000$
    The problem is that i don't know how can i make it so that when i right click a sign, it will execute the /sword command and the same time withdraw 1000$ from my balance.

    I have done these, dunno if they are even right:

    MAIN CLASS
    Code:
    public class Supers extends JavaPlugin{
    
        public static Economy econ = null; // Vault
    
    
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new SupersListeners(), this); // Rekisteröi sign listenerin
            if (!setupEconomy() ) { // Vault
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName())); // Vault
                getServer().getPluginManager().disablePlugin(this); // Vault
                getLogger().info("Supers Enabled!");
                return; //Vault
            }
       
            }
    
        public void onDisable() {
            getLogger().info("Supers Disabled!");
       
        }
    
        private boolean setupEconomy() { //Vault
            if (getServer().getPluginManager().getPlugin("Vault") == null) { //Vault
                return false; //Vault
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); //Vault
            if (rsp == null) { //Vault
                return false; //Vault
            }
            econ = rsp.getProvider(); //Vault
            return econ != null; //Vault
        }
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("sword")) {
                EconomyResponse r = econ.withdrawPlayer(player.getName(), 1000); // Yliviivaus koska uusi UUID Systeemi!
                if (r.transactionSuccess()) {
                    player.sendMessage(ChatColor.RED + "Super-Sword given to " + ChatColor.RED + player.getName());
                    ItemStack supersword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    ItemMeta meta = supersword.getItemMeta();
                    meta.setDisplayName(ChatColor.RED + "Super Sword");
                    supersword.setItemMeta(meta);
                    supersword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 10);
               
                    player.getInventory().addItem(supersword);
                    return true;
               
               
                }
                else  {
                    player.sendMessage(ChatColor.RED + "You cannot afford the item!");
                    return true;
                }
            }
            return false;
        }
    }
    LISTENER CLASS
    Code:
    public class SupersListeners implements Listener{
    
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            if (e.getLine(0).equalsIgnoreCase("[Sword]")) {
                e.setLine(0, "§5[Super]");
                e.setLine(1, "§4§bSword");
            }
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if (s.getLine(0).equalsIgnoreCase("§5[Super]")) {
                }
            }
        }
    
    }
    
    The question is that do i even need the listener class?

    TLDR; I wanna make a buy sign which will give me diamond sword with sharpness X named "Super Sword" and would cost 1000$
     
    Last edited: Jul 7, 2015
  2. When the player clicks on the sign (you already have that part, the playerInteract) you can let the player perform a command with Player.performCommand(command);
     
  3. Offline

    SuippoKala

    I still need help with this people, i'd appreciate it much if you would help me! :)
     
  4. Offline

    glassbillen

    @SuippoKala
    Okay, try with
    Code:
    Bukkit.getServer().dispatchCommand(e.getPlayer(), "sword");
     
  5. Offline

    SuippoKala

    I got my code changed a bit:
    Code:
    package me.Wife;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Supers extends JavaPlugin implements Listener {
       
        public static Economy econ = null;
    
       
        // I didn't have vault so you will have to add it
       
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);   
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
               
            getServer().getPluginManager().registerEvents(this, this);   
           
            }
       
        @Override
        public void onDisable() {
            // Add vault disable
        }
       
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("super")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command");
                    return true;
                }
                Player player = (Player) sender;
                // if can buy
                giveSword(player);
            }
            return false;
        }
       
        @EventHandler
        public void SignClick(PlayerInteractEvent e) {
            if (e.getClickedBlock().getType() != Material.SIGN)
                return;
            Sign sign = (Sign) e.getClickedBlock();
            if (sign.getLine(0).equalsIgnoreCase("[Sword]")) {
                sign.setLine(0, "§5[Super]");
                sign.setLine(1, "§4§bSword");
                giveSword(e.getPlayer());
            }
        }
       
        private void giveSword(Player player) {
            player.sendMessage(ChatColor.RED + "Super-Sword given to " + ChatColor.RED + player.getName());
            Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "* " + player.getName() + ChatColor.DARK_AQUA + " bought Super Sword! *");
            ItemStack supersword = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = supersword.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Super Sword");
            supersword.setItemMeta(meta);
            supersword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 10);
           
            player.getInventory().addItem(supersword);
            player.playSound(player.getLocation(), Sound.ENDERDRAGON_DEATH, 1.0F, 1.0F);
        }
        }
    
    Now i see only one error which is on
    Code:
    getServer().getPluginManager().registerEvents(this, this);   
    and it's a syntax error, how can i fix it?
     
  6. @SuippoKala You have it after you returned and you don't need 2 for the one class.
     
  7. Offline

    SuippoKala

    Yeah i noticed that, now my code is:
    Code:
    public class Supers extends JavaPlugin implements Listener {
       
        public static Economy econ = null;
       
    
       
        // I didn't have vault so you will have to add it
       
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);   
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }  
       
        @Override
        public void onDisable() {
            // Add vault disable
        }
       
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("super")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command");
                    return true;
                }
                Player player = (Player) sender;
                EconomyResponse r = econ.withdrawPlayer(player.getName(), 1000);
                // if can buy
                giveSword(player);
            }
            return false;
        }
       
        @EventHandler
        public void SignClick(PlayerInteractEvent e) {
            if (e.getClickedBlock().getType() != Material.SIGN)
                return;
            Sign sign = (Sign) e.getClickedBlock();
            if (sign.getLine(0).equalsIgnoreCase("[Sword]")) {
                sign.setLine(0, "§5[Super]");
                sign.setLine(1, "§4§bSword");
                giveSword(e.getPlayer());
            }
        }
       
        private void giveSword(Player player) {
            player.sendMessage(ChatColor.RED + "Super-Sword given to " + ChatColor.RED + player.getName());
            Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "* " + player.getName() + ChatColor.DARK_AQUA + " bought Super Sword! *");
            ItemStack supersword = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = supersword.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Super Sword");
            supersword.setItemMeta(meta);
            supersword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 10);
           
            player.getInventory().addItem(supersword);
            player.playSound(player.getLocation(), Sound.ENDERDRAGON_DEATH, 1.0F, 1.0F);
        }
        }
    When i create a sign ingame "[Sword]" nothing happens, have i failed somewhere?
     
  8. Offline

    teej107

    There is a SIGN_POST and WALL_SIGN Material. I think SIGN is the Material when it's in your inventory.
     
    Hawktasard likes this.
  9. Offline

    SuippoKala

    Still doesn't work!
     
  10. Offline

    teej107

    @SuippoKala Post new code and state what doesn't work exactly
     
  11. Offline

    HenkDeKipGaming

    can't you just copy paste everything from the command to the sign click event?
    it might make the code a bit longer, but it should work :)
     
  12. Offline

    SuippoKala

    @teej107
    Code:
    package me.Wife;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Supers extends JavaPlugin implements Listener {
      
        public static Economy econ = null;
      
    
      
        // I didn't have vault so you will have to add it
      
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);  
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        } 
      
        @Override
        public void onDisable() {
            // Add vault disable
        }
      
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("super")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command");
                    return true;
                }
                Player player = (Player) sender;
                EconomyResponse r = econ.withdrawPlayer(player.getName(), 1000);
                // if can buy
                giveSword(player);
            }
            return false;
        }
      
        @EventHandler
        public void SignClick(PlayerInteractEvent e) {
            if (e.getClickedBlock().getType() != Material.WALL_SIGN)
                return;
            Sign sign = (Sign) e.getClickedBlock();
            if (sign.getLine(0).equalsIgnoreCase("[Sword]")) {
                sign.setLine(0, "§5[Super]");
                sign.setLine(1, "§4§bSword");
                giveSword(e.getPlayer());
            }
        }
      
        private void giveSword(Player player) {
            player.sendMessage(ChatColor.RED + "Super-Sword given to " + ChatColor.RED + player.getName());
            Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "* " + player.getName() + ChatColor.DARK_AQUA + " bought Super Sword! *");
            ItemStack supersword = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = supersword.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Super Sword");
            supersword.setItemMeta(meta);
            supersword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 10);
          
            player.getInventory().addItem(supersword);
            player.playSound(player.getLocation(), Sound.ENDERDRAGON_DEATH, 1.0F, 1.0F);
        }
        }
    
    When i try to create a sign and put [Sword] on line 0, the sign wont change.
    It is still black and right clicking wont give me the sword.
     
  13. PlayerInteractEvent is when you click on something.
    If you want that it should detect when you're done editing the sign use the SignChangeEvent
     
  14. Offline

    HenkDeKipGaming

    maybe you should change the sign on place, so like:
    Code:
    @EventHandler
            public void onSignChange1(SignChangeEvent e)
            {
             if (e.getLine(0).equalsIgnoreCase("[Sword]")) {
               e.setLine(0, "§5[Super]");
               e.setLine(1, "§4§bSword");
             }
           }
    
    and then check for right click, like:

    Code:
    public void onPlayerInteract1(PlayerInteractEvent e) {
                if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
                   if ((e.getClickedBlock().getState() instanceof Sign)) {
                     Sign s = (Sign)e.getClickedBlock().getState();
                     if (s.getLine(1).equalsIgnoreCase("§4§bSword")) {
                       giveSword(e.getPlayer());
                     }
    }
    }
    
    maybe this helped :)
    if it helped, please like :D i'd appreciate that!
    if your problem is solved by this, please mark your thread as solved :)

    Greetings!

    because you liked my post, does this mean your problem is solved?

    if so, please mark your thread as solved :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
    SuippoKala likes this.
  15. Offline

    SuippoKala

    Well the sign works now, when i type [Sword] it changes it's color to those given on the code, when i right click it it doesn't give me anything?
     
  16. Could you please try changing:
    (e.getClickedBlock().getState() instanceof Sign)
    to:
    (e.getClickedBlock().getType().equals(Material.WALL_SIGN))
    Not sure if that will make any difference, but it's worth a shot. You can also add a check for a sign placed on the ground, but the one I just gave you is for a wall sign only.
     
  17. Offline

    HenkDeKipGaming

    my code works fine to me, maybe the sword giving part just doesn't work...
     
  18. block.getState() instanceof Sign should work fine if you imported everything right.
    Debug to see where it doesn't work
     
Thread Status:
Not open for further replies.

Share This Page