Events messing with other Events

Discussion in 'Plugin Development' started by SilverNinja555, Dec 6, 2017.

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

    SilverNinja555

    I really do not understand why :
    Code:
    @EventHandler
    
        public void wolfSpawn(EntitySpawnEvent e){
            Entity original = e.getEntity();
            Biome biome = original.getLocation().getBlock().getBiome();
            Material block = original.getLocation().getBlock().getType();
            int random = (int) (Math.random() * 1000);
            if(!original.equals(EntityType.ENDER_CRYSTAL) && !original.equals(EntityType.ENDER_DRAGON) && !original.equals(EntityType.WITHER)){
                if(biome.equals(Biome.SWAMPLAND)){
                    if(random < 5 && (block.equals(Material.DIRT) || block.equals(Material.GRASS_PATH))){
                        e.setCancelled(true);
                        spawnPoisonWolf(instance, original);
                    }
                }
    }
    }
    Messes with :
    Code:
    @EventHandler
    
        public void spawnWolfCheck(InventoryClickEvent e){
    
            HumanEntity user = e.getWhoClicked();
    
            ItemStack item = e.getCurrentItem();
    
            if(e.getInventory().equals(wolfMenu)){
    
                if(item.getData().getItemType().equals(Material.BONE)){
    
                    user.getLocation().getWorld().spawnEntity(user.getLocation(), EntityType.WOLF);
    
                    e.setCancelled(true);
    
                }
    }
    }
    By the way, the InventoryClickEvent worked before I added the EntitySpawnEvent.
     
  2. So for one don't use the human entity, use player to see the inventory click.

    Can you post your registering of the events inside pastebin and the full clas, please?
     
  3. Offline

    SilverNinja555

  4. Offline

    timtower Administrator Administrator Moderator

    @SilverNinja555 Enums need to be compared with ==
    Strings with equals or equalsIgnoreCase
     
  5. Offline

    SilverNinja555

  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    SilverNinja555

  8. Offline

    timtower Administrator Administrator Moderator

    @SilverNinja555 Did you try to debug to see to which check the code runs?
     
  9. Offline

    SilverNinja555

    @timtower I first tested the InventoryClickEvent(btw, change the == wolfMenu to .equals). It for some reason doesn't register the clicking with the EntitySpawnEvent, but works fine without the EntitySpawnEvent.
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    SilverNinja555

  12. Offline

    timtower Administrator Administrator Moderator

    Could you post the code that you used to check that?
     
  13. Offline

    SilverNinja555

    @timtower

    I just did:
    Code:
        public void spawnWolfCheck(InventoryClickEvent e){
          
            Player user = (Player) e.getWhoClicked();
    user.sendMessage("event triggered");
            ItemStack item = e.getCurrentItem();
    
            if(e.getInventory().equals(wolfMenu)){
    user.sendMessage("in wolfMenu");
                if(item.getData().getItemType() == Material.BONE){
    
                    user.getLocation().getWorld().spawnEntity(user.getLocation(), EntityType.WOLF);
    
                    e.setCancelled(true);
    
               
    }
    }
    It did not send the "event triggered"
     
  14. Offline

    SilverNinja555

    So far, every single plugin that I tried to use EntitySpawnEvent has gotten an error when I enable the server, and it makes my whole plugin not work.
     
  15. Care to share the error?

    Also, if your debug message "event triggered" isn't being sent to the player, I would think the event isn't being handled. Ensure you have "@EventHandler" above it, as well as having the class handling the event registered in your main plugin class.
     
  16. Offline

    SilverNinja555

    Attached Files:

  17. Offline

    Zombie_Striker

    @SilverNinja555
    It seems you have another method that is listening for EntitySpawnEvent which is causing the error. The newer updates do not have that event, so you will need to use another event if you intend to support newer versions.
     
  18. Offline

    SilverNinja555

    @Zombie_Striker What other events are there that can be used to replace EntitySpawnEvent?
     
  19. Offline

    Zombie_Striker

    @SilverNinja555
    • CreatureSpawnEvent if the entity is a creature
    • ItemSpawnEvent if the entity is an Item instance.
     
  20. Offline

    SilverNinja555

    Code:
    public void entitySpawn(CreatureSpawnEvent event){
            LivingEntity entity = event.getEntity();
            if(enabled && !entity.hasMetadata("drops") && !entity.hasMetadata("originalMob")){
                entity.remove();
                Silverfish silverfish = (Silverfish) entity.getWorld().spawnEntity(entity.getLocation(), EntityType.SILVERFISH);
                silverfish.setMaxHealth(entity.getMaxHealth());
                silverfish.setMetadata("originalMob", new FixedMetadataValue(Main.plugin, entity.getType().getName()));
                return;
            }
        }
    I changed it to a CreatureSpawnEvent. This plugin pretty much is meant to replace all spawns with silverfish. I haven't ever used a "SpawnEvent" so I have no idea what this error is:
     

    Attached Files:

  21. Offline

    Zombie_Striker

    @SilverNinja555
    You're creating an inescapable loop. When a creature spawns, you spawn another creature, which then spawns another creature, ect. Make sure the entity is not a silverfish before you spawn another silverfish.
     
  22. Offline

    SilverNinja555

    @Zombie_Striker I changed it, but doesn't the metadata check stop the loop? Or does the plugin set the metadata after you spawn it?

    Also, how would I exclude a mob that spawns? When you kill a silverfish, I make it spawn the original mob and then kill it so it would drop all original mob drops. But, with the CreatureSpawnEvent, it just endlessly spawns silverfish.
     
    Last edited: Mar 1, 2018
  23. Offline

    MattTheBeast

    @SilverNinja555
    Why not make the spawn reason custom then check in your creaturespawnevent if the reason if custom?

    To Spawn your entity:

    Code:
        public void spawnEntity(Location location){
            World mcWorld = ((CraftWorld) location.getWorld()).getHandle();
            EntitySilverfish silverfish = new EntitySilverfish(mcWorld);
            mcWorld.addEntity(silverfish, CreatureSpawnEvent.SpawnReason.CUSTOM);
        }
    
    
    To ingore your entity on CreatureSpawnEvent:

    Code:
    
        public void entitySpawn(CreatureSpawnEvent event){
            if(!event.getSpawnReason().equals(SpawnReason.CUSTOM)){
                //Code goes here
            }
        }
    
    
     
  24. Offline

    SilverNinja555

    @MattTheBeast I've never really used EntitySilverfish, but instead used Silverfish, but I'm guessing EntityLiving is pretty much LivingEntity. EntityLiving cannot be instantiated. How would I create a new EntityLiving?
     
  25. Offline

    MattTheBeast

    @SilverNinja555 Silverfish is the bukkit entity, to get the bukkit entity you must use .getBukkitEntity() , I modifyied the methode for you so it returns the bukkit silverfish.

    Always make sure your making a nms EntityLiving not the bukkit LivingEntity, once you spawn the entity then get the bukkit entity.

    Code:
        public Silverfish spawnEntity(Location location){
            World mcWorld = ((CraftWorld) location.getWorld()).getHandle();
            EntitySilverfish silverfish = new EntitySilverfish(mcWorld);
            mcWorld.addEntity(silverfish, SpawnReason.CUSTOM);
            return (Silverfish) silverfish.getBukkitEntity();
        }
    
     
    Last edited: Mar 6, 2018
  26. @SilverNinja555 *Summons EntiyySilverNinja* Is there a good reason for you to use EntitySilverfish here? You would have to import net.minecraft.server.v1_12_R1.(some more stuff) in order to use it. Any time an update occurs the server will have that version (v1_12_R1) change to what it currently is. Basically, your plugin will *only* work on 1.12.2 and not any older version or a newer version. Use Silverfish unless you are trying to do things that Bukkit/Spigot does not provide a method of doing already (spawning a mob and setting plugin metadata have methods provided by Bukkit). Same goes for CraftWorld and any NMS import.
     
    Last edited: Mar 11, 2018
Thread Status:
Not open for further replies.

Share This Page