I don't understand the New API Update

  • Red, could you kindly explain what the KillNPC Event does?


    Ive been playing with it and Tried to get it to Show the player name with no luck, Ive Tried casting player to the event and Still Nothing


    How do I get the player name out of:


    String TheKiller= (String) event.getKiller(); ?


    the event seems limited at the moment

  • well first of all the event is called NpcDeathEvent so you just need to create an @EventMethod method of this event to intercept all things related to it. Something like this:


    Java
    @EventMethod
    public void NPCDeathEvent (NpcDeathEvent event){
    Player player = event.getkiller()
    getServer().broadcastTextMessage("Killer: " + player.getName())
    }
  • it would help If I didn't forget "@EventMethod" every time i use an new event,too :S and "Player" cannot be called Within the event. thatis wht I ay its Limited and don;t know what to do woth it.


    I get j.c Cannoft be cast to rsingworld.net.objects.player, nothing comes out in the debug console.

  • The getKiller() function of the NpcDeathEvent does not necessarily return a player (see javadoc). It returns an Object which can be null if the npc wasn't killed by a player. Ideally you have to check if there is a killer and if the killer is actually a player (and if it's an instance of Player, you need to cast the result):

    Java
    @EventMethod
    public void onNpcDeath(NpcDeathEvent event){
    if(event.getKiller() != null && event.getKiller() instanceof Player){
    //npc was killed by a player
    Player player = (Player) event.getKiller();
    }
    else{
    //npc wasn't killed by a player (e.g. npc drowned etc)
    }
    }
  • Here is my test method for this.


    Now, when I kill a NPC (with an axe, if that matters) in game the log shows:



    Code
    Loaded Worldpart (v3) from cache: -1 - 0 (129ms)
    < < < < MEDIEVAL REALMS > > > > Player Spawned. . .
    < < < < MEDIEVAL REALMS > > > > NPC Death Event called. . .
    < < < < MEDIEVAL REALMS > > > > NPC killed by an object
    < < < < MEDIEVAL REALMS > > > > NPC was NOT killed by a Player
    NPC (ID: 131) deleted!

    Which implies it's not an instance of a Player object? :huh:

  • may be like:

    Java
    if (event.getKiller().getClass().equals(net.risingworld.api.objects.Player.class)){
    System.out.println(PREFIX + "NPC was killed by a player");
    Player ply = (Player)event.getKiller();
    }
    if (event.getKiller().getClass().equals(net.risingworld.api.objects.Npc.class)){
    System.out.println(PREFIX + "NPC was killed by a npc");
    Npc npc = (Npc)event.getKiller();
    }

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!