Make My Kit Usable Only Once

Discussion in 'Plugin Development' started by iWesley23, Apr 10, 2014.

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

    iWesley23

    I have a plugin I am making for kits. I have it for when people donate they can use the command, but I want it to only be usable once. How do I do it? Thanks. Heres my code if you need it. Also, please say where I add it at, just beginning to code.

    Code:java
    1. package me.JMPYT.servercore;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.PlayerInventory;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Kits extends JavaPlugin {
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    15. if (!(sender instanceof Player)) {
    16. sender.sendMessage(ChatColor.RED + "Only players can get kits!");
    17. return true;
    18. }
    19.  
    20. Player p = (Player) sender;
    21. PlayerInventory pi = p.getInventory();
    22.  
    23. if (cmd.getName().equalsIgnoreCase("vip")) {
    24. if (!sender.hasPermission("servercore.kitvip")){
    25. sender.sendMessage(ChatColor.RED + "You Do Not Have Permission!");
    26. }
    27. pi.addItem(new ItemStack(Material.IRON_PICKAXE, 1));
    28. pi.addItem(new ItemStack(Material.IRON_SWORD, 1));
    29. pi.addItem(new ItemStack(Material.IRON_AXE, 1));
    30. pi.addItem(new ItemStack(Material.IRON_HOE, 1));
    31. pi.addItem(new ItemStack(Material.IRON_SPADE, 1));
    32. pi.addItem(new ItemStack(Material.IRON_INGOT, 8));
    33. pi.addItem(new ItemStack(Material.DIAMOND, 8));
    34. pi.addItem(new ItemStack(Material.GOLD_INGOT, 8));
    35. pi.addItem(new ItemStack(Material.EMERALD, 8));
    36.  
    37. p.sendMessage(ChatColor.GREEN + "You Have Reveived Your Kit!");
    38. }
    39.  
    40. if (cmd.getName().equalsIgnoreCase("vipplus")) {
    41. if (!sender.hasPermission("servercore.kitvipp")){
    42. sender.sendMessage(ChatColor.RED + "You Do Not Have Permission!");
    43. }
    44. pi.addItem(new ItemStack(Material.IRON_SWORD, 1));
    45. pi.addItem(new ItemStack(Material.STONE_PICKAXE, 1));
    46. pi.addItem(new ItemStack(Material.APPLE, 5));
    47. p.sendMessage(ChatColor.GREEN + "You Have Reveived Your Kit!");
    48. }
    49. return true;
    50. }
    51. }


    Thanks if you help!
     
  2. Offline

    BillyBobJoe168

    You could make a config and when they do /vip or /vipplus add them to the list. Also, at the beginning or the /vip and /vipplus, you will need to check if the player is on the list and if so, tell them that they can't get the kit again.
     
  3. Offline

    Joshuak52

    You can make an Arraylist and when they do the kit add them to the list and on the command if in the list don't allow them to use the kit
     
  4. Offline

    spacerocket

    This might not be what you're looking for, but you can have a command for server operators (which plugins like Buycraft can execute) that will add players to a list of who can use the vip/vipplus commands. On the command, if the player is not in that list: don't execute code. If the list contains that player, remove from list and execute code.

    Example:
    Code:
    if(cmd.getName().equalsIgnoreCase("authorizedonator") && args.length > 0) {
       list.add(Bukkit.getPlayer(args[0]));
        return true;
    }
     
  5. Offline

    iWesley23

    When they donate on enjin, it runs a command "pex user <name> group set VIP, VIPP, Donator, and what not" And they will have the permission to use that command. I wouldn't need to add them to a list or whatever. Should have said that first. lol So what could I add to allow it only once.
     
  6. Offline

    MrInspector

    You could take the advice you already have gotten. ;)

     
  7. Offline

    Joshuak52

    @iWesley23 As I said before, once they use the command add them to a list so they can't do it anymore.
     
  8. Offline

    BillyBobJoe168

    I would prefer a config because if I understand correctly, the arraylist will be cleared onDisable();
     
  9. Offline

    iWesley23

    Yeah what BillyBobJoe said.
     
  10. Offline

    XvBaseballkidvX

    This is what I suggest doing:
    Code:java
    1. HashMap<String, Boolean> map = new HashMap<String, Boolean>();
    2.  
    3.  
    4. //Set the status to true
    5. public void setTrue(String name){
    6. Main.getInstance().getConfig().set(name, true);
    7. map.put(name, true);
    8. Main.getInstance().saveConfig(); //Save the config
    9.  
    10. }
    11.  
    12. //Return the player's kit status!
    13. public boolean getStatus(String name){
    14. if(map.containsKey(name)){
    15. return map.get(name);
    16. }else{
    17. return true;
    18. }
    19. }
    20.  
    21. //Seeing if the player can use the kit
    22. public boolean canUse(String name){
    23. if(map.containsKey(name)){
    24. return getStatus(name);
    25. }else{
    26. return true;
    27. }
    28. }
    29.  
    30. //Looping through all of the entries in the config
    31. public void loadConfig(){
    32. for(String keys : Main.getInstance().getConfig().getKeys(false)){
    33. boolean status = (Boolean) Main.getInstance().getConfig().get(keys);
    34. map.put(keys, status);
    35. }
    36. }


    If I made any errors please tag me! :p
     
  11. Offline

    iWesley23

    Where would I put that? Could you put it in the code for me? I am new to coding.
     
  12. Offline

    BillyBobJoe168

    Well, just do this.
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("command")) {
    2. if (!getConfig().getStringList("stringnamehere").contains(player.getName())) {
    3. //do code
    4. List<String> listname = getConfig().getStringList("stringnamehere");
    5. listname.add(player.getName());
    6. getConfig().set("stringnamehere", listname);
    7. saveConfig();
    8. } else {
    9. player.sendMessage("You already chose this kit!");
    10. }
    11. }
     
  13. Offline

    iWesley23

    So I would have it like this?
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("vip")) {
    2. if (!getConfig().getStringList("taken").contains(player.getName())) {
    3. //do code
    4. List<String> listname = getConfig().getStringList("taken");
    5. listname.add(player.getName());
    6. getConfig().set("taken", listname);
    7. saveConfig();
    8. } else {
    9. player.sendMessage("You already chose this kit!");
    10. }
    11. }


    And would (player.getName())) { be (sender.getName())) {? Cus I get an error when I use player.
     
  14. Offline

    MOMOTHEREAL

    To declare player, add this if statement:
    Code:
    if (sender instanceof Player) {
        Player player = (Player) sender;
        //Put BillyBobJoe's code here
    }
     
  15. Offline

    iWesley23

    Ok, it works now. But, do I import "list.java.awt or list.java.util"?
     
  16. Offline

    Zethariel

    Use the import statement. For an example look at the top of your class - it has all sorts of imports for each component used.
     
  17. Offline

    iWesley23

    I know I should import it, but which one do I import. the java.awt or java.util
     
  18. Offline

    Zethariel

  19. Offline

    iWesley23

    Ok, thanks!

    Actually, it works, but it still gives you the items. What's wrong. here's my code.

    http://pastebin.com/Yb4WyXHj

    EDIT: Fixed the problem! Thanks for the help guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page