Check if a player is in a config? (PlayerInteractEvent)

Discussion in 'Plugin Development' started by russjr08, May 26, 2012.

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

    russjr08

    Hi! I want to check if a player is in the config file before doing something in the PlayerInteractEvent. I was thinking of something like..

    Code:Java
    1.  
    2. for(player : configPlayers){
    3. if(player.equals(configPlayers)
    4. {
    5. Do that code here
    6. }
    7. }
    8.  


    Apparently I can't convert a string to a player though. Anyone know an approach on how I should do this?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    you should convert the player into a string. specifically their name.
     
  3. Offline

    russjr08

    Okay, I've gotten the actual player name as a string, but something in my for loop isn't right... Either that or I'm completely wrong with what I should be doing

    Code:Java
    1.  
    2.  
    3. public void clearChest(Block block, Player player) {
    4. List<String> configPlayers = plugin.getConfig().getStringList(
    5. "Punishment.Players");
    6.  
    7. // String playerName = player.getName();
    8.  
    9. for (String playerName : configPlayers) {
    10. playerName = player.getName();
    11. if (playerName.equals(configPlayers)) {
    12. Chest chest = (Chest) block.getState();
    13. chest.getInventory().clear();
    14. }
    15. }
    16.  
    17. }
    18.  
    19.  


    I've added my name to the config, however, the chest doesn't clear when I open it. The chest will clear if I take out the requirement about being in the config, so its something to do with the if/for statement.
     
  4. configPlayers.contanis(playername) should be enough, print some debug messages to see which code works and which doesn't.
     
  5. Offline

    Njol

    This part of your code makes no sense at all. You iterate over all player names in the list, then set the currently iterated player to the player you want to check (so basically you void the loop). Then you check if the player's name is equal to the whole list of player names, which obviously always returns false.
     
  6. Offline

    russjr08

    You're right. What I was wanting to do was it loop through each player in the config file (Mapped to an array) and check if the player name was equal to that.

    However, now I'm trying to print statements to verify where the code has been executed, its like the event freezes :confused:

    Code:Java
    1.  
    2. List<String> configPlayers = plugin.getConfig().getStringList(
    3. "Punishment.Players");
    4. System.out.println(configPlayers.toString() + "<-- Should have printed list of players in config");
    5.  


    Non of that is printed. In the beginning of the event I have a statement to print "Event Received" and I get that in the console. After that though it seems like nothing happens.
     
  7. Since when does List's toString() print the values ? AFAIK it doesn't.

    Maybe your node is failing because you're using capital letters in your nodes names, but I kinda doubt that's the case.

    And if your event "freezes" you should post the event code then.
     
  8. Offline

    russjr08

    Event:

    Code:Java
    1.  
    2. @EventHandler
    3. public void EntityInteract(PlayerInteractEvent event) {
    4. System.out.println("Received Event");
    5. Player player = event.getPlayer();
    6. Block block = event.getClickedBlock();
    7.  
    8. List<String> configPlayers = plugin.getConfig().getStringList(
    9. "Punishment.Players");
    10.  
    11. String configPlayersList = configPlayers.toString();
    12. System.out.println(configPlayersList + "<-- Should have printed list of players in config");
    13.  
    14. if (block.getState() instanceof Chest) {
    15. // Location location = event.getPlayer().getLocation();
    16. // System.out.println(location);
    17. // BlockState chest = location.getBlock().getState();
    18.  
    19. // Material chest = block.getState().getType();
    20.  
    21. // Chest chest = (Chest)block.getState();
    22. // chest.getInventory().clear();
    23. // clearChest(block, player);
    24.  
    25.  
    26.  
    27. String playerName = player.getName();
    28. System.out.println(configPlayers.toString());
    29.  
    30. if (configPlayers.contains(playerName)) {
    31. Chest chest = (Chest) block.getState();
    32. chest.getInventory().clear();
    33. System.out.println("Contained player name: " + playerName);
    34.  
    35. } else {
    36. System.out.println("Did not contain player name: " + playerName);
    37. }
    38.  
    39. }
    40.  
    41. }


    clearChest method:

    Code:Java
    1.  
    2. public void clearChest(Block block, Player player) {
    3. List<String> configPlayers = plugin.getConfig().getStringList(
    4. "Punishment.Players");
    5.  
    6. String playerName = player.getName();
    7.  
    8. /*
    9.   * for (String playerName : configPlayers) { playerName =
    10.   * player.getName(); if (playerName.equals(configPlayers)) { Chest chest
    11.   * = (Chest) block.getState(); chest.getInventory().clear(); } }
    12.   */
    13.  
    14. // TODO Get this to work
    15. //Chest chest = (Chest) block.getState();
    16. //chest.getInventory().clear();
    17. if (configPlayers.contains(playerName)) {
    18. Chest chest = (Chest) block.getState();
    19. chest.getInventory().clear();
    20. System.out.println("Contained player name: " + playerName);
    21.  
    22. } else {
    23. System.out.println("Did not contain player name: " + playerName);
    24. }
    25.  
    26. }


    I don't know why the event is just "Pausing".
     
Thread Status:
Not open for further replies.

Share This Page