Exiting the event if player doesn't have enough material

Discussion in 'Plugin Development' started by Huffmanbran, Oct 20, 2014.

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

    Huffmanbran

    I've created a flare gun plugin, essentially it uses the ProjectileHitEvent and sets the arrow to a giant firework (flare). The ammo to load the bow(blaster) is currently ONE egg, yes that is a strange ammo type but it doesn't matter. You load it by typing the command "/loadflare". In that command statement I added a checker to see if the player had at least one egg, if they do have enough the code will run exactly how I want it. However, what if the player runs out of ammunition after they already typed the /loadflare command? Because right now it's stuck forever in the event. So I need to somehow check if the player constantly has enough ammunition after every shot.

    Here is my Command class:
    Code:java
    1. public class FlareCommand implements CommandExecutor {
    2.  
    3. protected static int EGG_COST = 1;
    4.  
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String label,
    7. String[] args) {
    8.  
    9. if ((sender instanceof Player)) {
    10. Player p = (Player) sender;
    11. if (args.length != 1) {
    12.  
    13. if (!FlareCore.players.contains(p.getName())) {
    14.  
    15. PlayerInventory pi = p.getInventory();
    16. ItemStack flareammo = new ItemStack(Material.EGG, EGG_COST);
    17. if (pi.contains(flareammo, EGG_COST)) {
    18. pi.remove(flareammo);
    19. FlareCore.players.add(p.getName());
    20. p.sendMessage(ChatColor.BLUE + "Blaster: "
    21. + ChatColor.WHITE + "Flare round loaded!");
    22. } else {
    23. p.sendMessage(ChatColor.RED
    24. + "Could not load flare round, missing ammunition!");
    25. p.sendMessage(ChatColor.RED + "Ammunition needed: Egg");
    26. return true;
    27. }
    28. }
    29. } else if (args.length != 0) {
    30. p.sendMessage(ChatColor.RED
    31. + "Invalid command syntax! You don't need an argument! (yet)");
    32. } else {
    33. return false;
    34.  
    35. }
    36. }
    37.  
    38. return true;
    39. }
    40.  
    41. }
    42.  


    And here is my Core class:
    Code:java
    1.  
    2. public class FlareCore extends JavaPlugin{
    3.  
    4. public static List<String> players = new ArrayList<String>();
    5.  
    6. public void onEnable(){
    7.  
    8. getCommand("toggleflare").setExecutor(new FlareCommand());
    9. getServer().getPluginManager().registerEvents(new ExplosiveListener(), this);
    10.  
    11. }
    12.  
    13. }
    14.  


    Thank you very much! :)
     
  2. Offline

    Unica

    Code:java
    1. if(flareammo.getAmount() > 0){
    2. // Enough ammo
    3. }else{
    4. // Out of ammo
    5. }
     
  3. Offline

    Huffmanbran

    I tried that, it won't work because flareammo doesn't know how to check if player inventory actually has the egg.

    I ended up trying this to see if this would check for the players inventory.
    Code:java
    1.  
    2. PlayerInventory pi = p.getInventory();
    3. ItemStack flareammo = new ItemStack(Material.EGG, EGG_COST);
    4. if (pi.equals(flareammo.getAmount()>0)) {
     
  4. Offline

    FerusGrim

    Code:java
    1. public boolean hasEgg(Player player) {
    2. for (ItemStack item : player.getInventory()) {
    3. if (item.getType() == Material.SOME_EGG) {
    4. return true;
    5. }
    6. }
    7. return false;
    8. }
     
Thread Status:
Not open for further replies.

Share This Page