Bow That Shoots Snowballs Help!

Discussion in 'Plugin Development' started by Zeinaty, Jun 26, 2015.

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

    Zeinaty

    Hi, this is one of my first plugins, and it's supposed to make it so that Bows shoot snowballs if they're named "gun" and they use up the snowballs in your inventory. It's sort of like the EnderBow plugin, but with snowballs instead.

    Anyway, I couldn't get it to work and I don't understand why. Here is the code:
    Code:
    package me.zeinaty;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin implements Listener {
      
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
      
      @EventHandler
      public void onShootBow(EntityShootBowEvent event)
      {
        if ((event.getEntity() instanceof Player))
        {
          Player player = (Player)event.getEntity();
          if (!player.hasPermission("splatcharger.fire"))
          {
            player.sendMessage(ChatColor.RED + "You don't have permission to do this.");
            event.setCancelled(true);
            return;
          }
          if ((event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2) || (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3))
          {
            if (player.getInventory().contains(Material.SNOW_BALL))
                if (!(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.WHITE + "gun")))
                    return;
            {
              if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2)
              {
                event.setCancelled(true);
                player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.SNOW_BALL, 1) });
                ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
              }
              else if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3)
              {
                event.setCancelled(true);
                ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
              }
            }
              event.setCancelled(true);
            }
          }
          else {}
        }
    }
    
    If anybody could help me out with this, that would be awesome! Thanks in advance!

    -Zeinaty
     
    Last edited: Jun 26, 2015
  2. Offline

    Funergy

  3. Offline

    Zeinaty

    @Funergy There aren't any errors (That's what you mean, right? Otherwise, I tried pressing the Debug icon and it just says "The selection cannot be launched, and there are no recent launches.").
     
    Last edited: Jun 26, 2015
  4. Offline

    ChintziSecilMC

    @Zeinaty You could try removing 1 snowball from the players inventory and then as soon as they shoot the arrow set it so it gets the shot entity then delete it and replace it with a snowball with the same velocity as the arrow
     
  5. Offline

    Gotten

    I had an issue like this in the past. Everything was coded correctly but whenever I shot a snowball nothing would happen. Or so it would seem. The snowball was hitting me the moment I shot it because I spawned it inside of me.

    When you shoot the snowball make sure you do (snowball here).setShooter(the shooter here) that way the snowball can actually go through the person who it. Hopefully this will fix
     
    Last edited: Jun 26, 2015
  6. Offline

    Zeinaty

    @ChintziSecilMC That's a good idea, although it's still bothering me that I can't figure out why my original code isn't working and I want to get it to work.

    @Gotten I will try that out! Thanks :)

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

    xXCapzXx

    Tell me if it works or not.
     
  8. Offline

    Zeinaty

    I tried it, but it didn't work :( I guess that wasn't the problem!

    @xXCapzXx Do you think that you know the problem?
     
    Last edited: Jun 26, 2015
  9. Offline

    xXCapzXx

    Send me the whole code.
     
  10. Offline

    Zeinaty

    The code from the original post is the entire code. But here it is again!

    Code:
    package me.zeinaty;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin implements Listener {
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
      @EventHandler
      public void onShootBow(EntityShootBowEvent event)
      {
        if ((event.getEntity() instanceof Player))
        {
          Player player = (Player)event.getEntity();
          if (!player.hasPermission("splatcharger.fire"))
          {
            player.sendMessage(ChatColor.RED + "You don't have permission to do this.");
            event.setCancelled(true);
            return;
          }
          if ((event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2) || (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3))
          {
            if (player.getInventory().contains(Material.SNOW_BALL))
                if (!(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.WHITE + "gun")))
                    return;
            {
              if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2)
              {
                event.setCancelled(true);
                player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.SNOW_BALL, 1) });
                ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
              }
              else if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3)
              {
                event.setCancelled(true);
                ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
              }
            }
              event.setCancelled(true);
            }
          }
          else {}
        }
    }
    
     
  11. Offline

    xXCapzXx

    I just edited it, lemme just test it first.
     
  12. Offline

    Zeinaty

    Cool, thanks! Let me know if it works or not!
     
  13. Offline

    xXCapzXx

    Works.

    Is this what you wanted:
    Code:
    package me.capz.destroy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
         public void onEnable() {
               Bukkit.getServer().getPluginManager().registerEvents(this, this);
           }
        
         @EventHandler
         public void onShootBow(EntityShootBowEvent event)
         {
             if(event.getProjectile() instanceof Arrow) {
            Arrow arrow = (Arrow)event.getProjectile();
           if ((event.getEntity() instanceof Player))
            
             event.getEntity().getWorld().spawn(arrow.getLocation(), Snowball.class);
           Snowball s = event.getEntity().getWorld().spawn(arrow.getLocation(), Snowball.class);
             s.setVelocity(arrow.getVelocity());
             s.setShooter(arrow.getShooter());
             s.setVelocity(arrow.getVelocity());
             event.setProjectile(s);
            
           {
        
           }
             Player player = (Player)event.getEntity();
             if (!player.hasPermission("splatcharger.fire"))
             {
               player.sendMessage(ChatColor.RED + "You don't have permission to do this.");
               event.setCancelled(true);
               return;
             }
             if ((event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2) || (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3))
             {
               if (player.getInventory().contains(Material.SNOW_BALL))
                   if (!(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.WHITE + "gun")))
                       return;
               {
                 if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 2)
                 {
                   event.setCancelled(true);
                   player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.SNOW_BALL, 1) });
                   ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                   player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
                 }
                 else if (event.getBow().getEnchantmentLevel(Enchantment.ARROW_INFINITE) == 3)
                 {
                   event.setCancelled(true);
                   ((Snowball)player.launchProjectile(Snowball.class)).setVelocity(event.getProjectile().getVelocity().multiply(1.1D));
                   player.playSound(player.getLocation(), Sound.SHOOT_ARROW, 1.0F, 1.0F);
                 }
               }
                 event.setCancelled(true);
               }
             }
             else {}
           }
    }
    
    Try it in game.
     
  14. Offline

    Zeinaty

    Thanks so much! Mind if I private message you whenever I'm stumped? :D

    EDIT: By the way, I messaged you some errors that I found, think you could have a look and fix them?

    One more question, how could I make it so that it only works when the bow is named "gun" (ignore case)? I tried doing that before with

    if (!(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.WHITE + "gun")))

    under

    if (player.getInventory().contains(Material.SNOW_BALL))

    but it didn't work (I didn't think it would work, just thought it'd be worth a shot.).

    Any ideas?

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

    xXCapzXx

    No, no, no no.
    You need itemmeta's for that, I can help with that tomorrow.
     
  16. Offline

    Zeinaty

    Alright, sounds good. Also, I private messaged you errors that I've found, you haven't replied, so I'll tell you them on here, too.

    1) it doesn't work on survival mode, only creative
    2) since it doesn't work on survival, I don't lose any snowballs because I can't shoot
    3) whenever I shoot on creative, a second snowball falls off of my player o-o

    That's all with the code you sent me, think you can fix it?
     
    Last edited: Jun 27, 2015
  17. Offline

    Zeinaty

  18. Offline

    Zombie_Striker

    @Zeinaty
    Please keep all help requests on this forum (do not private message people for help)

    By "Doesn't work in survival", does that mean that the arrows do not get changed into snowballs or that the arrows in the inventory do not disappear.

    Stick to JavaNamingConventions, and remove all the redundancies in your plugin.
     
  19. Offline

    Zeinaty

    Sorry, I didn't know about that rule of private messaging.

    Anyway, I meant that in Survival, nothing changes with the Bow. It just shoots arrows like it normally would if I had arrows in my inventory.
     
  20. Offline

    Zombie_Striker

    @Zeinaty
    Code:
       @EventHandler
         public void onShootBow(EntityShootBowEvent event)
         {
           if ((event.getEntity() instanceof Player))
             Projectile s = event.getEntity().getWorld().spawn(new Location (100,100,100), Snowball.class);// the exact location does not matter.
             event.setProjectile(s);
          
           {
      
           }// What do these do?
    
    
     
  21. Offline

    xTrollxDudex

    nothing
     
  22. Offline

    Zombie_Striker

  23. Offline

    Zeinaty

    Thanks, I didn't even notice that was there.

    Do you think that you could fix any problems that I stated above?

    Sorry, I'm kind of new at making plugins, this is my first "real" one.
     
  24. Offline

    Zombie_Striker

    It seems that you want to remove snowballs instead of arrows. To do this, you would need a loop that goes through all the itemstacks in a players inventory. So for removing snowbals, you would set the amount of snowballs to amount-1, and if the amount-1 is <= 0, you would get the players inventory and .remove(that itemstack).

    [edit], The base game tries to look in a players inventory for arrows, the player would need at least one arrow in order for this to work. (you can give the player back an arrow after he fires a snowball to make sure he always has one)
     
  25. Offline

    Zeinaty

    Thanks for the reply. Now I think that I should learn some more Java before I try to do this because I don't really know how I could do all of these things. I'll be gone for this week so I can't do anything else with my code until I get back.. I hate to ask you this, but do you think that you can make me this plugin and I have a look at the code for my understanding?

    EDIT: Oh, I see what you mean by infinite arrows, but how come the EnderBow plugin doesn't require an arrow in the player's inventory? I'm trying to make my plugin work the same but with Snowballs instead.
     
    Last edited: Jun 28, 2015
  26. Offline

    Zombie_Striker

    @Zeinaty
    Its a basic concept in order to remove itemStacks, Here's the code with explanations as to what each line does:
    Code:
    boolean hasSnowball = false;
    // this will check if the player has a snowball.
    
    for(ItemStack Itemstack : player.getInventory,getContents){
    // This line will loop through all the slots in a players inventory and get the item from each one.
    
    if(hasSnowball)
    break;
    // If this is true, then we already removed the snowball and don't need to remove any more
    
    if(Itemstack == null)
    continue;
    // We only want items that actually exist.
    
    if(Itemstack.getType() == Material.SNOWBALL){
    // Now we check if the itemstack is a snowball
    
    if(Itemstack.getAmount > 1){
    // if the amount is greater than one
    Itemstack.setAmount(Itemstack.getAmount() - 1);
    //removing one from the itemstack
    }else{
    // if the player only has one snowball in their inventory
    player.getInventory().remove(ItemStack)
    }
    
    hasSnowball = true;
    // The snowball is removed
    
    }
    
    }
    If you're using eclipse or another development program, use ctrl shft f or the format button to make this look clean.
     
  27. Offline

    Zeinaty

    Thanks so much! But I'm about to leave to go camping in a minute, so I can't edit the code for the rest of the week. Like I said, if you could do it for me, that would be awesome!
     
  28. Offline

    Zeinaty

  29. Offline

    Zeinaty

  30. Can you explain to me what the state of your issue is, any errors, and how it should work?
     
Thread Status:
Not open for further replies.

Share This Page