I literally need help on this

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

Thread Status:
Not open for further replies.
  1. Okay, So I've posted on the forums a few times with the same subject, No final answer.

    Essentially, My problem is, Is that I have a guns plugin I'm making and I'm trying to do this:

    Check if the item is Sulphur, (ammo for the gun)
    Then check if it has the ItemMeta of (desired itemmeta)
    Then remove 1 of Sulphur.

    BUT If the player does not have the sulphur with the itemmeta name it will not fire the gun.

    So far, This is what I've come up with this issue:
    1. ItemStack bullet = new ItemStack(Material.SULPHUR);
    3. bullet.setItemMeta("Ammo"))
    4. if(bullet.getItemMeta().equals("Ammo"))
    5. if(bullet.getItem().getType == Material.SULPHUR)) {
    6. player.getInventory().remove(bullet, 1)
    7. }
    9. if(!(player.getInventory.contains(bullet)) {
    10. event.setCancelled(true);
    11. return;
    12. }

    Please, I REALLY Need help on this... I've been trying to find a solution since like 4-6 days ago.

    And yes, I've debugged that, Does not work.
  2. Offline


    TheBUGBadCheeseburger Stop making a new forum almost every day, you'll get more help if you just stick to one.

    Loop through the items in the player's inventory, checking if the item isn't null, and if the type equals Material.Sulphur, if so continue through the code

    Get the item stack's ItemMeta, and see if the DisplayName equals "Ammo"

    Get the player's inventory and get the sulpher item stack, and if the amount is greater than 1, remove 1. else just remove the stack.

    Then finally break out of the loop.
  3. Offline

    The Fancy Whale

    Why are you canceling a player interact event?
  4. The Fancy Whale Because, I wanted it so that when the player doesn't have any Sulphur in their inventory with the itemmeta of Ammo, It cancels the event completely. So that the gun doesn't fire anymore unless they have that in their inventory.

    xXSniperzzXx_SD I know how to do the other stuff. Thanks!
    But how would I loop through the inv? Would it be something like player.getInventory().getType Or something? I forget. That's probably my issue.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 7, 2016
  5. Offline


    for(ItemStack item : inventory.getcontents)
  6. Dread9Nought Do you know how I would break out of the loop?
  7. Offline


  8. xXSniperzzXx_SD Could you just write the code out, Because I generally can't figure out how to do this. And its annoying.

    Can somebody help me with this? ;_;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 7, 2016
  9. Offline


    1. for(ItemStack is : player.getInventory().getContents())
    2. {
    3. if(is.getType() == Material.SULPHUR)
    4. {
    5. //itemmeta checks
    6. //to exit out of the loop, do:
    7. break;
    8. }
    9. }
  10. Offline


    Bart You'd also want to make sure the itemstack isn't null, or getting its type will return an error

    I made a video on using Item Meta
  11. xXSniperzzXx_SD Alright so is this right? Could you help me?

    2. }for(ItemStack is : player.getInventory().getContents())
    3. {
    4. if(is.getType() == Material.SULPHUR) {
    5. if(is.getItemMeta().equals("ยง6Ammo")) {
    6. player.getInventory().remove(is);
    7. player.launchProjectile(Snowball.class);
    8. }
    10. }
    11. if(is == null) {
    12. return;
    14. }
    15. {
    16. break;
    17. }
    18. }

    Is that right? Or do i have to change anything? What i was trying to go at there was i didn't want the player to launch a snowball unless they have ammo in their inventory.
  12. Offline


  13. xXSniperzzXx_SD I watched the entire video. It explained a little bit, But not what I needed.
    I came up with this:
    2. }for(ItemStack is : player.getInventory().getContents()) {
    4. }
    5. ItemStack bullet = new ItemStack(Material.SULPHUR, 1);
    6. ItemMeta id = bullet.getItemMeta();
    7. id.setDisplayName("Ammo");
    8. bullet.setItemMeta(id);
    9. if(player.getInventory().contains(bullet)) {
    10. if(bullet.getItemMeta().equals(id)) {
    11. player.getInventory().remove(bullet);
    12. player.launchProjectile(Snowball.class);
    13. {
    14. }
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 7, 2016
  14. Offline


    TheBUGBadCheeseburger normally you'd want to wait atleast 12 hours before re-tagging as it can be considered spam.

    If you look at what I posted a while ago it tells you exactially what you would need to do. And if you can't figure it out, then I'd suggest looking at some more coding tutorials
  15. Offline


    Based on the questions that you asked and the code that you posted, I'm guessing you don't fully understand the Java Programming Language. PLEASE learn Java before Bukkit API.
  16. Offline


    That still has some flaws

    I bet you there are people here that can teach you a thing or two about Java that learned purely from developing Bukkit plugins. Everyone starts learning somewhere, he's just starting to learn here.
  17. Barinade Exactly! Thank you for understanding the situation I'm in.
    I started learning Java directly from bukkit, But I know quite a bit of HTML, and I used to do Python.
    So I have a good picture of programming languages.
  18. Offline


    If you still need help with this add my Skype (level70dwarfhunter) and install Saros to Eclipse
Thread Status:
Not open for further replies.

Share This Page