Need help with getTypeId()

Discussion in 'Plugin Development' started by Minermax7, Apr 6, 2014.

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

    Minermax7

    Hey guys,
    I am making my own sort of Essentials plugin called ManyCommands. I recently was playing around and decided to create something so if you left or right click with a bone in your hand you shoot an arrow! I just want to figure out what other method to use so that these aren't deprecated. Here is my code so far:
    Code:java
    1. @EventHandler
    2. public void onPlayerUse(PlayerInteractEvent evt) {
    3. Player player = evt.getPlayer();
    4. if(player.getItemInHand().getTypeId()==352)player.shootArrow();
    5. }

    Currently the .getTypeId(), and the shootArrow are underlined in yellow with a strikethrough as deprecated. I was wondering if any of you knew an alternative to this, it will help me out lots! :)
    Thanks,
    Dylan.
     
  2. Offline

    Funergy

    using item ids are not handy to use! Just use getType() and Material.

    if(player.getItemInHand().getType()== Material.BONE_MEAL)player.shootArrow();
    and I think the player.shootArrow(); doesn't work but you can try it
     
  3. Do getType(), your test becomes (Material.Bone.compareTo(player.getItemInHAnd().getType())==0)
    And I think it is launchProjectile(Arrow.class) instead of shootArrow()
     
  4. Offline

    Barnyard_Owl

    Hartorn
    Why Material.BONE.compareTo(player.getItemInHand().getType())==0 instead of Material.BONE == player.getItemInHand().getType()?
     
  5. Barnyard_Owl :
    True, no reason to do that, since it is enum.
     
  6. Offline

    Minermax7

    I added this
    Code:java
    1. @EventHandler
    2. public void onPlayerUse(PlayerInteractEvent evt) {
    3. Player player = evt.getPlayer();
    4. if(player.Material.BONE.compareTo(player.getItemInHand().getType())==0);
    5. }

    And the Material is underlined red saying "Material cannot be resolved or is not a field"
    I'm sorry if I am doing it wrong, I have started up coding again and I end up getting the weirdest mistakes when people tell me to add something.
     
  7. Offline

    Barnyard_Owl

    It's not player.Material... Material is an enum. It's just Material.BONE.
     
  8. Offline

    Minermax7

    Okay,
    Code:java
    1. @EventHandler
    2. public void onPlayerUse(PlayerInteractEvent evt) {
    3. Player player = evt.getPlayer();
    4. if(Material.BONE.compareTo(player.getItemInHand().getType())==0);
    5. }

    I have added that and imported and It's fine now, how would I now make it shoot an arrow? I have used player.shootArrow() in previous attempts and it works but its deprecated, and I tried the advice from Hartorn but it underlined it all pretty much. Do I need to create anything else? I am still a beginner in Java making my way but I just need guidance haha. :)
     
  9. Offline

    Minermax7

  10. Offline

    MOMOTHEREAL

    ItemStack bone = new ItemStack(Material.BONE, 1);
    ItemMeta meta = bone.getItemMeta();
    meta.setLore(Arrays.asList("lore1", "lore2"));
    meta.setDisplayName("Troller");
    bone.setItemMeta(meta);
     
  11. With something like this.
    Code:
    ItemMeta  meta =player.getItemInHand().getItemMeta();
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.RED + "Troller");
    meta .setLore(lore);
    player.getItemInHand().setItemMeta(meta );
    Edit : Was too slow ^^
     
    MOMOTHEREAL likes this.
  12. Offline

    Minermax7

    MOMOTHEREAL When I try your code, I run the command ingame and nothing happens, no text or anything comes up and I don't get any items...
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("alauncher")) {
    2. ItemStack bone = new ItemStack(Material.BONE, 1);
    3. ItemMeta meta = bone.getItemMeta();
    4. meta.setLore(Arrays.asList("lore1", "lore2"));
    5. meta.setDisplayName("Troller");
    6. bone.setItemMeta(meta);
    7. }
     
  13. Offline

    BillyGalbreath

    Because you just created an itemStack but never put it anywhere (in the world, in player's inventory, etc). You have to put it somewhere for it to exist, otherwise its just a thought that gets forgotten when the method is finished executing.
     
  14. Just replace
    Code:
      ItemStack bone = new ItemStack(Material.BONE, 1);
    by
    Code:
      ItemStack bone =player.getItemInHand();
     
  15. Offline

    Minermax7

    BillyGalbreath Ok, sorry for bugging you. But where could I put the code to get the players inventory and that?
     
  16. Offline

    MOMOTHEREAL

    Minermax7 Add player.getInventory().addItem(bone);
     
Thread Status:
Not open for further replies.

Share This Page