Items not being removed. (Sort of)

Discussion in 'Plugin Development' started by GEETAFF, Jul 29, 2014.

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

    GEETAFF

    OK, I am coding a gun so I know how to in the future. I am having trouble with the Ammo. So, I want it so when i shoot a bullet, it removes 1 nether wart (ammo) from the players inventory. It works, however it stays the same amount of ammunition, until you relog. For an example: If i have 64 ammo and I shoot 2 bullets, it still appears as 64. That is, until I log off, and come back on it changes to 62 ammo. If I shoot all 64 the gun wont fire anymore, however the ammo is still in my inventory. Here is my code:

    Code:
    package me.dragon.CrafterCraftRP;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener
    {
        Logger pluginLogger = Bukkit.getLogger();
     
        @Override
        public void onEnable()
        {
            pluginLogger.info("Plugin Enabled Successfully");
            getConfig().options().copyDefaults(true);
            saveConfig();
     
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        //SniperRifle
        ItemStack sniper = new ItemStack(Material.STONE_HOE);
        {
            ItemMeta sniperMeta = sniper.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            sniperMeta.setDisplayName(ChatColor.DARK_RED + "[" + ChatColor.DARK_GRAY + "Sniper" + ChatColor.DARK_RED + "]");
            lore.add(ChatColor.GREEN + "Sniper");
            sniperMeta.setLore(lore);
            sniper.setItemMeta(sniperMeta);
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
     
            if (!(event.getAction() == Action.RIGHT_CLICK_AIR)) return;
     
            if (!(event.getItem().getType() == Material.STONE_HOE)) return;
     
            if (!event.getPlayer().getInventory().containsAtLeast(new ItemStack(Material.NETHER_WARTS), 1)) return;
     
            Snowball snowball = event.getPlayer().launchProjectile(Snowball.class);
            snowball.setShooter(event.getPlayer());
            snowball.setVelocity(snowball.getVelocity().multiply(3));
            ItemStack ammo = new ItemStack(Material.NETHER_WARTS, 1);
            player.getInventory().remove(ammo);
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e)
        {
            if (e.getDamager() instanceof Snowball)
            {
                Snowball s = (Snowball) e.getDamager();
     
                if (s.getShooter() instanceof Player)
                {
                    Player shooter = (Player) s.getShooter();
     
                    if (shooter.getItemInHand().getType() == Material.STONE_HOE)
                    {
                        e.setDamage(10.0);
                    }
                }
            }
        }
     
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            Player player = (Player) sender;
     
            if(label.equalsIgnoreCase("givesniper"))
            {
                if(player.hasPermission("sniper.sniper"))
                {
                    player.getInventory().addItem(sniper);
                }
            }
     
            return false;
        }
    }
     
  2. Offline

    jthort

    GEETAFF You are forgetting to update the inventory, that should fix your issue.
     
  3. Offline

    GEETAFF

    jthort Now it isnt even removing the item :I
     
  4. Offline

    _Filip

    What is your new code?
     
  5. Offline

    GEETAFF

    TheSpherret

    Code:
    package me.dragon.CrafterCraftRP;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener
    {
        Logger pluginLogger = Bukkit.getLogger();
     
        @Override
        public void onEnable()
        {
            pluginLogger.info("Plugin Enabled Successfully");
            getConfig().options().copyDefaults(true);
            saveConfig();
     
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        //SniperRifle
        ItemStack sniper = new ItemStack(Material.STONE_HOE);
        {
            ItemMeta sniperMeta = sniper.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            sniperMeta.setDisplayName(ChatColor.DARK_RED + "[" + ChatColor.DARK_GRAY + "Sniper" + ChatColor.DARK_RED + "]");
            lore.add(ChatColor.GREEN + "Sniper");
            sniperMeta.setLore(lore);
            sniper.setItemMeta(sniperMeta);
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
     
            if (!(event.getAction() == Action.RIGHT_CLICK_AIR)) return;
     
            if (!(event.getItem().getType() == Material.STONE_HOE)) return;
     
            if (!event.getPlayer().getInventory().containsAtLeast(new ItemStack(Material.NETHER_WARTS), 1)) return;
     
            Snowball snowball = event.getPlayer().launchProjectile(Snowball.class);
            snowball.setShooter(event.getPlayer());
            snowball.setVelocity(snowball.getVelocity().multiply(3));
            ItemStack ammo = new ItemStack(Material.NETHER_WARTS, 1);
            player.getInventory().remove(ammo);
            player.updateInventory();
     
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e)
        {
            if (e.getDamager() instanceof Snowball)
            {
                Snowball s = (Snowball) e.getDamager();
     
                if (s.getShooter() instanceof Player)
                {
                    Player shooter = (Player) s.getShooter();
     
                    if (shooter.getItemInHand().getType() == Material.STONE_HOE)
                    {
                        e.setDamage(10.0);
                    }
                }
            }
        }
     
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            Player player = (Player) sender;
     
            if(label.equalsIgnoreCase("givesniper"))
            {
                if(player.hasPermission("sniper.sniper"))
                {
                    player.getInventory().addItem(sniper);
                }
            }
     
            return false;
        }
    }
     
  6. Offline

    _Filip

  7. Offline

    ulsa

  8. Offline

    fireblast709

    ulsa wat
    GEETAFF
    - Don't use Bukkit.getLogger(), use the Logger you get from JavaPlugin
    - no need for enable messages
    - use removeItem to remove ItemStacks. Then you can check if the Map returned is empty to check if they had the ammo (and not shoot a projectile if they did not)
    - rather than checking if the player has an item in their hand, use Metadata. Otherwise you can quickly switch items held, changing the damage.
    - don't cast sender to Player without checking if it is a Player. There are other sources for command execution (console, command blocks, etc)
    - use cmd.getName() for alias support (label is the alias, so if you would add 'sniper' as an alias, you would have to add a check for that when you use label. With cmd.getName(), however, it will always be the main command name)
     
    jthort likes this.
Thread Status:
Not open for further replies.

Share This Page