Solved Spawn a Mob in Gamemode 2

Discussion in 'Spigot Plugin Development' started by Dubbly, Feb 18, 2018.

  1. Hello everyone,

    I want to be able to spawn a creeper in gamemode 2, so I tried to write a code, which checks if player clicked a block with a creeper monster egg and where. After that the creeper will be spawned there and it will remove 1 creeper spawn egg from player's inventory, but it didn't work. Can you find the mistake, please?

    Code:
    Code (Text):
    @SuppressWarnings("deprecation")
            @EventHandler
               public void onInteract(PlayerInteractEvent e){
                   ItemStack CE = new ItemStack(Material.MONSTER_EGG, EntityType.CREEPER.getTypeId());
                    if(e.getPlayer().getInventory().getItemInHand().equals(CE)) {
                        if (e.getAction() == (Action.RIGHT_CLICK_BLOCK)) {
                            e.getPlayer().getInventory().remove(CE);
                            e.getPlayer().getWorld().spawnEntity(e.getClickedBlock().getLocation(), EntityType.CREEPER);
                           
                           
                        }
                   
                    }
                }
     
  2. I did something relatively simple 2 years ago. Code is pretty bad, but it should do the trick.


    Code (Text):
            Player p = e.getPlayer();

            //Check the player's inventory slot specific to their item hand
            int slot = p.getInventory().getHeldItemSlot();

            //As they right click, remove that item (monster egg(s)) in their held item slot
            p.getInventory().removeItem(new ItemStack(Material.MONSTER_EGG) );
            p.getInventory().removeItem(new ItemStack(Material.MONSTER_EGGS) );
            ItemStack item = p.getInventory().getItem(slot);


            item.setAmount(item.getAmount() - 1);

            p.getInventory().setItem(slot, item);
     
  3. What exactly isn't working?

    If it's doing nothing then my assumption is going to be it's because you use #equals() to compare two ItemStacks. Using #equals() will take the item amount into account. You are better off using #isSimilar() because this method ignores the item amount.
     
    • Agree Agree x 1
  4. So here is my code, but it still doesn't work:

    Code (Text):
    @SuppressWarnings("deprecation")
            @EventHandler
               public void onInteract(PlayerInteractEvent e){
                 
                 
                   
                 
                   ItemStack CE = new ItemStack(Material.MONSTER_EGG);
                    if(e.getPlayer().getInventory().getItemInHand().isSimilar(CE)) {
                        if (e.getAction() == (Action.RIGHT_CLICK_BLOCK)) {
                            int slot = e.getPlayer().getInventory().getHeldItemSlot();
                            ItemStack item = e.getPlayer().getInventory().getItem(slot);
                            item.setAmount(item.getAmount() -1);
                            e.getPlayer().getInventory().setItem(slot, item);
                            e.getPlayer().getWorld().spawnEntity(e.getClickedBlock().getLocation(), EntityType.CREEPER);
                           
                           
                        }
                   
                    }
                }
     
  5. What part isn't working though?

    Add some debug code to see how far along it passes through first.
    Is it not taking the item away thus making it infinite spawning?
    Is the creeper not spawning or in the wrong location?
    Etc.
     
  6. This part doesn't work:
    Code (Text):
     if(e.getPlayer().getInventory().getItemInHand().isSimilar(CE)) {
     
  7. Why are you even creating a new ItemStack. That's inefficient and useless.

    Just compare the Type, Material etc directly.

    ItemStack#getType == Material.STONE
     
  8. It worked! Thank you