Skip to content

Commit

Permalink
only start language server on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Oct 6, 2024
1 parent f63ea4e commit d6027b2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class GroovyScript {

public static final Logger LOGGER = LogManager.getLogger(ID);

private static ModContainer grsContainer;
private static GroovyScriptSandbox sandbox;
private static RunConfig runConfig;
private static ModContainer scriptMod;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void onConstruction(FMLConstructionEvent event) {
@Mod.EventHandler
public void onInit(FMLInitializationEvent event) {
if (ModSupport.TINKERS_CONSTRUCT.isLoaded()) TinkersConstruct.init();
if (Boolean.parseBoolean(System.getProperty("groovyscript.run_ls"))) {
if (event.getSide().isClient() && Boolean.parseBoolean(System.getProperty("groovyscript.run_ls"))) {
runLanguageServer();
}
}
Expand Down Expand Up @@ -296,10 +297,21 @@ public static void postScriptRunResult(ICommandSender sender, boolean onlyLogFai
}
}

@ApiStatus.Internal
public static boolean runLanguageServer() {
if (languageServerThread != null) return false;
languageServerThread = new Thread(() -> GroovyScriptLanguageServerImpl.listen(getSandbox().getScriptRoot()));
languageServerThread.start();
return true;
}

public static void doForGroovyScript(Runnable runnable) {
ModContainer current = Loader.instance().activeModContainer();
if (grsContainer == null) {
grsContainer = Loader.instance().getIndexedModList().get(ID);
}
Loader.instance().setActiveModContainer(grsContainer);
runnable.run();
Loader.instance().setActiveModContainer(current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.cleanroommc.groovyscript.documentation.Documentation;
import com.cleanroommc.groovyscript.network.NetworkHandler;
import com.cleanroommc.groovyscript.network.SReloadScripts;
import com.cleanroommc.groovyscript.network.StartLanguageServerPacket;
import com.cleanroommc.groovyscript.sandbox.GroovyLogImpl;
import com.cleanroommc.groovyscript.sandbox.LoadStage;
import net.minecraft.command.ICommandSender;
Expand Down Expand Up @@ -110,10 +111,8 @@ public GSCommand() {
}));

addSubcommand(new SimpleCommand("runLS", (server, sender, args) -> {
if (GroovyScript.runLanguageServer()) {
sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Starting language server"));
} else {
sender.sendMessage(new TextComponentString(TextFormatting.YELLOW + "Language server is already running"));
if (sender instanceof EntityPlayerMP player) {
NetworkHandler.sendToPlayer(new StartLanguageServerPacket(), player);
}
}, "runLanguageServer"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class NetworkHandler {
public static void init() {
registerS2C(SReloadScripts.class);
registerS2C(SCopy.class);
registerS2C(StartLanguageServerPacket.class);
registerC2S(CReload.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cleanroommc.groovyscript.network;

import com.cleanroommc.groovyscript.GroovyScript;
import net.minecraft.client.Minecraft;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;

public class StartLanguageServerPacket implements IPacket {

@Override
public void encode(PacketBuffer buf) {}

@Override
public void decode(PacketBuffer buf) {}

@Override
public IPacket executeClient(NetHandlerPlayClient handler) {
if (GroovyScript.runLanguageServer()) {
Minecraft.getMinecraft().player.sendMessage(new TextComponentString(TextFormatting.GREEN + "Starting language server"));
} else {
Minecraft.getMinecraft().player.sendMessage(new TextComponentString(TextFormatting.YELLOW + "Language server is already running"));
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ public class GroovyScriptLanguageServerImpl extends GroovyLanguageServer<GroovyS

@SuppressWarnings("InfiniteLoopStatement")
public static void listen(File root) {
GroovyLog.get().infoMC("Starting Language server");
GroovyScript.doForGroovyScript(() -> GroovyLog.get().infoMC("Starting Language server"));
var languageServerContext = new GroovyScriptLanguageServerContext();

while (true) {
var server = new GroovyScriptLanguageServerImpl(root, languageServerContext);
try (var serverSocket = new ServerSocket(GroovyScriptConfig.languageServerPort);
var socket = serverSocket.accept()) {
try (var serverSocket = new ServerSocket(GroovyScriptConfig.languageServerPort); var socket = serverSocket.accept()) {

GroovyScript.LOGGER.info("Accepted connection from: {}", socket.getInetAddress());
GroovyScript.doForGroovyScript(() -> GroovyScript.LOGGER.info("Accepted connection from: {}", socket.getInetAddress()));

var launcher = Launcher.createLauncher(server, LanguageClient.class, socket.getInputStream(), socket.getOutputStream());
server.connect(launcher.getRemoteProxy());

launcher.startListening().get();
} catch (Exception e) {
GroovyScript.LOGGER.error("Connection failed", e);
GroovyScript.doForGroovyScript(() -> GroovyScript.LOGGER.error("Connection failed", e));
}
}
}
Expand All @@ -61,7 +60,8 @@ public GroovyScriptFeaturesService getGroovyScriptFeaturesService() {
}

@Override
protected @NotNull GroovyScriptServices createGroovyServices(ICompilationUnitFactory compilationUnitFactory, ILanguageServerContext languageServerContext) {
protected @NotNull GroovyScriptServices createGroovyServices(ICompilationUnitFactory compilationUnitFactory,
ILanguageServerContext languageServerContext) {
return new GroovyScriptServices(compilationUnitFactory, languageServerContext);
}
}

0 comments on commit d6027b2

Please sign in to comment.