Server.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package me.km;
  2. import me.hammerle.snuviscript.config.SnuviConfig;
  3. import me.kcm.events.Hooks;
  4. import me.km.blockprotections.BlockProtectionBank;
  5. import me.km.blockprotections.BlockProtectionEvents;
  6. import me.km.blockprotections.DummyBlockProtection;
  7. import me.km.blockprotections.IBlockProtection;
  8. import me.km.databank.DataBank;
  9. import me.km.events.CustomEventCaller;
  10. import me.km.overrides.ModPlayerList;
  11. import me.km.permissions.ModCommandManager;
  12. import me.km.permissions.PermissionManager;
  13. import me.km.playerbank.DummyPlayerBank;
  14. import me.km.playerbank.IPlayerBank;
  15. import me.km.playerbank.PlayerBank;
  16. import me.km.playerbank.PlayerManager;
  17. import me.km.plots.DummyProtection;
  18. import me.km.plots.ProtectionBank;
  19. import me.km.plots.ProtectionEvents;
  20. import me.km.scheduler.SnuviScheduler;
  21. import me.km.snuviscript.SnuviLogger;
  22. import me.km.utils.ReflectionUtils;
  23. import net.minecraft.server.dedicated.DedicatedServer;
  24. import net.minecraftforge.api.distmarker.Dist;
  25. import net.minecraftforge.api.distmarker.OnlyIn;
  26. import net.minecraftforge.common.MinecraftForge;
  27. import me.km.plots.IProtection;
  28. import me.km.snuviscript.CommandGiveUp;
  29. import me.km.snuviscript.CommandScript;
  30. import me.km.snuviscript.DummyScriptBank;
  31. import me.km.snuviscript.IScriptBank;
  32. import me.km.snuviscript.MinecraftFunctions;
  33. import me.km.snuviscript.ScriptBank;
  34. import me.km.snuviscript.ScriptEvents;
  35. import me.km.snuviscript.Scripts;
  36. @OnlyIn(Dist.DEDICATED_SERVER)
  37. public class Server
  38. {
  39. private static SnuviLogger logger;
  40. private static ModCommandManager commands;
  41. private static DedicatedServer server;
  42. private static SnuviScheduler scheduler;
  43. private static DataBank databank;
  44. private static Scripts scripts;
  45. public static ScriptEvents scriptEvents;
  46. public static void onStart(DedicatedServer serverIn)
  47. {
  48. server = serverIn;
  49. logger = new SnuviLogger();
  50. SnuviConfig conf = new SnuviConfig(logger, "", "config");
  51. if(conf.exists())
  52. {
  53. conf.load();
  54. }
  55. boolean debug = conf.getBoolean("debug", false);
  56. // permissions
  57. PermissionManager perms = new PermissionManager(debug);
  58. logger.setPlayerLoggingData(server, perms);
  59. if(debug)
  60. {
  61. logger.print("------------------------------------------------");
  62. logger.print("Starting server in debug mode");
  63. logger.print("------------------------------------------------");
  64. }
  65. // scheduler
  66. scheduler = new SnuviScheduler(logger);
  67. // mod player list hook
  68. Hooks.setPlayerListFunction(ds ->
  69. {
  70. ds.setPlayerList(new ModPlayerList(ds, scheduler));
  71. });
  72. // scripts
  73. scripts = new Scripts(logger, scheduler);
  74. scriptEvents = new ScriptEvents(scripts, server, perms);
  75. MinecraftForge.EVENT_BUS.register(scriptEvents);
  76. // command manager
  77. commands = new ModCommandManager(true, perms, scriptEvents, scripts, scheduler);
  78. ReflectionUtils.setCommandManager(server, commands);
  79. commands.registerCommand(new CommandGiveUp(scripts));
  80. commands.registerCommand(new CommandScript(scripts, server));
  81. // data base connection
  82. databank = new DataBank(logger, conf.getString("user", "root"), conf.getString("password", ""));
  83. if(!databank.openDataBankConnection())
  84. {
  85. logger.print("------------------------------------------------");
  86. logger.print("Starting server with dummy databank");
  87. logger.print("------------------------------------------------");
  88. }
  89. databank.startReconnecting(scheduler);
  90. // protections / block protections / player manager
  91. IProtection protection;
  92. IBlockProtection blockProtection;
  93. IPlayerBank playerBank;
  94. if(databank.isDummyDatabank())
  95. {
  96. protection = new DummyProtection();
  97. blockProtection = new DummyBlockProtection();
  98. playerBank = new DummyPlayerBank();
  99. }
  100. else
  101. {
  102. protection = new ProtectionBank(databank);
  103. blockProtection = new BlockProtectionBank(databank);
  104. playerBank = new PlayerBank(databank);
  105. }
  106. MinecraftForge.EVENT_BUS.register(new ProtectionEvents(protection, perms));
  107. MinecraftForge.EVENT_BUS.register(new BlockProtectionEvents(blockProtection, perms));
  108. PlayerManager playerManager = new PlayerManager(logger, playerBank);
  109. MinecraftForge.EVENT_BUS.register(playerManager);
  110. CustomEventCaller customEventCaller = new CustomEventCaller(playerManager, server, scheduler);
  111. scripts.setCustomEventCaller(customEventCaller);
  112. MinecraftForge.EVENT_BUS.register(customEventCaller);
  113. // scripts
  114. IScriptBank scriptBank;
  115. if(databank.isDummyDatabank())
  116. {
  117. scriptBank = new DummyScriptBank();
  118. }
  119. else
  120. {
  121. scriptBank = new ScriptBank(databank, playerManager);
  122. }
  123. MinecraftFunctions.registerFunctions(
  124. scripts.getScriptManager(), scripts, perms, scheduler, server, playerBank,
  125. customEventCaller, scriptBank, databank, blockProtection, protection);
  126. scripts.startScript("startscript");
  127. }
  128. public static void onStop()
  129. {
  130. scripts.startScript("endscript");
  131. scheduler.stop();
  132. databank.closeDataBankConnection();
  133. scheduler.getWorker().stop();
  134. }
  135. public static void executeCommand(String s)
  136. {
  137. commands.handleCommand(server.getCommandSource(), s);
  138. }
  139. }