Skip to content

Commit e472e32

Browse files
committed
Added code
1 parent cc1ffe1 commit e472e32

File tree

9 files changed

+150
-150
lines changed

9 files changed

+150
-150
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/build/
66
/out/
77
*.iml
8+
run/

README.MD

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
# Gradle Template
2-
A template project for my gradle based repos
1+
# ViaProxyMultiLaunch
2+
Launch another jar file in combination with ViaProxy.
3+
4+
## Usage
5+
The server <b>has</b> to be in a separate folder because of file conflicts with ViaProxy.\
6+
To use this plugin you have to launch ViaProxy instead of the normal server jar file.\
7+
The server jar has to be added to the launch arguments of ViaProxy:
8+
`````shell
9+
[ViaProxy launch] --serverjar server.jar
10+
`````

build.gradle

Lines changed: 6 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
plugins {
22
id "java"
3-
id "maven-publish"
4-
id "signing"
53
}
64

75
java.toolchain.languageVersion = JavaLanguageVersion.of(8)
@@ -11,157 +9,19 @@ archivesBaseName = project.maven_name
119
group = project.maven_group
1210
version = project.maven_version
1311

14-
configurations {
15-
include
16-
17-
implementation.extendsFrom include
18-
api.extendsFrom include
19-
}
20-
2112
repositories {
13+
mavenLocal()
2214
mavenCentral()
2315
maven {
2416
name = "lenni0451 releases"
2517
url = "https://maven.lenni0451.net/releases"
2618
}
27-
//maven {
28-
// name = "Sonatype Snapshots"
29-
// url "https://oss.sonatype.org/content/repositories/snapshots/"
30-
//}
31-
32-
//ivy { //include "waterfall:1.19:504@jar"
33-
// url "https://papermc.io/api/v2/projects"
34-
// patternLayout {
35-
// artifact "/[organisation]/versions/[module]/builds/[revision]/downloads/[organisation]-[module]-[revision].[ext]"
36-
// }
37-
// metadataSources {
38-
// artifact()
39-
// }
40-
//}
41-
}
42-
43-
dependencies {
44-
if (new File(projectDir, "libs").exists()) {
45-
include fileTree(include: ["*.jar"], dir: "libs")
46-
}
47-
48-
implementation "com.google.code.findbugs:jsr305:3.0.2"
49-
//include "net.lenni0451:Reflect:1.0.0"
50-
}
51-
52-
java {
53-
withSourcesJar()
54-
withJavadocJar()
55-
}
56-
57-
processResources {
58-
//Config file: "version: ${version}"
59-
inputs.properties(
60-
"version": project.maven_version
61-
)
62-
63-
filesMatching("config") {
64-
expand(
65-
"version": project.maven_version
66-
)
67-
}
68-
}
69-
70-
artifacts {
71-
archives javadocJar, sourcesJar
72-
}
73-
74-
jar {
75-
dependsOn configurations.include
76-
from {
77-
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
78-
configurations.include.collect {
79-
zipTree(it)
80-
}
81-
} {
82-
exclude "META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA"
83-
}
84-
85-
manifest {
86-
attributes(
87-
"Main-Class": "net.lenni0451.gradletemplate.Main",
88-
"Multi-Release": "true"
89-
)
90-
}
91-
}
92-
93-
test {
94-
useJUnitPlatform()
95-
testLogging {
96-
events "passed", "skipped", "failed"
97-
}
98-
maxParallelForks Runtime.runtime.availableProcessors()
99-
}
100-
101-
publishing {
102-
repositories {
103-
maven {
104-
name = "reposilite"
105-
def releasesUrl = "https://maven.lenni0451.net/releases"
106-
def snapshotsUrl = "https://maven.lenni0451.net/snapshots"
107-
url = project.maven_version.endsWith("SNAPSHOT") ? snapshotsUrl : releasesUrl
108-
109-
credentials(PasswordCredentials)
110-
authentication {
111-
basic(BasicAuthentication)
112-
}
113-
}
114-
maven {
115-
name = "ossrh"
116-
def releasesUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
117-
def snapshotsUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
118-
url = project.maven_version.endsWith("SNAPSHOT") ? snapshotsUrl : releasesUrl
119-
120-
credentials(PasswordCredentials)
121-
authentication {
122-
basic(BasicAuthentication)
123-
}
124-
}
125-
}
126-
publications {
127-
maven(MavenPublication) {
128-
artifactId = project.maven_name
129-
groupId = project.maven_group
130-
version = project.maven_version
131-
132-
from components.java
133-
134-
pom {
135-
name = rootProject.name
136-
description = "A template for gradle projects"
137-
url = "https://github.com/Lenni0451/GradleTemplate"
138-
licenses {
139-
license {
140-
name = "MIT License"
141-
url = "https://github.com/Lenni0451/GradleTemplate/blob/main/LICENSE"
142-
}
143-
}
144-
developers {
145-
developer {
146-
id = "Lenni0451"
147-
}
148-
}
149-
scm {
150-
connection = "scm:git:git://github.com/Lenni0451/GradleTemplate.git"
151-
developerConnection = "scm:git:ssh://github.com/Lenni0451/GradleTemplate.git"
152-
url = "github.com/Lenni0451/GradleTemplate"
153-
}
154-
}
155-
}
19+
maven {
20+
name = "ViaVersion"
21+
url "https://repo.viaversion.com"
15622
}
15723
}
15824

159-
signing {
160-
sign configurations.archives
161-
sign publishing.publications.maven
162-
}
163-
164-
project.tasks.withType(PublishToMavenRepository).forEach {
165-
it.dependsOn(project.tasks.withType(Sign))
25+
dependencies {
26+
compileOnly "net.raphimc:ViaProxy:3.0.14"
16627
}
167-
build.dependsOn(test)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ org.gradle.parallel=true
44
org.gradle.configureondemand=true
55

66
#Maven settings
7-
maven_name=GradleTemplate
7+
maven_name=ViaProxyMultiLaunch
88
maven_group=net.lenni0451
99
maven_version=1.0.0

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ pluginManagement {
55
}
66
}
77

8-
rootProject.name = "GradleTemplate"
8+
rootProject.name = "ViaProxyMultiLaunch"

src/.gitkeep

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.lenni0451.multilaunch;
2+
3+
import net.raphimc.viaproxy.util.logging.Logger;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.OutputStream;
8+
import java.util.concurrent.TimeUnit;
9+
10+
public class JarLauncher {
11+
12+
private final String jarPath;
13+
private Thread watchDogThread;
14+
private Process process;
15+
private OutputStream outputStream;
16+
17+
public JarLauncher(final String jarPath) {
18+
this.jarPath = jarPath;
19+
}
20+
21+
public void launch() throws IOException {
22+
File serverJarFile = new File(this.jarPath);
23+
24+
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + "/bin/java", "-jar", serverJarFile.getAbsolutePath());
25+
pb.directory(serverJarFile.getParentFile());
26+
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
27+
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
28+
this.process = pb.start();
29+
this.outputStream = this.process.getOutputStream();
30+
31+
this.watchDogThread = new Thread(() -> {
32+
try {
33+
this.process.waitFor();
34+
} catch (InterruptedException e) {
35+
return;
36+
}
37+
Logger.LOGGER.info("Server process stopped!");
38+
System.exit(0);
39+
}, "ServerWatchDog");
40+
this.watchDogThread.setDaemon(true);
41+
this.watchDogThread.start();
42+
}
43+
44+
public void stop() throws IOException {
45+
this.watchDogThread.interrupt();
46+
Logger.LOGGER.info("Stopping server (force shutdown after 1 minute)...");
47+
this.outputStream.write("stop\n".getBytes());
48+
this.outputStream.flush();
49+
try {
50+
this.process.waitFor(1, TimeUnit.MINUTES);
51+
} catch (InterruptedException e) {
52+
Logger.LOGGER.warn("Server stop was interrupted after 1 minute!");
53+
this.process.destroy();
54+
}
55+
}
56+
57+
public Process getProcess() {
58+
return this.process;
59+
}
60+
61+
public OutputStream getOutputStream() {
62+
return this.outputStream;
63+
}
64+
65+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.lenni0451.multilaunch;
2+
3+
import joptsimple.OptionSpec;
4+
import net.lenni0451.lambdaevents.EventHandler;
5+
import net.raphimc.viaproxy.plugins.PluginManager;
6+
import net.raphimc.viaproxy.plugins.ViaProxyPlugin;
7+
import net.raphimc.viaproxy.plugins.events.ConsoleCommandEvent;
8+
import net.raphimc.viaproxy.plugins.events.PostOptionsParseEvent;
9+
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
10+
11+
import java.io.IOException;
12+
import java.io.OutputStream;
13+
import java.nio.charset.StandardCharsets;
14+
15+
public class Main extends ViaProxyPlugin {
16+
17+
private OptionSpec<String> serverjarArg;
18+
private JarLauncher launcher;
19+
20+
@Override
21+
public void onEnable() {
22+
PluginManager.EVENT_MANAGER.register(this);
23+
}
24+
25+
@EventHandler
26+
public void onPreOptionsParse(PreOptionsParseEvent event) {
27+
this.serverjarArg = event.getParser().accepts("serverjar", "The path to the server jar to launch").withRequiredArg().ofType(String.class).required();
28+
}
29+
30+
@EventHandler
31+
public void onPostOptionsParse(PostOptionsParseEvent event) {
32+
String serverJar = event.getOptions().valueOf(this.serverjarArg);
33+
try {
34+
this.launcher = new JarLauncher(serverJar);
35+
this.launcher.launch();
36+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
37+
try {
38+
if (Main.this.launcher.getProcess().isAlive()) Main.this.launcher.stop();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}));
43+
} catch (Throwable t) {
44+
t.printStackTrace();
45+
System.exit(-1);
46+
}
47+
}
48+
49+
@EventHandler
50+
public void onConsoleCommand(ConsoleCommandEvent event) throws IOException {
51+
event.setCancelled(true);
52+
OutputStream os = this.launcher.getOutputStream();
53+
os.write(event.getCommand().getBytes(StandardCharsets.UTF_8));
54+
if (event.getArgs().length != 0) {
55+
os.write(' ');
56+
os.write(String.join(" ", event.getArgs()).getBytes(StandardCharsets.UTF_8));
57+
}
58+
os.write('\n');
59+
os.flush();
60+
}
61+
62+
}

src/main/resources/viaproxy.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: "MultiLaunch"
2+
version: "1.0.0"
3+
author: "Lenni0451"
4+
main: "net.lenni0451.multilaunch.Main"

0 commit comments

Comments
 (0)