Spawn pig

Discussion in 'Plugin Development' started by InspectorFacepalm, May 29, 2013.

Thread Status:
Not open for further replies.
  1. So I want to know how to make a event where you can spawn a pig on rightclick with a wooden hoe, is this possible :l
     
  2. Offline

    Minecrell

  3. Offline

    hubeb

    Look up BCBros on YouTube, they have a tutorial on spawning mobs.
     
  4. hubeb Ill look at it if I cant fix it :I
    Minecrell already done, doesn't work
     
  5. Offline

    xize

    you could use PlayerInteractEvent
    also inside the PlayerInteractEvent you can use event.getPlayer().getWorld().spawnEntity(Location, EntityType.PIG);
     
  6. Offline

    hubeb

    Let me see the code, I'll try and fix it when I get home in 20 mins
     
  7. the (Location,
    gives me a error
    also thanks hubeb
     
  8. Offline

    Minecrell

    InspectorFacepalm hubeb Well spawning a mob is just
    Code:java
    1. world.spawn(location, entity-class);

    So this example:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. if (event.hasItem() && (event.getItem().getType() == Material.WOOD_HOE) && (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    4. Location loc = event.getClickedBlock().getLocation();
    5. loc.getWorld().spawn(loc, Pig.class);
    6. }
    7. }

    I don't think you really have to look a tutorial how to spawn a mob :p
     
  9. Doesn't work :/
     
  10. Offline

    Minecrell

  11. Offline

    xize

    that's true I didn't meaned the code a literal but as psuodo however I can give you a working code:

    Code:java
    1.  
    2. @EventHandler
    3. public void pigss(PlayerInteractEvent e) {
    4. if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    5. if(e.getPlayer().getInventory().getItemInHand().getType() == Material.WOOD_HOE) {
    6. Location loc = e.getPlayer().getTargetBlock(null, 50).getLocation();
    7. e.getPlayer().getWorld().spawnEntity(loc, EntityType.PIG);
    8. } else {
    9. return;
    10. }
    11. } else {
    12. return;
    13. }
    14. }
    15.  
     
  12. I went ingame, got a wooden hoe, doesn't spawn a pig when I right click the block
    I've tried left clicking the block, left clicking in the air, right clicking in the air for a possible error, doesnt spawn a pig
     
  13. Offline

    Minecrell

  14. doesn't work too
     
  15. Offline

    Minecrell

    Well, at the moment the pig spawns inside the block, but it works. Maybe you forgot to register your events?
     
  16. Offline

    xize

    try this I just tested this but I saw the pigs where sufficating xD
    Code:java
    1.  
    2. @EventHandler
    3. public void pigss(PlayerInteractEvent e) {
    4. if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    5. if(e.getPlayer().getInventory().getItemInHand().getType() == Material.WOOD_HOE) {
    6. Location loc = e.getPlayer().getTargetBlock(null, 50).getLocation().getBlock().getRelative(BlockFace.UP).getLocation();
    7. e.getPlayer().getWorld().spawnEntity(loc, EntityType.PIG);
    8. } else {
    9. return;
    10. }
    11. } else {
    12. return;
    13. }
    14. }
    15.  
     
  17. Offline

    Minecrell

    xize Yep, same for my code :p
     
  18. still doesn't spawn pigs D:
     
  19. Offline

    Minecrell

    InspectorFacepalm
     
  20. already done, still doesn't spawn da pigs D;
     
  21. Offline

    Garris0n

  22. Offline

    Minecrell

    Can you show us your code? I only used a simple plugin to test it so I don't know what's the problem with your code.
    My test plugin: http://pastebin.com/6Xrx0AvM
     
  23. Full code
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class HarryPotterSpells extends JavaPlugin
      implements Listener
    {
    
        @Override
        public void onEnable() {
                this.getServer().getPluginManager().registerEvents(this, this);
        }
         
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            if (event.hasItem() && (event.getItem().getType() == Material.WOOD_HOE) && (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                Location loc = event.getClickedBlock().getRelative(BlockFace.UP).getLocation();
                loc.getWorld().spawn(loc, Pig.class);
            }
        }
    
        
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if ((cmd.getName().equalsIgnoreCase("spelllist")) && 
                
                  ((sender instanceof Player))) {
                  {
                          sender.sendMessage(ChatColor.GOLD + "-= Spells =-");
                          sender.sendMessage(ChatColor.RED + "Use /SpellInfo [Spell] to check the description of the spells ");
                          sender.sendMessage(ChatColor.GOLD + " Aguamenti ");
                          sender.sendMessage(ChatColor.GOLD + " Alarte Ascendare ");
                          sender.sendMessage(ChatColor.GOLD + " Avada Kedavra ");
                          sender.sendMessage(ChatColor.GOLD + " Confringo ");
                          sender.sendMessage(ChatColor.GOLD + " Confundo ");
                          sender.sendMessage(ChatColor.GOLD + " Deprimo ");
                          sender.sendMessage(ChatColor.GOLD + " Episkey ");
                          sender.sendMessage(ChatColor.GOLD + " Evanesco ");
                          sender.sendMessage(ChatColor.GOLD + " Expelliarmus ");
                          sender.sendMessage(ChatColor.GOLD + " Multicorfors ");
                          sender.sendMessage(ChatColor.GOLD + " Reducto ");
                          sender.sendMessage(ChatColor.GOLD + " Sonorus ");
                          sender.sendMessage(ChatColor.GOLD + " Spongify ");
                          sender.sendMessage(ChatColor.GOLD + " Wingardium Leviosa ");
    
                          return true;
                  }
               
                  }
                
          
    
        return true;
        
      }
    }
    
    don't mind /spelllist part, it was for HarryPotterSpells project, but should work either ways.
     
  24. Offline

    Minecrell

    InspectorFacepalm Looking good... I don't know why it isn't working with your plugin :/
     
  25. Offline

    Garris0n

  26. nope I have only 1 plugin on my server, thats the spawn pig thingy
     
  27. Offline

    bfgbfggf

    Hyym add debug messages in event and check where code stop work.
     
  28. How do I exactly do that ;/
     
  29. Offline

    bfgbfggf

    this.getServer().getPluginManager().registerEvents(this, this);
    hyym. That "this" is needed? Try just remove that
    getServer().getPluginManager().registerEvents(this, this);
     
  30. removed it, doesnt work
     
Thread Status:
Not open for further replies.

Share This Page