the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#pragma once
2#include "ConsoleInputSource.h"
3#include "..\Minecraft.World\ArrayWithLength.h"
4#include "..\Minecraft.World\SharedConstants.h"
5#include "..\Minecraft.World\C4JThread.h"
6
7class ServerConnection;
8class Settings;
9class PlayerList;
10class EntityTracker;
11class ConsoleInput;
12class ConsoleCommands;
13class LevelStorageSource;
14class ChunkSource;
15class INetworkPlayer;
16class LevelRuleset;
17class LevelType;
18class ProgressRenderer;
19class CommandDispatcher;
20
21#define MINECRAFT_SERVER_SLOW_QUEUE_DELAY 250
22
23#if defined _XBOX_ONE || defined _XBOX || defined __ORBIS__ || defined __PS3__ || defined __PSVITA__
24#define _ACK_CHUNK_SEND_THROTTLING
25#endif
26
27typedef struct _LoadSaveDataThreadParam
28{
29 LPVOID data;
30 __int64 fileSize;
31 const wstring saveName;
32 _LoadSaveDataThreadParam(LPVOID data, __int64 filesize, const wstring &saveName) : data( data ), fileSize( filesize ), saveName( saveName ) {}
33} LoadSaveDataThreadParam;
34
35typedef struct _NetworkGameInitData
36{
37 __int64 seed;
38 LoadSaveDataThreadParam *saveData;
39 DWORD settings;
40 LevelGenerationOptions *levelGen;
41 DWORD texturePackId;
42 bool findSeed;
43 unsigned int xzSize;
44 unsigned char hellScale;
45 ESavePlatform savePlatform;
46
47 _NetworkGameInitData()
48 {
49 seed = 0;
50 saveData = NULL;
51 settings = 0;
52 levelGen = NULL;
53 texturePackId = 0;
54 findSeed = false;
55 xzSize = LEVEL_LEGACY_WIDTH;
56 hellScale = HELL_LEVEL_LEGACY_SCALE;
57 savePlatform = SAVE_FILE_PLATFORM_LOCAL;
58 }
59} NetworkGameInitData;
60
61using namespace std;
62
63// 4J Stu - 1.0.1 updates the server to implement the ServerInterface class, but I don't think we will use any of the functions that defines so not implementing here
64class MinecraftServer : public ConsoleInputSource
65{
66public:
67 static const wstring VERSION;
68 static const int TICK_STATS_SPAN = SharedConstants::TICKS_PER_SECOND * 5;
69
70// static Logger logger = Logger.getLogger("Minecraft");
71 static unordered_map<wstring, int> ironTimers;
72
73private:
74 static const int DEFAULT_MINECRAFT_PORT = 25565;
75 static const int MS_PER_TICK = 1000 / SharedConstants::TICKS_PER_SECOND;
76
77 // 4J Stu - Added 1.0.1, Not needed
78 //wstring localIp;
79 //int port;
80public:
81 ServerConnection *connection;
82 Settings *settings;
83 ServerLevelArray levels;
84
85private:
86 PlayerList *players;
87
88 // 4J Stu - Added 1.0.1, Not needed
89 //long[] tickTimes = new long[TICK_STATS_SPAN];
90 //long[][] levelTickTimes;
91private:
92 ConsoleCommands *commands;
93 bool running;
94 bool m_bLoaded;
95public:
96 bool stopped;
97 int tickCount;
98
99public:
100 wstring progressStatus;
101 int progress;
102private:
103// vector<Tickable *> tickables = new ArrayList<Tickable>(); // 4J - removed
104 CommandDispatcher *commandDispatcher;
105 vector<ConsoleInput *> consoleInput; // 4J - was synchronizedList - TODO - investigate
106public:
107 bool onlineMode;
108 bool animals;
109 bool npcs;
110 bool pvp;
111 bool allowFlight;
112 wstring motd;
113 int maxBuildHeight;
114 int playerIdleTimeout;
115 bool forceGameType;
116
117private:
118 // 4J Added
119 //int m_lastSentDifficulty;
120
121public:
122 // 4J Stu - This value should be incremented every time the list of players with friends-only UGC settings changes
123 // It is sent with PreLoginPacket and compared when it comes back in the LoginPacket
124 DWORD m_ugcPlayersVersion;
125
126 // This value is used to store the texture pack id for the currently loaded world
127 DWORD m_texturePackId;
128
129public:
130 MinecraftServer();
131 ~MinecraftServer();
132private:
133 // 4J Added - LoadSaveDataThreadParam
134 bool initServer(__int64 seed, NetworkGameInitData *initData, DWORD initSettings, bool findSeed);
135 void postProcessTerminate(ProgressRenderer *mcprogress);
136 bool loadLevel(LevelStorageSource *storageSource, const wstring& name, __int64 levelSeed, LevelType *pLevelType, NetworkGameInitData *initData);
137 void setProgress(const wstring& status, int progress);
138 void endProgress();
139 void saveAllChunks();
140 void saveGameRules();
141 void stopServer(bool didInit);
142#ifdef _LARGE_WORLDS
143 void overwriteBordersForNewWorldSize(ServerLevel* level);
144 void overwriteHellBordersForNewWorldSize(ServerLevel* level, int oldHellSize);
145
146#endif
147public:
148 void setMaxBuildHeight(int maxBuildHeight);
149 int getMaxBuildHeight();
150 PlayerList *getPlayers();
151 void setPlayers(PlayerList *players);
152 ServerConnection *getConnection();
153 bool isAnimals();
154 void setAnimals(bool animals);
155 bool isNpcsEnabled();
156 void setNpcsEnabled(bool npcs);
157 bool isPvpAllowed();
158 void setPvpAllowed(bool pvp);
159 bool isFlightAllowed();
160 void setFlightAllowed(bool allowFlight);
161 bool isCommandBlockEnabled();
162 bool isNetherEnabled();
163 bool isHardcore();
164 int getOperatorUserPermissionLevel();
165 CommandDispatcher *getCommandDispatcher();
166 Pos *getCommandSenderWorldPosition();
167 Level *getCommandSenderWorld();
168 int getSpawnProtectionRadius();
169 bool isUnderSpawnProtection(Level *level, int x, int y, int z, shared_ptr<Player> player);
170 void setForceGameType(bool forceGameType);
171 bool getForceGameType();
172 static __int64 getCurrentTimeMillis();
173 int getPlayerIdleTimeout();
174 void setPlayerIdleTimeout(int playerIdleTimeout);
175
176public:
177 void halt();
178 void run(__int64 seed, void *lpParameter);
179
180 void broadcastStartSavingPacket();
181 void broadcastStopSavingPacket();
182
183private:
184 void tick();
185public:
186 void handleConsoleInput(const wstring& msg, ConsoleInputSource *source);
187 void handleConsoleInputs();
188// void addTickable(Tickable tickable); // 4J removed
189 static void main(__int64 seed, void *lpParameter);
190 static void HaltServer(bool bPrimaryPlayerSignedOut=false);
191
192 File *getFile(const wstring& name);
193 void info(const wstring& string);
194 void warn(const wstring& string);
195 wstring getConsoleName();
196 ServerLevel *getLevel(int dimension);
197 void setLevel(int dimension, ServerLevel *level); // 4J added
198 static MinecraftServer *getInstance() { return server; } // 4J added
199 static bool serverHalted() { return s_bServerHalted; }
200 static bool saveOnExitAnswered() { return s_bSaveOnExitAnswered; }
201 static void resetFlags() { s_bServerHalted = false; s_bSaveOnExitAnswered = false; }
202
203 bool flagEntitiesToBeRemoved(unsigned int *flags); // 4J added
204private:
205 //4J Added
206 static MinecraftServer *server;
207
208 static bool setTimeOfDayAtEndOfTick;
209 static __int64 setTimeOfDay;
210 static bool setTimeAtEndOfTick;
211 static __int64 setTime;
212
213 static bool m_bPrimaryPlayerSignedOut; // 4J-PB added to tell the stopserver not to save the game - another player may have signed in in their place, so ProfileManager.IsSignedIn isn't enough
214 static bool s_bServerHalted; // 4J Stu Added so that we can halt the server even before it's been created properly
215 static bool s_bSaveOnExitAnswered; // 4J Stu Added so that we only ask this question once when we exit
216
217 // 4J - added so that we can have a separate thread for post processing chunks on level creation
218 static int runPostUpdate(void* lpParam);
219 C4JThread* m_postUpdateThread;
220 bool m_postUpdateTerminate;
221 class postProcessRequest
222 {
223 public:
224 int x, z;
225 ChunkSource *chunkSource;
226 postProcessRequest(int x, int z, ChunkSource *chunkSource) : x(x), z(z), chunkSource(chunkSource) {}
227 };
228 vector<postProcessRequest> m_postProcessRequests;
229 CRITICAL_SECTION m_postProcessCS;
230public:
231 void addPostProcessRequest(ChunkSource *chunkSource, int x, int z);
232
233public:
234 static PlayerList *getPlayerList() { if( server != NULL ) return server->players; else return NULL; }
235 static void SetTimeOfDay(__int64 time) { setTimeOfDayAtEndOfTick = true; setTimeOfDay = time; }
236 static void SetTime(__int64 time) { setTimeAtEndOfTick = true; setTime = time; }
237
238 C4JThread::Event* m_serverPausedEvent;
239private:
240 // 4J Added
241 bool m_isServerPaused;
242
243 // 4J Added - A static that stores the QNet index of the player that is next allowed to send a packet in the slow queue
244#ifdef _ACK_CHUNK_SEND_THROTTLING
245 static bool s_hasSentEnoughPackets;
246 static __int64 s_tickStartTime;
247 static vector<INetworkPlayer *> s_sentTo;
248 static const int MAX_TICK_TIME_FOR_PACKET_SENDS = 35;
249#else
250 static int s_slowQueuePlayerIndex;
251 static int s_slowQueueLastTime;
252 static bool s_slowQueuePacketSent;
253#endif
254
255 bool IsServerPaused() { return m_isServerPaused; }
256
257private:
258 // 4J Added
259 bool m_saveOnExit;
260 bool m_suspending;
261
262public:
263 static bool chunkPacketManagement_CanSendTo(INetworkPlayer *player);
264 static void chunkPacketManagement_DidSendTo(INetworkPlayer *player);
265#ifndef _ACK_CHUNK_SEND_THROTTLING
266 static void cycleSlowQueueIndex();
267#endif
268
269 void chunkPacketManagement_PreTick();
270 void chunkPacketManagement_PostTick();
271
272 void setSaveOnExit(bool save) { m_saveOnExit = save; s_bSaveOnExitAnswered = true; }
273 void Suspend();
274 bool IsSuspending();
275
276 // 4J Stu - A load of functions were all added in 1.0.1 in the ServerInterface, but I don't think we need any of them
277};