Guns Meta Data help

Discussion in 'Plugin Development' started by TheBUGBadCheeseburger, Mar 16, 2014.

Thread Status:
Not open for further replies.
  1. Hello,

    So I'm creating a plugin, And Its a guns plugin. Anyways, I want to make it so that the specified gun only fires a bullet (a fireball) when it has the meta data of ("Gun") or ("Rifle").

    Here's the code I'm trying to implement the Meta Data into.
    Code:java
    1. if (!(e.getItem().getType() == Material.WOOD_HOE)) return; {
    2. }
    3. Player player = e.getPlayer();
    4. PlayerInventory inventory = player.getInventory();
    5.  
    6.  
    7. {
    8. }
    9.  
    10. Snowball s = e.getPlayer().launchProjectile(Snowball.class);
    11. e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.NOTE_BASS, 1, 1);


    Technically I want it so that when the player has a WOOD_HOE in their hand named "Wooden Hoe" The gun won't fire, But if it has the Meta data of "Gun" or something, Then it fires.



    One last question, Is when they fire a gun, I want it to do something like:
    Code:java
    1. e.getPlayer().getInventory().contents().removeItem(Material.SULPHUR -1)


    So that if they have sulphur in their inventory named "Ammo" or "Rifle Ammo" it removes -1 from their inventory on the gun shooting.



    Thanks!
     
  2. Offline

    The Fancy Whale

    Code:java
    1. if (item.getItemMeta().getDisplayName().equalsIgnoreCase("Rifle")){
    2. //code here
    3. }


    Code:java
    1. ItemStack bullet = new ItemStack(Material.SULPHUR, 1);
    2.  
    3. player.getInventory().removeItem(bullet);
    4. player.updateInventory();
     
  3. Offline

    The Fancy Whale

    TheBUGBadCheeseburger I don't think sulphur is an item... I am not sure what item you want it to remove
     
  4. Offline

    FuZioN720

  5. Offline

    AoH_Ruthless

    TheBUGBadCheeseburger
    Don't blindly copy + paste code. Your issue is coming from the fact that you are using item.getItemMeta().... when your itemStack name is bullet. Just fix that and you are good to go.
     
    The Fancy Whale likes this.
  6. The Fancy Whale I tested it out, It worked with a few minor adjustments, Although it removes the entire stack of gunpower.
     
  7. Offline

    AoH_Ruthless

  8. AoH_Ruthless Thanks!

    Also, Is there a way to make it so the Event returns or stops when you run out of Gunpowder? That might be a little complicated considering that the event doesn't fire if you don't have the wooden hoe named "Rifle"
     
  9. Offline

    FuZioN720

    TheBUGBadCheeseburger

    Try this:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. final Player player = event.getPlayer();
    4. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    5. ItemStack item = player.getItemInHand();
    6. if(item.getType().equals(Material.WOOD_HOE)){
    7. if (item.hasItemMeta() == true){
    8. if (item.getItemMeta().getDisplayName().equals("Gun") || item.getItemMeta().getDisplayName().equals("Rifle")){
    9. final Projectile projectile = player.launchProjectile(LargeFireball.class);
    10. player.getWorld().playSound(player.getLocation(), Sound.NOTE_BASS, 1, 1);
    11. projectile.getWorld().createExplosion(projectile.getLocation(), 0.5F, true);
    12. event.setCancelled(true);
    13.  
    14. ItemStack is = new ItemStack(Material.SULPHUR, 1);
    15. ItemMeta im = null;
    16. im = is.getItemMeta();
    17. im.setDisplayName("Ammo");
    18. is.setItemMeta(im);
    19. player.getInventory().remove(is);
    20. return;
    21. }
    22. }
    23. }
    24. if (player.getItemInHand() == null){
    25. event.setCancelled(true);
    26. }
    27. }
    28. }
     
  10. FuZioN720 Is there a possible way to make it so that when the Sulphur runs out the gun doesn't fire anymore? Or play a creeper hiss sound when it doesn't have any ammo?
     
  11. Offline

    The Fancy Whale

    Code:java
    1. if(player.getInventory().contains(Material.SULPHUR)){
    2. }

    and use playEffect();
     
  12. Offline

    FuZioN720

    @TheBUGBadCheeseburger

    Try this:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. final Player player = event.getPlayer();
    4. if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    5. //Move Sulphur here
    6. ItemStack is = new ItemStack(Material.SULPHUR, 1);
    7. ItemMeta im = null;
    8. im = is.getItemMeta();
    9. im.setDisplayName("Ammo");
    10. is.setItemMeta(im);
    11.  
    12. //Checks if player has the item
    13. if(!(player.getInventory().contains(is))){
    14. //if not
    15. player.getWorld().playSound(player.getLocation(), Sound.FIZZ, 15.0F, 1.0F);
    16. event.setCancelled(true);
    17. return;
    18. }
    19.  
    20.  
    21.  
    22. ItemStack item = player.getItemInHand();
    23. if(item.getType().equals(Material.WOOD_HOE)){
    24. if (item.hasItemMeta() == true){
    25. if (item.getItemMeta().getDisplayName().equals("Gun") || item.getItemMeta().getDisplayName().equals("Rifle")){
    26. final Projectile projectile = player.launchProjectile(LargeFireball.class);
    27. player.getWorld().playSound(player.getLocation(), Sound.NOTE_BASS, 1, 1);
    28. projectile.getWorld().createExplosion(projectile.getLocation(), 0.5F, true);
    29. event.setCancelled(true);
    30.  
    31. player.getInventory().remove(is);
    32. return;
    33. }
    34. }
    35. }
    36. if (player.getItemInHand() == null){
    37. event.setCancelled(true);
    38. }
    39. }
    40. }
     
  13. Offline

    The Fancy Whale

    Much spoon feed. Very unwowe xD
     
  14. Offline

    AoH_Ruthless

    This is also a spoonfeed. Just because your code is shorter doesn't mean you aren't just giving it out without teaching why your code works and why the OP's code doesn't.
     
  15. FuZioN720 I don't even....

    Please no spoonfeeding, The reason I'm posting this is to LEARN how to use the proper method, Not Copy + Pasting stuff.
    I like to make plugins that are unique to my ownership, Not someone else's code.
     
Thread Status:
Not open for further replies.

Share This Page