CreatureSpawnEvent Custom Name

Discussion in 'Plugin Development' started by GamingChrisYT, May 13, 2017.

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

    GamingChrisYT

    Hey I writte a Bukkit plugin, I spawn every 15 sek an Zombie and want to kill him 2sek later. here is that what I try but the error says that the CustomName in the CreaturSpawn event is null.

    Here the Class:

    Code:
    public class Main extends JavaPlugin implements Listener {
        ArrayList<Player> canSeeEntitys = new ArrayList<Player>();
    
        @Override
        public void onEnable() {
    
            Bukkit.getPluginManager().registerEvents(this, this);
    
        }
    
        @EventHandler
        public void OnJoin(PlayerJoinEvent e) {
            final Player p = e.getPlayer();
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    
                @Override
                public void run() {
    
                    int x = p.getLocation().getBlockX();
                    int y = p.getLocation().getBlockY();
                    int z = p.getLocation().getBlockZ();
                    double yaw = p.getLocation().getYaw();
    
                    if (yaw >= -45 && yaw < 45) {
                        z = z - 2;
                    } else if (yaw >= 45 && yaw < 135) {
                        x = x + 2;
                    } else if (yaw >= 135 && yaw < 181 || yaw > 181 && yaw < -135) {
                        z = z + 2;
                    } else if (yaw >= -135 && yaw < -45) {
                        x = x - 2;
                    }
                    y++;
                    long l = 20;
                    Location loc = new Location(p.getWorld(), x, y, z);
                    Zombie zom = (Zombie) p.getWorld().spawnCreature(loc, CreatureType.ZOMBIE);
                    zom.setCustomName(p.getName());
                    zom.setHealth(1);
                    zom.setCanPickupItems(false);
    
                    p.sendMessage(p.getLocation().getYaw() + " ");
                }
    
            }, 0, 300);
    
        }
    
       
        @EventHandler
        public void mob(CreatureSpawnEvent e) {
    
            if (e.getEntity() instanceof Zombie) {
    
                Zombie zom = (Zombie) e.getEntity();
                if (zom.getCustomName() != null) {
                    for (Player all : Bukkit.getOnlinePlayers()) {
                        if (zom.getCustomName().equalsIgnoreCase(all.getName())) {
    
                            Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    
                                @Override
                                public void run() {
    
                                    zom.setHealth(0);
    
                                }
    
                            }, 20 * 2);
                        }
    
                    }
                }
            }
    
        }
    
    }
     
  2. Offline

    martian3333

  3. Offline

    GamingChrisYT

    spigot-1.8.8-R0.1-SNAPSHOT-latest.jar
     
  4. Offline

    martian3333

    @GamingChrisYT
    In your OnJoin i suggest using spawnEntity instead of spawnCreature.... that shouldn't make a huge difference. I also suggest when you're checking if zom.getCustomName != null you also && !zom.getCustomName().isEmpty()
     
Thread Status:
Not open for further replies.

Share This Page