Solved How to check if an player's helmet is an IronBlock?

Discussion in 'Plugin Development' started by jusjus112, Mar 28, 2014.

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

    jusjus112

    I have an problem to check if the player's helmet is an Iron Block. When i try this, it works, but when they have nothing as an helmet, the code stops where it is air!
    Here is my code:
    Code:java
    1. if (!(player.getInventory().getHelmet().getType() == Material.IRON_BLOCK)) {
    2. // I have try some other ways!
    3.  
    4. if (!(player.getInventory().getHelmet().getType().equals(Material.IRON_BLOCK))) {
    5.  
     
  2. Offline

    2MBKindiegames

    Why is there an exlamation mark? What do you want to do?
     
  3. Offline

    Vandrake

    make sure the getHelmet is not null and check if the helmet type is not air, then check if its iron block
     
  4. Offline

    2MBKindiegames

    It should be something like this:
    Code:java
    1. if (player.getInventory().getHelmet() != null) {
    2. if (player.getInventory().getHelmet().getType() != Material.AIR) {
    3. if (player.getInventory().getHelmet().getType() == Material.IRON_BLOCK) {
    4. //Do things
    5. }
    6. }
    7. }
     
  5. Offline

    jusjus112

    2MBKindiegames
    I have now this, with something from you code:
    Code:java
    1. @EventHandler
    2. public void IRON_BLOCK(InventoryClickEvent event) {
    3. Player player = (Player) event.getWhoClicked();
    4. ItemStack clicked = event.getCurrentItem();
    5. Inventory inventory = event.getInventory();
    6. Random random = new Random();
    7. int randomString = random.nextInt(2);
    8. if (inventory.getName().equals(Menu.hatshop.getName())) {
    9. if (clicked.getType() == Material.IRON_BLOCK) {
    10. event.setCancelled(true);
    11. player.closeInventory();
    12. if (player.getInventory().getHelmet() != null) {
    13. if (player.getInventory().getHelmet().getType() != Material.AIR) {
    14. if (player.getInventory().getHelmet().getType() == Material.IRON_BLOCK) { //
    15. if(FlyingBlocksPlugin.playerPoints.getAPI().take(player.getName(), 200)) {
    16. player.getInventory().setHelmet(new ItemStack(Material.IRON_BLOCK));
    17. if (randomString == 1) {
    18. player.sendMessage(MsgType.ACCESOIRIES + "I cant figure out that you buying an IRON BLOCK as an head???");
    19. }else {
    20. player.sendMessage(MsgType.ACCESOIRIES + "Tell me what the point is of buying an IRON BLOCK as head..");
    21. }
    22. } else {
    23. player.sendMessage(MsgType.ACCESOIRIES + "Hmm... I think you must check your points before buying this item!");
    24. }
    25. } else {
    26. player.sendMessage(MsgType.ACCESOIRIES + "You have already buyt this item.... -,-");
    27. }
    28. }
    29. }
    30. }
    31. }
    32. }

    But it dont seem to work!
     
  6. Offline

    Vandrake

    did you try to update inventory? After everything
     
  7. Offline

    jusjus112

    Vandrake
    But it dont set the item on my head! Only if i have the ironblock in my head it says that im already have it! But if it is empty, it dont do anything!
     
  8. Offline

    Vandrake

    Ofcourse... Think :/ Your code will only run if your helmet is an iron block.
     
  9. Offline

    jusjus112

    Vandrake
    My code looks now:
    This is when the player dont have an ironblock on his head! But i dont do anything! I dont understand
    Code:java
    1. @EventHandler
    2. public void IRON_BLOCK(InventoryClickEvent event) {
    3. Player player = (Player) event.getWhoClicked();
    4. ItemStack clicked = event.getCurrentItem();
    5. Inventory inventory = event.getInventory();
    6. Random random = new Random();
    7. int randomString = random.nextInt(2);
    8. if (inventory.getName().equals(Menu.hatshop.getName())) {
    9. if (clicked.getType() == Material.IRON_BLOCK) {
    10. event.setCancelled(true);
    11. player.closeInventory();
    12. if (player.getInventory().getHelmet().getType() == Material.AIR) {
    13. if (player.getInventory().getHelmet().getType() != Material.IRON_BLOCK) { //
    14. if(FlyingBlocksPlugin.playerPoints.getAPI().take(player.getName(), 200)) {
    15. player.getInventory().setHelmet(new ItemStack(Material.IRON_BLOCK));
    16. if (randomString == 1) {
    17. player.sendMessage(MsgType.ACCESOIRIES + "I cant figure out that you buying an IRON BLOCK as an head???");
    18. }else {
    19. player.sendMessage(MsgType.ACCESOIRIES + "Tell me what the point is of buying an IRON BLOCK as head..");
    20. }
    21. } else {
    22. player.sendMessage(MsgType.ACCESOIRIES + "Hmm... I think you must check your points before buying this item!");
    23. }
    24. } else {
    25. player.sendMessage(MsgType.ACCESOIRIES + "You have already buyt this item.... -,-");
    26. }
    27. }
    28. }
    29. }
    30. }
     
  10. Offline

    2MBKindiegames

    Put this between line 15 & 16
     
Thread Status:
Not open for further replies.

Share This Page