Custom Horse Egg Problem.

Discussion in 'Plugin Development' started by BlueBearPvP, Apr 20, 2016.

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

    BlueBearPvP

    Hey Guys,

    So I wanted to create a custom recipe that gives you a horse egg. When a player place the horse egg it spawns in a custom horse. So my problem is that I dont know how to remove the horse egg when the player place it.
    Here is the code:

    Recipe:

    Code:
    public void ninerecipe() {
              ItemStack bearBare = new ItemStack(Material.MONSTER_EGG, 1,(byte) 100);
              ItemMeta meta = bearBare.getItemMeta();
              meta.setDisplayName(ChatColor.GREEN + "BearBare");
              meta.setLore(Arrays.asList(ChatColor.GRAY + "  ", ChatColor.BLUE + "Extra Ulitmate - Mob", ChatColor.GRAY + "  ", ChatColor.GRAY + "With great speed and heights", ChatColor.GRAY +"this animal is undefeatable!"));
              bearBare.setItemMeta(meta);
          
              ShapedRecipe ninerecipe = new ShapedRecipe(bearBare);
              ninerecipe.shape(
                            "PS ",
                            "BBB",
                            "B B"          
                                 );
              ninerecipe.setIngredient('P', Material.SKULL_ITEM, (short) 3);
              ninerecipe.setIngredient('S', Material.SADDLE);
              ninerecipe.setIngredient('B', Material.BONE);
          
              Bukkit.getServer().addRecipe(ninerecipe);
          
          }
    Listener:

    Code:
    @EventHandler
         public void onPlaceBearBare(PlayerInteractEvent e) {
             Player p = e.getPlayer();
             if (e.getAction().equals((Object)Action.RIGHT_CLICK_BLOCK) && e.getItem()!= null) {
                 if (e.getItem().getType().equals((Object)Material.MONSTER_EGG) && e.getItem().getDurability() == 100
                         && e.getItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "BearBare")) {
                     e.setCancelled(true);
                     p.getInventory().removeItem(new ItemStack(Material.MONSTER_EGG, 1)); //Where I try to remove the egg from inventory.
                     Horse horse = (Horse)e.getClickedBlock().getWorld().spawnEntity(e.getClickedBlock().getLocation().add(0.0, 1.0,0.0), EntityType.HORSE);
                     horse.setAdult();
                     horse.setOwner((AnimalTamer)e.getPlayer());
                     horse.setVariant(Horse.Variant.SKELETON_HORSE);
                     horse.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                     horse.setCustomName(ChatColor.GREEN + "BearBare");
                     horse.setCustomNameVisible(true);
                     horse.setMaxHealth(40.0);
                     horse.setHealth(20.0);
                     horse.setJumpStrength(5.0);
                     horse.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 25, 1));
                 }
             }
         }
    Note: My events are registered and all the horse stuff works fine. Thanks for any help. :D
    There are also no errors in the console.
     
  2. get the item in hand amount
    set it to amount -1 if it is > 1 else set the item in hand to null
     
  3. Offline

    Zombie_Striker

    1. You have to compare enums using '=='
    2. Don't cast the Material to an object.

    You can't remove a new instance of an itemstack. You have to get the instance that is already in the player's hand and remove that itemstack.
     
  4. Offline

    BlueBearPvP

    @Zombie_Striker

    I fix it! Thanks so much. There is one more problem. When I right-click it, it remove the BearBare egg and also other different type of monster eggs in my inventory. Do you know how to fix this? Thanks.

    Code:
    @EventHandler
         public void onPlaceBearBare(PlayerInteractEvent e) {
             Player p = e.getPlayer();
             if (e.getAction().equals((Object)Action.RIGHT_CLICK_BLOCK) && e.getItem()!= null) {
                 if (e.getItem().getType() == Material.MONSTER_EGG && e.getItem().getDurability() == 100
                         && e.getItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "BearBare")) {   
                                 
                     e.setCancelled(true);
                     p.getInventory().remove(Material.MONSTER_EGG);
                    
                     Horse horse = (Horse)e.getClickedBlock().getWorld().spawnEntity(e.getClickedBlock().getLocation().add(0.0, 1.0,0.0), EntityType.HORSE);
                     horse.setAdult();
                     horse.setOwner((AnimalTamer)e.getPlayer());
                     horse.setVariant(Horse.Variant.UNDEAD_HORSE);
                     horse.getInventory().setSaddle(new ItemStack(Material.SADDLE));
                     horse.getInventory().setArmor(new ItemStack(Material.DIAMOND_BARDING));
                     horse.setCustomName(ChatColor.GREEN + "BearBare");
                     horse.setCustomNameVisible(true);
                     horse.setMaxHealth(40.0);
                     horse.setHealth(20.0);
                     horse.setJumpStrength(5.0);
                     horse.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 25, 3));
                    
                    
                 }
             }
     
  5. You're removing any item that's a monster egg, whether it's custom or not. Get the custom item stack and remove it from the player's inventory.
     
Thread Status:
Not open for further replies.

Share This Page