Reloading System?

Discussion in 'Plugin Development' started by JD_Guy17, Mar 23, 2014.

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


    I have a basic guns class and I am not sure how to make it so when a player shoots lets say 8 bullets the gun reloads. Is there a way to do this?

    Here is my gun class:
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. final Player p = e.getPlayer();
    4. if (!(p.getInventory().getItemInHand().getType() == Material.STONE_SPADE)) {
    5. return;
    6. }
    8. ItemStack iteminhand = p.getInventory().getItemInHand();
    9. if (!(iteminhand.hasItemMeta())) {
    10. return;
    11. }
    13. if (!(iteminhand.getItemMeta().getDisplayName().equalsIgnoreCase("gun"))) {
    14. return;
    15. }
    18. if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction() == Action.LEFT_CLICK_BLOCK) {
    20. if (!(p.getInventory().containsAtLeast(ammo, 1))) {
    21. p.sendMessage(ChatColor.RED + "Out of ammo");
    22. return;
    23. }
    25. if (timer.contains(p)) {
    26. return;
    27. }
    29. p.launchProjectile(Snowball.class);
    31. p.getInventory().removeItem(ammo);
    32. p.playSound(p.getLocation(), Sound.EXPLODE, 1, 1);
    34. timer.add(p);
    36. Weapons.getInstance().timer(p, timer, time);
    37. }
    38. }

    Thanks in advance :)
  2. Offline


    C'mon, this doesn't event require Java knowledge, just common sense.

    Make an int counter, and icrement it each time someone shoots. Make a hashmap of counters for multiple players.
  3. Offline


    So making a HashMap would be non java, common sense? lol
  4. Offline


    Well, making an int counter is also Java, so yeah, you need to have at least basic knowledge of how computers work and 1st grader math knowledge to make Bukkit plugins. Unfortunately. amount of Bukkit developers that meet those requirements is too small.
    TigerHix, Qwahchees and Panjab like this.
  5. Offline


    Okay, let me try to tackle this one.
    I was once completely blinded without any common sense of how programming worked, or computers for that matter, and I've been around them forever.

    Okay, basically:
                        int bullets = gun.getMaxAmmo();
                        for (int i = 0; i <= bullets; i++) {
                            if (bullets == i) {
                                p.sendMessage("No ammo.");
    Now I'm not sure if this would work with multiple guns, but this should work for a single player online, firing one gun. Use a HashMap to store the player's name, and amount of ammo they have.

    The problem you have here is, multiple guns, multiple people, etc. The best way I could see you doing this really, is to set the item amount in the slot to the amount of ammo they have. Thus, you check if the player has only 1 item left, which means no ammo.
  6. Offline


    Qwahchees What?! That doesn't make any sense, why are you using a loop for this and why are you comparing each number to the max ammo?

    1. public HashMap<String, Integer> playerAmmo = new HashMap<String, Integer>();
    3. public void shootGun(Player p) {
    4. int ammo = playerAmmo.get(p.getName());
    5. if(ammo <= 0) {
    6. p.sendMessage("No ammo!");
    7. } else {
    8. //Shoot the gun
    9. playerAmmo.put(p.getName(), ammo - 1);
    10. }
    11. }
    13. public void reloadGun(Player p) {
    14. int maxAmmo = 12;
    15. playerAmmo.put(p.getName(), maxAmmo);
    16. }
    Garris0n likes this.
  7. Offline


    Ad237 Because a loop doesn't require you to use resources to create a HashMap, which has memory overhead.

    Second, I'm comparing it because when it reaches maxAmmo, that's when you used all your ammo, thus needing reloading.
  8. Offline


    Qwahchees Are you serious? A HashMap is a lot less intensive than looping. Also why loop through every possible ammo amount anyway? Just use
    1. if(ammoLeft == 0) {
    2. player.sendMessage("No ammo!");
    3. }
  9. Offline


    Listen for when the player shoots, add 1 to the int that their name is matched to in the HashMap. Get the number and check if it's equal to 8. If so, reload the gun and reset the int in the HashMap.
Thread Status:
Not open for further replies.

Share This Page