Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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

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

  • Author
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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.