Item Usage and Durability

Discussion in 'Plugin Development' started by ShadowLAX, Sep 21, 2013.

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

    ShadowLAX

    Hi bukkit, I have a few questions:

    1) When an Item is used (say a fishing rod) how would I make it so cancels the durability taken or resets it to its maximum value? and also, what event would I use?

    2) When a player steps in water, how would I then kill the player as soon as they touch it, and what event would I use? I've tried PlayerMoveEvent and PlayerInteractEvent, but neither seemed to work, I'm guessing I did something wrong >.<

    Thanks!
     
  2. Offline

    chasechocolate

    1. I don't think there is a specific event, but for that, you would listen to PlayerFishEvent

    2. It's PlayerMoveEvent, so you must have done something wrong. Check if event.getTo().getBlock().getType() is WATER or STATIONARY_WATER.
     
  3. Offline

    ShadowLAX

    chasechocolate Sorry for the late reply :( I tried both of these, and neither worked >.< I'll attach my code for these events:

    PlayerMoveEvent:

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. Player player = e.getPlayer();
    4. if (this.ffingame.contains(player.getName())) {
    5. if (e.getTo().getBlock().getType() == Material.STATIONARY_WATER || e.getTo().getBlock().getType() == Material.WATER) {
    6. player.setHealth(0.0);
    7. }
    8. }
    9.  
    10. }


    Code:java
    1. PlayerFishEvent:
    2.  
    3. @EventHandler
    4.  
    5. public void PlayerUseFish(PlayerFishEvent e) {
    6.  
    7. Player player = e.getPlayer();
    8.  
    9. if (plugin.ffingame.contains(player.getName())) {
    10.  
    11. if (e.getCaught().getType() == EntityType.PLAYER) {
    12.  
    13. e.setCancelled(true);
    14.  
    15. }
    16.  
    17. }
    18.  
    19. }


    And yes, I have them registered in my Main class :p

    bump, help pls D:

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

    chasechocolate

    ShadowLAX bump every 12 hours. Also, the first one should be working, add some debug lines. For the second one, use player.getItemInHand().setDurability(Material.FISHING_ROD.getMaxDurability()).
     
  5. ShadowLAX

    You can use PlayerInteractEvent and check if the type is FISHING_ROD.

    For the water thing, do something like

    Code:java
    1. public void move(PlayerMoveEvent e)
    2. {
    3.  
    4. Material pBlock = e.getPlayer().getLocation().getBlock().getType();
    5. Material down = e.getPlayer().getLocation().getBlock().getRelative(BlockFace.DOWN).getType();
    6. if(down == Material.STATIONARY_WATER || down == Material.WATER || pBlock == Material.STATIONARY_WATER || pBlock == Material.WATER)
    7. {
    8.  
    9. //do player killing stuffs.
    10.  
    11. }
    12.  
    13. }



    Hope that helped.
     
  6. Offline

    ShadowLAX

  7. Offline

    Quantix

    That code would kill the player even if he jumps across a block of water. I would just stick with the first method of getting the block at the players feet and maybe add getting the block above that one (for players walking into water with just their head).

    Although, even with this modification the "water detection" won't work all the time. The players bounding box can take up more than one block, allowing the player to move up water falls while technically being in an air block as the player.getLocation.getBlock() returns the block in the middle of the player's bounding box. For a more accurate detection, get all twelve blocks a player's bounding box can possibly encompass at the same time (getting the corner blocks and middle ones) and checking if they are water.
     
  8. Offline

    ShadowLAX

    Quantix Uh thanks, a little complicated though xD
     
Thread Status:
Not open for further replies.

Share This Page