package com.ingotpowered;

import com.ingotpowered.api.Config;
import com.ingotpowered.api.Ingot;
import com.ingotpowered.api.Player;
import com.ingotpowered.api.commands.CommandRegistry;
import com.ingotpowered.api.events.EventFactory;
import com.ingotpowered.net.packets.Packet;
import com.ingotpowered.world.ChunkThread;
import com.ingotpowered.net.NetManager;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class IngotServer extends Ingot {

    public static IngotServer server = new IngotServer();

    public static void main(String[] args) {
        Ingot.setServer(server);
        server.init();
    }

    public Config config = new Config();
    public ConcurrentHashMap<String, IngotPlayer> playerMap = new ConcurrentHashMap<String, IngotPlayer>();
    public NetManager netManager = new NetManager(playerMap);
    public ThreadKeepAlive threadKeepAlive = new ThreadKeepAlive();
    public ChunkThread[] chunkThreads = new ChunkThread[Runtime.getRuntime().availableProcessors()];
    public EventFactory eventFactory;
    public PluginLoader pluginLoader = new PluginLoader();
    public ConsoleCommands consoleCommands = new ConsoleCommands();
    public CommandRegistry commandRegistry = new CommandRegistry();

    private void init() {
        config.load();
        netManager.start();
        threadKeepAlive.start();
        for (int i = 0; i < chunkThreads.length; i++) {
            chunkThreads[i] = new ChunkThread();
            chunkThreads[i].start();
        }
        eventFactory = new EventFactory();
        pluginLoader.load();
        consoleCommands.startHandling();
    }

    public void dispatchCommand(String command) {
        consoleCommands.dispatchConsoleCommand(command);
    }

    public void sendGlobalPacket(Packet packet) {
        Iterator<IngotPlayer> iterator = playerMap.values().iterator();
        while (iterator.hasNext()) {
            iterator.next().channel.pipeline().writeAndFlush(packet);
        }
    }

    public IngotPlayer getPlayer(String name) {
        return playerMap.get(name);
    }

    public void stop() {
        consoleCommands.stopped = true;
        netManager.shutdown();
        threadKeepAlive.interrupt();
        try {
            threadKeepAlive.join();
        } catch (Exception ex) { }
        for (ChunkThread c : chunkThreads) {
            c.interrupt();
        }
        pluginLoader.unload();
        eventFactory.destroyThreadPool();
        System.exit(0);
    }

    public CommandRegistry getCommandRegistry() {
        return commandRegistry;
    }

    public Config getConfig() {
        return config;
    }

    public EventFactory getEventFactory() {
        return eventFactory;
    }
}

@@ -95,44 +95,8 @@
 ot;%0A
-import com.ingotpowered.api.Player;%0A
 impo
@@ -238,16 +238,69 @@
 Packet;%0A
+import com.ingotpowered.net.packets.play.PacketChat;%0A
 import c
@@ -2502,32 +2502,235 @@
 exit(0);%0A    %7D%0A%0A
+    public void broadcastMessage(String message) %7B%0A        PacketChat packet = new PacketChat(IngotPlayer.JSON_CHAT_MESSAGE_BASE.replace(%22$%7Bmessage%7D%22, message));%0A        sendGlobalPacket(packet);%0A    %7D%0A%0A
     public Comma

Allow global messages