Skip to content

Add Connect API Service #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: connect
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ on: [ pull_request ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up JDK 1.8
uses: actions/setup-java@v2
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '8'
cache: 'gradle'
distribution: 'corretto'
java-version: '17'

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Build with Maven
- name: Build with Gradle
run: ./gradlew build

- name: Archive artifacts (Floodgate Bungee)
- name: Archive artifacts (Connect Bungee)
uses: actions/upload-artifact@v2
if: success()
with:
name: Floodgate Bungee
name: Connect Bungee
path: bungee/build/libs/connect-bungee.jar

- name: Archive artifacts (Floodgate Spigot)
- name: Archive artifacts (Connect Spigot)
uses: actions/upload-artifact@v2
if: success()
with:
name: Floodgate Spigot
name: Connect Spigot
path: spigot/build/libs/connect-spigot.jar

- name: Archive artifacts (Floodgate Velocity)
- name: Archive artifacts (Connect Velocity)
uses: actions/upload-artifact@v2
if: success()
with:
name: Floodgate Velocity
name: Connect Velocity
path: velocity/build/libs/connect-velocity.jar
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set up JDK 1.8
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '8'
distribution: 'corretto'
java-version: '17'

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Build with Maven
- name: Build with Gradle
run: ./gradlew build

- name: Rename JARs for Latest Pre-Release
Expand Down
Empty file added ap/build.gradle.kts
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,16 @@
* @link https://github.com/GeyserMC/Floodgate
*/

package com.minekube.connect;
package com.minekube.connect.ap;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.minekube.connect.api.ConnectApi;
import com.minekube.connect.api.inject.PlatformInjector;
import com.minekube.connect.api.logger.ConnectLogger;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;

public final class SpigotPlatform extends ConnectPlatform {
@Inject private JavaPlugin plugin;

@Inject
public SpigotPlatform(ConnectApi api, PlatformInjector platformInjector,
ConnectLogger logger, Injector injector) {
super(api, platformInjector, logger, injector);
}

@Override
public boolean enable(Module... postInitializeModules) {
boolean success = super.enable(postInitializeModules);
if (!success) {
Bukkit.getPluginManager().disablePlugin(plugin);
return false;
}
return true;
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class AutoBindProcessor extends ClassProcessor {
public AutoBindProcessor() {
super("com.minekube.connect.util.AutoBind");
}
}
}
221 changes: 221 additions & 0 deletions ap/src/main/java/com/minekube/connect/ap/ClassProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Floodgate
*/

package com.minekube.connect.ap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

/*
* Copied from Geyser
*/
public class ClassProcessor extends AbstractProcessor {
private final String annotationClassName;

private Path outputPath;

private final Set<String> locations = new HashSet<>();

public ClassProcessor(String annotationClassName) {
this.annotationClassName = annotationClassName;
}

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

processingEnv.getMessager().printMessage(
Kind.NOTE, "Initializing processor " + annotationClassName
);

String outputFile = processingEnv.getOptions().get("metadataOutputFile");
if (outputFile != null && !outputFile.isEmpty()) {
outputPath = Paths.get(outputFile);
}
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
if (!roundEnv.errorRaised()) {
complete();
}

return false;
}

if (!contains(annotations, annotationClassName)) {
return false;
}

for (Element element : roundEnv.getRootElements()) {
if (element.getKind() != ElementKind.CLASS) {
continue;
}

if (!contains(element.getAnnotationMirrors(), annotationClassName)) {
continue;
}

TypeElement typeElement = (TypeElement) element;
locations.add(typeElement.getQualifiedName().toString());
}
return false;
}

public boolean contains(Collection<? extends TypeElement> elements, String className) {
if (elements.isEmpty()) {
return false;
}

for (TypeElement element : elements) {
if (element.getQualifiedName().contentEquals(className)) {
return true;
}
}

return false;
}

public boolean contains(List<? extends AnnotationMirror> elements, String className) {
if (elements.isEmpty()) {
return false;
}

for (AnnotationMirror element : elements) {
if (element.getAnnotationType().toString().equals(className)) {
return true;
}
}

return false;
}

public void complete() {
// Read existing annotation list and verify each class still has this annotation
try (BufferedReader reader = createReader()) {
if (reader != null) {
reader.lines().forEach(canonicalName -> {
if (!locations.contains(canonicalName)) {

TypeElement element =
processingEnv.getElementUtils().getTypeElement(canonicalName);

if (element != null && element.getKind() == ElementKind.CLASS &&
contains(element.getAnnotationMirrors(), annotationClassName)) {
locations.add(canonicalName);
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}

if (!locations.isEmpty()) {
try (BufferedWriter writer = createWriter()) {
for (String location : locations) {
writer.write(location);
writer.newLine();
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Did not find any classes annotated with " + annotationClassName
);
}

processingEnv.getMessager().printMessage(
Kind.NOTE, "Completed processing for " + annotationClassName
);
}

private BufferedReader createReader() throws IOException {
if (outputPath != null) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Reading existing " + annotationClassName + " list from " + outputPath
);

return Files.newBufferedReader(outputPath);
}

FileObject obj = processingEnv.getFiler().getResource(
StandardLocation.CLASS_OUTPUT, "", annotationClassName
);

if (obj != null) {
processingEnv.getMessager().printMessage(
Kind.NOTE,
"Reading existing " + annotationClassName + " list from " + obj.toUri()
);

try {
return new BufferedReader(obj.openReader(false));
} catch (NoSuchFileException ignored) {}
}
return null;
}

private BufferedWriter createWriter() throws IOException {
if (outputPath != null) {
processingEnv.getMessager().printMessage(
Kind.NOTE, "Writing " + annotationClassName + " to " + outputPath
);

return Files.newBufferedWriter(outputPath);
}

FileObject obj = processingEnv.getFiler().createResource(
StandardLocation.CLASS_OUTPUT, "", annotationClassName
);

processingEnv.getMessager().printMessage(
Kind.NOTE, "Writing " + annotationClassName + " to " + obj.toUri()
);

return new BufferedWriter(obj.openWriter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.minekube.connect.ap.AutoBindProcessor
8 changes: 8 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
plugins {
idea // used to let Intellij recognize protobuf generated sources
}

dependencies {
api("com.google.code.gson", "gson", Versions.gsonVersion)

compileOnly("io.netty", "netty-transport", Versions.nettyVersion)
api("build.buf", "connect-kotlin-okhttp", Versions.connectKotlinVersion)
api("build.buf.gen", "minekube_connect_grpc_java", Versions.minekube_connect_grpc_javaVersion)
api("build.buf.gen", "minekube_connect_bufbuild_connect-kotlin", Versions.minekube_connect_bufbuild_connectKotlinVersion)
api("build.buf.gen", "minekube_connect_protocolbuffers_java", Versions.minekube_connect_protocolbuffers_javaVersion)
}
12 changes: 12 additions & 0 deletions api/src/main/java/com/minekube/connect/api/Clients.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.minekube.connect.api;


import build.buf.gen.minekube.connect.v1alpha1.ConnectServiceGrpc.ConnectServiceBlockingStub;
import build.buf.gen.minekube.connect.v1alpha1.ConnectServiceGrpc.ConnectServiceFutureStub;
import build.buf.gen.minekube.connect.v1alpha1.ConnectServiceGrpc.ConnectServiceStub;

public interface Clients {
ConnectServiceBlockingStub getConnectServiceBlockingStub();
ConnectServiceFutureStub getConnectServiceFutureStub();
ConnectServiceStub getConnectServiceStub();
}
9 changes: 9 additions & 0 deletions api/src/main/java/com/minekube/connect/api/ConnectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ static ConnectApi getInstance() {
* @return ConnectPlayer if the given uuid is a player tunneled by Connect
*/
ConnectPlayer getPlayer(UUID uuid);

/**
* Get authenticated Connect API Services clients ready to use.
*
* @return Clients
*/
default Clients getClients() {
return InstanceHolder.getClients();
}
}
Loading