Solved Command + Event

Discussion in 'Plugin Development' started by willy00, Dec 9, 2013.

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

    willy00

    Hi, im making a plugin where you can open bookshelves. I have a command where if you type "/display on" while looking at a bookshelf, if a player clicks on a book inside the bookshelf, they will get a copy of it in their inventory.
    The problem is it doesnt like mixing commands with events!
    Here is my code:
    Code:java
    1. private Player sender;
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. return false;
    5.  
    6. }
    7.  
    8. Player player = (Player) sender;
    9.  
    10. Inventory bookshelf = player.getServer().createInventory(null, 9, ChatColor.GRAY + "BookShelf");
    11.  
    12. if (args[0].equalsIgnoreCase("display on")) {
    13. if (player.hasPermission("player.display")) {
    14. if (player.getTargetBlock(null, 5).getType() == Material.BOOKSHELF) {
    15.  
    16. public void onClickInventory(InventoryClickEvent event1) {
    17. HumanEntity player1 = event1.getWhoClicked();
    18.  
    19. Inventory bookshelf1 = player.getServer().createInventory(null, 9, ChatColor.GRAY + "BookShelf");
    20.  
    21. if (player1.getInventory() == (bookshelf1)) {
    22. event1.setResult(Result.DENY);
    23. if (event1.getCurrentItem().getType() == Material.BOOK) {
    24. Material i = event1.getCurrentItem().getType();
    25. player1.getInventory().addItem(Material(i));
    26. player1.closeInventory();
    27. return true;


    Please Help!
     
  2. Offline

    Rocoty

    On command store data some place. On event read the data and clear
     
  3. Offline

    Wolfey

    You can't put 2 words when checking arguments like you're doing on line 12.
    Code:java
    1.  
    2. if (args[0].equalsIgnoreCase("display on")) {
    3.  

    And oh, by the way, you can't make an event inside a command. lol
     
  4. Offline

    willy00

    not sure how to do that. could you show me?

    thankyou and i know. i was just showing what i needed. I realised that when i made the mistake but wasnt sure whati could do. thanks anyway! :D

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

    drtshock

    Wait, why are you trying to put an event method instead of a command method? Store things in an object in the class in your command and then check them in your event method.
     
    chaseoes likes this.
  6. Offline

    MrInspector

    willy00 you can't put a event inside a command as people have already said ( too late :( ) :p

    I remember a year ago I did the same exact thing though :p
     
  7. Offline

    willy00

    yes. but could some1 please just give me an example?
     
  8. Offline

    drtshock

    I'm not going to write code for you. If you need help with how to write methods then I suggest looking at some java tutorials :\
     
    L33m4n123 and chaseoes like this.
  9. Offline

    willy00

    Could some1 please just show me how to do this?

    anyone, please?!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  10. Online

    timtower Administrator Administrator Moderator

    How about a per player toggle instead of putting functions in functions?
     
  11. Offline

    willy00

    how do you do that? could you show me some code so i can learn?
     
  12. Online

    timtower Administrator Administrator Moderator

    Probably the closest that you can get
    Code:java
    1. List<String> list = new ArrayList<String>();
    2. Inventory customInv = Bukkit.getServer().createInventory(null, 9, ChatColor.GRAY + "BookShelf");
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if(!(sender instanceof Player)) {
    5. sender.sendMessage("Nope");
    6. return true;
    7. }
    8. Player player = (Player) sender;
    9. if(args.length==0){
    10. sender.sendMessage("No arguments");
    11. return true;
    12. }
    13. if(args[0].equalsIgnoreCase("on")){
    14. if (!(player.hasPermission("player.display") && player.getTargetBlock(null, 5).getType() == Material.BOOKSHELF)) {
    15. return true;
    16. }
    17. if(!list.contains(player.getName())){
    18. list.add(player.getName());
    19. player.openInventory(customInv);
    20. }
    21. return true;
    22. }
    23. return false;
    24. }
    25. public void onClickInventory(InventoryClickEvent event) {
    26. Player player = (Player) event.getWhoClicked();
    27. if(list.contains(player.getName())){
    28. if (ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("BookShelf")) {
    29. event.setResult(Result.DENY);
    30. if (event.getCurrentItem().getType() == Material.BOOK) {
    31. player.getInventory().addItem(event.getCurrentItem().clone());
    32. player.closeInventory();
    33. }
    34. }
    35. }
    36. }
     
  13. Offline

    willy00

    thankyou so much!!!!!!!!!!!!!
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page