I need help with only one time use a command

Discussion in 'Plugin Development' started by MrSliping, Nov 8, 2015.

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

    MrSliping

    Hello,

    I need help, I are making a plugin like a /gift for my server but I want a code for that the command only can be used one time, you can help me please?. I are newbie in java programming. Thank you.
    This is my full code:

    Code:
    package com.gmail.joseitoinfos.uranuscraft;
    
    
    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 UranusCraft extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getLogger().info("onEnable se ejecuto correctamente!");
            System.out.print("Plugin de UranusCraft cargado!");
        }
        @Override
        public void onDisable() {
            getLogger().info("onDisable se ejecuto correctamente!");
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            PlayerInventory inv = player.getInventory();
            if(cmd.getName().equalsIgnoreCase("regalo")){//Si el jugador ha escrito /basic entonces haga lo siguiente...;
                if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Este comando sólo puede ser ejecutado por un jugador.");
                }
                player.sendMessage(ChatColor.GREEN + "Haz recibido tu regalo, disfrutalo");
                ItemStack exp = new ItemStack(Material.EXP_BOTTLE, 64);
                ItemStack vaca = new ItemStack(Material.MONSTER_EGG, 2, (short) 92);
                inv.addItem(exp);
                inv.addItem(vaca);
                return true;
        }
            return false;
        }
    }
     
    Last edited: Nov 8, 2015
  2. Offline

    TheNewTao

    @MrSliping

    Create a really long cooldown, like a couple years. Use System.currentTimeMillis() and HashMaps.
     
  3. Offline

    Binner_Done

    You can probably use scoreboard objective points, when /gift is activated, set the score of objective gift to one, at the if(cmd.getName().equalsIgnoreCase("gift"){}
    You check if the score of gift is 1, then else it to your main command
     
  4. Offline

    MrSliping

    You can send me the code? I know more or less what did you said but I don't know how implement that.
    Thank you.
     
  5. Offline

    Reynergodoy

    As for the one time command, you should add a check from PlayerJoinEvent:
    > if (!p.hasPermission("First.Join") {
    // Add permission for player ( may require permissionsEX or groupmanager to make it easier for you )
    // Add permission "Wish.OneTime"
    }
    _______________________________________________________________________
    On the command put
    > if (p.hasPermission("Wish.OneTime") {
    // Do stuff
    // Remove permission "Wish.OneTime"
    }
    _______________________________________________________________________
    Note that First.Join will remain, so when a player joins again he won't receive "Wish.OneTime" again
     
    Last edited by a moderator: Nov 8, 2015
  6. Offline

    Scimiguy

    @MrSliping
    IF you want code written for you, submit a Plugin Request
    We are not here to write code for you.

    Both of the above options are downright awful.

    You should either store the players that have used their wish in a file, or use Permissions as @Reynergodoy has suggested.



    @Reynergodoy
    This is an English forum. This is also a friendly forum.
    These people have taken the time and effort to give their best English explanation, the least you can do is accommodate their request and ignore the fact that they don't speak YOUR language.
    The world is a big place, not everyone needs to do things the way you do.

    Re-directing to an "English tutorial" is not helping anyone, and is downright rude, to be honest.
     
  7. Offline

    MrSliping

    Thanks for your help. I said you give me the code because if you said anything what I don't know I will be confused.
    I will try you reply Reynergodoy.
    So can someone else help me?
     
  8. Offline

    Reynergodoy

    as I said the UPPER CASE things aren't shouts, just to focus attention, everything is "educational" I wrote it wrong to point where he wrote wrong, but now that i'm re-reading, it sounds quite rude, I will edit this
    And also link him to english tutorials, a good english means a good java
    http://livemocha.com
    or
    http://livemocha.com/pages/languages/ingles-2/?lang=es
    (It's free... I hope)
     
  9. Offline

    Xerox262

    No developer is gonna give you their code when it comes to this, especially because this is basic Java, what you want to do is make a set of all players that have used their command, then save it on disable and load it on enable.
    Can you not tell what it was by looking at it? Even with no knowledge of that language I can understand what it means. I don't even know what language it is.
     
  10. Offline

    Scimiguy

    @MrSliping
    Keep a list of players who have used their Wish in a Config file
    When the plugin loads (the onEnable() event), withdraw the list from your config
    and save it back into it when the plugin shuts down (onDisable())

    When you use your command, add the player to the list with yourList.add(Player#getUniqueId())
     
  11. Offline

    MrSliping

    I think i understood a little, but how do i do so when i let them use the command again it doesn't let them using the player#uniqueid
     
  12. Offline

    Scimiguy

    check if the list contains their UUID, if it does, then don't let them

    if (!list.contains(UUID)) {}
     
  13. Offline

    nbrandwine

  14. Offline

    MrSliping

    I do this.. The players who used the command are in the config.yml but still can use the command, here is the code.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            String playerName = player.getName();
            List<String> regalos = new ArrayList<String>();
            regalos.addAll(getConfig().getStringList("Configuracion.Usuarios"));
            PlayerInventory inv = player.getInventory();
            if(cmd.getName().equalsIgnoreCase("regalo") && (regalos.contains(playerName))) {
                  player.sendMessage(ChatColor.RED + "Ya haz recibido tu regalo");
                  } else {
                    regalos.add(playerName);
                    getConfig().set("Configuracion.Usuario", regalos);
                    saveConfig();
                    player.sendMessage(ChatColor.GREEN + "Haz recibido tu regalo, disfrutalo");
                    ItemStack exp = new ItemStack(Material.EXP_BOTTLE, 64);
                    ItemStack vaca = new ItemStack(Material.MONSTER_EGG, 2, (short) 92);
                    inv.addItem(exp);
                    inv.addItem(vaca);
                  }
                return true;
        }
    }
     
  15. Offline

    Scimiguy

    DON'T set all those variables BEFORE you check your command, you're just wasting time and memory.

    Ugh.. there's so many things wrong with this code.. I don't even want to explain it
    Can't you just submit a plugin request for this?

    Sigh...:
    1. Check if the command is your command before you store all your needed variables
    2. Don't store useless variables (player, exp, vaca?)
    3. Get your configuration list before someone uses the command.. that way you don't need to keep grabbing it every single time.
    4. your cmd.getName() check should be separate to your regalos.contains() check.
    5. Don't store player names, store UUIDs. Player Names can change, UUIDs can't
    6. Space your braces properly
    sigh..
     
  16. What if you just create a hash map before your onCommand method, then before anything else after checking the command label, test to see if the senders uuid is in the hashmap, and if it's not just do everything you want to and at the end of doing everything, add the players uuid to the hashmap.
     
  17. Offline

    mcdorli

    First off, I have a problem with the "testing the commandLabel", use the cmd variable instead, it contains every commandaliases. Second, I think this was mentioned before.
     
  18. Why do you need a hash map?
    Just use a list.
    Code:
    List<String> regalos = new ArrayList<String>();
       regalos.addAll(getConfig().getStringList("Configuracion.Usuarios"));
    change it to
    Code:
    List<String> regalos = getConfig().getStringList("Confriguracion.Usarios);
    Then try to split up the contains thing if it still doesnt work.
    If that wont work too, then just use a for loop, but that is more complicated.
     
  19. Offline

    MrSliping

    Still won't working.
    Here is the code:
    Code:
    public class UranusCraft extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getLogger().info("onEnable se ejecuto correctamente!");
            loadConfiguration();
            System.out.print("Plugin de UranusCraft cargado!");
        }
        @Override
        public void onDisable() {
            getLogger().info("onDisable se ejecuto correctamente!");
            saveConfig();
        }
       
        public void loadConfiguration(){
            getConfig().options().copyDefaults(true); // NOTE: You do not have to use "plugin." if the class extends the java plugin
            //Save the config whenever you manipulate it
            saveConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            String p = player.getName();
            List<String> regalos = getConfig().getStringList("Cofiguracion.Usuarios");
            PlayerInventory inv = player.getInventory();
            if(cmd.getName().equalsIgnoreCase("regalo")){
                if (regalos.contains(player.getName())) {
                player.sendMessage(ChatColor.RED + "Ya haz recibido tu regalo");
            }
                else {
                regalos.add(p);
                getConfig().set("Configuracion.Usuario", regalos);
                saveConfig();
                player.sendMessage(ChatColor.GREEN + "Haz recibido tu regalo, disfrutalo");
                ItemStack exp = new ItemStack(Material.EXP_BOTTLE, 64);
                ItemStack vaca = new ItemStack(Material.MONSTER_EGG, 2, (short) 92);
                inv.addItem(exp);
                inv.addItem(vaca);
            }
            return true;
            }
        return false;
    }
    }
    Any help? Thanks.
     
  20. Make the list outside of your Command Method @MrSliping and add all the peoples UUID's in the list on enable.
     
    Last edited: Nov 9, 2015
  21. Offline

    Scimiguy

  22. Offline

    Reynergodoy

    My message sounds quite confuse I know(I was in a hurry), but heshould know english, because he does not understand a letter of what we say nor java, a simple problem that could be solved in 5 or 6 lines
     
  23. Offline

    MrSliping

    I know more or less english, but the english I think not is the problem here..
    I are here because I need help with a code, I don't know what doing, the players is added in the config file but stills can use the command.
    Any help?
    Thanks.
     
  24. Offline

    Scimiguy

    Read back through the existing replies
    Youve been given the answer several times now

    Make a list of player uuids

    When you do your wish, store the players uuid in the list
    When they try to do it again, check if they are in the list and stop them if they are
     
  25. Offline

    MrSliping

    So how I make a list of players UUID? I tried change:
    Code:
    String p = player.getName();
    for:

    Code:
    UUID p = player.getUniqueId();
    but in that line I got the error:
    Code:
    regalos.add(p);
    Here is the full code:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            UUID p = player.getUniqueId();
            List<String> regalos = getConfig().getStringList("Cofiguracion.Usuarios");
            PlayerInventory inv = player.getInventory();
            if(cmd.getName().equalsIgnoreCase("regalo")){
                if (regalos.contains(p)) {
                player.sendMessage(ChatColor.RED + "Ya haz recibido tu regalo");
                }
                else {
                regalos.add(p);
                getConfig().set("Configuracion.Usuario", regalos);
                saveConfig();
                player.sendMessage(ChatColor.GREEN + "Haz recibido tu regalo, disfrutalo");
                ItemStack exp = new ItemStack(Material.EXP_BOTTLE, 64);
                ItemStack vaca = new ItemStack(Material.MONSTER_EGG, 2, (short) 92);
                inv.addItem(exp);
                inv.addItem(vaca);
            }
            return true;
            }
        return false;
        }
    Any help?
    Thanks!
     
  26. Offline

    Zombie_Striker

    @MrSliping
    At this point, I will tell you two things:
    1. Coding requires thinking. Why does one thing work, but not the other?
    2. Coding requires knowledge. You need to understand what you're actually putting down. The thing inside those signs "<>" is what type of object you are storing. You are currently trying to Store strings because it looks like "<String>".

    Use 1. and 2. to figure out how to turn that List (Which really should be a Set) into a UUID.
     
  27. Offline

    MrSliping

    Ok, I do that but when the player execute the command his UUID is not added to the config.
    So still can use the command.
    Here is the code:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            UUID p = player.getUniqueId();
            List<String> list = getConfig().getStringList("UUIDs");
            List<UUID> uuids = new ArrayList<UUID>();
            PlayerInventory inv = player.getInventory();
            if(cmd.getName().equalsIgnoreCase("regalo")){
                if (list.contains(p)){
                player.sendMessage(ChatColor.RED + "Ya haz recibido tu regalo");
                }
                else
                for (String string: list)
                uuids.add(UUID.fromString(string));
                getConfig().set("UUIDs", list);
                saveConfig();
                player.sendMessage(ChatColor.GREEN + "Haz recibido tu regalo, disfrutalo");
                ItemStack exp = new ItemStack(Material.EXP_BOTTLE, 64);
                ItemStack vaca = new ItemStack(Material.MONSTER_EGG, 2, (short) 92);
                inv.addItem(exp);
                inv.addItem(vaca);
                }
            return true;
        }
    }
    Any help?
    Thanks!.
     
  28. Offline

    Scimiguy

    Because you're not storing uuids?
     
  29. Offline

    Zombie_Striker

    @MrSliping
    There is something so wrong about this. 1 is that you still have the list, 2. that it's using a string to store uuids and 3. YOU HAVE A SEPARATE LIST THAT STORES UUIDS,,BUT YOU"RE NOT EVEN LOADING THE UUIDS THERE!!!
     
  30. Offline

    MrSliping

    I don't know, I need remove any list or something else?
    I are keeping some steps what I find.
    I are newbie in java programming.
    Can you tell me what I need doing?
    What I need modify?
    According the steps what I did follow that code is supposedly correct, but as I said, I'm newbie in java.
    Sorry..

    You can be more explicative?
    Thanks.
     
    Last edited: Nov 9, 2015
Thread Status:
Not open for further replies.

Share This Page