Solved CreatureSpawnEvent with Metadata

Discussion in 'Plugin Development' started by mrgreen33gamer, Jul 21, 2014.

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

    mrgreen33gamer

    Hello,

    I am having some trouble :(! I need to make it to were I can allow ONLY certain mobs spawn within a WorldGuard region. I got the format setup and everything, but it doesn't want to work :(!

    Here is my code:

    Code:java
    1.  
    2. @EventHandler
    3. public void spawnCreature(PlayerInteractEvent event){
    4. Player player = event.getPlayer();
    5.  
    6. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    7. if(player.getItemInHand().getType() == Material.DIAMOND_AXE){
    8. if(event.getClickedBlock().getType() != Material.AIR){
    9. Zombie customzombie = (Zombie)player.getWorld().spawnEntity(event.getClickedBlock().getLocation(), EntityType.ZOMBIE);
    10. customzombie.setMetadata("zombie-spawn", new FixedMetadataValue(this.plugin, "true"));
    11. customzombie.setCustomName("Fatty");
    12. customzombie.setCustomNameVisible(true);
    13. }
    14. }
    15. }
    16. }
    17.  
    18. @EventHandler
    19. public void onMobSpawn(CreatureSpawnEvent event){
    20. if(event.getEntity() instanceof Zombie){
    21. WorldGuardPlugin wg = (WorldGuardPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
    22. ApplicableRegionSet set = wg.getRegionManager(event.getEntity().getWorld()).getApplicableRegions(event.getEntity().getLocation());
    23. for (ProtectedRegion region : set) {
    24. if(region.getId().equals("mobgame")){
    25. if(!event.getEntity().hasMetadata("zombie-spawn")){
    26. event.setCancelled(true);
    27. }
    28. }
    29. }
    30. }
    31. }
    32.  


    Some people say this: "How can a creature spawn before it has metadata." Or "How can the event check if the creature has the Metadata before it is even spawned?"

    In my code, first I set the creature spawn WITH the metadata value, then It checks for it. I don't think is it possible to check for a creature that spawns with Metadata. What I am trying to do is only spawn certain creatures within a WorldGuard region that I call. Mobs that I want to put in the worldguard region. I don't want natural spawns.
     
  2. Offline

    fireblast709

    mrgreen33gamer schedule a 0 tick task (or use runTask) which checks the meta and removes if it is not there. It should work like the following:
    - call to spawnEntity
    - CreatureSpawnEvent is fired, task is scheduled
    - spawnEntity returns, add meta
    - task is ran (should even be the same tick). If the entity has meta it will not be removed.
     
  3. Offline

    mrgreen33gamer

    fireblast709
    SOLVED. So this is what I did. I checked the type of spawn, like Natural or Custom. I denied all that wasn't custom and spawned mobs in the arena, without telling the region: /region flag (name) mob-spawning deny
     
Thread Status:
Not open for further replies.

Share This Page