Help with CreatureSpawnEvent

Discussion in 'Plugin Help/Development/Requests' started by JigokuSaru, Apr 12, 2015.

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

    JigokuSaru

    As i stated, I'm having an issue figuring out How to use CreatureSpawnEvent. I want to check to see what spawns such as:
    Code:
    public void onSpawn(CreatureSpawnEvent cse){
       if(cse.getEntity() instanceof Villager)
          Villager v = (Villager) cse.getEntity()
           v.setCustomNameVisible()
    Something similar to this and if i can add this after the if:
    Code:
    if(cse.getSpawnReason.equals(SpawnReason.SPAWNER_EGG))
    So all in all How can i check if an entity spawns, see if its a villager , see if it was from a spawn egg, And maybe if it has a name {Tom} For instance, then if all that is met it has its name set visible, it has a potion effect, and what not maybe a set profetion
    I would love the help :D
     
  2. Offline

    sgavster

    Does this not work?
    Do you have events registered, and @EventHandler?
     
  3. Offline

    JigokuSaru

    @sgavster I do have @EventHandler and no it does not work the villager spawns with out effects of any kind
     
  4. Offline

    Zombie_Striker

    get the SpawnReason and compare to see if it's the reason is SpawnReason.NATURAL.

    Also, setCustomNameVisable requires a boolean, and Replace .equals with == When comparing Objects.
     
  5. Offline

    JigokuSaru

    Ok wouldn't natural be for any spawned mob of the type like a creeper In a cave?

    Ok I think i found a slight problem why its not working, My spawn egg im using has a name,since it has to have a name, my code looks like this:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onSpawn(CreatureSpawnEvent e){
            if(e.getEntity() instanceof Villager){
                if(e.getSpawnReason() == SpawnReason.DEFAULT ){
                if(e.getEntity().getCustomName() == "Tommy"){
                Villager v = (Villager) e.getEntity();
                v.setBaby();
                v.setCustomNameVisible(true);
                Bukkit.broadcastMessage(ChatColor.GREEN + "Hello WORLD :D!");
                }
    which was ok untill the custom name part came in. i would like some help before Tuesday (EST)
     
    Last edited by a moderator: Apr 13, 2015
  6. Offline

    teej107

    == compares the Object in memory, not the value. You need to compare the values of the two Objects which you can do with #equals()
     
  7. Offline

    JigokuSaru

    @teej107 The just breaks the hole thing and throws an error: Could not pass event CreatureSpawnEvent to Test
    I know its just that line because I removed it and it fixed it
     
  8. Invisible

    nverdier

    @JigokuSaru Please post the updated code before you changed that back.
     
  9. Offline

    JigokuSaru

    @nverdier
    This is my currant code:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onSpawn(CreatureSpawnEvent e){
            if(e.getEntity() instanceof Villager){
                if(e.getSpawnReason() == SpawnReason.SPAWNER_EGG ){
                    if(e.getEntity().getCustomName().equals("Tommy")){
                Villager v = (Villager) e.getEntity();
                v.setBaby();
                v.setCustomNameVisible(true);
                Bukkit.broadcastMessage(ChatColor.GREEN + "Hello WORLD :D!");
    Oh and the egg im spawning from has a ChatColor to it.
     
    Last edited: Apr 13, 2015
  10. Invisible

    nverdier

    @JigokuSaru Well ummm that indentation is a bit awkward... But what is the error thrown?
     
  11. Offline

    JigokuSaru

    @nverdier
    I know the indentation suck and it throws a "could not pass CreatureSpawnEvent to Test 1.0" something like that
     
  12. Offline

    Konato_K

    @JigokuSaru As far I know getCustomName returns null if the LivingEntity does not have one.
     
  13. Offline

    JigokuSaru

    @Konato_K I do know that but I dont think it has anything to do with this, and Im sure the Villager has a name since the spawn egg Has a set name "Tommy" in red even when any villager spawns it tosses the error in my face. Kinda upsetting to see an error
     
  14. Offline

    Konato_K

    @JigokuSaru Yes, I'm aware of that, but maybe the CreatureSpawnEvent fires before the name is actually applied? I'm just throwing out ideas, and you're not posting the error, so we can't help.
     
  15. Offline

    JigokuSaru

    @Konato_K
    Yeah I posted the error twice...

    Sorry for this but i really need to find out why a Event cant pass the plugin
     
    Last edited by a moderator: Apr 14, 2015
  16. Offline

    ItsMattHogan

    Can you post the full stacktrace please.
     
  17. Offline

    JigokuSaru

  18. Offline

    timtower Moderator Moderator

  19. Offline

    Konato_K

    @JigokuSaru You didn't, this
    is not an error and is not useful at all.
     
  20. Offline

    JigokuSaru

    @Konato_K
    1) Thats what comes up in the error line so i figured its the error
    2)
    Show Spoiler

    Code:
    [10:28:39 ERROR]: Could not pass event CreatureSpawnEvent to TestCustomMobType v
    1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[craftbukkit.jar:git-Bukkit-1a1bc08]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1a1bc08]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callCreatureSp
    awnEvent(CraftEventFactory.java:258) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.World.addEntity(World.java:933) [craftbu
    kkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.ItemMonsterEgg.spawnCreature(ItemMonster
    Egg.java:134) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.ItemMonsterEgg.a(ItemMonsterEgg.java:114
    ) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.ItemMonsterEgg.interactWith(ItemMonsterE
    gg.java:54) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.ItemStack.placeItem(ItemStack.java:117)
    [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:502) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :708) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_40]
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [craftbukki
    t.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:6
    70) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    36) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    26) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :534) [craftbukkit.jar:git-Bukkit-1a1bc08]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
            at Test.Test.onSpawn(Test.java:59) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_40]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[craftbukkit.jar:git-Bukkit-1a1bc08]

    @timtower this?
     
  21. Offline

    bwfcwalshy Retired Staff

    @JigokuSaru Please post Test.java line 59 and anything related to that line.
     
  22. Invisible

    nverdier

    @JigokuSaru The error is on line 59. Please post the entire code.

    EDIT: Ninja'd
     
  23. Offline

    JigokuSaru

    Code:
    package Test;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Test extends JavaPlugin implements Listener {
       
       
        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this,this);
        }
           
           
        public void onDisable(){
           
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            @SuppressWarnings("deprecation")
            ItemStack tom = new ItemStack(383, 1 , (short) 120);
            if(label.equalsIgnoreCase("tom")){
                Player player = (Player) sender;
                player.getInventory().addItem(setName(tom,ChatColor.RED +  "Tommy", null));
                return true;
            }
            return false;
        }
        private ItemStack setName(ItemStack is, String name, List<String> lore){
            ItemMeta im = is.getItemMeta();
            if(name!=null){
                im.setDisplayName(name);
            }
            if(lore!=null){
                im.setLore(lore);
             }
            is.setItemMeta(im);
            return is;
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onSpawn(CreatureSpawnEvent e){
            if(e.getEntity() instanceof Villager){
                if(e.getSpawnReason() == SpawnReason.SPAWNER_EGG ){
                    if(e.getEntity().getCustomName().equals("Tommy")){
                Villager v = (Villager) e.getEntity();
                v.setBaby();
                v.setCustomNameVisible(true);
                Bukkit.broadcastMessage(ChatColor.GREEN + "Hello WORLD :D!");
                }
            }
            }
        }
        }
           
        
    @nverdier
     
  24. Offline

    bwfcwalshy Retired Staff

    @JigokuSaru cmd.getName not label
    Check if they have a custom name before comparing it.
     
  25. Invisible

    nverdier

    @JigokuSaru
    on
    Make sure the entity has a custom name before comparing it.

    OMG STAHP NINJA'ING ME WALSHY!!!!!!!!!

    Edited by bwfcwalshy: Sorry bae ;) <3
     
    Last edited by a moderator: Apr 14, 2015
  26. Offline

    JigokuSaru

    @bwfcwalshy
    That first part is not giving an issue the cmd workd fine

    @nverdier
    Ok how will i do that?
     
    Last edited by a moderator: Apr 14, 2015
  27. Invisible

    nverdier

    It will work fine, but if you use Command#getName() it will return the name in the plugin.yml, regardless of the alias you use.

    Just check that it's not null...
     
  28. Offline

    JigokuSaru

    OH so if(e.getEntity.getCustomName() != null)
    and ok i was told using label is easyer
    @nverdier
     
  29. Offline

    bwfcwalshy Retired Staff

    @JigokuSaru It's shorter but overall worse.

    If there is a #hasCustomNams use that else that will be fine.
     
  30. Offline

    JigokuSaru

    @bwfcwalshy
    No hasCustomName so ill be using that

    Ok now its not working but no errors

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page