Solved Blocks not saving to list

Discussion in 'Plugin Development' started by JjPwN1, Sep 1, 2014.

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

    JjPwN1

    Code:java
    1. List<Block> blocks = new ArrayList<Block>();
    2. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    3. if(event.getClickedBlock() != null){
    4. blocks.add(event.getClickedBlock());
    5. pzplayer.sendMessage("Selected block " + event.getClickedBlock().getType().toString() + " at x" + event.getClickedBlock().getLocation().getX() +
    6. " y" + event.getClickedBlock().getLocation().getY() + " z" + event.getClickedBlock().getLocation().getZ());
    7. pzplayer.sendMessage("You have selected a block! (" + gold + blocks.size() + yellow + ")");
    8. }
    9. }


    When you right click a block, it's supposed to save that block to the list, but it just doesn't want to. Even after right-clicking five blocks, it will still send the message, "You have selected a block! (1)," meaning the list's size is not increasing. I'm stuck here, any help is appreciated. I can post more code if requested.
     
  2. Offline

    Ivan

    First of all you should never store the Block object since it's just a placeholder and is subject to change at any time. You have to save the locations instead. Otherwise you're going to end up with some serious troubles.
     
    Datdenkikniet likes this.
  3. JjPwN1 Your list is a local variable, not a field. This means that it's discarded at the end of the method. I suggest you learn the basics of Java before making plugins. I recommend the Oracle tutorials and/or a Java book
     
    JjPwN1 likes this.
  4. Offline

    JjPwN1


    True, and I did switch it from saving blocks to saving locations but it did not fix anything. I thought it would be easier to keep it as blocks due to what I'm doing with the list, but no matter.

    Code:java
    1. List<Location> blocks = new ArrayList<Location>();
    2. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    3. if(event.getClickedBlock() != null){
    4. blocks.add(event.getClickedBlock().getLocation());
    5. pzplayer.sendMessage("Selected block " + event.getClickedBlock().getType().toString() + " at x" + event.getClickedBlock().getLocation().getX() +
    6. " y" + event.getClickedBlock().getLocation().getY() + " z" + event.getClickedBlock().getLocation().getZ());
    7. pzplayer.sendMessage("You have selected a block! (" + gold + blocks.size() + yellow + ")");
    8. }
    9. }


    Just tested it in-game once more to see if anything changed, but it still does not save the locations.

    Ahhh, you're correct. It's always a silly mistake that'll cause the biggest grief.

    On that last comment however, I'll just let it slide.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. JjPwN1 Let it slide if you want but I stand by it :)
     
Thread Status:
Not open for further replies.

Share This Page