Gun Shooting help

Discussion in 'Plugin Development' started by The Fancy Whale, Jun 30, 2014.

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

    The Fancy Whale

    I know there is a check for if a player is blocking (player.isBlocking()) however I need a way to stop them from blocking. I am currently creating a gun-like thing with an iron sword. I am trying to make it so you have a stack of iron swords and when you shoot them you lose 1 sword. However, it seems the material is messed up due to the player blocking. I used the same code for shooting with an axe and shooting with a pickaxe but it only doesn't work with the sword.
    Here is the code in case you are wondering:
    Code:java
    1. public class FireKnife {
    2.  
    3. @SuppressWarnings("deprecation")
    4. public static void shootKnife(Player player){
    5. Egg tomo = player.launchProjectile(Egg.class);
    6. tomo.setShooter(player);
    7. tomo.setVelocity(tomo.getVelocity().multiply(3.0));
    8. player.updateInventory();
    9. }
    10.  
    11. }
    12.  


    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onInteract(PlayerInteractEvent event){
    4. if (Game.getStage() != GameStage.INGAME) return;
    5. Player player = event.getPlayer();
    6. if (event.getAction() == Action.RIGHT_CLICK_BLOCK ||
    7. event.getAction() == Action.RIGHT_CLICK_AIR){
    8. ItemStack inHand = player.getInventory().getItemInHand();
    9. if (player.getInventory().getItemInHand().getType() == Material.IRON_SWORD){
    10. if (inHand.getAmount() >= 2){
    11. ItemStack newHand = new ItemStack(Material.IRON_SWORD, inHand.getAmount - 1);
    12. player.getInventory().setItemInHand(newHand);
    13.  
    14. else {
    15. player.getInventory().setItemInHand(null);
    16. }
    17. FireKnife.shootKnife(player);
    18. }
    19.  
    20. player.updateInventory();
    21. }
    22.  
    23. }

    I find this especially strange because it can tell that I am holding an iron sword as the material shoots, its just you don't lose a sword every time.
    Any help is greatly appreciated! If you have any questions please ask! Thanks!
     
  2. Offline

    guangong4

    The Fancy Whale
    Why not use PlayerInteractEvent and check for event.getAction() == Action.RIGHT_CLICK_AIR and event.getAction() == Action.RIGHT_CLICK_BLOCK?
     
  3. Offline

    The Fancy Whale

    Because it can't be both. You can't right click a block while also only right clicking the air. Also, that is not the issue.
     
  4. Offline

    guangong4

    The Fancy Whale
    I stand corrected, I meant "or" instead of "and".

    Is the exact problem that you can't make the player unblock, or make the ItemStack number update, or both?

    The reason I suggested PlayerInteractEvent is that I'm assuming you're making the player "unblock" in order to be able to fire again. With PlayerInteractEvent, it triggers over and over again even if the player is holding down the mouse button.
     
  5. Offline

    The Fancy Whale

    I'm sorry I don't really understand what you are saying. My code does say right click block OR right click air. I am using the playerinteractevent. And the issue is not that it is firing too much, but the itemstacks are not being modified properly...
     
  6. Offline

    MCForger

    The Fancy Whale
    A couple of things I see. First you do not have to set the player for the projectile since its launched from the Player entity. Two, on your new itemstack line there is no () after getAmount. Also why not just do inHand.setAmount(inHand.getAmount() - 1); instead of creating a new itemstack?
     
  7. Offline

    Sarrabiscos

    The Fancy Whale if the above solution doesnt work, try use this method: player.updateInventory();
    It's deprecated, but it still works and there is no replacement wet.
     
  8. Offline

    MCForger

    Sarrabiscos
    He has that at the bottom of the event code.
     
  9. Offline

    The Fancy Whale

    MCForger Neither of those fix the issue but thanks for the suggestions.
    Sarrabiscos If you look at the end of the code that is implemented already
    I have decided to just not use the sword and use a different tool. It was not worth the effort. It is due to the blocking but I don't believe this can be fixed without something like player.setBlocking(boolean);
     
  10. Offline

    MCForger

    The Fancy Whale
    Did the setAmount on the itemstack in hand not work?

    The Fancy Whale
    To stop blocking check if the player has a sword in the player's hand. Then check the interaction type and make sure they are right clicking then you could cancel the event.

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

    The Fancy Whale

    Nope. It has something to do with the blocking
     
  12. Offline

    MCForger

    The Fancy Whale
    When the player is blocking and the event is called you could make a task the runs every second after and when the player is not blocking removes the item (Also cancel the task).
     
  13. Offline

    Heirteir

    The Fancy Whale
    I don't really think it's possible when your holding a pickaxe or an axe your not actually blocking at all but with a sword that's when your blocking.
     
Thread Status:
Not open for further replies.

Share This Page