I got an Error with this code.

Discussion in 'Plugin Development' started by DrOreo002, Aug 12, 2017.

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

    DrOreo002

    Error log : Link

    Code
    Code:
          @SuppressWarnings("deprecation")
        @EventHandler(priority = EventPriority.HIGHEST)
          private final void OnClick(PlayerInteractEvent e) {
            Player player = e.getPlayer();
                      ItemStack item = player.getItemInHand();
                      if (item.getItemMeta().hasDisplayName()) {
                         
                          if (item.getType() != null) {
                          if (item.getType().equals(Material.STICK) && item.getItemMeta().getDisplayName().contains(ChatColor.GREEN + "Painter")) {
                              Block block = e.getClickedBlock();
                              if (e.getClickedBlock() != null && block.getType().equals(Material.WOOL) && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                                  if (plugin.black.contains(true)) {
                                      block.setTypeIdAndData(35, DyeColor.BLACK.getWoolData(), true);
                                  }
                                  if (plugin.white.contains(true)) {
                                  block.setTypeIdAndData(35, DyeColor.WHITE.getWoolData(), true);
                                  }
                                  if (plugin.red.contains(true)) {
                                      block.setTypeIdAndData(35, DyeColor.RED.getWoolData(), true);
                                  }
                                  if (plugin.orange.contains(true)) {
                                      block.setTypeIdAndData(35, DyeColor.ORANGE.getWoolData(), true);
                                  }
                                  if (plugin.gray.contains(true)) {
                                      block.setTypeIdAndData(35, DyeColor.GRAY.getWoolData(), true);
                                  }
                                  if (plugin.blue.contains(true)) {
                                      block.setTypeIdAndData(35, DyeColor.BLUE.getWoolData(), true);
                                  }
                              }
                          }
                      }
                      }
                  }
    }
    Help?
     
  2. Offline

    Zombie_Striker

    Do not suppress warnings. The reason you got this warning is because you are using getItemInHand, which should no longer be used. Instead, use getItemInMainHand

    1. Method names should start with a lower case
    2. Why is this final? What is the purpose of this?
    3. Why is it private?

    Main problem: The item in the player's hand can be null. You need to null check the instance instead of the type.

    The type will never be null. If the item does not exist, it will be the item that is null.

    Don't use .equals for this. For primatives, use ==, as that only checks the memory location of the object, not all the values for it,

    Do not use contains for this. It uses more processing power than .equals.

    The clicked block should never be null. If the player is not clicking a block, this should return the air block 6 meters away.

    What are you doing here? What is black,blue, and the rest, and why are you checking if it contains a boolean (which can only be in two states, true or false)? Why not just change black to a boolean, and directly compare the values?
     
Thread Status:
Not open for further replies.

Share This Page