Item replace...

Discussion in 'Plugin Development' started by hayabusha, Jul 9, 2012.

  1. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    When right-click a player, I want to milk the bucket.
    but. After the item is milk, it will go back to the empty bucket.
    Do you have a good idea?

    test code:
    Code:
    @EventHandler
    public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
     
          Entity entity = event.getRightClicked();
          Player player = event.getPlayer();
          ItemStack itemStack = player.getItemInHand();
     
          if (entity.getType() == EntityType.PLAYER && itemStack.getType() == Material.BUCKET)
          {
            player.setItemInHand(new ItemStack(Material.MILK_BUCKET));
          }
         
    }
    I'm sorry not good at English...
  2. Offline

    jamietech

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You'll need to add 'itemStack != null' before you check the type of the item just to prevent errors when the player has nothing in their hand.
    Also, what's wrong with your current code, it seems as though it should work properly.
  3. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I was examined, and it was replaced food, they eat it.
    This is a bug of bukkit or minecraft?
  4. Offline

    jamietech

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This isn't a bug; 'eating' (or drinking, in this case) of milk removes all bad active potion effects from the user.
  5. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Not hold down the right-click.
    ended up drinking with a single click.

    My screen effects that are not drinking(or eating).
    From the point of view of other users, seem to like (or eating) are drinking.
  6. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I don't understand, you want to: when a player right click another one, the bucket become full of milk and then the bucket return empty?

    For this, try this code:
    Code:
        @EventHandler
        public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
     
              Player clicked = (Player) event.getRightClicked();
              Player player = event.getPlayer();
              ItemStack milkbucket = new ItemStack(Material.MILK_BUCKET);
              ItemStack emptybucket = new ItemStack(Material.BUCKET);
     
              if (player.getItemInHand().getType() == Material.BUCKET && event.getPlayer() == player && event.getRightClicked() == clicked) {
                player.setItemInHand(milkbucket); // milk the bucket
                player.setItemInHand(emptybucket); // the milk bucket return empty
                return;
              }     
        }
  7. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I want to:
    1.Right-click on the player
    2.If I have an empty bucket
    3.Replace the milk bucket
    4.End.

    But.
    In the test code after this, they drink the milk.
    I want to not drink it.
  8. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    event.setCancelled(true) might cancel the attempt to drik the milk ?
    Still, doesn't drinking involve an animation and a delay on drinking... ? Why are you holding the click then ?
  9. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  10. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I just tested it and it works for PlayerInteractEvent, only it doesn't block the animation and the sound, as long as you hold down the button you'll hear the sound, the animation will play once, but the item will not be consumed.

    You might also check if your setCancelled even triggers, maybe there's something wrong with the conditions.
  11. Offline

    hayabusha

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hmm ....
    In my environment,
    I did not hold down the button,
    But,Goes back to the empty bucket.

    I do cause unknown...
    My environment
    Server: Bukkit 1.2.5-R4.0 #2222 This plugin only.
    Client: Minecraft1.2.5 NoMOD.

    GetMilk Code
    Code:
      @EventHandler
      public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
        if (event.isCancelled())
        {
          return;
        }
     
        Entity entity = event.getRightClicked();
        Player player = event.getPlayer();
        ItemStack itemStack = player.getItemInHand();
        Material material = itemStack.getType();
     
        if (entity.getType() == EntityType.PLAYER)
        {
          if (material == Material.BUCKET)
          {
            ItemStack milk = new ItemStack(Material.MILK_BUCKET);
     
            player.setItemInHand(milk);
     
            event.setCancelled(true);
          }
        }
      }
  12. Offline

    jamietech

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Not related to your issue but you can just add 'ignoreCancelled = true' to your eventhandler annotation and remove the return code.

Share This Page