Sry Man
Sry Man

Reputation: 18

Need help in minecraft bukkit java development with kit command

I have made plugin that have 2 kits first with name Iron and the other with the name DProtect so i want them to give items and i made everything and i made a cool down the problem is when i type /kit Iron or /kit DProtect it doesn't work i tried to do a lot of things and doesn't work this is the code can i have some help ?

package Rivals.iSryMan.Kits.commands;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import Rivals.iSryMan.Kits.Main;

public class Kit implements CommandExecutor{
    private HashMap<UUID,Long> ironcooldown = new HashMap<UUID,Long>();
    private int ironcooldowntime = 300;

    private HashMap<UUID,Long> DProtectcooldown = new HashMap<UUID,Long>();
    private int DProtectcooldowntime = 5259487;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(sender instanceof Player) {
            Player p = (Player)sender;
            if(args.length == 1) {
                //Kit Iron
                if(args[0].equals("Iron")) {
                if(ironcooldown.containsKey(p.getUniqueId())) {
                    long ironsecondsleft = ( (ironcooldown.get(p.getUniqueId())/ 1000) + ironcooldowntime) - (System.currentTimeMillis() / 1000);
                    if(ironsecondsleft > 0) {
                        p.sendMessage(Main.Color(Main.prefix + "You must wait " + ironsecondsleft + " before you take that kit again!"));
                    }else if(ironsecondsleft == 0) {
                        final ItemStack ironhelmet = new ItemStack(Material.IRON_HELMET);
                        final ItemStack ironchestplate = new ItemStack(Material.IRON_CHESTPLATE);
                        final ItemStack ironleggings = new ItemStack(Material.IRON_LEGGINGS);
                        final ItemStack ironboots = new ItemStack(Material.IRON_BOOTS);
                        p.getInventory().addItem(ironhelmet);
                        p.getInventory().addItem(ironchestplate);
                        p.getInventory().addItem(ironleggings);
                        p.getInventory().addItem(ironboots);

                        p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));
                    }
                }
                //Kit Iron


                //Kit DProtect
                } else if (args[0].equals("DProtect")) {
                if(DProtectcooldown.containsKey(p.getUniqueId())) {
                    long DProtectsecondsleft = ( (DProtectcooldown.get(p.getUniqueId()) / 1000) + DProtectcooldowntime) - (System.currentTimeMillis() / 1000);
                    if(DProtectsecondsleft > 0) {
                        p.sendMessage(Main.Color(Main.prefix + "You must wait " + DProtectsecondsleft + " before you take that kit again!"));
                    }else if(DProtectcooldowntime == 0) {



                        final ItemStack dphelmet = new ItemStack(Material.IRON_HELMET);
                        final ItemMeta dphelmetmeta = dphelmet.getItemMeta();
                        final ItemStack dpchestplate = new ItemStack(Material.IRON_CHESTPLATE);
                        final ItemMeta dpchestplatemetaa = dphelmet.getItemMeta();
                        final ItemStack dpleggings = new ItemStack(Material.IRON_LEGGINGS);
                        final ItemMeta dpleggingsmeta = dphelmet.getItemMeta();
                        final ItemStack dpboots = new ItemStack(Material.IRON_BOOTS);
                        final ItemMeta dpbootsmeta = dphelmet.getItemMeta();

                        ArrayList<String> dplore = new ArrayList<String>();

                        dplore.add(Main.Color("&bAuthentic Protection 4 Armor"));


                        //Set Meta
                        dphelmet.setItemMeta(dphelmetmeta);
                        dpchestplate.setItemMeta(dpchestplatemetaa);
                        dpleggings.setItemMeta(dpleggingsmeta);
                        dpboots.setItemMeta(dpbootsmeta);
                        //Set Meta

                        //Set Lore
                        dphelmetmeta.setLore(dplore);
                        dpchestplatemetaa.setLore(dplore);
                        dpleggingsmeta.setLore(dplore);
                        dpbootsmeta.setLore(dplore);
                        //Set Lore

                        //Adding Kit
                        p.getInventory().addItem(dphelmet);
                        p.getInventory().addItem(dpchestplate);
                        p.getInventory().addItem(dpleggings);
                        p.getInventory().addItem(dpboots);
                        //Adding Kit

                        p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));

                    }
                }
                //Kit DProtect
                }
                    return true;
                }

        }
        return false;
    }



}

Upvotes: 0

Views: 252

Answers (3)

exro
exro

Reputation: 50

It seems that you haven't added the player to the HashMap so it will return false so you might want to make your HashMaps public and static and inside your PlayerJoinEvent (if you have one) add

Kit.ironcooldown.put(p.getUniqueId(), 0);

or if you don't have a PlayerJoinEvent and/or you don't want to do that, you can put before your args check

if (!ironcooldown.containsKey(p.getUniqueId()) {
    ironcooldown.put(p.getUniqueId(), 0);
}

Do the same for Dprotectcooldown.

Hope this helped!

Upvotes: 0

Lucan
Lucan

Reputation: 3595

Although Slinthn answered your questions, I thought I'd add a bit more of an explanation.

You declare a private HashMap for both kits cooldown's: ironcooldown and DProtectcooldown.

You check within your onCommand if the player executing the command is in that HashMap in your condition if(ironcooldown.containsKey(p.getUniqueId())), however, that condition will never be true because the player's UUID is never added to the HashMap.

It looks to me like you want to do something like the following (half pseudo-code)

if(!(sender instanceof Player))
    return true;
if(args.length == 0)
    return true;
Player p = (Player) sender;
if(args[0].equalsIgnoreCase("iron")){
    if(ironcooldown.containsKey(p.getUniqueId())){
        //Do check if they are still on cooldown
        //... if so, return here
    }
    ironcooldown.put(p.getUniqueId(), System.currentTimeMillis());
    givePlayerKit(Kits.IRON, p); //made up function that takes Enum & Player
}
//etc

Upvotes: 0

Slinthn
Slinthn

Reputation: 31

Have you made sure:

  1. Your command is registered in onEnable() and plugin.yml?
  2. You type specifically 'Iron' with a capital I? You should allow for different cases, using equalsIgnoreCase()
  3. This is probably the issue. Your HashMap does not contain the UUID of the CommandExecutor at first, therefore it never gets passed the ironcooldown.containsKey(p.getUniqueId()). I would recommend checking if they are in it. If they are, check if the time is 0, otherwise add them after the code has been executed.

Let me know if this works. Also, you should fix the indentation of your code - it confused me at first and it may confuse you in the future. This is not a big deal, however.

Upvotes: 2

Related Questions