Solved I need a bit of ItemStack help.

Discussion in 'Plugin Development' started by xXRobbie21Xx, Aug 22, 2014.

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

    xXRobbie21Xx

    Ok guys, as much as i hate to do this, i must ask for some advice. I am making a guns plugin and i have a few custom items (ammo, guns, ect) and i am having trouble with the whole distribution prosess.
    Here is an itemstack to base it this off of.
    Code:
        ItemStack ammo = new ItemStack(Material.GHAST_TEAR, 1);
    
    Here are the questions i have:
    1.) Do i need to give the item its metadata in every method its used in?
    2.) I need to give/take this itemstack from peoples inventories in different quantities, whats the easyiest way to do that? Do i make methods for it? Or simply use .setAmount?

    I understand i just made a complete fool out of myself, but if anybody can give me a helping hand, it truely would be appreciated. Im not asking to be spoon fed code, but a slight push in the right direction would do the trick.
     
  2. Offline

    mythbusterma

    xXRobbie21Xx

    What methods is it used in, exactly?

    The easiest way to do that is to search their inventory for the material, then for the ItemMeta, then change the size of the stack, and then update their inventory.
     
  3. Offline

    mine-care

    Setammount will work but misfires some times, use check of players inventory contains at least a amount of ghast tear before removing, if it does, player.removeItem(itemstack);// or something similar
    I don't know if you ise the item in a method but it already has metadata and depends on how u use it.
    And no you haven't made a fool of yourself, there are no stupid or foolish questions, here are only such answers...
     
  4. Offline

    xXRobbie21Xx

    mythbusterma mine-care
    Im just going to past my whole class, if thats okay.
    I tried to mess around a bit with creating my own methods in order to work around it but as you can see i need to take one bullet away when fired and i need to give a player bullets when using /ammostock, seen at the bottom.

    Also, as far as the metadata goes, do i need to set it in every class its used in?

    Thanks for letting me know how its done, really means alot

    Code:java
    1. public class main extends JavaPlugin implements Listener {
    2.  
    3. public Map<UUID, Long> cantFire = new HashMap<UUID, Long>();
    4. private final int coolDownTime = 1;
    5.  
    6. ItemStack ammo = new ItemStack(Material.GHAST_TEAR, 1);
    7.  
    8. public void onEnable() {
    9. getServer().getPluginManager().registerEvents(this, this);
    10.  
    11.  
    12. ShapedRecipe basicBullet = new ShapedRecipe(ammo)
    13. .shape(" c ", "igi", " r ").setIngredient('c', Material.COAL)
    14. .setIngredient('i', Material.IRON_INGOT)
    15. .setIngredient('g', Material.SULPHUR)
    16. .setIngredient('r', Material.REDSTONE);
    17. getServer().addRecipe(basicBullet);
    18.  
    19. }
    20.  
    21. public void onDisable() {
    22.  
    23. }
    24.  
    25.  
    26.  
    27.  
    28. public void giveAmmo(Player name, int amount) {
    29. Player p = (Player) name;
    30. ItemStack ammo = new ItemStack(Material.GHAST_TEAR, amount);
    31. ItemMeta md = ammo.getItemMeta();
    32. md.setDisplayName("Ammo");
    33. ammo.setItemMeta(md);
    34.  
    35. p.getInventory().addItem(ammo);
    36. p.sendMessage("Ammo giving worked from .ammo");
    37. return;
    38.  
    39.  
    40. }
    41.  
    42. public void takeAmmo(Player name, int amount) {
    43. Player p = (Player) name;
    44. ItemStack ammo = new ItemStack(Material.GHAST_TEAR, amount);
    45. ItemMeta md = ammo.getItemMeta();
    46. md.setDisplayName("Ammo");
    47. ammo.setItemMeta(md);
    48.  
    49. if (p.getInventory().contains(ammo)) {
    50. p.getInventory().remove(ammo);
    51. p.sendMessage("Ammo taking worked!");
    52. }
    53. }
    54.  
    55.  
    56.  
    57.  
    58.  
    59. @SuppressWarnings("deprecation")
    60. @EventHandler
    61. public void onPlayerInteract(PlayerInteractEvent e) {
    62. Player p = e.getPlayer();
    63. Action action = e.getAction();
    64. int coolDownMillis = coolDownTime * 1000;
    65. long lastUsed = 0;
    66.  
    67. if (cantFire.containsKey(p.getUniqueId())) {
    68. lastUsed = cantFire.get(p.getUniqueId());
    69. }
    70.  
    71. if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
    72. if (p.getItemInHand().getType() == Material.DIAMOND_HOE) {
    73. if (p.getInventory().contains(Material.GHAST_TEAR)) {
    74.  
    75. if (System.currentTimeMillis() - lastUsed >= coolDownMillis) {
    76. cantFire.put(p.getUniqueId(),System.currentTimeMillis());
    77.  
    78. Snowball snowball = (p.launchProjectile(Snowball.class));
    79. snowball.setVelocity(snowball.getVelocity().multiply(2));
    80. p.sendMessage("Works");
    81. takeAmmo(p, 1);
    82. p.updateInventory();
    83.  
    84. } else {
    85. p.sendMessage("Gun overheating!");
    86. }
    87. } else {
    88. p.sendMessage("You dont have any ammo!");
    89. }
    90.  
    91. }
    92. }
    93.  
    94. }
    95.  
    96. @EventHandler
    97. public void onEntityDamage(EntityDamageByEntityEvent e) {
    98. if (e.getDamager() instanceof Snowball) {
    99. Snowball s = (Snowball) e.getDamager();
    100. if (s.getShooter() instanceof Player) {
    101. Player shooter = (Player) s.getShooter();
    102. if (shooter.getItemInHand().getType() == Material.DIAMOND_HOE) {
    103. e.setDamage(10.0);
    104. }
    105.  
    106. }
    107.  
    108. }
    109. }
    110.  
    111.  
    112. @Override
    113. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    114. Player p = (Player) sender;
    115. if (sender instanceof Player) {
    116. if (commandLabel.equalsIgnoreCase("AmmoStock")) {
    117. p.sendMessage("You recieved an ammo drop off!");
    118. giveAmmo(p, 3);
    119.  
    120. }
    121.  
    122. }
    123.  
    124. return false;
    125. }
     
  5. Offline

    mine-care

    I think you need it for each method, and please fix this:

    Player p = (Player) sender;
    if (sender instanceof Player) {...}
    If goes first.
     
  6. Offline

    mythbusterma

    xXRobbie21Xx

    Instead you should see if the player's inventory contains it via Inventory#first(Material), then get the ItemStack in that location, check if it has the correct metadata via ItemStack#getItemMeta().getName().equalsIgnoreCase("something") and then change the size of said stack to one lower.
     
  7. Offline

    xXRobbie21Xx

    mine-care Okay cool, and thanks, i planned on revising everything right after i got some help :p

    mythbusterma Ok, that makes sense. Thanks

    I appreciate it!
     
  8. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page