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 ;
2013-08-27 22:15:56 -04:00
import java.io.File ;
import java.io.FileInputStream ;
import java.io.IOException ;
import java.util.Collections ;
2012-06-01 23:07:32 -04:00
import java.util.List ;
2013-08-27 22:15:56 -04:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.zip.ZipEntry ;
import java.util.zip.ZipFile ;
2012-12-08 20:21:03 -08:00
import net.minecraft.entity.Entity ;
2012-04-02 18:09:06 -04:00
import net.minecraft.server.MinecraftServer ;
2013-08-27 22:15:56 -04:00
import net.minecraft.util.StringTranslate ;
2013-11-10 11:49:06 -05:00
import com.google.common.collect.ImmutableList ;
2012-04-05 11:34:57 -04:00
import cpw.mods.fml.common.FMLCommonHandler ;
2013-08-27 22:15:56 -04: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 ;
2013-12-02 23:46:42 -05:00
import cpw.mods.fml.common.network.packet.EntitySpawnAdjustmentPacket ;
import cpw.mods.fml.common.network.packet.EntitySpawnPacket ;
import cpw.mods.fml.common.network.packet.ModMissingPacket ;
2012-08-22 23:46:16 -04:00
import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration ;
2012-12-09 22:00:51 -05:00
import cpw.mods.fml.common.registry.GameData ;
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
*/
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
*/
2012-08-02 02:08:30 -02:30
public void finishServerLoading ()
2012-04-05 10:07:52 -04:00
{
Loader . instance (). initializeMods ();
2012-08-13 20:28:53 -04:00
LanguageRegistry . reloadLanguageTable ();
2012-12-09 22:00:51 -05:00
GameData . initializeServerGate ( 1 );
2012-04-05 10:07:52 -04:00
}
2012-07-03 22:39:35 -04:00
@Override
public void haltGame ( String message , Throwable exception )
{
throw new RuntimeException ( message , exception );
}
2012-04-05 16:22:47 -04:00
/**
* Get the server instance
*/
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
}
2012-08-09 08:40:32 -04:00
@Override
2012-08-22 23:46:16 -04:00
public Entity spawnEntityIntoClientWorld ( EntityRegistration er , EntitySpawnPacket packet )
2012-08-09 08:40:32 -04:00
{
// NOOP
return null ;
}
@Override
public void adjustEntityLocationOnClient ( EntitySpawnAdjustmentPacket entitySpawnAdjustmentPacket )
{
// NOOP
}
2012-08-12 22:45:18 -04:00
@Override
2012-08-26 22:13:43 -04:00
public void displayMissingMods ( ModMissingPacket modMissingPacket )
{
// NOOP on server
}
2012-12-07 01:52:16 -05:00
@Override
public boolean shouldServerShouldBeKilledQuietly ()
{
return false ;
}
2013-07-02 01:39:40 -04:00
@Override
public void addModAsResource ( ModContainer container )
{
2013-08-27 22:15:56 -04:00
File source = container . getSource ();
2013-09-20 17:02:56 -04:00
try
2013-08-27 22:15:56 -04:00
{
if ( source . isDirectory ())
{
searchDirForENUSLanguage ( source , "" );
}
else
{
searchZipForENUSLanguage ( source );
}
}
2013-09-20 17:02:56 -04:00
catch ( IOException ioe )
2013-08-27 22:15:56 -04:00
{
2013-09-20 17:02:56 -04:00
2013-08-27 22:15:56 -04:00
}
}
private static final Pattern assetENUSLang = Pattern . compile ( "assets/(.*)/lang/en_US.lang" );
private void searchZipForENUSLanguage ( File source ) throws IOException
{
ZipFile zf = new ZipFile ( source );
for ( ZipEntry ze : Collections . list ( zf . entries ()))
{
Matcher matcher = assetENUSLang . matcher ( ze . getName ());
if ( matcher . matches ())
{
FMLLog . fine ( "Injecting found translation data in zip file %s at %s into language system" , source . getName (), ze . getName ());
StringTranslate . inject ( zf . getInputStream ( ze ));
}
}
2013-09-02 00:12:36 -04:00
zf . close ();
2013-08-27 22:15:56 -04:00
}
private void searchDirForENUSLanguage ( File source , String path ) throws IOException
{
for ( File file : source . listFiles ())
{
String currPath = path + file . getName ();
if ( file . isDirectory ())
{
searchDirForENUSLanguage ( file , currPath + '/' );
}
Matcher matcher = assetENUSLang . matcher ( currPath );
if ( matcher . matches ())
{
FMLLog . fine ( "Injecting found translation data at %s into language system" , currPath );
StringTranslate . inject ( new FileInputStream ( file ));
}
}
2013-07-02 01:39:40 -04: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
}
2012-04-02 18:09:06 -04:00
}