001package net.minecraft.client;
002
003import cpw.mods.fml.relauncher.FMLRelauncher;
004import cpw.mods.fml.relauncher.Side;
005import cpw.mods.fml.relauncher.SideOnly;
006import java.applet.Applet;
007import java.awt.BorderLayout;
008import java.awt.Canvas;
009import net.minecraft.util.Session;
010
011@SideOnly(Side.CLIENT)
012public class MinecraftApplet extends Applet
013{
014    /** Reference to the applet canvas. */
015    private Canvas mcCanvas;
016
017    /** Reference to the Minecraft object. */
018    private Minecraft mc;
019
020    /** Reference to the Minecraft main thread. */
021    private Thread mcThread = null;
022
023    public void init()
024    {
025        FMLRelauncher.appletEntry(this);
026    }
027
028    public void fmlInitReentry()
029    {
030        this.mcCanvas = new CanvasMinecraftApplet(this);
031        boolean flag = "true".equalsIgnoreCase(this.getParameter("fullscreen"));
032        this.mc = new MinecraftAppletImpl(this, this.mcCanvas, this, this.getWidth(), this.getHeight(), flag);
033        this.mc.minecraftUri = this.getDocumentBase().getHost();
034
035        if (this.getDocumentBase().getPort() > 0)
036        {
037            this.mc.minecraftUri = this.mc.minecraftUri + ":" + this.getDocumentBase().getPort();
038        }
039
040        if (this.getParameter("username") != null && this.getParameter("sessionid") != null)
041        {
042            this.mc.session = new Session(this.getParameter("username"), this.getParameter("sessionid"));
043            this.mc.getLogAgent().logInfo("Setting user: " + this.mc.session.username);
044            System.out.println("(Session ID is " + this.mc.session.sessionId + ")");
045        }
046        else
047        {
048            this.mc.session = new Session("Player", "");
049        }
050
051        this.mc.setDemo("true".equals(this.getParameter("demo")));
052
053        if (this.getParameter("server") != null && this.getParameter("port") != null)
054        {
055            this.mc.setServer(this.getParameter("server"), Integer.parseInt(this.getParameter("port")));
056        }
057
058        this.mc.hideQuitButton = !"true".equals(this.getParameter("stand-alone"));
059        this.setLayout(new BorderLayout());
060        this.add(this.mcCanvas, "Center");
061        this.mcCanvas.setFocusable(true);
062        this.mcCanvas.setFocusTraversalKeysEnabled(false);
063        this.validate();
064    }
065
066    public void startMainThread()
067    {
068        if (this.mcThread == null)
069        {
070            this.mcThread = new Thread(this.mc, "Minecraft main thread");
071            this.mcThread.start();
072        }
073    }
074
075    public void start()
076    {
077        FMLRelauncher.appletStart(this);
078    }
079
080    public void fmlStartReentry()
081    {
082        if (this.mc != null)
083        {
084            this.mc.isGamePaused = false;
085        }
086    }
087
088    public void stop()
089    {
090        if (this.mc != null)
091        {
092            this.mc.isGamePaused = true;
093        }
094    }
095
096    public void destroy()
097    {
098        this.shutdown();
099    }
100
101    /**
102     * Called when the applet window is closed.
103     */
104    public void shutdown()
105    {
106        if (this.mcThread != null)
107        {
108            this.mc.shutdown();
109
110            try
111            {
112                this.mcThread.join(10000L);
113            }
114            catch (InterruptedException interruptedexception)
115            {
116                try
117                {
118                    this.mc.shutdownMinecraftApplet();
119                }
120                catch (Exception exception)
121                {
122                    exception.printStackTrace();
123                }
124            }
125
126            this.mcThread = null;
127        }
128    }
129}