2012-04-02 18:09:06 -04:00
/*
* The FML Forge Mod Loader suite. Copyright (C) 2012 cpw
2012-04-05 10:07:52 -04:00
*
2012-04-02 18:09:06 -04:00
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or any later version.
2012-04-05 10:07:52 -04:00
*
2012-04-02 18:09:06 -04:00
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
2012-04-05 10:07:52 -04:00
*
2012-04-02 18:09:06 -04:00
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package cpw.mods.fml.server ;
2014-04-05 01:47:19 +02:00
import java.io.File ;
2014-04-01 14:30:47 +02:00
import java.util.Iterator ;
2012-06-01 23:07:32 -04:00
import java.util.List ;
2014-01-16 19:45:42 -05:00
import java.util.Set ;
2014-04-01 14:30:47 +02:00
import net.minecraft.command.ServerCommand ;
2013-12-31 09:45:26 -05:00
import net.minecraft.network.INetHandler ;
2014-01-16 19:45:42 -05:00
import net.minecraft.network.NetHandlerPlayServer ;
2013-12-06 11:17:40 -05:00
import net.minecraft.network.NetworkManager ;
2012-04-02 18:09:06 -04:00
import net.minecraft.server.MinecraftServer ;
2014-03-11 00:50:55 -07:00
import net.minecraft.server.dedicated.DedicatedServer ;
2014-04-05 01:47:19 +02:00
import net.minecraft.world.storage.SaveFormatOld ;
2014-04-01 14:30:47 +02:00
2013-11-10 11:49:06 -05:00
import com.google.common.collect.ImmutableList ;
2014-04-01 14:30:47 +02:00
2012-04-05 11:34:57 -04:00
import cpw.mods.fml.common.FMLCommonHandler ;
2014-04-01 14:30:47 +02:00
import cpw.mods.fml.common.FMLLog ;
2012-04-05 11:34:57 -04:00
import cpw.mods.fml.common.IFMLSidedHandler ;
2012-04-03 17:04:26 -04:00
import cpw.mods.fml.common.Loader ;
2013-07-02 01:39:40 -04:00
import cpw.mods.fml.common.ModContainer ;
2014-04-01 14:30:47 +02:00
import cpw.mods.fml.common.StartupQuery ;
2014-01-16 19:45:42 -05:00
import cpw.mods.fml.common.eventhandler.EventBus ;
import cpw.mods.fml.common.network.FMLNetworkEvent ;
2012-08-13 20:28:53 -04:00
import cpw.mods.fml.common.registry.LanguageRegistry ;
2012-12-15 23:21:26 -05:00
import cpw.mods.fml.relauncher.Side ;
2012-04-02 18:09:06 -04:00
2012-04-05 16:22:47 -04:00
/**
* Handles primary communication from hooked code into the system
2012-07-03 22:39:35 -04:00
*
2012-09-15 18:04:56 -07:00
* The FML entry point is {@link #beginServerLoading(MinecraftServer)} called from
2013-01-21 21:37:29 -05:00
* {@link net.minecraft.server.dedicated.DedicatedServer}
2012-07-03 22:39:35 -04:00
*
2012-04-19 22:40:59 -04:00
* Obfuscated code should focus on this class and other members of the "server"
* (or "client") code
2012-07-03 22:39:35 -04:00
*
2012-04-05 16:22:47 -04:00
* The actual mod loading is handled at arms length by {@link Loader}
2012-07-03 22:39:35 -04:00
*
2012-04-19 22:40:59 -04:00
* It is expected that a similar class will exist for each target environment:
* Bukkit and Client side.
2012-07-03 22:39:35 -04:00
*
2012-04-05 16:22:47 -04:00
* It should not be directly modified.
2012-07-03 22:39:35 -04:00
*
2012-04-05 16:22:47 -04:00
* @author cpw
2012-07-03 22:39:35 -04:00
*
2012-04-05 16:22:47 -04:00
*/
2012-04-05 11:34:57 -04:00
public class FMLServerHandler implements IFMLSidedHandler
2012-04-05 10:07:52 -04:00
{
2012-04-05 16:22:47 -04:00
/**
* The singleton
*/
2012-04-05 11:34:57 -04:00
private static final FMLServerHandler INSTANCE = new FMLServerHandler ();
2012-04-05 10:07:52 -04:00
2012-04-05 16:22:47 -04:00
/**
* A reference to the server itself
*/
2012-04-05 10:07:52 -04:00
private MinecraftServer server ;
2012-08-09 09:21:16 -04:00
private FMLServerHandler ()
{
FMLCommonHandler . instance (). beginLoading ( this );
}
2012-04-05 16:22:47 -04:00
/**
2012-04-19 22:40:59 -04:00
* Called to start the whole game off from
* {@link MinecraftServer#startServer}
2012-07-03 22:39:35 -04:00
*
2012-04-05 16:22:47 -04:00
* @param minecraftServer
*/
2014-01-17 23:19:22 -05:00
@Override
2012-08-02 02:08:30 -02:30
public void beginServerLoading ( MinecraftServer minecraftServer )
2012-04-05 10:07:52 -04:00
{
server = minecraftServer ;
Loader . instance (). loadMods ();
}
2012-04-05 16:22:47 -04:00
/**
* Called a bit later on during server initialization to finish loading mods
*/
2014-01-17 23:19:22 -05:00
@Override
2012-08-02 02:08:30 -02:30
public void finishServerLoading ()
2012-04-05 10:07:52 -04:00
{
Loader . instance (). initializeMods ();
}
2012-07-03 22:39:35 -04:00
@Override
public void haltGame ( String message , Throwable exception )
{
throw new RuntimeException ( message , exception );
}
2014-04-01 21:56:53 +02:00
@Override
2014-04-05 01:47:19 +02:00
public File getSavesDirectory ()
2014-04-01 21:56:53 +02:00
{
2014-04-05 01:47:19 +02:00
return (( SaveFormatOld ) server . getActiveAnvilConverter ()). savesDirectory ;
2014-04-01 21:56:53 +02:00
}
2012-04-05 16:22:47 -04:00
/**
* Get the server instance
*/
2014-01-17 23:19:22 -05:00
@Override
2012-04-05 10:07:52 -04:00
public MinecraftServer getServer ()
{
return server ;
}
/**
* @return the instance
*/
2012-04-05 11:34:57 -04:00
public static FMLServerHandler instance ()
2012-04-05 10:07:52 -04:00
{
return INSTANCE ;
}
2012-06-01 23:07:32 -04:00
/* (non-Javadoc)
* @see cpw.mods.fml.common.IFMLSidedHandler#getAdditionalBrandingInformation()
*/
@Override
public List < String > getAdditionalBrandingInformation ()
{
2012-11-16 22:39:47 -05:00
return ImmutableList . < String > of ();
2012-06-01 23:07:32 -04:00
}
2012-06-02 15:13:55 -04:00
/* (non-Javadoc)
* @see cpw.mods.fml.common.IFMLSidedHandler#getSide()
*/
@Override
public Side getSide ()
{
return Side . SERVER ;
}
2012-08-08 00:31:24 -04:00
@Override
public void showGuiScreen ( Object clientGuiElement )
{
2012-08-11 14:12:39 -04:00
2012-08-08 00:31:24 -04:00
}
2014-04-01 14:30:47 +02:00
@Override
public void queryUser ( StartupQuery query ) throws InterruptedException
{
if ( query . getResult () == null )
{
FMLLog . warning ( "%s" , query . getText ());
query . finish ();
}
else
{
String text = query . getText () +
"\n\nRun the command /fml confirm or or /fml cancel to proceed." +
"\nAlternatively start the server with -Dfml.queryResult=confirm or -Dfml.queryResult=cancel to preselect the answer." ;
FMLLog . warning ( "%s" , text );
2014-04-01 21:56:53 +02:00
if ( ! query . isSynchronous ()) return ; // no-op until mc does commands in another thread (if ever)
2014-04-01 14:30:47 +02:00
boolean done = false ;
while ( ! done && server . isServerRunning ())
{
if ( Thread . interrupted ()) throw new InterruptedException ();
DedicatedServer dedServer = ( DedicatedServer ) server ;
// rudimentary command processing, check for fml confirm/cancel and stop commands
synchronized ( dedServer . pendingCommandList )
{
for ( Iterator < ServerCommand > it = dedServer . pendingCommandList . iterator (); it . hasNext (); )
{
String cmd = it . next (). command . trim (). toLowerCase ();
if ( cmd . equals ( "/fml confirm" ))
{
FMLLog . info ( "confirmed" );
query . setResult ( true );
done = true ;
it . remove ();
}
else if ( cmd . equals ( "/fml cancel" ))
{
FMLLog . info ( "cancelled" );
query . setResult ( false );
done = true ;
it . remove ();
}
else if ( cmd . equals ( "/stop" ))
{
StartupQuery . abort ();
}
}
}
Thread . sleep ( 10L );
}
query . finish ();
}
}
2012-08-12 22:45:18 -04:00
@Override
2012-12-07 01:52:16 -05:00
public boolean shouldServerShouldBeKilledQuietly ()
{
return false ;
}
2013-07-02 01:39:40 -04:00
@Override
public void addModAsResource ( ModContainer container )
{
2014-01-17 17:47:30 -05:00
LanguageRegistry . instance (). loadLanguagesFor ( container , Side . SERVER );
2013-07-02 01:39:40 -04:00
}
2014-01-17 17:47:30 -05:00
2013-07-10 00:42:40 -04:00
@Override
public void updateResourcePackList ()
{
2013-09-20 17:02:56 -04:00
2013-07-10 00:42:40 -04:00
}
2013-09-13 19:04:25 -05:00
@Override
public String getCurrentLanguage ()
{
return "en_US" ;
}
2013-09-20 17:02:56 -04:00
@Override
public void serverStopped ()
{
// NOOP
}
2013-12-06 11:17:40 -05:00
@Override
public NetworkManager getClientToServerNetworkManager ()
{
throw new RuntimeException ( "Missing" );
}
2013-12-31 09:45:26 -05:00
@Override
public INetHandler getClientPlayHandler ()
{
return null ;
}
2014-01-16 14:58:28 -05:00
@Override
public void waitForPlayClient ()
{
// NOOP
}
2014-01-16 19:45:42 -05:00
@Override
public void fireNetRegistrationEvent ( EventBus bus , NetworkManager manager , Set < String > channelSet , String channel , Side side )
{
bus . post ( new FMLNetworkEvent . CustomPacketRegistrationEvent < NetHandlerPlayServer > ( manager , channelSet , channel , side , NetHandlerPlayServer . class ));
}
2014-04-01 21:56:53 +02:00
2014-03-11 00:50:55 -07:00
@Override
public boolean shouldAllowPlayerLogins ()
{
return DedicatedServer . allowPlayerLogins ;
}
2012-04-02 18:09:06 -04:00
}