Solved Clearing Inventory Leaves Ghost Items

Discussion in 'Plugin Development' started by robbo5899, Feb 7, 2014.

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

    robbo5899

    I'm making a plugin so that when a player right clicks certain blocks in their inventory it executes code, specifically adding them to a team and moving them onto the next step, the next step requires their inventory to be cleared however when I clear it it leaves ghost items that don't go until interacted with.

    Code:java
    1. @EventHandler
    2. public void PlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))){
    5. if(player.getItemInHand().getType().toString() == "DIAMOND_BLOCK"){
    6. Team.addToTeam(TeamType.BLUE, player);
    7. player.getInventory().clear();
    8. } else if(player.getItemInHand().getType().toString() == "REDSTONE_BLOCK"){
    9. Team.addToTeam(TeamType.RED, player);
    10. player.getInventory().clear();
    11. } else if(player.getItemInHand().getType().toString() == "EMERALD_BLOCK"){
    12. Team.addToTeam(TeamType.GREEN, player);
    13. player.getInventory().clear();
    14. } else if(player.getItemInHand().getType().toString() == "GOLD_BLOCK"){
    15. Team.addToTeam(TeamType.YELLOW, player);
    16. player.getInventory().clear();
    17.  
    18. }
    19. }


    Any help/advice you can offer is greatly appreciated :)
     
  2. Offline

    HungerCraftNL

    Code:java
    1. @EventHandler
    2. public void PlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))){
    5. if(player.getItemInHand().getType().toString() == "DIAMOND_BLOCK"){
    6. Team.addToTeam(TeamType.BLUE, player);
    7. player.getInventory().clear();
    8. }
    9. if(player.getItemInHand().getType().toString() == "REDSTONE_BLOCK"){
    10. Team.addToTeam(TeamType.RED, player);
    11. player.getInventory().clear();
    12. }
    13. if(player.getItemInHand().getType().toString() == "EMERALD_BLOCK"){
    14. Team.addToTeam(TeamType.GREEN, player);
    15. player.getInventory().clear();
    16. }
    17. if(player.getItemInHand().getType().toString() == "GOLD_BLOCK"){
    18. Team.addToTeam(TeamType.YELLOW, player);
    19. player.getInventory().clear();
    20. }
    21. }
     
  3. Offline

    robbo5899


    Didn't make any difference :(
     
  4. Offline

    Garris0n

    You should use .getType().equals(Material.DIAMOND_BLOCK) etc.
     
  5. Offline

    SenseTheGod

    Maybe you need to add a little
    Code:java
    1.  
    2. player.updateInventory();
    3.  
     
  6. Offline

    robbo5899

    Garris0n
    The code still executed, this doesn't change it, is it better practice to use .equals()?

    SenseTheGod
    Thank you, this fixed it :D
     
  7. Offline

    Garris0n

    Yes, it is better practice. Also, I'm surprised it worked at all seeing as you're comparing strings with ==.
     
  8. Offline

    SenseTheGod

    Haha, Your the first person i ever helped! Your welcome man <3
     
  9. Offline

    Cirno

    Might be a necrobump/pointless information, but I think toString would return the same string object because of the string pool in the JVM.
     
  10. Offline

    robbo5899

    Cirno toString() makes it return "GOLD_BLOCK" rather than "Material.GOLD_BLOCK", I use it for a different purpose later on, just keeping my code all the same :)
     
  11. For future reference, using toString() on items doesn't always return their Material name. For instance, SHOVEL returns as SPADE.
     
  12. Offline

    unrealdesign

    But would valueOf("SPADE") and valueOf("SHOVEL") both return the correct thing?
     
  13. unrealdesign Can't say for sure, as I've never tried it. I just used the strings for a custom enchantments plugin at one time.
     
Thread Status:
Not open for further replies.

Share This Page