CMD once per life

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

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

    EvilKittyCat123

    Okay, so I have made the command /play on kitpvp server. This will teleport players to the arena dropzone. But people can quickly type this command when they are low on health. So I would like to know how I set this command to only work once per life. How do I do that?
     
  2. Offline

    Welite

    Save player name to some String list in config and make a condition if the list contains players name it will cancel the command.
     
  3. Offline

    transcend_

    Create a map or list. Than whenever they type the command check if they're in that map or list, if they are return and stop the code, if something else add them to the list or map and execute your code. Than on player death take them off that list
     
  4. Offline

    unforgiven5232

    I know a easy way of doing this without config, if u can wait an hour or so I can write it up for u
     
  5. Offline

    EvilKittyCat123

    Welite
    I'm not that good at making bukkit plugins..
    transcend_
    I'm not that good at making bukkit plugins..

    unforgiven5232
    Thank you so much :)
    I'll study and copy the code so I can learn from it to use it the next time.
     
  6. Offline

    Dahwn

    EvilKittyCat123
    Simply put the player into a list if he types the command and use a PlayerDeathEvent listener to remove him from the list when he dies!

    -Dahwn
     
  7. Offline

    mazentheamazin

    EvilKittyCat123
    Programmer Rule 1. Forget the phrase Copy and Paste
    Regarding your question, what Welite and transcend_ are talking about have no reference the Bukkit API, but core Java. API's should not be touched until understanding the whole language, learning Bukkit before learning Java can cause you to become reliant on some methods of the API, and if looking to code Java outside of Bukkit, can lead you to be lost.
     
    xTigerRebornx likes this.
  8. Offline

    MrInspector

    Well for that, if I'm writing the same code just somewhere else, I just copy and paste it from the place where I did it originally to save time (If i'm working a project for my self or for fun, I usually don't, I usually do it when I'm put on a timer and I need to get a plugin done quick for someone), so it's not entirely true if you want a quicker plugin. :p
     
  9. Offline

    mazentheamazin

    MrInspector
    Well, if I gave you code like this:
    Code:java
    1. if(foo) {
    2. dobar();
    3. }
    4.  
    5. public void dobar() {
    6. System.out.println("Omg such foo. very bar. wow.");
    7. }

    Lets just say you don't understand it, and you copy and paste it, you may not understand what dobar() does, this is why normally when I give out code, I comment everything so they can understand every single part of what they did, not just took code that somebody else wrote for them. With this being said, they can now be able to remember these concepts and use them in other plugins, other than copy and paste again.
     
  10. Offline

    EvilKittyCat123

    I like transcend's idea. But how do I do it?
     
  11. Offline

    idontcare1025

    Have a list of players. On the death event, if a player isn't added, add them, and on the send command event, remove them if they're on it, or cancel the event if they're not.
     
  12. Offline

    EvilKittyCat123

    idontcare1025
    Yeah, but as I said, I have no idea how to do that. I just want a simple bukkit plugin. Could anyone give me the code for it?
     
  13. Code:java
    1. @EventHandler
    2. public void onDeathEvent(PlayerDeathEvent event) {
    3. Player p = event.getPlayer();
    4. if (p!=null && p instanceof Player) {
    5. getConfig().set("death.player." + p, true);
    6. }
    7. }


    I've not tested it, but you just want to set death.player.(playername) to true one they die, and false when they join the arena.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  14. Offline

    Konkz

    PHP:
    private ArrayList<Stringlifecmd = new ArrayList<String>(); // Making an array list, containing string, which we will use to store player. As storing a player object is bad!
     
    public boolean onCommand(CommandSender senderCommand commandString aliasString[] args) {
     
    Player p = (Playersender// Normal command structure, making the Sender into player.
     
     
    if (alias.equalsIgnoreCase("mycmd")) { // If player makes command /mycmd it will
      
    lifecmd.add(p.getName()); //add the sender to the arraylist, as we store string we need to   get their name
      
    //using p.getName();
    }
     
    @
    EventHandler
    public void onDeath(PlayerDeathEvent event) { //On death we remove the
     
    Player p e.getPlayer(); //player from the arraylist, after first checking
     
    if (lifecmd.contains(p.getName()) { //if they are inside of it. This should help us prevent
      
    lifecmd.remove(p.getName()); //null pointer exceptions.
     
    }
    }
    This is a very skim outline, you'd have to check if executor is an instanceof player etc.
    You should be able to work this out from here! Else I suggest watching some YouTube videos.
     
    TigerHix likes this.
  15. Offline

    TigerHix

    Add a field to your main class.

    Code:java
    1. public static List<UUID> USED_PLAYERS = new ArrayList<UUID>();


    And, add following code into the method onCommand() (I suppose you use onCommand() to detect player's commands). "Plugin" is the name of your main class. "player" is the one who typed the command.

    Code:java
    1. if (Plugin.USED_PLAYERS.contains(player.getUniqueId())) {
    2. player.sendMessage("You have already used this command. Go suicide so you can use it again.");
    3. } else {
    4. // Do command stuff
    5. player.sendMessage("You have used this command and you won't be able to use it later unless you die.");
    6. Plugin.USED_PLAYERS.add(player.getUniqueId());
    7. }


    Of course we need to remove the UUID from the list, when a player dies.

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDeath(PlayerDeathEvent event) {
    4. if (Plugin.USED_PLAYERS.contains(event.getEntity().getUniqueId())) {
    5. Plugin.USED_PLAYERS.remove(event.getEntity().getUniqueId());
    6. }
    7. }
    8.  


    I think this is self-explanatory, but if you have any questions feel free to ask me. :)




    EDIT: Oh.. theres already a same reply above me.. :(
     
  16. Offline

    Konkz


    I really have to start using UUID :p
    I mostly only get to use it once, when I retrieve the UUID as then the API I use for the minigame server converts that into a player name so nothing changes really.

    Plus side of making server-based minigames (Y)
     
Thread Status:
Not open for further replies.

Share This Page