Jump to content

GetServer() is Null / What is the best way to get a permanent server instance?


Recommended Posts

Posted (edited)

Apologies if this is a common topic. I've searched through topics on this forum and on Google, and the code I've found is mostly deprecated. I'm currently trying to get the server instance using the below code:

 

Spoiler

//Make given AI attack a random player on the server
    public AITargetPlayer(Entity Attacker)
    {
        
        //Current Minecraft server
        MinecraftServer Curr_Server = Attacker.getServer();
        if(Curr_Server != null)
        {
            LOGGER.info("SERVER IS ACTIVE");
            
            //Get curent game type. If is in survival, then, target player
            GameType Curr_GameType = Curr_Server.getDefaultGameType();
            if(Curr_GameType == GameType.SURVIVAL)
            {    
                
                LOGGER.info("GAME TYPE IS SURVIVAL");
                //Get list of current players in the game
                PlayerList Player_List = Attacker.getServer().getPlayerList();
                
                //Get random player index to have this mob attack
                int PlayerCount = Player_List.getPlayerCount();
                
                //If players in game, then
                if(PlayerCount > 0)
                {    
                    
            
                    java.util.List<ServerPlayer> All_Players = Player_List.getPlayers();                
                    LOGGER.info("Targeting Player " + PlayerCount);
                        
                    RNG R = new RNG(0,  PlayerCount);
                    int Selected_Index = (int)R.Value;
                                
                    //Grab playwer in the list
                    ServerPlayer Targeted_Player = All_Players.get(Selected_Index);
                        
                    //Cast to generic monster type
                    Monster Monster_Class = (Monster)Attacker; 
                        
                    //Set attack target
                    Monster_Class.setTarget(Targeted_Player);
                    
                    //Set move block
                    Monster_Class.setAggressive(true);
                    Monster_Class.getNavigation().moveTo(Targeted_Player, Monster_Class.getSpeed());
                    LOGGER.info("PATHFINDING...");
                }
            }
        }
    }

 

I'm guessing this is the incorrect way to grab the server instance, as it's mostly null? What would be a better way, instead? Any documentation that might help on this? This class is invoked from a LivingEvent event, and "Attacker" is always a spawned Monster.

 

Thanks!

Edited by NuclearLavaLamp
Posted (edited)

you can get the server via ServerLifecycleHooks.getCurrentServer,
if the game is running (there is a World and you are on server) it will never return null

also note there is a code feature which you can use to post code here

Edited by Luis_ST
  • Like 1
Posted
9 minutes ago, Luis_ST said:

you can get the server via ServerLifecycleHooks.getCurrentServer,
if the game is running (there is a World) it will never return null

also note there is a code feature which you can use to post code here

That did the trick. Thank you! 🙂

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.