A few questions about methods and commands

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, May 8, 2013.

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

    CRAZYxMUNK3Y

    I have a few questions about using methods in another class in my commands and getting blocks during a command.

    1. I currently have a basic onCommand with this in it and was wondering how i could get the player that used the command in another class and send a message to them using the showHelp() method.
    Code:
    if(args.length == 1){
                        if(args[1].equalsIgnoreCase("lockdoor")){
                            lockDoor();
                        }
                    }else if(args.length > 1){
                        player.sendMessage(ChatColor.RED + "[DoorLock] Too many arguments! use /doorlock for information");
                    }else{
                        showHelp();
                    }
    
    2. How would i go about getting a block being hit after a command was issued? What i am trying to do is;
    > Command is used
    > Player hits block
    > Players name and block location is saved to file

    I was thinking something along these lines,
    Code:
    if(args[1].equalsIgnoreCase("lockdoor")){
                            player.sendMessage("Please hit a door");
                            if(/*player hits door, do something*/){
                                lockDoor();
                            }
                        }
    
    Just ask if you have any questions.
     
  2. Offline

    savagesun

    Well.. maybe have a static HashMap of player names and booleans that hold the information you get.
    Code:
    public static Map<String, Boolean> playerMap = new HashMap<String, Boolean>();
    Check whenever someone joins the server and add their name to the map with the boolean set to false. Then in your command get the name of the sender (assuming it's a player) use it to change the boolean value to true, and when you're checking for block interactions check the map with the player's name to see if they have used the command.

    edit: As for saving the name and block data, just use some kind of file stream to write the name and block data in the interaction event, assuming that they have used the command previously.
     
  3. Offline

    CubieX

    I would use a HashSet instead (no need for a separate boolean here) and only add the players name to it, after he used the command.
    Then do your actions when the player hits a block while beeing on the list and delete him afterwards.
    You should also delete him, when he disconnects.

    Saving can be done with the Bukkit config api (-> Google) by using the statdard config.yml or a separate yml file.

    (I would also recommmend not to make your HashSet static. Make it a field and pass it by reference if needed.)
     
Thread Status:
Not open for further replies.

Share This Page