Plugin Help, Multiple commands

Discussion in 'Plugin Development' started by cowman001, Sep 28, 2012.

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

    cowman001

    So i am fairly new to using Eclipse and using The minecraft class files. I have taken 2 years in java and know what i am doing however i need to figure out how to time between command use. I haven't figured it out yet but i would like it so my players should have to wait about 20 hours between command use for my /bible command. This way players can not spam / dupe books that they get from using the commmand. If someone could please help me it would be much appreciated. I'll check this in the morning,
    Cowman001

    code,

    Code:
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class bible extends JavaPlugin {
        @Override
        public void onEnable(){
            getLogger().info("onEnable has been invoked!");
           
        }
        @Override
        public void onDisable(){
            getLogger().info("onDisable has been invoked");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("bible")){
                if(!(sender instanceof Player)) {
                    sender.sendMessage("This command can only be run by a player!");
                }else{
                    Player player = (Player) sender;
                    player.sendMessage("May the power of christ compel you!\nDon't forget, commune at the church on sundays!");
                    ItemStack book = new ItemStack(Material.BOOK, 1);
                    PlayerInventory pi = player.getInventory();
                    pi.addItem(book);
                   
                }
                return true;
            }
            return false;
        }
           
        }
       
       
     
    
     
  2. Offline

    MrFigg

  3. Offline

    cowman001

    okay so i got that down but now i have another problem, i got the timer and a display message to work but when it comes to me trying to make like another command say /bible read it messes up the /bible command and it wont work at all anymore.. i made the part where /bible read is coded and commented it so it would work again but i cant tell if its my codeing or my plugin.yml...

    main bible class code
    Code:
    package com.gmail.cwkitchen.bible;
     
    import java.util.HashMap;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class bible extends JavaPlugin {
        public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
        @Override
        public void onEnable(){
            getLogger().info("onEnable has been invoked!");
           
        }
        @Override
        public void onDisable(){
            getLogger().info("onDisable has been invoked");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            int cooldownTime = 10800; //10800
            /*
            if ( cmd.getName().equalsIgnoreCase("bibleread"));
                Player player1 = (Player) sender;
                int itemId = player1.getItemInHand().getType().getId();
                if (itemId == 340){
                    player1.sendMessage(ChatColor.GREEN+"The Church of the Cowman was first originated in some sort of dungeon, or some guys basement. The commandments of the cowman are as follows:\n1: Thal shalt pvp just dont half ass thy trees.\n2: Thal shalt not build closer than 200 meters from spawn.\n3: Thal shalt follow thy rules at spawn or thal shalt be punnished!\n4: Thal shalt go to commmune at thy church on sudays.");
                }*/
           
           
            if ( cooldowns.containsKey(sender.getName())){
                long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
                    double Hours = secondsLeft / 3600;
               
               
                    if(secondsLeft>0){
                    sender.sendMessage(ChatColor.GREEN+"We can't just hand out free bibles all the time! Check back in:\n"+Hours+" Hours\n");
                    return true;
                    }
            }
           
                cooldowns.put(sender.getName(), System.currentTimeMillis());
                if(cmd.getName().equalsIgnoreCase("bible")){
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.GREEN+"This command can only be run by a player!");
                }else{
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.GREEN+"May the power of christ compel you!\nDon't forget, commune at the church on sundays!");
                    ItemStack book = new ItemStack(Material.BOOK, 1);
                    PlayerInventory pi = player.getInventory();
                    pi.addItem(book);
                   
                }
                return true;
            }
            return false;
        }
           
        }
       
       
     
    

    pluin.yml

    Code:
    name: bible
    main: com.gmail.cwkitchen.bible.bible
    version: 1.0.0
    description: bible mod for server.
    commands:
        bible:
          description: Bible command, gives you bible.
          usage: /bible
          permission: bible.bible
          permission-message: You don't have permission to accept jesus into your heart.
     
  4. Offline

    MrFigg

    What you want to look at are the arguments. For commands they're stored in the String[] args variable.
    Code:
    package com.gmail.cwkitchen.bible;
     
    import java.util.HashMap;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class bible extends JavaPlugin {
        public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
     
        @Override
        public void onEnable(){
            getLogger().info("onEnable has been invoked!");
        }
     
        @Override
        public void onDisable(){
            getLogger().info("onDisable has been invoked");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.GREEN+"This command can only be run by a player!");
                return true;
            }
           
            Player player = (Player) sender;
           
            if(args.length>0&&args[0].equalsIgnoreCase("read")) {
                int itemId = player.getItemInHand().getType().getId();
                if(itemId == 340) {
                    player.sendMessage(ChatColor.GREEN+"The Church of the Cowman was first originated in some sort of dungeon, or some guys basement. The commandments of the cowman are as follows:\n1: Thal shalt pvp just dont half ass thy trees.\n2: Thal shalt not build closer than 200 meters from spawn.\n3: Thal shalt follow thy rules at spawn or thal shalt be punnished!\n4: Thal shalt go to commmune at thy church on sudays.");
                }
                return true;
            }
           
            int cooldownTime = 10800; //10800
            if(cooldowns.containsKey(sender.getName())) {
                long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
                double Hours = secondsLeft / 3600;         
                if(secondsLeft>0) {
                    sender.sendMessage(ChatColor.GREEN+"We can't just hand out free bibles all the time! Check back in:\n"+Hours+" Hours\n");
                    return true;
                }
            }
            cooldowns.put(sender.getName(), System.currentTimeMillis());
            player.sendMessage(ChatColor.GREEN+"May the power of christ compel you!\nDon't forget, commune at the church on sundays!");
            ItemStack book = new ItemStack(Material.BOOK, 1);
            PlayerInventory pi = player.getInventory();
            pi.addItem(book);
            return true;
        }
    }
     
    cowman001 likes this.
  5. Offline

    cowman001

    Worked perfectly. Thank you so much, helped me out big time there, even i i don't know how that worked. xD
     
Thread Status:
Not open for further replies.

Share This Page