diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index de36b9dd..d219fc9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,11 +16,15 @@ jobs: - uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: '17' + java-version: '21' - - name: validate gradle wrapper + - name: wrapper validation uses: gradle/actions/wrapper-validation@v4 + - name: make gradle wrapper executable + if: ${{ runner.os != 'Windows' }} + run: chmod +x ./gradlew + - name: build run: ./gradlew build --stacktrace diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6c1a18f1..f531fae0 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -2,7 +2,7 @@ name: Release on: [workflow_dispatch] # Manual trigger jobs: build: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-24.04 steps: - name: checkout repository uses: actions/checkout@v4 @@ -10,11 +10,15 @@ jobs: - uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: '17' + java-version: '21' - - name: validate gradle wrapper + - name: wrapper validation uses: gradle/actions/wrapper-validation@v4 + - name: make gradle wrapper executable + if: ${{ runner.os != 'Windows' }} + run: chmod +x ./gradlew + - name: build run: ./gradlew build publish --stacktrace env: diff --git a/build.gradle b/build.gradle index 4b8ea6e6..5936a717 100644 --- a/build.gradle +++ b/build.gradle @@ -3,8 +3,8 @@ plugins { id 'eclipse' id 'idea' id 'maven-publish' - id 'fabric-loom' version '1.10-SNAPSHOT' apply false - id 'ploceus' version '1.10-SNAPSHOT' apply false + id 'fabric-loom' version '1.12-SNAPSHOT' apply false + id 'ploceus' version '1.12-SNAPSHOT' apply false } setUpJar(project) @@ -49,7 +49,7 @@ def setUpJar(project) { project.group = "${project.rootProject.root_maven_group}" project.ploceus { - setGeneration(2) + setIntermediaryGeneration(2) } project.repositories { @@ -194,7 +194,7 @@ def setUpModule(project, String... dependencies) { } } project.ploceus { - setGeneration(2) + setIntermediaryGeneration(2) } def libraries = getLibraryDependencies(project, dependencies) diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index a4b76b95..1b33c55b 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cea7a793..ca025c83 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index f5feea6d..23d15a93 100755 --- a/gradlew +++ b/gradlew @@ -86,8 +86,7 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s -' "$PWD" ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -115,7 +114,7 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar +CLASSPATH="\\\"\\\"" # Determine the Java command to use to start the JVM. @@ -206,7 +205,7 @@ fi DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Collect all arguments for the java command: -# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, # and any embedded shellness will be escaped. # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be # treated as '${Hostname}' itself on the command line. @@ -214,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" # Stop when "xargs" is not available. diff --git a/gradlew.bat b/gradlew.bat index 9d21a218..db3a6ac2 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -70,11 +70,11 @@ goto fail :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar +set CLASSPATH= @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index 84b106ee..cde5852a 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -91,7 +104,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -101,6 +114,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w01a-mc12w17a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java index 607cc6a3..4380fc76 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -91,7 +104,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -101,6 +114,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java index a17b3451..23a39ca3 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -88,7 +101,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -98,6 +111,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc12w21a-mc1.6.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java index a17b3451..23a39ca3 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -88,7 +101,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -98,6 +111,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc13w36a-09051446-mc1.13/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java index a151a2dc..23a39ca3 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java @@ -3,18 +3,19 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -88,7 +101,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -98,6 +111,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc18w30a-mc18w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java index a151a2dc..23a39ca3 100644 --- a/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/common/MinecraftServerMixin.java @@ -3,18 +3,19 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -88,7 +101,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -98,6 +111,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mc19w04a-mc1.14.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index 016150f0..67ea8332 100644 --- a/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -90,7 +103,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -100,6 +113,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.0-mcb1.2_02/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index 84b106ee..cde5852a 100644 --- a/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -91,7 +104,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -101,6 +114,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.4-1507-mcb1.7.3/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index 84b106ee..cde5852a 100644 --- a/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -91,7 +104,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -101,6 +114,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index f5111503..c7b6a056 100644 --- a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -90,7 +103,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -100,6 +113,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.1.0-mcserver-a0.1.4/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java new file mode 100644 index 00000000..be030e30 --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/api/server/MinecraftServerInstance.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.lifecycle.api.server; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; + +/** + * A wrapper class for getting the {@linkplain MinecraftServer} instance. + */ +public class MinecraftServerInstance { + + /** + * Retrieves the current Minecraft server instance, + * or throws an exception. + * + * @return the current Minecraft server instance + */ + public static MinecraftServer get() { + return MinecraftServerAccess.getInstance(); + } +} diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java index 016150f0..67ea8332 100644 --- a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/mixin/server/MinecraftServerMixin.java @@ -10,11 +10,12 @@ import net.minecraft.server.MinecraftServer; import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.lifecycle.impl.server.MinecraftServerAccess; @Mixin(MinecraftServer.class) public class MinecraftServerMixin { - @Unique private boolean osl$lifecycle$stopped; + @Unique private int osl$lifecycle$shutdownDepth; @Inject( method = "run", @@ -23,7 +24,8 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$start(CallbackInfo ci) { - MinecraftServerEvents.START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerAccess.INSTANCE = (MinecraftServer)(Object)this; + MinecraftServerEvents.START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -35,7 +37,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$ready(CallbackInfo ci) { - MinecraftServerEvents.READY.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -45,9 +47,20 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$stop(CallbackInfo ci) { - if (!osl$lifecycle$stopped) { - osl$lifecycle$stopped = true; - MinecraftServerEvents.STOP.invoker().accept((MinecraftServer)(Object)this); + if (osl$lifecycle$shutdownDepth++ == 0) { + MinecraftServerEvents.STOP.invoker().accept(MinecraftServerAccess.INSTANCE); + } + } + + @Inject( + method = "shutdown", + at = @At( + value = "HEAD" + ) + ) + private void osl$lifecycle$stopped(CallbackInfo ci) { + if (--osl$lifecycle$shutdownDepth == 0) { + MinecraftServerAccess.INSTANCE = null; } } @@ -58,7 +71,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$startTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_START.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_START.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -68,7 +81,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$endTick(CallbackInfo ci) { - MinecraftServerEvents.TICK_END.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.TICK_END.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -78,7 +91,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$loadWorld(CallbackInfo ci) { - MinecraftServerEvents.LOAD_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.LOAD_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -90,7 +103,7 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$prepareWorld(CallbackInfo ci) { - MinecraftServerEvents.PREPARE_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.PREPARE_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } @Inject( @@ -100,6 +113,6 @@ public class MinecraftServerMixin { ) ) private void osl$lifecycle$readyWorld(CallbackInfo ci) { - MinecraftServerEvents.READY_WORLD.invoker().accept((MinecraftServer)(Object)this); + MinecraftServerEvents.READY_WORLD.invoker().accept(MinecraftServerAccess.INSTANCE); } } diff --git a/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java new file mode 100644 index 00000000..eb9c5e2b --- /dev/null +++ b/libraries/lifecycle-events/lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/lifecycle/impl/server/MinecraftServerAccess.java @@ -0,0 +1,16 @@ +package net.ornithemc.osl.lifecycle.impl.server; + +import net.minecraft.server.MinecraftServer; + +public class MinecraftServerAccess { + + public static MinecraftServer INSTANCE; + + public static MinecraftServer getInstance() { + if (INSTANCE == null) { + throw new IllegalStateException("no MinecraftServer instance available right now"); + } + + return INSTANCE; + } +} diff --git a/libraries/networking-impl/build.gradle b/libraries/networking-impl/build.gradle new file mode 100644 index 00000000..e98ba2c6 --- /dev/null +++ b/libraries/networking-impl/build.gradle @@ -0,0 +1 @@ +setUpLibrary(project) diff --git a/libraries/networking-impl/gradle.properties b/libraries/networking-impl/gradle.properties new file mode 100644 index 00000000..fdc41376 --- /dev/null +++ b/libraries/networking-impl/gradle.properties @@ -0,0 +1,2 @@ +version = 0.1.0 +archives_base_name = networking-impl diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/build.gradle b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/build.gradle new file mode 100644 index 00000000..8f761724 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc13w36a-09051446-mc1.13', + 'networking-mc13w41a-mc18w30b' +) diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/gradle.properties b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/gradle.properties new file mode 100644 index 00000000..435c0b41 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/gradle.properties @@ -0,0 +1,7 @@ +environment = * +min_mc_version = 1.13-pre3 +max_mc_version = 1.13-pre3 +mc_version_range = >=1.13-rc.3 <=1.13-rc.3 + +minecraft_version = 1.13-pre3 +feather_build = 1 diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..6748a0b3 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,53 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java similarity index 75% rename from libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java index a755af19..ef5dbdfb 100644 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -15,20 +15,21 @@ import net.minecraft.client.network.handler.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin implements INetworkHandler { +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private Minecraft minecraft; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +39,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -62,7 +63,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { ci.cancel(); } } @@ -73,12 +74,12 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..06467881 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..64f448dd --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,63 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private PacketByteBuf data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Inject( + method = "m_9429910", + cancellable = true, + at = @At( + value = "INVOKE", + shift = Shift.AFTER, + target = "Lnet/minecraft/server/network/handler/ServerPlayPacketHandler;handleCustomPayload(Lnet/minecraft/network/packet/c2s/play/CustomPayloadC2SPacket;)V" + ) + ) + private void osl$networking$skipBufferRelease(CallbackInfo ci) { + // there's a call to ByteBuf.release() that we want to skip + // so that we can queue packet handling to the main thread + // Vanilla does this by throwing an exception but for the sake + // of version compat we cannot use the same approach + ci.cancel(); + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..97912aa8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,31 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private Identifier channel; + @Shadow private PacketByteBuf data; + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return IdentifierChannelIdentifierParser.fromIdentifier(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..032c1691 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServerMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 89% rename from libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java index 180abcf1..e88d1f5a 100644 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java @@ -15,10 +15,10 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.IPlayerManager; +import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess; @Mixin(PlayerManager.class) -public class PlayerManagerMixin implements IPlayerManager { +public class PlayerManagerMixin implements PlayerManagerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private List players; diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 76% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index 60288278..ac66d440 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -16,12 +16,13 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private ServerPlayerEntity player; @@ -29,7 +30,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -50,7 +51,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } @@ -61,12 +62,12 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/resources/osl.networking.mixins.json similarity index 79% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/resources/osl.networking.mixins.json index 202183f7..f64bbc23 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/resources/osl.networking.mixins.json @@ -5,12 +5,14 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", "common.PlayerManagerMixin", "common.ServerPlayNetworkHandlerMixin" ], "client": [ "client.ClientPlayNetworkHandlerMixin", - "client.CustomPayloadS2CPacketMixin" + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/build.gradle b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/build.gradle new file mode 100644 index 00000000..3664b731 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc18w30a-mc18w50a', + 'networking-mc18w31a-mc1.14.4' +) diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/gradle.properties b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/gradle.properties new file mode 100644 index 00000000..be43d59e --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/gradle.properties @@ -0,0 +1,7 @@ +environment = * +min_mc_version = 1.13-pre4 +max_mc_version = 1.13.2 +mc_version_range = >=1.13-rc.4 <=1.13.2 + +minecraft_version = 1.13.2 +feather_build = 2 diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..2785b5e8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java similarity index 75% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java index a755af19..ef5dbdfb 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -15,20 +15,21 @@ import net.minecraft.client.network.handler.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin implements INetworkHandler { +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private Minecraft minecraft; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +39,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -62,7 +63,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { ci.cancel(); } } @@ -73,12 +74,12 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..06467881 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..7853cd80 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private Identifier channel; + @Shadow private PacketByteBuf data; + + @Inject( + method = "m_9429910", + cancellable = true, + at = @At( + value = "INVOKE", + shift = Shift.AFTER, + target = "Lnet/minecraft/server/network/handler/ServerPlayPacketHandler;handleCustomPayload(Lnet/minecraft/network/packet/c2s/play/CustomPayloadC2SPacket;)V" + ) + ) + private void osl$networking$skipBufferRelease(CallbackInfo ci) { + // there's a call to ByteBuf.release() that we want to skip + // so that we can queue packet handling to the main thread + // Vanilla does this by throwing an exception but for the sake + // of version compat we cannot use the same approach + ci.cancel(); + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return IdentifierChannelIdentifierParser.fromIdentifier(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..97912aa8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,31 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private Identifier channel; + @Shadow private PacketByteBuf data; + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return IdentifierChannelIdentifierParser.fromIdentifier(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..032c1691 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServerMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 89% rename from libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java index 180abcf1..e88d1f5a 100644 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java @@ -15,10 +15,10 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.IPlayerManager; +import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess; @Mixin(PlayerManager.class) -public class PlayerManagerMixin implements IPlayerManager { +public class PlayerManagerMixin implements PlayerManagerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private List players; diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 76% rename from libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index 60288278..ac66d440 100644 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -16,12 +16,13 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private ServerPlayerEntity player; @@ -29,7 +30,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -50,7 +51,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } @@ -61,12 +62,12 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/resources/osl.networking.mixins.json similarity index 70% rename from libraries/networking/networking-mc18w31a-mc1.14.4/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/resources/osl.networking.mixins.json index 36ceef9e..f64bbc23 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc1.13-pre4-mc1.13.2/src/main/resources/osl.networking.mixins.json @@ -5,11 +5,14 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", "common.PlayerManagerMixin", "common.ServerPlayNetworkHandlerMixin" ], "client": [ - "client.ClientPlayNetworkHandlerMixin" + "client.ClientPlayNetworkHandlerMixin", + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/build.gradle b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/build.gradle new file mode 100644 index 00000000..435282a3 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mc12w01a-mc12w17a', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/gradle.properties b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/gradle.properties similarity index 100% rename from libraries/networking/networking-mc11w49a-mc12w16a/gradle.properties rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..08410b9e --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,55 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.CustomPayloadPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + private static CustomPayloadPacket newCustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + CustomPayloadPacket p = new CustomPayloadPacket(); + p.channel = StringChannelIdentifierParser.toString(channel); + p.data = data; + return p; + } +} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index a583e081..47f751c1 100644 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.minecraft.network.packet.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 65% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 1de7fa9b..6faff580 100644 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,22 +12,26 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.MultiplayerWorld; import net.minecraft.network.packet.CustomPayloadPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private MultiplayerWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +41,7 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -55,8 +58,8 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet); + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; } @Override @@ -65,12 +68,17 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { serverChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet); } } diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java new file mode 100644 index 00000000..8183ea1b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java @@ -0,0 +1,39 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadPacket.class) +public class CustomPayloadPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 16 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public byte[] osl$networking$getData() { + return data; + } +} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java similarity index 100% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java diff --git a/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java new file mode 100644 index 00000000..5cdbf6c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.server; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java similarity index 100% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java similarity index 68% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java index 7e50711b..fd9e42b4 100644 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -16,20 +15,22 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -43,8 +44,8 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); + public boolean osl$networking$canRunOffMainThread() { + return true; } @Override @@ -53,12 +54,17 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { clientChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet); } } diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json similarity index 75% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json index 67872f8d..8ef551df 100644 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc11w49a-mc12w16a/src/main/resources/osl.networking.mixins.json @@ -4,13 +4,16 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.CustomPayloadPacketMixin", "common.PacketHandlerMixin" ], "client": [ - "client.ClientNetworkHandlerMixin" + "client.ClientNetworkHandlerMixin", + "client.MinecraftMixin" ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/build.gradle b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/build.gradle new file mode 100644 index 00000000..435282a3 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mc12w01a-mc12w17a', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/gradle.properties b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/gradle.properties similarity index 100% rename from libraries/networking/networking-mc12w17a-mc12w17a/gradle.properties rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..12ba9d51 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,53 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + private static CustomPayloadPacket newCustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + return new CustomPayloadPacket(StringChannelIdentifierParser.toString(channel), data); + } +} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index a583e081..47f751c1 100644 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.minecraft.network.packet.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 65% rename from libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 1de7fa9b..6faff580 100644 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,22 +12,26 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.MultiplayerWorld; import net.minecraft.network.packet.CustomPayloadPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private MultiplayerWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +41,7 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -55,8 +58,8 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet); + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; } @Override @@ -65,12 +68,17 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { serverChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet); } } diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java new file mode 100644 index 00000000..f5e2b624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java @@ -0,0 +1,39 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadPacket.class) +public class CustomPayloadPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public byte[] osl$networking$getData() { + return data; + } +} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java similarity index 100% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketHandlerMixin.java diff --git a/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java new file mode 100644 index 00000000..5cdbf6c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.server; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java similarity index 100% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java similarity index 71% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java index 35197aaa..a07c8490 100644 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -16,20 +15,22 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -56,8 +57,8 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); + public boolean osl$networking$canRunOffMainThread() { + return true; } @Override @@ -66,12 +67,17 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { clientChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet); } } diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json similarity index 75% rename from libraries/networking/networking-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json index 67872f8d..8ef551df 100644 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc12w17a-mc12w17a/src/main/resources/osl.networking.mixins.json @@ -4,13 +4,16 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.CustomPayloadPacketMixin", "common.PacketHandlerMixin" ], "client": [ - "client.ClientNetworkHandlerMixin" + "client.ClientNetworkHandlerMixin", + "client.MinecraftMixin" ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/build.gradle b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/build.gradle new file mode 100644 index 00000000..d7d42d27 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mc12w18a-mc12w19a', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/gradle.properties b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/gradle.properties similarity index 100% rename from libraries/networking/networking-mc12w18a-mc12w19a/gradle.properties rename to libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..f97e0a06 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,53 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } + + private static CustomPayloadPacket newCustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + return new CustomPayloadPacket(StringChannelIdentifierParser.toString(channel), data); + } +} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 66% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 4c469af8..a5a8a07b 100644 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,22 +12,25 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.ClientWorld; import net.minecraft.network.packet.CustomPayloadPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private ClientWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +40,7 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -62,23 +64,28 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet)) { ci.cancel(); } } + @Override + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; + } + @Override public boolean osl$networking$isPlayReady() { return serverChannels != null; } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java new file mode 100644 index 00000000..f5e2b624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java @@ -0,0 +1,39 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadPacket.class) +public class CustomPayloadPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public byte[] osl$networking$getData() { + return data; + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..0842d547 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerLoginNetworkHandlerMixin.java similarity index 100% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerLoginNetworkHandlerMixin.java diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 69% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index cc8e6a20..c05277df 100644 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -16,20 +15,21 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -50,23 +50,28 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } + @Override + public boolean osl$networking$canRunOffMainThread() { + return true; + } + @Override public boolean osl$networking$isPlayReady() { return clientChannels != null; } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json similarity index 73% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json index e90f6883..62d8d97e 100644 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc12w18a-mc12w19a/src/main/resources/osl.networking.mixins.json @@ -4,12 +4,15 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.CustomPayloadPacketMixin", + "common.MinecraftServerMixin", "common.ServerLoginNetworkHandlerMixin", "common.ServerPlayNetworkHandlerMixin" ], "client": [ - "client.ClientNetworkHandlerMixin" + "client.ClientNetworkHandlerMixin", + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/build.gradle b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/build.gradle new file mode 100644 index 00000000..2ba2de65 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mc12w21a-mc1.6.4', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/gradle.properties b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/gradle.properties similarity index 77% rename from libraries/networking/networking-mc12w21a-mc13w39b/gradle.properties rename to libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/gradle.properties index f77fabb6..19360a2c 100644 --- a/libraries/networking/networking-mc12w21a-mc13w39b/gradle.properties +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/gradle.properties @@ -3,6 +3,6 @@ min_mc_version = 12w21a max_mc_version = 13w39b mc_version_range = >=1.3.1-alpha.12.21.a <=1.7-alpha.13.39.b -minecraft_version = 13w39b +minecraft_version = 1.5.2 feather_build = 1 -nests_build = 2 +nests_build = 5 diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..f97e0a06 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,53 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(Networking::newCustomPayloadPacket); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } + + private static CustomPayloadPacket newCustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + return new CustomPayloadPacket(StringChannelIdentifierParser.toString(channel), data); + } +} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 66% rename from libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 4c469af8..a5a8a07b 100644 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,22 +12,25 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.ClientWorld; import net.minecraft.network.packet.CustomPayloadPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private ClientWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +40,7 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -62,23 +64,28 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet)) { ci.cancel(); } } + @Override + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; + } + @Override public boolean osl$networking$isPlayReady() { return serverChannels != null; } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalConnectionMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalConnectionMixin.java new file mode 100644 index 00000000..61f65ca8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalConnectionMixin.java @@ -0,0 +1,32 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.LocalConnection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(LocalConnection.class) +public class LocalConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "accept", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfo ci) { + if (Connections.checkAsyncHandling(packet, listener)) { + ci.cancel(); + } + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java new file mode 100644 index 00000000..f5e2b624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java @@ -0,0 +1,39 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.CustomPayloadPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadPacket.class) +public class CustomPayloadPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public byte[] osl$networking$getData() { + return data; + } +} diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..355179c1 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tickWorlds", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 93% rename from libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java index eaca81eb..524a0e78 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java @@ -10,7 +10,7 @@ import net.minecraft.network.Connection; import net.minecraft.server.MinecraftServer; import net.minecraft.server.PlayerManager; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; diff --git a/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/RemoteConnectionMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/RemoteConnectionMixin.java new file mode 100644 index 00000000..485ba3d4 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/RemoteConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.RemoteConnection; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(RemoteConnection.class) +public class RemoteConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 66% rename from libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index b1f392b1..c05277df 100644 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,23 +12,24 @@ import net.minecraft.network.packet.CustomPayloadPacket; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -50,23 +50,28 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } + @Override + public boolean osl$networking$canRunOffMainThread() { + return true; + } + @Override public boolean osl$networking$isPlayReady() { return clientChannels != null; } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json similarity index 67% rename from libraries/networking/networking-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json index 2ec9d276..5b686811 100644 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc12w21a-mc13w39b/src/main/resources/osl.networking.mixins.json @@ -5,11 +5,15 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "common.CustomPayloadPacketMixin", + "common.MinecraftServerMixin", "common.PlayerManagerMixin", + "common.RemoteConnectionMixin", "common.ServerPlayNetworkHandlerMixin" ], "client": [ - "client.ClientNetworkHandlerMixin" + "client.ClientNetworkHandlerMixin", + "common.LocalConnectionMixin", + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/build.gradle b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/build.gradle new file mode 100644 index 00000000..8f761724 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc13w36a-09051446-mc1.13', + 'networking-mc13w41a-mc18w30b' +) diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/gradle.properties b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/gradle.properties similarity index 100% rename from libraries/networking/networking-mc13w41a-mc14w20b/gradle.properties rename to libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..10f44a33 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java similarity index 75% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java index 54cf473b..ef5dbdfb 100644 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -14,22 +14,22 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin implements INetworkHandler { +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private Minecraft minecraft; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -39,7 +39,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -63,7 +63,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { ci.cancel(); } } @@ -74,12 +74,12 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(Identifier channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..84dc5bdc --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrap(data); + } +} diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..f1730adf --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrap(data); + } +} diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..355179c1 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tickWorlds", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketMixin.java new file mode 100644 index 00000000..658a8e05 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketMixin.java @@ -0,0 +1,28 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(Packet.class) +public class PacketMixin { + + @Inject( + method = "canBeHandledOffMainThread", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$asyncCustomPayloads(CallbackInfoReturnable cir) { + // TODO: somehow only do this for channels OSL has listeners for + if (this instanceof CustomPayloadPacketAccess) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 100% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 76% rename from libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index 60288278..ac66d440 100644 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -16,12 +16,13 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private ServerPlayerEntity player; @@ -29,7 +30,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -50,7 +51,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } @@ -61,12 +62,12 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json new file mode 100644 index 00000000..e08148f7 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc13w41a-mc14w20b/src/main/resources/osl.networking.mixins.json @@ -0,0 +1,23 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.ornithemc.osl.networking.impl.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", + "common.PacketMixin", + "common.PlayerManagerMixin", + "common.ServerPlayNetworkHandlerMixin" + ], + "client": [ + "client.ClientPlayNetworkHandlerMixin", + "client.MinecraftMixin" + ], + "server": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/build.gradle b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/build.gradle new file mode 100644 index 00000000..8f761724 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc13w36a-09051446-mc1.13', + 'networking-mc13w41a-mc18w30b' +) diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/gradle.properties b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/gradle.properties similarity index 100% rename from libraries/networking/networking-mc14w21a-mc14w30c/gradle.properties rename to libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..10f44a33 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java similarity index 75% rename from libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java index a755af19..ef5dbdfb 100644 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -15,20 +15,21 @@ import net.minecraft.client.network.handler.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin implements INetworkHandler { +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private Minecraft minecraft; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -38,7 +39,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) private void osl$networking$handleLogin(CallbackInfo ci) { // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -62,7 +63,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { ci.cancel(); } } @@ -73,12 +74,12 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..06467881 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..84dc5bdc --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrap(data); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..f1730adf --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private byte[] data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrap(data); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..032c1691 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServerMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 75% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java index eaca81eb..e88d1f5a 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java @@ -1,5 +1,7 @@ package net.ornithemc.osl.networking.impl.mixin.common; +import java.util.List; + import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -13,11 +15,13 @@ import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess; @Mixin(PlayerManager.class) -public class PlayerManagerMixin { +public class PlayerManagerMixin implements PlayerManagerAccess { @Shadow @Final private MinecraftServer server; + @Shadow @Final private List players; @Inject( method = "onLogin", @@ -28,4 +32,9 @@ public class PlayerManagerMixin { private void osl$networking$handleLogin(Connection connection, ServerPlayerEntity player, CallbackInfo ci) { ServerConnectionEvents.LOGIN.invoker().accept(server, player); } + + @Override + public List osl$networking$getAll() { + return players; + } } diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java similarity index 76% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java index ffa9c8d3..ac66d440 100644 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -12,17 +12,17 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.resource.Identifier; import net.minecraft.server.MinecraftServer; import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { @Shadow @Final private MinecraftServer server; @Shadow @Final private ServerPlayerEntity player; @@ -30,7 +30,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -51,7 +51,7 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { ci.cancel(); } } @@ -62,12 +62,12 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); } @Override - public boolean osl$networking$isRegisteredChannel(Identifier channel) { - return clientChannels != null && clientChannels.contains(channel); + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); } } diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json similarity index 70% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json index 36ceef9e..f64bbc23 100644 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json @@ -5,11 +5,14 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", "common.PlayerManagerMixin", "common.ServerPlayNetworkHandlerMixin" ], "client": [ - "client.ClientPlayNetworkHandlerMixin" + "client.ClientPlayNetworkHandlerMixin", + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/build.gradle b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/build.gradle new file mode 100644 index 00000000..8f761724 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc13w36a-09051446-mc1.13', + 'networking-mc13w41a-mc18w30b' +) diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/gradle.properties b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/gradle.properties similarity index 100% rename from libraries/networking/networking-mc14w31a-mc1.13-pre2/gradle.properties rename to libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..10f44a33 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java new file mode 100644 index 00000000..ef5dbdfb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -0,0 +1,85 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; + +@Mixin(ClientPlayNetworkHandler.class) +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { + + @Shadow @Final private Minecraft minecraft; + + /** + * Channels that the server is listening to. + */ + @Unique private Set serverChannels; + + @Inject( + method = "handleLogin", + at = @At( + value = "TAIL" + ) + ) + private void osl$networking$handleLogin(CallbackInfo ci) { + // send channel registration data as soon as login occurs + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + + ClientConnectionEvents.LOGIN.invoker().accept(minecraft); + } + + @Inject( + method = "onDisconnect", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleDisconnect(CallbackInfo ci) { + ClientConnectionEvents.DISCONNECT.invoker().accept(minecraft); + serverChannels = null; + } + + @Inject( + method = "handleCustomPayload", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + ci.cancel(); + } + } + + @Override + public boolean osl$networking$isPlayReady() { + return serverChannels != null; + } + + @Override + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..06467881 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..64f448dd --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,63 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private PacketByteBuf data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Inject( + method = "m_9429910", + cancellable = true, + at = @At( + value = "INVOKE", + shift = Shift.AFTER, + target = "Lnet/minecraft/server/network/handler/ServerPlayPacketHandler;handleCustomPayload(Lnet/minecraft/network/packet/c2s/play/CustomPayloadC2SPacket;)V" + ) + ) + private void osl$networking$skipBufferRelease(CallbackInfo ci) { + // there's a call to ByteBuf.release() that we want to skip + // so that we can queue packet handling to the main thread + // Vanilla does this by throwing an exception but for the sake + // of version compat we cannot use the same approach + ci.cancel(); + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..69736722 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,42 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private String channel; + @Shadow private PacketByteBuf data; + + @ModifyConstant( + method = "read", + constant = @Constant( + intValue = 20 + ) + ) + private int osl$networking$modifyMaxChannelLength(int maxLength) { + return StringChannelIdentifierParser.MAX_LENGTH; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..032c1691 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,18 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServerMixin implements BlockableEventLoop, TaskRunnerAccess { + + @Override + public boolean osl$networking$submit(Runnable task) { + this.submit(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java new file mode 100644 index 00000000..e88d1f5a --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java @@ -0,0 +1,40 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.List; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.Connection; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; + +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess; + +@Mixin(PlayerManager.class) +public class PlayerManagerMixin implements PlayerManagerAccess { + + @Shadow @Final private MinecraftServer server; + @Shadow @Final private List players; + + @Inject( + method = "onLogin", + at = @At( + value = "TAIL" + ) + ) + private void osl$networking$handleLogin(Connection connection, ServerPlayerEntity player, CallbackInfo ci) { + ServerConnectionEvents.LOGIN.invoker().accept(server, player); + } + + @Override + public List osl$networking$getAll() { + return players; + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java new file mode 100644 index 00000000..ac66d440 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -0,0 +1,73 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +@Mixin(ServerPlayNetworkHandler.class) +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { + + @Shadow @Final private MinecraftServer server; + @Shadow @Final private ServerPlayerEntity player; + + /** + * Channels that the client is listening to. + */ + @Unique private Set clientChannels; + + @Inject( + method = "onDisconnect", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleDisconnect(CallbackInfo ci) { + ServerConnectionEvents.DISCONNECT.invoker().accept(server, player); + clientChannels = null; + } + + @Inject( + method = "handleCustomPayload", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + ci.cancel(); + } + } + + @Override + public boolean osl$networking$isPlayReady() { + return clientChannels != null; + } + + @Override + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); + } +} diff --git a/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json new file mode 100644 index 00000000..f64bbc23 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json @@ -0,0 +1,22 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.ornithemc.osl.networking.impl.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", + "common.PlayerManagerMixin", + "common.ServerPlayNetworkHandlerMixin" + ], + "client": [ + "client.ClientPlayNetworkHandlerMixin", + "client.MinecraftMixin" + ], + "server": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/build.gradle b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/build.gradle new file mode 100644 index 00000000..f5d7d200 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mc13w16a-04192037-mc1.14.4', + 'lifecycle-events-mc19w04a-mc1.14.4', + 'networking-mc18w31a-mc1.14.4' +) diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/gradle.properties b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/gradle.properties new file mode 100644 index 00000000..2e7bb240 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/gradle.properties @@ -0,0 +1,7 @@ +environment = * +min_mc_version = 18w43a +max_mc_version = 1.14.4 +mc_version_range = >=1.14-alpha.18.43.a <=1.14.4 + +minecraft_version = 1.14.4 +feather_build = 2 diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..2785b5e8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadS2CPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data))); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving client channel registration data + ServerPlayNetworkingImpl.sendNoCheck(context.player(), HandshakePayload.CHANNEL, HandshakePayload.server()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) -> + new CustomPayloadC2SPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data))); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java new file mode 100644 index 00000000..ef5dbdfb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -0,0 +1,85 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; + +@Mixin(ClientPlayNetworkHandler.class) +public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess { + + @Shadow @Final private Minecraft minecraft; + + /** + * Channels that the server is listening to. + */ + @Unique private Set serverChannels; + + @Inject( + method = "handleLogin", + at = @At( + value = "TAIL" + ) + ) + private void osl$networking$handleLogin(CallbackInfo ci) { + // send channel registration data as soon as login occurs + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + + ClientConnectionEvents.LOGIN.invoker().accept(minecraft); + } + + @Inject( + method = "onDisconnect", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleDisconnect(CallbackInfo ci) { + ClientConnectionEvents.DISCONNECT.invoker().accept(minecraft); + serverChannels = null; + } + + @Inject( + method = "handleCustomPayload", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { + if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { + ci.cancel(); + } + } + + @Override + public boolean osl$networking$isPlayReady() { + return serverChannels != null; + } + + @Override + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { + serverChannels = new LinkedHashSet<>(channels); + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..2749c72d --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public abstract class MinecraftMixin extends BlockableEventLoop implements TaskRunnerAccess { + + private MinecraftMixin(String name) { + super(name); + } + + @Override + public boolean osl$networking$submit(Runnable task) { + this.execute(task); + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java new file mode 100644 index 00000000..7853cd80 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadC2SPacket.class) +public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private Identifier channel; + @Shadow private PacketByteBuf data; + + @Inject( + method = "m_9429910", + cancellable = true, + at = @At( + value = "INVOKE", + shift = Shift.AFTER, + target = "Lnet/minecraft/server/network/handler/ServerPlayPacketHandler;handleCustomPayload(Lnet/minecraft/network/packet/c2s/play/CustomPayloadC2SPacket;)V" + ) + ) + private void osl$networking$skipBufferRelease(CallbackInfo ci) { + // there's a call to ByteBuf.release() that we want to skip + // so that we can queue packet handling to the main thread + // Vanilla does this by throwing an exception but for the sake + // of version compat we cannot use the same approach + ci.cancel(); + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return IdentifierChannelIdentifierParser.fromIdentifier(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java new file mode 100644 index 00000000..97912aa8 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadS2CPacketMixin.java @@ -0,0 +1,31 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; + +@Mixin(CustomPayloadS2CPacket.class) +public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess { + + @Shadow private Identifier channel; + @Shadow private PacketByteBuf data; + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return IdentifierChannelIdentifierParser.fromIdentifier(channel); + } + + @Override + public PacketBuffer osl$networking$getData() { + return PacketBuffers.wrapped(data.copy()); + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java new file mode 100644 index 00000000..eb2bc2be --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/MinecraftServerMixin.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.BlockableEventLoop; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServerMixin extends BlockableEventLoop implements TaskRunnerAccess { + + private MinecraftServerMixin(String name) { + super(name); + } + + @Override + public boolean osl$networking$submit(Runnable task) { + this.execute(task); + return true; + } +} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java similarity index 100% rename from libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java rename to libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PlayerManagerMixin.java diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java new file mode 100644 index 00000000..ac66d440 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java @@ -0,0 +1,73 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +@Mixin(ServerPlayNetworkHandler.class) +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess { + + @Shadow @Final private MinecraftServer server; + @Shadow @Final private ServerPlayerEntity player; + + /** + * Channels that the client is listening to. + */ + @Unique private Set clientChannels; + + @Inject( + method = "onDisconnect", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleDisconnect(CallbackInfo ci) { + ServerConnectionEvents.DISCONNECT.invoker().accept(server, player); + clientChannels = null; + } + + @Inject( + method = "handleCustomPayload", + cancellable = true, + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { + if (ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { + ci.cancel(); + } + } + + @Override + public boolean osl$networking$isPlayReady() { + return clientChannels != null; + } + + @Override + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { + clientChannels = new LinkedHashSet<>(channels); + } +} diff --git a/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/resources/osl.networking.mixins.json new file mode 100644 index 00000000..f64bbc23 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mc18w43a-mc1.14.4/src/main/resources/osl.networking.mixins.json @@ -0,0 +1,22 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.ornithemc.osl.networking.impl.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "common.CustomPayloadC2SPacketMixin", + "common.CustomPayloadS2CPacketMixin", + "common.MinecraftServerMixin", + "common.PlayerManagerMixin", + "common.ServerPlayNetworkHandlerMixin" + ], + "client": [ + "client.ClientPlayNetworkHandlerMixin", + "client.MinecraftMixin" + ], + "server": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/build.gradle b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/build.gradle new file mode 100644 index 00000000..47646e3c --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mcinf-20100630-1340-mca1.2.6', + 'networking-mca1.0.16-mca1.2.6' +) diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/gradle.properties b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/gradle.properties new file mode 100644 index 00000000..94398336 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/gradle.properties @@ -0,0 +1,10 @@ +environment = client +min_mc_version = a1.0.16 +max_mc_version = a1.2.6 +mc_version_range = >=1.0.0-alpha.0.16 <=1.0.0-alpha.2.6 + +minecraft_version = a1.2.6 +feather_build = 1 +raven_build = 2 +sparrow_build = 2 +nests_build = 6 diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java new file mode 100644 index 00000000..eb557624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java @@ -0,0 +1,74 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; + +public class CustomPayloadPacket extends Packet implements CustomPayloadPacketAccess { + + private String channel; + private int size; + private byte[] data; + + public CustomPayloadPacket() { + } + + public CustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + this.channel = StringChannelIdentifierParser.toString(channel); + this.data = data; + this.size = data.length; + + if (this.data != null && this.size > Short.MAX_VALUE) { + throw new IllegalArgumentException("Payload may not be larger than 32k"); + } + } + + @Override + public void read(DataInputStream input) throws IOException { + this.channel = input.readUTF(); + this.size = input.readShort(); + if (this.size > 0 && this.size < Short.MAX_VALUE) { + this.data = new byte[this.size]; + input.readFully(this.data); + } + } + + @Override + public void write(DataOutputStream output) throws IOException { + output.writeUTF(this.channel); + output.writeShort(this.size); + if (this.data != null) { + output.write(this.data); + } + } + + @Override + public void handle(PacketHandler handler) { + if (handler instanceof INetworkHandler) { + ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); + } + } + + @Override + public int getSize() { + return 2 + this.channel.length() * 2 + 2 + this.data.length; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(this.channel); + } + + @Override + public byte[] osl$networking$getData() { + return this.data; + } +} diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..191575c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,36 @@ +package net.ornithemc.osl.networking.impl; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving server channel registration data + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + // no-op + } +} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index 420bdcda..283c296d 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.ornithemc.osl.networking.impl.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 61% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 66b4df3d..2f22e265 100644 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,21 +12,26 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.MultiplayerWorld; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.HandshakePayload; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private MultiplayerWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -36,6 +40,9 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { ) ) private void osl$networking$handleLogin(CallbackInfo ci) { + // send channel registration data as soon as login occurs + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + ClientConnectionEvents.LOGIN.invoker().accept(minecraft); } @@ -51,8 +58,8 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet); + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; } @Override @@ -61,12 +68,17 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { serverChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet); } } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java similarity index 100% rename from libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java rename to libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityMixin.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityMixin.java new file mode 100644 index 00000000..4315e91a --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityMixin.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.client.entity.mob.player.LocalClientPlayerEntity; +import net.minecraft.client.network.handler.ClientNetworkHandler; + +import net.ornithemc.osl.networking.impl.access.LocalClientPlayerAccess; + +@Mixin(LocalClientPlayerEntity.class) +public class LocalClientPlayerEntityMixin implements LocalClientPlayerAccess { + + @Shadow + private ClientNetworkHandler networkHandler; + + @Override + public ClientNetworkHandler osl$networking$getNetworkHandler() { + return this.networkHandler; + } +} diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java similarity index 100% rename from libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java rename to libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json similarity index 77% rename from libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json index 64d62d1d..ded2c522 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mca1.0.16-mca1.2.6/src/main/resources/osl.networking.mixins.json @@ -4,12 +4,14 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.PacketAccessor" ], "client": [ "client.ClientNetworkHandlerMixin", "client.HandshakePacketMixin", - "client.LocalClientPlayerEntityAccessor" + "client.LocalClientPlayerEntityMixin", + "client.MinecraftMixin" ], "server": [ ], diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/build.gradle b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/build.gradle new file mode 100644 index 00000000..2e14f6bb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mcb1.4-1507-mcb1.7.3', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/gradle.properties b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/gradle.properties similarity index 100% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/gradle.properties rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/gradle.properties diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java new file mode 100644 index 00000000..eb557624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java @@ -0,0 +1,74 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; + +public class CustomPayloadPacket extends Packet implements CustomPayloadPacketAccess { + + private String channel; + private int size; + private byte[] data; + + public CustomPayloadPacket() { + } + + public CustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + this.channel = StringChannelIdentifierParser.toString(channel); + this.data = data; + this.size = data.length; + + if (this.data != null && this.size > Short.MAX_VALUE) { + throw new IllegalArgumentException("Payload may not be larger than 32k"); + } + } + + @Override + public void read(DataInputStream input) throws IOException { + this.channel = input.readUTF(); + this.size = input.readShort(); + if (this.size > 0 && this.size < Short.MAX_VALUE) { + this.data = new byte[this.size]; + input.readFully(this.data); + } + } + + @Override + public void write(DataOutputStream output) throws IOException { + output.writeUTF(this.channel); + output.writeShort(this.size); + if (this.data != null) { + output.write(this.data); + } + } + + @Override + public void handle(PacketHandler handler) { + if (handler instanceof INetworkHandler) { + ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); + } + } + + @Override + public int getSize() { + return 2 + this.channel.length() * 2 + 2 + this.data.length; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(this.channel); + } + + @Override + public byte[] osl$networking$getData() { + return this.data; + } +} diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..296d88d0 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,45 @@ +package net.ornithemc.osl.networking.impl; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving server channel registration data + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index 420bdcda..283c296d 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.ornithemc.osl.networking.impl.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 66% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 66b4df3d..c48e552f 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,21 +12,25 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.MultiplayerWorld; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private MultiplayerWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -51,8 +54,8 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet); + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; } @Override @@ -61,12 +64,17 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { serverChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet); } } diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java similarity index 100% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java similarity index 100% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java diff --git a/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java new file mode 100644 index 00000000..5cdbf6c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.server; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java similarity index 94% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java index d57902df..4227d62e 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java @@ -52,7 +52,7 @@ public class ServerLoginNetworkHandlerMixin { if (player != null) { if (ornithe) { // send channel registration data as soon as login occurs - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); + ServerPlayNetworkingImpl.sendNoCheck(player, HandshakePayload.CHANNEL, HandshakePayload.server()); } ServerConnectionEvents.LOGIN.invoker().accept(server, player); diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java similarity index 68% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java index 11335ca1..381c0697 100644 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -15,21 +14,23 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -43,8 +44,8 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); + public boolean osl$networking$canRunOffMainThread() { + return true; } @Override @@ -53,12 +54,17 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { clientChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet); } } diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json similarity index 75% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json index e2526e75..71939fcc 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json @@ -4,13 +4,16 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.PacketAccessor" ], "client": [ "client.ClientNetworkHandlerMixin", - "client.HandshakePacketMixin" + "client.HandshakePacketMixin", + "client.MinecraftMixin" ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/build.gradle b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/build.gradle new file mode 100644 index 00000000..1eb75d01 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a', + 'networking-mcb1.0-mc13w39b' +) diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/gradle.properties b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/gradle.properties similarity index 100% rename from libraries/networking/networking-mcb1.5-mc11w48a/gradle.properties rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/gradle.properties diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java similarity index 57% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java index 5d859b11..fd63fa5a 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java @@ -8,26 +8,27 @@ import net.minecraft.network.PacketHandler; import net.minecraft.network.packet.Packet; -import net.ornithemc.osl.networking.api.Channels; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -public class CustomPayloadPacket extends Packet { +public class CustomPayloadPacket extends Packet implements CustomPayloadPacketAccess { - public String channel; - public int size; - public byte[] data; + private String channel; + private int size; + private byte[] data; public CustomPayloadPacket() { } - public CustomPayloadPacket(String channel, byte[] data) { - this.channel = channel; + public CustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + this.channel = StringChannelIdentifierParser.toString(channel); this.data = data; - if (data != null) { - this.size = data.length; - if (this.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } + this.size = data.length; + + if (this.data != null && this.size > Short.MAX_VALUE) { + throw new IllegalArgumentException("Payload may not be larger than 32k"); } } @@ -38,7 +39,7 @@ public CustomPayloadPacket(String channel, byte[] data) { @Override public void read(DataInputStream input) { try { - this.channel = readString(input, Channels.MAX_LENGTH); + this.channel = readString(input, StringChannelIdentifierParser.MAX_LENGTH); this.size = input.readShort(); if (this.size > 0 && this.size < Short.MAX_VALUE) { this.data = new byte[this.size]; @@ -71,6 +72,16 @@ public void handle(PacketHandler handler) { @Override public int getSize() { - return 2 + this.channel.length() * 2 + 2 + this.size; + return 2 + this.channel.length() * 2 + 2 + this.data.length; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(this.channel); + } + + @Override + public byte[] osl$networking$getData() { + return this.data; } } diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..296d88d0 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,45 @@ +package net.ornithemc.osl.networking.impl; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp); + MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy); + ClientPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + // send channel registration data as a response to receiving server channel registration data + ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client()); + + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ClientConnectionEvents.PLAY_READY.invoker().accept(context.minecraft()); + }); + } + + @Override + public void initServer() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index 420bdcda..283c296d 100644 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.ornithemc.osl.networking.impl.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java similarity index 66% rename from libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java index 66b4df3d..c48e552f 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -13,21 +12,25 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.client.world.MultiplayerWorld; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; @Mixin(ClientNetworkHandler.class) -public class ClientNetworkHandlerMixin implements INetworkHandler { +public class ClientNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private Minecraft minecraft; + @Shadow private Minecraft minecraft; + @Shadow private MultiplayerWorld world; /** * Channels that the server is listening to. */ - @Unique private Set serverChannels; + @Unique private Set serverChannels; @Inject( method = "handleLogin", @@ -51,8 +54,8 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ClientPlayNetworkingImpl.handle(minecraft, (ClientNetworkHandler)(Object)this, packet); + public boolean osl$networking$canRunOffMainThread() { + return minecraft != null && minecraft.world != null && minecraft.player != null && world != null; } @Override @@ -61,12 +64,17 @@ public class ClientNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return serverChannels != null && serverChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { serverChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return serverChannels != null && serverChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientNetworkHandler)(Object)this, packet); } } diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java similarity index 100% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/HandshakePacketMixin.java diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java new file mode 100644 index 00000000..85baea9b --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/MinecraftMixin.java @@ -0,0 +1,44 @@ +package net.ornithemc.osl.networking.impl.mixin.client; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.Minecraft; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(Minecraft.class) +public class MinecraftMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "HEAD" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java similarity index 100% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java new file mode 100644 index 00000000..5cdbf6c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.server; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java similarity index 94% rename from libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java index d57902df..4227d62e 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java @@ -52,7 +52,7 @@ public class ServerLoginNetworkHandlerMixin { if (player != null) { if (ornithe) { // send channel registration data as soon as login occurs - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); + ServerPlayNetworkingImpl.sendNoCheck(player, HandshakePayload.CHANNEL, HandshakePayload.server()); } ServerConnectionEvents.LOGIN.invoker().accept(server, player); diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java similarity index 68% rename from libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java index 11335ca1..381c0697 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -15,21 +14,23 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -43,8 +44,8 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); + public boolean osl$networking$canRunOffMainThread() { + return true; } @Override @@ -53,12 +54,17 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { clientChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet); } } diff --git a/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json new file mode 100644 index 00000000..71939fcc --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcb1.5-mc11w48a/src/main/resources/osl.networking.mixins.json @@ -0,0 +1,23 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.ornithemc.osl.networking.impl.mixin", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "common.ConnectionMixin", + "common.PacketAccessor" + ], + "client": [ + "client.ClientNetworkHandlerMixin", + "client.HandshakePacketMixin", + "client.MinecraftMixin" + ], + "server": [ + "server.MinecraftServerMixin", + "server.ServerLoginNetworkHandlerMixin", + "server.ServerPlayNetworkHandlerMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/build.gradle b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/build.gradle new file mode 100644 index 00000000..8bb4e9a0 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/build.gradle @@ -0,0 +1,5 @@ +setUpModule(project, + 'entrypoints-mcin-20091223-1459-mc1.5.2', + 'lifecycle-events-mcserver-a0.2.0-mcserver-a0.2.8', + 'networking-mcserver-a0.2.2-mcserver-a0.2.8' +) diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/gradle.properties b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/gradle.properties new file mode 100644 index 00000000..dc090d1c --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/gradle.properties @@ -0,0 +1,10 @@ +environment = server +min_mc_version = server-a0.1.2_01 +max_mc_version = server-a0.2.8 +mc_version_range = >=1.0.0-alpha.1.2.1 <=1.0.0-alpha.2.8 + +minecraft_version = server-a0.2.8 +feather_build = 1 +raven_build = 1 +sparrow_build = 1 +nests_build = 3 diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java new file mode 100644 index 00000000..eb557624 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java @@ -0,0 +1,74 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.StringChannelIdentifierParser; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; + +public class CustomPayloadPacket extends Packet implements CustomPayloadPacketAccess { + + private String channel; + private int size; + private byte[] data; + + public CustomPayloadPacket() { + } + + public CustomPayloadPacket(NamespacedIdentifier channel, byte[] data) { + this.channel = StringChannelIdentifierParser.toString(channel); + this.data = data; + this.size = data.length; + + if (this.data != null && this.size > Short.MAX_VALUE) { + throw new IllegalArgumentException("Payload may not be larger than 32k"); + } + } + + @Override + public void read(DataInputStream input) throws IOException { + this.channel = input.readUTF(); + this.size = input.readShort(); + if (this.size > 0 && this.size < Short.MAX_VALUE) { + this.data = new byte[this.size]; + input.readFully(this.data); + } + } + + @Override + public void write(DataOutputStream output) throws IOException { + output.writeUTF(this.channel); + output.writeShort(this.size); + if (this.data != null) { + output.write(this.data); + } + } + + @Override + public void handle(PacketHandler handler) { + if (handler instanceof INetworkHandler) { + ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); + } + } + + @Override + public int getSize() { + return 2 + this.channel.length() * 2 + 2 + this.data.length; + } + + @Override + public NamespacedIdentifier osl$networking$getChannel() { + return StringChannelIdentifierParser.fromString(this.channel); + } + + @Override + public byte[] osl$networking$getData() { + return this.data; + } +} diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java new file mode 100644 index 00000000..5875b287 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl; + +import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; +import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; +import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; +import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { + + @Override + public void init() { + // no-op + } + + @Override + public void initClient() { + // no-op + } + + @Override + public void initServer() { + MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp); + MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy); + ServerPlayNetworkingImpl.setUpPacketFactory(CustomPayloadPacket::new); + ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (context, payload) -> { + ((NetworkHandlerAccess)context.networkHandler()).osl$networking$registerChannels(payload.channels); + ServerConnectionEvents.PLAY_READY.invoker().accept(context.server(), context.player()); + }); + } +} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java similarity index 55% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java rename to libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java index 420bdcda..283c296d 100644 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java @@ -1,17 +1,9 @@ package net.ornithemc.osl.networking.impl.interfaces.mixin; -import java.util.Set; - import net.ornithemc.osl.networking.impl.CustomPayloadPacket; public interface INetworkHandler { boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - } diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java new file mode 100644 index 00000000..8cbf1eeb --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ConnectionMixin.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.networking.impl.mixin.common; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.network.Connection; +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.Connections; + +@Mixin(Connection.class) +public class ConnectionMixin { + + @Shadow private PacketHandler listener; + + @Inject( + method = "read", + cancellable = true, + at = @At( + value = "INVOKE", + target = "Ljava/util/List;add(Ljava/lang/Object;)Z" + ) + ) + private void osl$networking$handlePacketsAsync(Packet packet, CallbackInfoReturnable cir) { + if (Connections.checkAsyncHandling(packet, listener)) { + cir.setReturnValue(true); + } + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java similarity index 100% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java rename to libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java diff --git a/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java new file mode 100644 index 00000000..5cdbf6c6 --- /dev/null +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/MinecraftServerMixin.java @@ -0,0 +1,48 @@ +package net.ornithemc.osl.networking.impl.mixin.server; + +import java.util.ArrayDeque; +import java.util.Queue; + +import org.objectweb.asm.Opcodes; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.server.MinecraftServer; + +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +@Mixin(MinecraftServer.class) +public class MinecraftServerMixin implements TaskRunnerAccess { + + @Unique + private Queue tasks = new ArrayDeque<>(); + + @Inject( + method = "tick", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/server/MinecraftServer;ticks:I" + ) + ) + private void osl$networking$runTasks(CallbackInfo ci) { + synchronized (this.tasks) { + while (!this.tasks.isEmpty()) { + this.tasks.poll().run(); + } + } + } + + @Override + public boolean osl$networking$submit(Runnable task) { + synchronized (this.tasks) { + this.tasks.add(task); + } + + return true; + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java similarity index 94% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java index d57902df..4227d62e 100644 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java @@ -52,7 +52,7 @@ public class ServerLoginNetworkHandlerMixin { if (player != null) { if (ornithe) { // send channel registration data as soon as login occurs - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); + ServerPlayNetworkingImpl.sendNoCheck(player, HandshakePayload.CHANNEL, HandshakePayload.server()); } ServerConnectionEvents.LOGIN.invoker().accept(server, player); diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java similarity index 68% rename from libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java rename to libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java index 11335ca1..381c0697 100644 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java @@ -3,7 +3,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -15,21 +14,23 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; import net.ornithemc.osl.networking.impl.CustomPayloadPacket; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; @Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { +public class ServerPlayNetworkHandlerMixin implements NetworkHandlerAccess, INetworkHandler { - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; + @Shadow private MinecraftServer server; + @Shadow private ServerPlayerEntity player; /** * Channels that the client is listening to. */ - @Unique private Set clientChannels; + @Unique private Set clientChannels; @Inject( method = "onDisconnect", @@ -43,8 +44,8 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); + public boolean osl$networking$canRunOffMainThread() { + return true; } @Override @@ -53,12 +54,17 @@ public class ServerPlayNetworkHandlerMixin implements INetworkHandler { } @Override - public void osl$networking$registerChannels(Set channels) { + public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) { + return clientChannels != null && clientChannels.contains(channel); + } + + @Override + public void osl$networking$registerChannels(Set channels) { clientChannels = new LinkedHashSet<>(channels); } @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); + public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { + return ServerPlayNetworkingImpl.handlePacket(server, (ServerPlayNetworkHandler)(Object)this, player, packet); } } diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json similarity index 84% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json rename to libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json index 2cfc21d4..788e819b 100644 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking-impl/networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json @@ -4,11 +4,13 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "common.ConnectionMixin", "common.PacketAccessor" ], "client": [ ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking-impl/src/main/resources/assets/ornithe-standard-libraries/networking-impl/icon.png b/libraries/networking-impl/src/main/resources/assets/ornithe-standard-libraries/networking-impl/icon.png new file mode 100644 index 00000000..ef588531 Binary files /dev/null and b/libraries/networking-impl/src/main/resources/assets/ornithe-standard-libraries/networking-impl/icon.png differ diff --git a/libraries/networking-impl/src/main/resources/fabric.mod.json b/libraries/networking-impl/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..d4df3431 --- /dev/null +++ b/libraries/networking-impl/src/main/resources/fabric.mod.json @@ -0,0 +1,41 @@ +{ + "schemaVersion": 1, + "id": "osl-networking-impl", + "version": "${version}", + "name": "Networking Implementation", + "description": "Implementation details of the OSL Networking API.", + "authors": [ + "OrnitheMC", + "Space Walker" + ], + "contact": { + "homepage": "https://ornithemc.net/", + "issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues", + "sources": "https://github.com/OrnitheMC/ornithe-standard-libraries" + }, + "license": "Apache-2.0", + "icon": "assets/ornithe-standard-libraries/networking-impl/icon.png", + "environment": "${environment}", + "entrypoints": { + "init": [ + "net.ornithemc.osl.networking.impl.Networking" + ], + "client-init": [ + "net.ornithemc.osl.networking.impl.Networking" + ], + "server-init": [ + "net.ornithemc.osl.networking.impl.Networking" + ] + }, + "mixins": [ + "osl.networking.mixins.json" + ], + "depends": { + "fabricloader": ">=0.16.0", + "minecraft": "${mc_version_range}", + "osl-core": ">=0.4.0", + "osl-entrypoints": ">=0.2.0", + "osl-lifecycle-events": ">=0.4.0", + "osl-networking": ">=0.9.0" + } +} diff --git a/libraries/networking/README.md b/libraries/networking/README.md index d017563f..329edc42 100644 --- a/libraries/networking/README.md +++ b/libraries/networking/README.md @@ -17,22 +17,39 @@ are also fired for connections to integrated servers. ## Networking Sending and receiving data is done through the `ClientPlayNetworking` and `ServerPlayNetworking` classes. -Mods can register network listeners through the `registerListener` and `registerListenerAsync` methods, -allowing them to receive data through specific channels. Sending data is done through the `send` methods. +Mods can register packet listeners through the `registerListener` methods, and send data through the `send` methods. -Each custom payload is tied to a channel. Only connections that have listeners on that channel will receive the payload, and on the receiving end, the payload will only be handled by the listener on that channel. In Minecraft versions 1.13-pre2 and below, a channel can be any `String` of length 20 or less. In Minecraft versions 1.13-pre4 and above, a channel can be any valid `Identifier`. For `String` channels, the convention is `<(abbreviated) mod id>|` (e.g. `Example|Cookie`), while for `Identifier` channels, the convention is `:` (e.g. `example:cookie`). +Custom payloads are sent over specific channels. Channels are namespaced identifiers, used to identify the payload being sent or received. +Channels identifiers should be constructed through the `ChannelIdentifieres` class. The convention is to use your mod id as the namespace, +and snake case for the identifier. -For ease of use data can be wrapped in custom payload objects. These must implement the `CustomPayload` interface -and must have a public construcor without parameters. An example can be seen below. +```java +public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie"); +``` + +You are expected to register your channels through the `ChannelRegistry`. + +```java +ChannelRegistry.register(COOKIE_CHANNEL); +``` + +You are expected to register your packet listeners in your mod initializer through `ClientPlayNetworking` and `ServerPlayNetworking`. + +```java +ClientPlayNetworking.registerListener(COOKIE_CHANNEL, (context, buffer) -> { }); +``` + +For ease of use data can be wrapped in custom payload objects. +These must implement the `CustomPayload` interface and must have a public constructor without parameters. +An example can be seen below. ```java package com.example; import java.io.IOException; -import net.minecraft.network.PacketByteBuf; - import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; public class CookiePayload implements CustomPayload { @@ -46,30 +63,43 @@ public class CookiePayload implements CustomPayload { } @Override - public void read(PacketByteBuf buffer) throws IOException { + public void read(PacketBuffer buffer) throws IOException { // deserialize data } @Override - public void write(PacketByteBuf buffer) throws IOException { + public void write(PacketBuffer buffer) throws IOException { // serialize data } } ``` -Listeners for these custom payload objects can be registered as follows: +A basic networking setup might look as follows. + ```java package com.example; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.entrypoints.api.ModInitializer; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; public class ExampleInitializer implements ModInitializer { + public static final NamespacedIdentifier COOKIE_CHANNEL = ChannelIdentifiers.from("example", "cookie"); + @Override public void init() { - ClientPlayNetworking.registerListener("Example|Cookie", CookiePayload::new, (minecraft, handler, payload) -> { + ChannelRegistry.register(COOKIE_CHANNEL, true, false); + } + + @Override + public void initClient() { + ClientPlayNetworking.registerListener(COOKIE_CHANNEL, CookiePayload::new, (context, payload) -> { + // ensure this listener is running on the main thread + context.ensureOnMainThread(); + // handle custom payload }); } diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/build.gradle b/libraries/networking/networking-mc1.13-pre4-mc18w30b/build.gradle deleted file mode 100644 index cadd1023..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mc13w16a-04192037-mc1.14.4', - 'lifecycle-events-mc18w30a-mc18w50a' -) diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/gradle.properties b/libraries/networking/networking-mc1.13-pre4-mc18w30b/gradle.properties deleted file mode 100644 index d6140262..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/gradle.properties +++ /dev/null @@ -1,7 +0,0 @@ -environment = * -min_mc_version = 1.13-pre4 -max_mc_version = 18w30b -mc_version_range = >=1.13-rc.4 <=1.13.1-alpha.18.30.b - -minecraft_version = 18w30b -feather_build = 1 diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index aee6a1cd..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import net.minecraft.network.PacketByteBuf; - -public interface CustomPayload { - - void read(PacketByteBuf buffer) throws IOException; - - void write(PacketByteBuf buffer) throws IOException; - -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java deleted file mode 100644 index e1944e62..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class PacketByteBufs { - - public static PacketByteBuf make() { - return make(Unpooled.buffer()); - } - - public static PacketByteBuf make(byte[] bytes) { - return make(Unpooled.wrappedBuffer(bytes)); - } - - public static PacketByteBuf make(ByteBuf buf) { - return new PacketByteBuf(buf); - } - - public static PacketByteBuf make(IOConsumer writer) throws IOException { - PacketByteBuf buffer = make(); - writer.accept(buffer); - return buffer; - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index cfb3c548..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,154 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListener(Identifier channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(Identifier channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(Identifier channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(Identifier channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(Identifier channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(Identifier channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(Identifier channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(Identifier channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(Identifier channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 4d0e1a1d..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,317 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListener(Identifier channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(Identifier channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, Identifier channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 7ef2cb48..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final Identifier CHANNEL = new Identifier("osl", "handshake"); - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(PacketByteBuf buffer) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = buffer.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(buffer.readIdentifier()); - } - } - } - - @Override - public void write(PacketByteBuf buffer) throws IOException { - buffer.writeInt(channels.size()); - - for (Identifier channel : channels) { - buffer.writeIdentifier(channel); - } - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java deleted file mode 100644 index 7e34822c..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -public class NetworkListener { - - private final T listener; - private final boolean async; - - public NetworkListener(T listener, boolean async) { - this.listener = listener; - this.async = async; - } - - public T get() { - return listener; - } - - public boolean isAsync() { - return async; - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index 5faf509d..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,183 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(data); - - return listener.handle(minecraft, handler, payload); - }, async); - } - - public static void registerListener(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(Identifier channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(Identifier channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(Identifier channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, CustomPayloadS2CPacket packet) { - Identifier channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, minecraft); - } - - try { - return listener.get().handle(minecraft, handler, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(Identifier channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(Identifier channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(Identifier channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(Identifier channel, PacketByteBuf data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(Identifier channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(Identifier channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(Identifier channel, PacketByteBuf data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(Identifier channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(Identifier channel, IOConsumer writer) { - try { - return new CustomPayloadC2SPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(Identifier channel, PacketByteBuf data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java deleted file mode 100644 index e4a813f5..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - -public interface ICustomPayloadPacket { - - Identifier osl$networking$getChannel(); - - PacketByteBuf osl$networking$getData(); - -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index e06f34db..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -import net.minecraft.resource.Identifier; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(Identifier channel); - -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java deleted file mode 100644 index 5719c0a7..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; - -@Mixin(CustomPayloadC2SPacket.class) -public class CustomPayloadC2SPacketMixin implements ICustomPayloadPacket { - - @Shadow private Identifier channel; - @Shadow private PacketByteBuf data; - - @Override - public Identifier osl$networking$getChannel() { - return channel; - } - - @Override - public PacketByteBuf osl$networking$getData() { - return data; - } -} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index d0448b0c..00000000 --- a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,286 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(data); - - return listener.handle(server, handler, player, payload); - }, async); - } - - public static void registerListener(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(Identifier channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(Identifier channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(Identifier channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadC2SPacket packet) { - ICustomPayloadPacket p = (ICustomPayloadPacket)packet; - - Identifier channel = p.osl$networking$getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, server); - } - - try { - return listener.get().handle(server, handler, player, p.osl$networking$getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, Identifier channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, Identifier channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, Identifier channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, Identifier channel, PacketByteBuf data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, Identifier channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, Identifier channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, Identifier channel, PacketByteBuf data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> true), channel, data); - } - - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.getPlayerManager().getAll(), filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(Identifier channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(Identifier channel, IOConsumer writer) { - try { - return new CustomPayloadS2CPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(Identifier channel, PacketByteBuf data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/build.gradle b/libraries/networking/networking-mc11w49a-mc12w16a/build.gradle deleted file mode 100644 index 48639a05..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mc12w01a-mc12w17a' -) diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index f0963ed6..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,70 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index dbe85275..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 7eda8cca..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 18cff165..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(Packet.readString(input, Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - Packet.writeString(channel, output); - } - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 86a22831..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - // empty impl - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index 6371ab97..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,179 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return makePacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - CustomPayloadPacket packet = new CustomPayloadPacket(); - - packet.channel = channel; - packet.data = data; - if (data != null) { - packet.size = data.length; - if (packet.size > 32767) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - - return packet; - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java deleted file mode 100644 index 64e9d6b1..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -import net.minecraft.network.packet.CustomPayloadPacket; - -import net.ornithemc.osl.networking.api.Channels; - -@Mixin(CustomPayloadPacket.class) -public class CustomPayloadPacketMixin { - - @ModifyConstant( - method = "read", - constant = @Constant( - intValue = 16 - ) - ) - private int osl$networking$modifyMaxChannelLength(int maxLength) { - return Channels.MAX_LENGTH; - } -} diff --git a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 4d56cdbb..00000000 --- a/libraries/networking/networking-mc11w49a-mc12w16a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,280 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return makePacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - CustomPayloadPacket packet = new CustomPayloadPacket(); - - packet.channel = channel; - packet.data = data; - if (data != null) { - packet.size = data.length; - if (packet.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - - return packet; - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/build.gradle b/libraries/networking/networking-mc12w17a-mc12w17a/build.gradle deleted file mode 100644 index 48639a05..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mc12w01a-mc12w17a' -) diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index f0963ed6..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,70 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index dbe85275..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 7eda8cca..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 18cff165..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(Packet.readString(input, Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - Packet.writeString(channel, output); - } - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 86a22831..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - // empty impl - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index 6371ab97..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,179 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return makePacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - CustomPayloadPacket packet = new CustomPayloadPacket(); - - packet.channel = channel; - packet.data = data; - if (data != null) { - packet.size = data.length; - if (packet.size > 32767) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - - return packet; - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java deleted file mode 100644 index 64e9d6b1..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -import net.minecraft.network.packet.CustomPayloadPacket; - -import net.ornithemc.osl.networking.api.Channels; - -@Mixin(CustomPayloadPacket.class) -public class CustomPayloadPacketMixin { - - @ModifyConstant( - method = "read", - constant = @Constant( - intValue = 16 - ) - ) - private int osl$networking$modifyMaxChannelLength(int maxLength) { - return Channels.MAX_LENGTH; - } -} diff --git a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 4d56cdbb..00000000 --- a/libraries/networking/networking-mc12w17a-mc12w17a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,280 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return makePacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - CustomPayloadPacket packet = new CustomPayloadPacket(); - - packet.channel = channel; - packet.data = data; - if (data != null) { - packet.size = data.length; - if (packet.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - - return packet; - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/build.gradle b/libraries/networking/networking-mc12w18a-mc12w19a/build.gradle deleted file mode 100644 index a722cfca..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mc12w18a-mc12w19a' -) diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 7eda8cca..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 18cff165..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(Packet.readString(input, Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - Packet.writeString(channel, output); - } - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index b7e45de5..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,168 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index a5519db4..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java deleted file mode 100644 index aeb43506..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -import net.minecraft.network.packet.CustomPayloadPacket; - -import net.ornithemc.osl.networking.api.Channels; - -@Mixin(CustomPayloadPacket.class) -public class CustomPayloadPacketMixin { - - @ModifyConstant( - method = "read", - constant = @Constant( - intValue = 20 - ) - ) - private int osl$networking$modifyMaxChannelLength(int maxLength) { - return Channels.MAX_LENGTH; - } -} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 17ba40b5..00000000 --- a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,269 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index d5adcfb8..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index e248c524..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,83 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 83d990f4..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 18cff165..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(Packet.readString(input, Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - Packet.writeString(channel, output); - } - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index b7e45de5..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,168 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index a5519db4..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java deleted file mode 100644 index aeb43506..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadPacketMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -import net.minecraft.network.packet.CustomPayloadPacket; - -import net.ornithemc.osl.networking.api.Channels; - -@Mixin(CustomPayloadPacket.class) -public class CustomPayloadPacketMixin { - - @ModifyConstant( - method = "read", - constant = @Constant( - intValue = 20 - ) - ) - private int osl$networking$modifyMaxChannelLength(int maxLength) { - return Channels.MAX_LENGTH; - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index de25d533..00000000 --- a/libraries/networking/networking-mc12w21a-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,269 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.CustomPayloadPacket; -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.getPlayerManager().players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index aee6a1cd..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import net.minecraft.network.PacketByteBuf; - -public interface CustomPayload { - - void read(PacketByteBuf buffer) throws IOException; - - void write(PacketByteBuf buffer) throws IOException; - -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java deleted file mode 100644 index 93b76c29..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class PacketByteBufs { - - public static PacketByteBuf make() { - return make(Unpooled.buffer()); - } - - public static PacketByteBuf make(byte[] bytes) { - return bytes == null ? make() : make(Unpooled.wrappedBuffer(bytes)); - } - - public static PacketByteBuf make(ByteBuf buf) { - return buf == null ? make() : new PacketByteBuf(buf); - } - - public static PacketByteBuf make(IOConsumer writer) throws IOException { - PacketByteBuf buffer = make(); - writer.accept(buffer); - return buffer; - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index d5adcfb8..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index 67cc2c08..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,174 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 5ad5c022..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(PacketByteBuf buffer) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = buffer.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(buffer.readString(Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(PacketByteBuf buffer) throws IOException { - buffer.writeInt(channels.size()); - - for (String channel : channels) { - buffer.writeString(channel); - } - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index 3174f5ee..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,184 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(PacketByteBufs.make(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, PacketByteBufs.make(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, CustomPayloadS2CPacket packet) { - String channel = packet.getChannel(); - Listener listener = LISTENERS.get(channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, PacketByteBuf data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, PacketByteBuf data) { - sendPacket(makePacket(channel, data)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadC2SPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java deleted file mode 100644 index 76ae7ad0..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -public interface ICustomPayloadPacket { - - // channel/data getters do exist in the Vanilla classes, - // but only in 13w41b and above, not in 13w41a - - String osl$networking$getChannel(); - - byte[] osl$networking$getData(); - -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index a5519db4..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/CustomPayloadS2CPacketMixin.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/CustomPayloadS2CPacketMixin.java deleted file mode 100644 index 2dcda1c8..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/CustomPayloadS2CPacketMixin.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; - -@Mixin(CustomPayloadS2CPacket.class) -public class CustomPayloadS2CPacketMixin implements ICustomPayloadPacket { - - @Shadow private String channel; - @Shadow private byte[] data; - - @Override - public String osl$networking$getChannel() { - return channel; - } - @Override - public byte[] osl$networking$getData() { - return data; - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java deleted file mode 100644 index e2311dd7..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; - -@Mixin(CustomPayloadC2SPacket.class) -public class CustomPayloadC2SPacketMixin implements ICustomPayloadPacket { - - @Shadow private String channel; - @Shadow private byte[] data; - - @Override - public String osl$networking$getChannel() { - return channel; - } - @Override - public byte[] osl$networking$getData() { - return data; - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 150bf476..00000000 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,309 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(PacketByteBufs.make(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, PacketByteBufs.make(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadC2SPacket packet) { - String channel = packet.getChannel(); - Listener listener = LISTENERS.get(channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, PacketByteBuf data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> true), channel, data); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.getPlayerManager().players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadS2CPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/build.gradle b/libraries/networking/networking-mc13w41a-mc18w30b/build.gradle similarity index 100% rename from libraries/networking/networking-mc13w41a-mc14w20b/build.gradle rename to libraries/networking/networking-mc13w41a-mc18w30b/build.gradle diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/gradle.properties b/libraries/networking/networking-mc13w41a-mc18w30b/gradle.properties new file mode 100644 index 00000000..f397496b --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/gradle.properties @@ -0,0 +1,7 @@ +environment = * +min_mc_version = 13w41a +max_mc_version = 18w30b +mc_version_range = >=1.7-alpha.13.41.a <=1.13.1-alpha.18.30.b + +minecraft_version = 18w30b +feather_build = 2 diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java new file mode 100644 index 00000000..e3de1820 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java @@ -0,0 +1,49 @@ +package net.ornithemc.osl.networking.api; + +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierImpl; +import net.ornithemc.osl.networking.impl.ChannelIdentifierException; +import net.ornithemc.osl.networking.impl.ChannelIdentifierParseException; + +/** + * Utility methods for converting {@link NamespacedIdentifier}s from and to {@link Identifier}s. + */ +public final class IdentifierChannelIdentifierParser { + + /** + * Convert the given {@code Identifier} to a {@link NamespacedIdentifier}. + * The returned channel identifier may be invalid. + * + * @return the {@code NamespacedIdentifier} represented by the {@code Identifier}. + */ + public static NamespacedIdentifier fromIdentifier(Identifier id) { + return new NamespacedIdentifierImpl(id.getNamespace(), id.getPath()); + } + + /** + * Convert the given {@code Identifier} to a {@code NamespacedIdentifier}. + * The returned channel identifier is always valid. If no valid channel + * identifier can be parsed from the given identifier, an exception is + * thrown. + * + * @return the {@code NamespacedIdentifier} represented by the {@code Identifier}. + * @throws ChannelIdentifierParseException + * if no valid {@code NamespacedIdentifier} can be parsed from the given {@code Identifier}. + */ + public static NamespacedIdentifier fromIdentifierOrThrow(Identifier id) { + try { + return ChannelIdentifiers.from(id.getNamespace(), id.getPath()); + } catch (ChannelIdentifierException e) { + throw ChannelIdentifierParseException.invalid(id.toString(), e); + } + } + + /** + * Convert the given {@code NamespacedIdentifier} to its {@code Identifier} representation. + */ + public static Identifier toIdentifier(NamespacedIdentifier id) { + return new Identifier(id.namespace(), id.identifier()); + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..ebf33fb7 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1227 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.util.ByteProcessor; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.resource.Identifier; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3i; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + private static final int VAR_VALUE_BITS = 7; + private static final int VAR_VALUE_MASK = 1 << VAR_VALUE_BITS - 1; + private static final int VAR_PARITY_VALUE = 1 << VAR_VALUE_BITS; + private static final int VAR_INT_MAX_BYTES = 5; + private static final int VAR_LONG_MAX_BYTES = 10; + + private static final int TEXT_JSON_MAX_LENGTH = 0x40000; + + // this module spans 13w41a-18w30b but BlockPos to long packing was added in 14w03a + private static final boolean SUPPORT_BLOCKPOS_PACKING = (BlockPos.class.getSuperclass() == Vec3i.class); + + final PacketByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this(new PacketByteBuf(delegate)); + } + + public PacketBuffer(PacketByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + return this.delegate.readVarInt(); + } + + public long readVarLong() { + long value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_LONG_MAX_BYTES) { + throw new RuntimeException("VarLong too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public byte[] readByteArray() { + return this.readByteArray(this.readableBytes()); + } + + public byte[] readByteArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("ByteArray with size " + length + " is bigger than allowed " + maxLength); + } + + byte[] values = new byte[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readByte(); + } + + return values; + } + + public int[] readIntArray() { + return this.readIntArray(this.readableBytes()); + } + + public int[] readIntArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("IntArray with size " + length + " is bigger than allowed " + maxLength); + } + + int[] values = new int[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarInt(); + } + + return values; + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength * 4) { + throw new RuntimeException("The received encoded string buffer length is longer than maximum allowed (" + length + " > " + maxLength * 4 + ")"); + } + if (length < 0) { + throw new RuntimeException("The received encoded string buffer length is less than zero! Weird string!"); + } + + String s = this.toString(this.readerIndex(), length, StandardCharsets.UTF_8); + this.readerIndex(this.readerIndex() + length); + + if (s.length() > maxLength) { + throw new RuntimeException("The received string length is longer than maximum allowed (" + length + " > " + maxLength + ")"); + } + + return s; + } + + public > T readEnum(Class type) { + return type.getEnumConstants()[this.readVarInt()]; + } + + public Date readDate() { + return new Date(this.readLong()); + } + + public UUID readUuid() { + return new UUID( + this.readLong(), + this.readLong() + ); + } + + public BlockPos readBlockPos() { + if (SUPPORT_BLOCKPOS_PACKING) { + return BlockPos.fromLong(this.readLong()); + } else { + throw new UnsupportedOperationException("BlockPos packing is not supported in this Minecraft version!"); + } + } + + public Identifier readIdentifier() { + return new Identifier(this.readString()); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public Text readText() { + return Text.Serializer.fromJson(this.readString(TEXT_JSON_MAX_LENGTH)); + } + + public NbtCompound readNbtCompound() { + return this.delegate.readNbtCompound(); + } + + public ItemStack readItem() { + return this.delegate.readItem(); + } + + public ByteBuf writeVarInt(int value) { + return this.delegate.writeVarInt(value); + } + + public ByteBuf writeVarLong(long value) { + while ((value & -128) != 0) { + this.writeByte((int) (value & VAR_VALUE_MASK) | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte((int) value); + + return this; + } + + public ByteBuf writeByteArray(byte[] values) { + this.writeVarInt(values.length); + + for (byte value : values) { + this.writeByte(value); + } + + return this; + } + + public ByteBuf writeIntArray(int[] values) { + this.writeVarInt(values.length); + + for (int value : values) { + this.writeVarInt(value); + } + + return this; + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + if (bytes.length > maxLength) { + throw new RuntimeException("String too big (was " + bytes.length + " bytes encoded, max " + maxLength + ")"); + } + + this.writeByteArray(bytes); + + return this; + } + + public ByteBuf writeEnum(Enum value) { + this.writeVarInt(value.ordinal()); + + return this; + } + + public ByteBuf writeDate(Date date) { + this.writeLong(date.getTime()); + + return this; + } + + public ByteBuf writeUuid(UUID uuid) { + this.writeLong(uuid.getMostSignificantBits()); + this.writeLong(uuid.getLeastSignificantBits()); + + return this; + } + + public ByteBuf writeBlockPos(BlockPos pos) { + if (SUPPORT_BLOCKPOS_PACKING) { + this.writeLong(pos.toLong()); + } else { + throw new UnsupportedOperationException("BlockPos packing is not supported in this Minecraft version!"); + } + + return this; + } + + public ByteBuf writeIdentifier(Identifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeText(Text text) { + return this.writeString(Text.Serializer.toJson(text), TEXT_JSON_MAX_LENGTH); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + return this.delegate.writeNbtCompound(nbt); + } + + public ByteBuf writeItem(ItemStack item) { + return this.delegate.writeItem(item); + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..516f6420 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.minecraft.network.PacketByteBuf; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } + + public static PacketByteBuf unwrapped(PacketBuffer buffer) { + return buffer.delegate; + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java similarity index 100% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java rename to libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java new file mode 100644 index 00000000..ddee55ed --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java @@ -0,0 +1,46 @@ +package net.ornithemc.osl.networking.api.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ClientPacketListener { + + /** + * Receive incoming data from the server. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ClientPacketListener { + } + + @FunctionalInterface + interface Buffer extends ClientPacketListener { + } + + @FunctionalInterface + interface Bytes extends ClientPacketListener { + } + + interface Context { + + /** + * @return the current Minecraft game instance. + */ + Minecraft minecraft(); + + /** + * @return the network handler that received the packet. + */ + ClientPlayNetworkHandler networkHandler(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java new file mode 100644 index 00000000..3b700508 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java @@ -0,0 +1,129 @@ +package net.ornithemc.osl.networking.api.client; + +import java.util.function.Supplier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; + +public final class ClientPlayNetworking { + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be deserialized into a {@code CustomPayload} object of the given type. + */ + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. + */ + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be given as a raw {@code byte[]}. + */ + public static void registerLegacyListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Remove the listener registered to the given channel. + */ + public static void unregisterListener(NamespacedIdentifier channel) { + ClientPlayNetworkingImpl.unregisterListener(channel); + } + + /** + * Check whether the connection is ready for data to be sent to the server. + */ + public static boolean isPlayReady() { + return ClientPlayNetworkingImpl.isPlayReady(); + } + + /** + * Check whether the given channel is open for data to be sent through it. + * This method will return {@code false} if the client is not connected to a + * server, or if the server has no listeners for the given channel. + */ + public static boolean isPlayReady(NamespacedIdentifier channel) { + return ClientPlayNetworkingImpl.isPlayReady(channel); + } + + /** + * Send a packet to the server through the given channel. The payload will + * only be written if the channel is open. + */ + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.send(channel, payload); + } + + /** + * Send a packet to the server through the given channel. The writer will + * only be called if the channel is open. + */ + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.send(channel, writer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.send(channel, bytes); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.sendNoCheck(channel, payload); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, writer); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, buffer); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.sendNoCheck(channel, bytes); + } +} diff --git a/libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java similarity index 100% rename from libraries/networking/networking-mc1.13-pre4-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java rename to libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java new file mode 100644 index 00000000..f64f13bb --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.api.server; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ServerPacketListener { + + /** + * Receive incoming data from the client. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ServerPacketListener { + } + + @FunctionalInterface + interface Buffer extends ServerPacketListener { + } + + @FunctionalInterface + interface Bytes extends ServerPacketListener { + } + + interface Context { + + /** + * @return the current MinecraftServer game instance. + */ + MinecraftServer server(); + + /** + * @return the network handler that received the packet. + */ + ServerPlayNetworkHandler networkHandler(); + + /** + * @return the player that received the packet. + */ + ServerPlayerEntity player(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java similarity index 52% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java rename to libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java index ecfc060e..88bff1f0 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java @@ -1,50 +1,45 @@ package net.ornithemc.osl.networking.api.server; -import java.io.IOException; import java.util.function.Supplier; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; public final class ServerPlayNetworking { /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be deserialized into a {@code CustomPayload} object of the given type. */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. */ - public static void registerListener(String channel, ByteBufListener listener) { + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be given as a raw {@code byte[]}. */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); + public static void registerLegacyListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Remove the listener registered to the given channel. */ - public static void unregisterListener(String channel) { + public static void unregisterListener(NamespacedIdentifier channel) { ServerPlayNetworkingImpl.unregisterListener(channel); } @@ -56,19 +51,19 @@ public static boolean isPlayReady(ServerPlayerEntity player) { } /** - * Check whether the given channel is open for data to be sent through it. + * Check whether the given channel is ready for data to be sent through it. * This method will return {@code false} if the client has no listeners for * the given channel. */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + return ServerPlayNetworkingImpl.isPlayReady(player, channel); } /** * Send a packet to the given player through the given channel. The payload * will only be written if the channel is open. */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(player, channel, payload); } @@ -76,29 +71,29 @@ public static void send(ServerPlayerEntity player, String channel, CustomPayload * Send a packet to the given player through the given channel. The writer * will only be called if the channel is open. */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(player, channel, writer); } /** * Send a packet to the given player through the given channel. */ - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(player, channel, buffer); } /** * Send a packet to the given player through the given channel. */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(player, channel, bytes); } /** * Send a packet to the given players through the given channel. The payload * will only be written if the channel is open for at least one player. */ - public static void send(Iterable players, String channel, CustomPayload payload) { + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(players, channel, payload); } @@ -106,22 +101,22 @@ public static void send(Iterable players, String channel, Cu * Send a packet to the given players through the given channel. The writer * will only be called if the channel is open for at least one player. */ - public static void send(Iterable players, String channel, IOConsumer writer) { + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(players, channel, writer); } /** * Send a packet to the given players through the given channel. */ - public static void send(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(players, channel, data); + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(players, channel, buffer); } /** * Send a packet to the given players through the given channel. */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(players, channel, bytes); } /** @@ -129,7 +124,7 @@ public static void send(Iterable players, String channel, by * channel. The payload will only be written if the channel is open for at * least one player. */ - public static void send(int dimension, String channel, CustomPayload payload) { + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(dimension, channel, payload); } @@ -138,7 +133,7 @@ public static void send(int dimension, String channel, CustomPayload payload) { * channel. The writer will only be called if the channel is open for at * least one player. */ - public static void send(int dimension, String channel, IOConsumer writer) { + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(dimension, channel, writer); } @@ -146,23 +141,23 @@ public static void send(int dimension, String channel, IOConsumer * Send a packet to the players in the given dimension through the given * channel. */ - public static void send(int dimension, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); + public static void send(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(dimension, channel, buffer); } /** * Send a packet to the players in the given dimension through the given * channel. */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); + public static void send(int dimension, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(dimension, channel, bytes); } /** * Send a packet to all players through the given channel. The payload will * only be written if the channel is open for at least one player. */ - public static void send(String channel, CustomPayload payload) { + public static void send(NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(channel, payload); } @@ -170,22 +165,22 @@ public static void send(String channel, CustomPayload payload) { * Send a packet to all players through the given channel. The writer will * only be called if the channel is open for at least one player. */ - public static void send(String channel, IOConsumer writer) { + public static void send(NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(channel, writer); } /** * Send a packet to all players through the given channel. */ - public static void send(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(channel, buffer); } /** * Send a packet to all players through the given channel. */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(channel, bytes); } /** @@ -194,8 +189,8 @@ public static void send(String channel, byte[] data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, payload); } /** @@ -204,8 +199,8 @@ public static void doSend(ServerPlayerEntity player, String channel, CustomPaylo * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, writer); } /** @@ -214,8 +209,8 @@ public static void doSend(ServerPlayerEntity player, String channel, IOConsumer< * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, buffer); } /** @@ -224,8 +219,8 @@ public static void doSend(ServerPlayerEntity player, String channel, PacketByteB * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, bytes); } /** @@ -234,8 +229,8 @@ public static void doSend(ServerPlayerEntity player, String channel, byte[] data * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, payload); } /** @@ -244,8 +239,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, writer); } /** @@ -254,8 +249,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, buffer); } /** @@ -264,8 +259,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, bytes); } /** @@ -274,8 +269,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, payload); } /** @@ -284,8 +279,8 @@ public static void doSend(int dimension, String channel, CustomPayload payload) * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, writer); } /** @@ -294,8 +289,8 @@ public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, writer); } /** @@ -334,8 +329,8 @@ public static void doSend(String channel, IOConsumer writer) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(channel, data); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, buffer); } /** @@ -344,46 +339,7 @@ public static void doSend(String channel, PacketByteBuf data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(channel, bytes); } } diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java new file mode 100644 index 00000000..a9bbf2ba --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -0,0 +1,62 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.IOException; +import java.util.LinkedHashSet; +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class HandshakePayload implements PacketPayload { + + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); + + public byte protocol; + public Set channels; + + public HandshakePayload() { + } + + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); + } + + public static HandshakePayload client() { + return new HandshakePayload(ClientPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); + } + + public static HandshakePayload server() { + return new HandshakePayload(ServerPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); + } + + @Override + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); + channels = new LinkedHashSet<>(); + + int channelCount = buffer.readInt(); + + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); + } + } + + @Override + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); + buffer.writeInt(channels.size()); + + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); + } + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..d2d5f3d6 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,15 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; + +@FunctionalInterface +public interface PacketFactory { + + // Packet is not generic prior to 14w31a + @SuppressWarnings("rawtypes") + Packet create(NamespacedIdentifier channel, PacketBuffer data); + +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..3241aa75 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,12 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + PacketBuffer osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..d3e3c4e0 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,15 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java new file mode 100644 index 00000000..6fc92ae4 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.List; + +import net.minecraft.server.entity.living.player.ServerPlayerEntity; + +public interface PlayerManagerAccess { + + List osl$networking$getAll(); + +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java new file mode 100644 index 00000000..8aa40704 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java @@ -0,0 +1,244 @@ +package net.ornithemc.osl.networking.impl.client; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Supplier; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.client.ClientPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +public final class ClientPlayNetworkingImpl { + + private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); + + private static PacketFactory packetFactory; + private static Minecraft minecraft; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ClientPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up client custom payload packet factory when it was already set up!"); + } + + ClientPlayNetworkingImpl.packetFactory = factory; + } + + public static void setUp(Minecraft minecraft) { + if (ClientPlayNetworkingImpl.minecraft == minecraft) { + throw new IllegalStateException("tried to set up client play networking when it was already set up!"); + } + if (ClientPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up client play networking when no custom payload packet factory was set up!"); + } + + ClientPlayNetworkingImpl.minecraft = minecraft; + ClientPlayNetworkingImpl.thread = Thread.currentThread(); + } + + public static void destroy(Minecraft minecraft) { + if (ClientPlayNetworkingImpl.minecraft != minecraft) { + throw new IllegalStateException("tried to destroy client play networking when it was not set up!"); + } + + ClientPlayNetworkingImpl.minecraft = null; + ClientPlayNetworkingImpl.thread = null; + } + + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); + + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + registerListenerInternal(channel, (context, buffer) -> { + T payload = initializer.get(); + payload.read(buffer); + + listener.handle(context, payload); + }); + } + + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + registerListenerInternal(channel, listener::handle); + } + + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + registerListenerInternal(channel, (context, buffer) -> listener.handle(context, buffer.readByteArray())); + } + + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isClientbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not client-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { + if (value != null) { + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); + } + + return listener; + }); + } + + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); + } + + // Packet is not generic prior to 14w31a + @SuppressWarnings("rawtypes") + public static boolean handlePacket(Minecraft minecraft, ClientPlayNetworkHandler handler, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); + + if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(); + PacketBuffer data = p.osl$networking$getData(); + + try { + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) minecraft).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); + } + + return true; + } + + return false; + } + + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, PacketBuffer data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + public static boolean isPlayReady() { + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); + return handler != null && handler.osl$networking$isPlayReady(); + } + + public static boolean isPlayReady(NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(channel)) { + sendInternal(channel, payload); + } + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(channel)) { + sendInternal(channel, writer); + } + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(channel)) { + sendInternal(channel, buffer); + } + } + + public static void send(NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(channel)) { + sendInternal(channel, bytes); + } + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(channel, writer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(channel, buffer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(channel, bytes); + } + + private static void sendInternal(NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(channel, PacketBuffers.make(writer)); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(channel, buffer); + } + + private static void sendInternal(NamespacedIdentifier channel, byte[] bytes) { + sendPacket(channel, PacketBuffers.wrap(bytes)); + } + + private static void sendPacket(NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isServerbound()) { + minecraft.getNetworkHandler().sendPacket(packetFactory.create(channel, data)); + } + } + + @FunctionalInterface + private interface ChannelListener { + + void handle(Context context, PacketBuffer buffer) throws IOException; + + class Context implements ClientPacketListener.Context { + + @Override + public Minecraft minecraft() { + return minecraft; + } + + @Override + public ClientPlayNetworkHandler networkHandler() { + return minecraft.getNetworkHandler(); + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } + } +} diff --git a/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java new file mode 100644 index 00000000..b73ca1a1 --- /dev/null +++ b/libraries/networking/networking-mc13w41a-mc18w30b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java @@ -0,0 +1,411 @@ +package net.ornithemc.osl.networking.impl.server; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.function.Supplier; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.network.packet.Packet; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.server.ServerPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.PlayerManagerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +public final class ServerPlayNetworkingImpl { + + private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); + + private static PacketFactory packetFactory; + private static MinecraftServer server; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ServerPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up server custom payload packet factory when it was already set up!"); + } + + ServerPlayNetworkingImpl.packetFactory = factory; + } + + public static void setUp(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server == server) { + throw new IllegalStateException("tried to set up server play networking when it was already set up!"); + } + if (ServerPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up server play networking when no custom payload packet factory was set up!"); + } + + ServerPlayNetworkingImpl.server = server; + ServerPlayNetworkingImpl.thread = Thread.currentThread(); + } + + public static void destroy(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server != server) { + throw new IllegalStateException("tried to destroy server play networking when it was not set up!"); + } + + ServerPlayNetworkingImpl.server = null; + ServerPlayNetworkingImpl.thread = null; + } + + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); + + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + registerListenerInternal(channel, (context, buffer) -> { + T payload = initializer.get(); + payload.read(buffer); + + listener.handle(context, payload); + }); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + registerListenerInternal(channel, listener::handle); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + registerListenerInternal(channel, (context, buffer) -> listener.handle(context, buffer.readByteArray())); + } + + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isServerbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not server-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { + if (value != null) { + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); + } + + return listener; + }); + } + + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); + } + + // Packet is not generic prior to 14w31a + @SuppressWarnings("rawtypes") + public static boolean handlePacket(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); + + if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(player); + PacketBuffer data = p.osl$networking$getData(); + + try { + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) server).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); + } + + return true; + } + + return false; + } + + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, PacketBuffer data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + public static boolean isPlayReady(ServerPlayerEntity player) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(); + } + + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, payload); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, writer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, buffer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, bytes); + } + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, payload); + } + + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, writer); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, buffer); + } + + public static void send(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, bytes); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(player, channel, payload); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(player, channel, writer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(player, channel, buffer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(player, channel, bytes); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(players, channel, payload); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(players, channel, writer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(players, channel, buffer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(players, channel, bytes); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, payload); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, writer); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, buffer); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, bytes); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(allPlayers(), channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(allPlayers(), channel, writer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(allPlayers(), channel, buffer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(allPlayers(), channel, bytes); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(player, channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(player, channel, PacketBuffers.make(writer)); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(player, channel, buffer); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(player, channel, PacketBuffers.wrap(bytes)); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(players, channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(players, channel, PacketBuffers.make(writer)); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(players, channel, buffer); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(players, channel, PacketBuffers.wrap(bytes)); + } + + private static Iterable allPlayers() { + return ((PlayerManagerAccess) server.getPlayerManager()).osl$networking$getAll(); + } + + private static Iterable collectPlayers(Predicate filter) { + return collectPlayers(allPlayers(), filter); + } + + private static Iterable collectPlayers(Iterable src, Predicate filter) { + List players = new ArrayList<>(); + + for (ServerPlayerEntity player : src) { + if (filter.test(player)) { + players.add(player); + } + } + + return players; + } + + private static void sendPacket(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + player.networkHandler.sendPacket(packetFactory.create(channel, data)); + } + } + + private static void sendPacket(Iterable players, NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + Packet packet = packetFactory.create(channel, data); + + for (ServerPlayerEntity player : players) { + player.networkHandler.sendPacket(packet); + } + } + } + + private interface ChannelListener { + + void handle(Context context, PacketBuffer buffer) throws IOException; + + class Context implements ServerPacketListener.Context { + + private final ServerPlayerEntity player; + + Context(ServerPlayerEntity player) { + this.player = player; + } + + @Override + public MinecraftServer server() { + return server; + } + + @Override + public ServerPlayNetworkHandler networkHandler() { + return player.networkHandler; + } + + @Override + public ServerPlayerEntity player() { + return player; + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } + } +} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/build.gradle b/libraries/networking/networking-mc14w21a-mc14w30c/build.gradle deleted file mode 100644 index 2f186572..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mc13w16a-04192037-mc1.14.4', - 'lifecycle-events-mc13w36a-09051446-mc1.13' -) diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index aee6a1cd..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import net.minecraft.network.PacketByteBuf; - -public interface CustomPayload { - - void read(PacketByteBuf buffer) throws IOException; - - void write(PacketByteBuf buffer) throws IOException; - -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java deleted file mode 100644 index e1944e62..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class PacketByteBufs { - - public static PacketByteBuf make() { - return make(Unpooled.buffer()); - } - - public static PacketByteBuf make(byte[] bytes) { - return make(Unpooled.wrappedBuffer(bytes)); - } - - public static PacketByteBuf make(ByteBuf buf) { - return new PacketByteBuf(buf); - } - - public static PacketByteBuf make(IOConsumer writer) throws IOException { - PacketByteBuf buffer = make(); - writer.accept(buffer); - return buffer; - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index d5adcfb8..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index dd12e8f0..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,201 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRawAsync(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRawAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index e248c524..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,83 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index f8ebbf6a..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,416 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRawAsync(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRawAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 5ad5c022..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(PacketByteBuf buffer) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = buffer.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(buffer.readString(Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(PacketByteBuf buffer) throws IOException { - buffer.writeInt(channels.size()); - - for (String channel : channels) { - buffer.writeString(channel); - } - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java deleted file mode 100644 index 7e34822c..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -public class NetworkListener { - - private final T listener; - private final boolean async; - - public NetworkListener(T listener, boolean async) { - this.listener = listener; - this.async = async; - } - - public T get() { - return listener; - } - - public boolean isAsync() { - return async; - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index 830dc28d..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,214 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(String channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(PacketByteBufs.make(data)); - - return listener.handle(minecraft, handler, payload); - }, async); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(String channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(String channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, PacketByteBufs.make(data)); - }, async); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerRaw(channel, listener, false); - } - - public static void registerListenerRawAsync(String channel, ByteArrayListener listener) { - registerListenerRaw(channel, listener, true); - } - - private static void registerListenerRaw(String channel, ByteArrayListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(String channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, CustomPayloadS2CPacket packet) { - String channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, minecraft); - } - - try { - return listener.get().handle(minecraft, handler, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, PacketByteBuf data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, PacketByteBuf data) { - sendPacket(makePacket(channel, data)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadC2SPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index a5519db4..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java deleted file mode 100644 index 71925ecd..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.List; - -import net.minecraft.server.entity.living.player.ServerPlayerEntity; - -public interface IPlayerManager { - - // a getAll method does exist but only in 1.8.1-pre3 and above - - List osl$networking$getAll(); - -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 0c5372fa..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,339 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.IPlayerManager; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(String channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(PacketByteBufs.make(data)); - - return listener.handle(server, handler, player, payload); - }, async); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(String channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(String channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, PacketByteBufs.make(data)); - }, async); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerRaw(channel, listener, false); - } - - public static void registerListenerRawAsync(String channel, ByteArrayListener listener) { - registerListenerRaw(channel, listener, true); - } - - private static void registerListenerRaw(String channel, ByteArrayListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(String channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadC2SPacket packet) { - String channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, server); - } - - try { - return listener.get().handle(server, handler, player, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, PacketByteBuf data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> true), channel, data); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(((IPlayerManager)server.getPlayerManager()).osl$networking$getAll(), filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadS2CPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json b/libraries/networking/networking-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json deleted file mode 100644 index b9030aa9..00000000 --- a/libraries/networking/networking-mc14w21a-mc14w30c/src/main/resources/osl.networking.mixins.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "required": true, - "minVersion": "0.8", - "package": "net.ornithemc.osl.networking.impl.mixin", - "compatibilityLevel": "JAVA_8", - "mixins": [ - "common.PlayerManagerMixin", - "common.ServerPlayNetworkHandlerMixin" - ], - "client": [ - "client.ClientPlayNetworkHandlerMixin" - ], - "server": [ - ], - "injectors": { - "defaultRequire": 1 - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/build.gradle b/libraries/networking/networking-mc14w31a-mc1.13-pre2/build.gradle deleted file mode 100644 index 2f186572..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mc13w16a-04192037-mc1.14.4', - 'lifecycle-events-mc13w36a-09051446-mc1.13' -) diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index aee6a1cd..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import net.minecraft.network.PacketByteBuf; - -public interface CustomPayload { - - void read(PacketByteBuf buffer) throws IOException; - - void write(PacketByteBuf buffer) throws IOException; - -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java deleted file mode 100644 index e1944e62..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class PacketByteBufs { - - public static PacketByteBuf make() { - return make(Unpooled.buffer()); - } - - public static PacketByteBuf make(byte[] bytes) { - return make(Unpooled.wrappedBuffer(bytes)); - } - - public static PacketByteBuf make(ByteBuf buf) { - return new PacketByteBuf(buf); - } - - public static PacketByteBuf make(IOConsumer writer) throws IOException { - PacketByteBuf buffer = make(); - writer.accept(buffer); - return buffer; - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index d5adcfb8..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index 8bde7c31..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,153 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index e248c524..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,83 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * This applies to connections to dedicated servers as - * well as connections to integrated servers. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 2a904225..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,316 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerAsync(String channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 5ad5c022..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(PacketByteBuf buffer) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = buffer.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(buffer.readString(Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(PacketByteBuf buffer) throws IOException { - buffer.writeInt(channels.size()); - - for (String channel : channels) { - buffer.writeString(channel); - } - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java deleted file mode 100644 index 7e34822c..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -public class NetworkListener { - - private final T listener; - private final boolean async; - - public NetworkListener(T listener, boolean async) { - this.listener = listener; - this.async = async; - } - - public T get() { - return listener; - } - - public boolean isAsync() { - return async; - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index f9078808..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,185 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(String channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(data); - - return listener.handle(minecraft, handler, payload); - }, async); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(String channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(String channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(String channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, CustomPayloadS2CPacket packet) { - String channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, minecraft); - } - - try { - return listener.get().handle(minecraft, handler, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, PacketByteBuf data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, PacketByteBuf data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadC2SPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadC2SPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index a5519db4..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java deleted file mode 100644 index 71925ecd..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/IPlayerManager.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.List; - -import net.minecraft.server.entity.living.player.ServerPlayerEntity; - -public interface IPlayerManager { - - // a getAll method does exist but only in 1.8.1-pre3 and above - - List osl$networking$getAll(); - -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 4921990c..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,286 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.IPlayerManager; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map> LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(String channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(String channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(data); - - return listener.handle(server, handler, player, payload); - }, async); - } - - public static void registerListener(String channel, ByteBufListener listener) { - registerListener(channel, listener, false); - } - - public static void registerListenerAsync(String channel, ByteBufListener listener) { - registerListener(channel, listener, true); - } - - private static void registerListener(String channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } - - private static void registerListenerImpl(String channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return new NetworkListener<>(listener, async); - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadC2SPacket packet) { - String channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); - - if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, server); - } - - try { - return listener.get().handle(server, handler, player, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, PacketByteBuf data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, PacketByteBuf data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, PacketByteBuf data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, PacketByteBuf data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, PacketByteBuf data) { - doSend(collectPlayers(p -> true), channel, data); - } - - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(((IPlayerManager)server.getPlayerManager()).osl$networking$getAll(), filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadS2CPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, PacketByteBuf data) { - return new CustomPayloadS2CPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json b/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json deleted file mode 100644 index b9030aa9..00000000 --- a/libraries/networking/networking-mc14w31a-mc1.13-pre2/src/main/resources/osl.networking.mixins.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "required": true, - "minVersion": "0.8", - "package": "net.ornithemc.osl.networking.impl.mixin", - "compatibilityLevel": "JAVA_8", - "mixins": [ - "common.PlayerManagerMixin", - "common.ServerPlayNetworkHandlerMixin" - ], - "client": [ - "client.ClientPlayNetworkHandlerMixin" - ], - "server": [ - ], - "injectors": { - "defaultRequire": 1 - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/gradle.properties b/libraries/networking/networking-mc18w31a-mc1.14.4/gradle.properties index 2f28bcb7..bd3aa07b 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/gradle.properties +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/gradle.properties @@ -4,4 +4,4 @@ max_mc_version = 1.14.4 mc_version_range = >=1.13.1-alpha.18.31.a <=1.14.4 minecraft_version = 1.14.4 -feather_build = 1 +feather_build = 2 diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index aee6a1cd..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import net.minecraft.network.PacketByteBuf; - -public interface CustomPayload { - - void read(PacketByteBuf buffer) throws IOException; - - void write(PacketByteBuf buffer) throws IOException; - -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java new file mode 100644 index 00000000..e3de1820 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/IdentifierChannelIdentifierParser.java @@ -0,0 +1,49 @@ +package net.ornithemc.osl.networking.api; + +import net.minecraft.resource.Identifier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierImpl; +import net.ornithemc.osl.networking.impl.ChannelIdentifierException; +import net.ornithemc.osl.networking.impl.ChannelIdentifierParseException; + +/** + * Utility methods for converting {@link NamespacedIdentifier}s from and to {@link Identifier}s. + */ +public final class IdentifierChannelIdentifierParser { + + /** + * Convert the given {@code Identifier} to a {@link NamespacedIdentifier}. + * The returned channel identifier may be invalid. + * + * @return the {@code NamespacedIdentifier} represented by the {@code Identifier}. + */ + public static NamespacedIdentifier fromIdentifier(Identifier id) { + return new NamespacedIdentifierImpl(id.getNamespace(), id.getPath()); + } + + /** + * Convert the given {@code Identifier} to a {@code NamespacedIdentifier}. + * The returned channel identifier is always valid. If no valid channel + * identifier can be parsed from the given identifier, an exception is + * thrown. + * + * @return the {@code NamespacedIdentifier} represented by the {@code Identifier}. + * @throws ChannelIdentifierParseException + * if no valid {@code NamespacedIdentifier} can be parsed from the given {@code Identifier}. + */ + public static NamespacedIdentifier fromIdentifierOrThrow(Identifier id) { + try { + return ChannelIdentifiers.from(id.getNamespace(), id.getPath()); + } catch (ChannelIdentifierException e) { + throw ChannelIdentifierParseException.invalid(id.toString(), e); + } + } + + /** + * Convert the given {@code NamespacedIdentifier} to its {@code Identifier} representation. + */ + public static Identifier toIdentifier(NamespacedIdentifier id) { + return new Identifier(id.namespace(), id.identifier()); + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..368bf2b5 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1113 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.util.ByteProcessor; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.resource.Identifier; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + final PacketByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this(new PacketByteBuf(delegate)); + } + + public PacketBuffer(PacketByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + return this.delegate.readVarInt(); + } + + public long readVarLong() { + return this.delegate.readVarLong(); + } + + public byte[] readByteArray() { + return this.delegate.readByteArray(); + } + + public byte[] readByteArray(int maxLength) { + return this.delegate.readByteArray(maxLength); + } + + public int[] readIntArray() { + return this.delegate.readIntArray(); + } + + public int[] readIntArray(int maxLength) { + return this.delegate.readIntArray(maxLength); + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + return this.delegate.readString(maxLength); + } + + public > T readEnum(Class type) { + return this.delegate.readEnum(type); + } + + public Date readDate() { + return this.delegate.readDate(); + } + + public UUID readUuid() { + return this.delegate.readUuid(); + } + + public BlockPos readBlockPos() { + return this.delegate.readBlockPos(); + } + + public Identifier readIdentifier() { + return this.delegate.readIdentifier(); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public Text readText() { + return this.delegate.readText(); + } + + public NbtCompound readNbtCompound() { + return this.delegate.readNbtCompound(); + } + + public ItemStack readItem() { + return this.delegate.readItem(); + } + + public ByteBuf writeVarInt(int value) { + return this.delegate.writeVarInt(value); + } + + public ByteBuf writeVarLong(long value) { + return this.delegate.writeVarLong(value); + } + + public ByteBuf writeByteArray(byte[] bytes) { + return this.delegate.writeByteArray(bytes); + } + + public ByteBuf writeIntArray(int[] values) { + return this.delegate.writeIntArray(values); + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + return this.delegate.writeString(s, maxLength); + } + + public ByteBuf writeEnum(Enum value) { + return this.delegate.writeEnum(value); + } + + public ByteBuf writeDate(Date date) { + return this.delegate.writeDate(date); + } + + public ByteBuf writeUuid(UUID uuid) { + return this.delegate.writeUuid(uuid); + } + + public ByteBuf writeBlockPos(BlockPos pos) { + return this.delegate.writeBlockPos(pos); + } + + public ByteBuf writeIdentifier(Identifier id) { + return this.delegate.writeIdentifier(id); + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeText(Text text) { + return this.delegate.writeText(text); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + return this.delegate.writeNbtCompound(nbt); + } + + public ByteBuf writeItem(ItemStack item) { + return this.delegate.writeItem(item); + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..516f6420 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,41 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.minecraft.network.PacketByteBuf; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } + + public static PacketByteBuf unwrapped(PacketBuffer buffer) { + return buffer.delegate; + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java deleted file mode 100644 index e1944e62..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketByteBufs.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.IOException; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; - -import net.minecraft.network.PacketByteBuf; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class PacketByteBufs { - - public static PacketByteBuf make() { - return make(Unpooled.buffer()); - } - - public static PacketByteBuf make(byte[] bytes) { - return make(Unpooled.wrappedBuffer(bytes)); - } - - public static PacketByteBuf make(ByteBuf buf) { - return new PacketByteBuf(buf); - } - - public static PacketByteBuf make(IOConsumer writer) throws IOException { - PacketByteBuf buffer = make(); - writer.accept(buffer); - return buffer; - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java new file mode 100644 index 00000000..ddee55ed --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java @@ -0,0 +1,46 @@ +package net.ornithemc.osl.networking.api.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ClientPacketListener { + + /** + * Receive incoming data from the server. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ClientPacketListener { + } + + @FunctionalInterface + interface Buffer extends ClientPacketListener { + } + + @FunctionalInterface + interface Bytes extends ClientPacketListener { + } + + interface Context { + + /** + * @return the current Minecraft game instance. + */ + Minecraft minecraft(); + + /** + * @return the network handler that received the packet. + */ + ClientPlayNetworkHandler networkHandler(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java index cfb3c548..3b700508 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java @@ -1,59 +1,43 @@ package net.ornithemc.osl.networking.api.client; -import java.io.IOException; import java.util.function.Supplier; -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; public final class ClientPlayNetworking { /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be deserialized into a {@code CustomPayload} object of the given type. */ - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. */ - public static void registerListener(Identifier channel, ByteBufListener listener) { + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { ClientPlayNetworkingImpl.registerListener(channel, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be given as a raw {@code byte[]}. */ - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - ClientPlayNetworkingImpl.registerListenerAsync(channel, listener); + public static void registerLegacyListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); } /** * Remove the listener registered to the given channel. */ - public static void unregisterListener(Identifier channel) { + public static void unregisterListener(NamespacedIdentifier channel) { ClientPlayNetworkingImpl.unregisterListener(channel); } @@ -69,15 +53,15 @@ public static boolean isPlayReady() { * This method will return {@code false} if the client is not connected to a * server, or if the server has no listeners for the given channel. */ - public static boolean canSend(Identifier channel) { - return ClientPlayNetworkingImpl.canSend(channel); + public static boolean isPlayReady(NamespacedIdentifier channel) { + return ClientPlayNetworkingImpl.isPlayReady(channel); } /** * Send a packet to the server through the given channel. The payload will * only be written if the channel is open. */ - public static void send(Identifier channel, CustomPayload payload) { + public static void send(NamespacedIdentifier channel, PacketPayload payload) { ClientPlayNetworkingImpl.send(channel, payload); } @@ -85,15 +69,22 @@ public static void send(Identifier channel, CustomPayload payload) { * Send a packet to the server through the given channel. The writer will * only be called if the channel is open. */ - public static void send(Identifier channel, IOConsumer writer) { + public static void send(NamespacedIdentifier channel, IOConsumer writer) { ClientPlayNetworkingImpl.send(channel, writer); } /** * Send a packet to the server through the given channel. */ - public static void send(Identifier channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.send(channel, bytes); } /** @@ -102,8 +93,8 @@ public static void send(Identifier channel, PacketByteBuf data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(Identifier channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.sendNoCheck(channel, payload); } /** @@ -112,8 +103,8 @@ public static void doSend(Identifier channel, CustomPayload payload) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(Identifier channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, writer); } /** @@ -122,33 +113,17 @@ public static void doSend(Identifier channel, IOConsumer writer) * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(Identifier channel, PacketByteBuf data) { - ClientPlayNetworkingImpl.doSend(channel, data); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, buffer); } - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, T payload) throws IOException; - - } - - public interface ByteBufListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; - + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.sendNoCheck(channel, bytes); } } diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java new file mode 100644 index 00000000..f64f13bb --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.api.server; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ServerPacketListener { + + /** + * Receive incoming data from the client. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ServerPacketListener { + } + + @FunctionalInterface + interface Buffer extends ServerPacketListener { + } + + @FunctionalInterface + interface Bytes extends ServerPacketListener { + } + + interface Context { + + /** + * @return the current MinecraftServer game instance. + */ + MinecraftServer server(); + + /** + * @return the network handler that received the packet. + */ + ServerPlayNetworkHandler networkHandler(); + + /** + * @return the player that received the packet. + */ + ServerPlayerEntity player(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java index 4f9a8053..d983696d 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java @@ -1,61 +1,46 @@ package net.ornithemc.osl.networking.api.server; -import java.io.IOException; import java.util.function.Supplier; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; -import net.minecraft.server.MinecraftServer; import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; import net.minecraft.world.dimension.DimensionType; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; public final class ServerPlayNetworking { /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be deserialized into a {@code CustomPayload} object of the given type. */ - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. */ - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. - */ - public static void registerListener(Identifier channel, ByteBufListener listener) { + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener may be called off the main thread. - * A channel can be any valid {@linkplain net.minecraft.resource.Identifier Identifier}. + * The data will be given as a raw {@code byte[]}. */ - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - ServerPlayNetworkingImpl.registerListenerAsync(channel, listener); + public static void registerLegacyListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Remove the listener registered to the given channel. */ - public static void unregisterListener(Identifier channel) { + public static void unregisterListener(NamespacedIdentifier channel) { ServerPlayNetworkingImpl.unregisterListener(channel); } @@ -67,19 +52,19 @@ public static boolean isPlayReady(ServerPlayerEntity player) { } /** - * Check whether the given channel is open for data to be sent through it. + * Check whether the given channel is ready for data to be sent through it. * This method will return {@code false} if the client has no listeners for * the given channel. */ - public static boolean canSend(ServerPlayerEntity player, Identifier channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + return ServerPlayNetworkingImpl.isPlayReady(player, channel); } /** * Send a packet to the given player through the given channel. The payload * will only be written if the channel is open. */ - public static void send(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(player, channel, payload); } @@ -87,22 +72,29 @@ public static void send(ServerPlayerEntity player, Identifier channel, CustomPay * Send a packet to the given player through the given channel. The writer * will only be called if the channel is open. */ - public static void send(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(player, channel, writer); } /** * Send a packet to the given player through the given channel. */ - public static void send(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(player, channel, bytes); } /** * Send a packet to the given players through the given channel. The payload * will only be written if the channel is open for at least one player. */ - public static void send(Iterable players, Identifier channel, CustomPayload payload) { + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(players, channel, payload); } @@ -110,15 +102,22 @@ public static void send(Iterable players, Identifier channel * Send a packet to the given players through the given channel. The writer * will only be called if the channel is open for at least one player. */ - public static void send(Iterable players, Identifier channel, IOConsumer writer) { + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(players, channel, writer); } /** * Send a packet to the given players through the given channel. */ - public static void send(Iterable players, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(players, channel, data); + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(players, channel, bytes); } /** @@ -126,7 +125,7 @@ public static void send(Iterable players, Identifier channel * channel. The payload will only be written if the channel is open for at * least one player. */ - public static void send(DimensionType dimension, Identifier channel, CustomPayload payload) { + public static void send(DimensionType dimension, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(dimension, channel, payload); } @@ -135,7 +134,7 @@ public static void send(DimensionType dimension, Identifier channel, CustomPaylo * channel. The writer will only be called if the channel is open for at * least one player. */ - public static void send(DimensionType dimension, Identifier channel, IOConsumer writer) { + public static void send(DimensionType dimension, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(dimension, channel, writer); } @@ -143,15 +142,23 @@ public static void send(DimensionType dimension, Identifier channel, IOConsumer< * Send a packet to the players in the given dimension through the given * channel. */ - public static void send(DimensionType dimension, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); + public static void send(DimensionType dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(dimension, channel, buffer); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel. + */ + public static void send(DimensionType dimension, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(dimension, channel, bytes); } /** * Send a packet to all players through the given channel. The payload will * only be written if the channel is open for at least one player. */ - public static void send(Identifier channel, CustomPayload payload) { + public static void send(NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(channel, payload); } @@ -159,15 +166,22 @@ public static void send(Identifier channel, CustomPayload payload) { * Send a packet to all players through the given channel. The writer will * only be called if the channel is open for at least one player. */ - public static void send(Identifier channel, IOConsumer writer) { + public static void send(NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(channel, writer); } /** * Send a packet to all players through the given channel. */ - public static void send(Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(channel, bytes); } /** @@ -176,8 +190,8 @@ public static void send(Identifier channel, PacketByteBuf data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, payload); } /** @@ -186,8 +200,8 @@ public static void doSend(ServerPlayerEntity player, Identifier channel, CustomP * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, writer); } /** @@ -196,8 +210,28 @@ public static void doSend(ServerPlayerEntity player, Identifier channel, IOConsu * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, payload); } /** @@ -206,8 +240,8 @@ public static void doSend(ServerPlayerEntity player, Identifier channel, PacketB * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, writer); } /** @@ -216,8 +250,8 @@ public static void doSend(Iterable players, Identifier chann * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, buffer); } /** @@ -226,8 +260,8 @@ public static void doSend(Iterable players, Identifier chann * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, bytes); } /** @@ -236,8 +270,8 @@ public static void doSend(Iterable players, Identifier chann * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(DimensionType dimension, Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, payload); } /** @@ -246,8 +280,8 @@ public static void doSend(DimensionType dimension, Identifier channel, CustomPay * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(DimensionType dimension, Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, writer); } /** @@ -256,18 +290,18 @@ public static void doSend(DimensionType dimension, Identifier channel, IOConsume * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(DimensionType dimension, Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, buffer); } /** - * Send a packet to all players through the given channel, without - * checking whether it is open. + * Send a packet to the players in the given dimension through the given + * channel, without checking whether it is open. * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Identifier channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, bytes); } /** @@ -276,8 +310,8 @@ public static void doSend(Identifier channel, CustomPayload payload) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Identifier channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(channel, payload); } /** @@ -286,33 +320,27 @@ public static void doSend(Identifier channel, IOConsumer writer) * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Identifier channel, PacketByteBuf data) { - ServerPlayNetworkingImpl.doSend(channel, data); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, writer); } - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, buffer); } - public interface ByteBufListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; - + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(channel, bytes); } } diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java index 7ef2cb48..a9bbf2ba 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -4,52 +4,59 @@ import java.util.LinkedHashSet; import java.util.Set; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; -public class HandshakePayload implements CustomPayload { +public class HandshakePayload implements PacketPayload { - public static final Identifier CHANNEL = new Identifier("osl", "handshake"); + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); - public Set channels; + public byte protocol; + public Set channels; public HandshakePayload() { } - public HandshakePayload(Set channels) { - this.channels = channels; + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); } public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); + return new HandshakePayload(ClientPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); } public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); + return new HandshakePayload(ServerPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); } @Override - public void read(PacketByteBuf buffer) throws IOException { + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); channels = new LinkedHashSet<>(); + int channelCount = buffer.readInt(); - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(buffer.readIdentifier()); - } + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); } } @Override - public void write(PacketByteBuf buffer) throws IOException { + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); buffer.writeInt(channels.size()); - for (Identifier channel : channels) { - buffer.writeIdentifier(channel); + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); } } } diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java deleted file mode 100644 index 7e34822c..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/NetworkListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -public class NetworkListener { - - private final T listener; - private final boolean async; - - public NetworkListener(T listener, boolean async) { - this.listener = listener; - this.async = async; - } - - public T get() { - return listener; - } - - public boolean isAsync() { - return async; - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 601d2f5d..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - // send channel registration data as a response to receiving client channel registration data - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - // empty impl - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..f2fe74a6 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,13 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; + +@FunctionalInterface +public interface PacketFactory { + + Packet create(NamespacedIdentifier channel, PacketBuffer data); + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..3241aa75 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,12 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.PacketBuffer; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + PacketBuffer osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..d3e3c4e0 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,15 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java new file mode 100644 index 00000000..6fc92ae4 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/PlayerManagerAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.List; + +import net.minecraft.server.entity.living.player.ServerPlayerEntity; + +public interface PlayerManagerAccess { + + List osl$networking$getAll(); + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java index 5faf509d..e9ba1661 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java @@ -10,174 +10,233 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.client.ClientPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; public final class ClientPlayNetworkingImpl { private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); + private static PacketFactory packetFactory; private static Minecraft minecraft; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ClientPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up client custom payload packet factory when it was already set up!"); + } + + ClientPlayNetworkingImpl.packetFactory = factory; + } public static void setUp(Minecraft minecraft) { if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); + throw new IllegalStateException("tried to set up client play networking when it was already set up!"); + } + if (ClientPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up client play networking when no custom payload packet factory was set up!"); } ClientPlayNetworkingImpl.minecraft = minecraft; + ClientPlayNetworkingImpl.thread = Thread.currentThread(); } public static void destroy(Minecraft minecraft) { if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); + throw new IllegalStateException("tried to destroy client play networking when it was not set up!"); } ClientPlayNetworkingImpl.minecraft = null; + ClientPlayNetworkingImpl.thread = null; } - public static final Map> LISTENERS = new LinkedHashMap<>(); + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (minecraft, handler, data) -> { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + registerListenerInternal(channel, (context, buffer) -> { T payload = initializer.get(); - payload.read(data); + payload.read(buffer); - return listener.handle(minecraft, handler, payload); - }, async); + listener.handle(context, payload); + }); } - public static void registerListener(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, false); + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + registerListenerInternal(channel, listener::handle); } - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, true); + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + registerListenerInternal(channel, (context, buffer) -> listener.handle(context, buffer.readByteArray())); } - private static void registerListener(Identifier channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isClientbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not client-bound - did you register it with the wrong settings?"); + } - private static void registerListenerImpl(Identifier channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { + CHANNEL_LISTENERS.compute(channel, (key, value) -> { if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); } - return new NetworkListener<>(listener, async); + return listener; }); } - public static void unregisterListener(Identifier channel) { - LISTENERS.remove(channel); + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); } - public static boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, CustomPayloadS2CPacket packet) { - Identifier channel = packet.getChannel(); - NetworkListener listener = LISTENERS.get(channel); + public static boolean handlePacket(Minecraft minecraft, ClientPlayNetworkHandler handler, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, minecraft); - } + ChannelListener.Context ctx = new ChannelListener.Context(); + PacketBuffer data = p.osl$networking$getData(); try { - return listener.get().handle(minecraft, handler, packet.getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) minecraft).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); } + + return true; } return false; } + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, PacketBuffer data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); return handler != null && handler.osl$networking$isPlayReady(); } - public static boolean canSend(Identifier channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); + public static boolean isPlayReady(NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); + return handler != null && handler.osl$networking$isPlayReady(channel); } - public static void send(Identifier channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(channel)) { + sendInternal(channel, payload); } } - public static void send(Identifier channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(channel)) { + sendInternal(channel, writer); } } - public static void send(Identifier channel, PacketByteBuf data) { - if (canSend(channel)) { - doSend(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(channel)) { + sendInternal(channel, buffer); } } - public static void doSend(Identifier channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); + public static void send(NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(channel)) { + sendInternal(channel, bytes); + } + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(channel, writer); } - public static void doSend(Identifier channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(channel, buffer); } - public static void doSend(Identifier channel, PacketByteBuf data) { - sendPacket(makePacket(channel, data)); + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(channel, bytes); } - private static Packet makePacket(Identifier channel, CustomPayload payload) { - return makePacket(channel, payload::write); + private static void sendInternal(NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } } - private static Packet makePacket(Identifier channel, IOConsumer writer) { + private static void sendInternal(NamespacedIdentifier channel, IOConsumer writer) { try { - return new CustomPayloadC2SPacket(channel, PacketByteBufs.make(writer)); + sendPacket(channel, PacketBuffers.make(writer)); } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); } } - private static Packet makePacket(Identifier channel, PacketByteBuf data) { - return new CustomPayloadC2SPacket(channel, data); + private static void sendInternal(NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(channel, buffer); } - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); + private static void sendInternal(NamespacedIdentifier channel, byte[] bytes) { + sendPacket(channel, PacketBuffers.wrap(bytes)); + } + + private static void sendPacket(NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isServerbound()) { + minecraft.getNetworkHandler().sendPacket(packetFactory.create(channel, data)); } } - private interface Listener { + @FunctionalInterface + private interface ChannelListener { + + void handle(Context context, PacketBuffer buffer) throws IOException; - boolean handle(Minecraft minecraft, ClientPlayNetworkHandler handler, PacketByteBuf data) throws IOException; + class Context implements ClientPacketListener.Context { + + @Override + public Minecraft minecraft() { + return minecraft; + } + @Override + public ClientPlayNetworkHandler networkHandler() { + return minecraft.getNetworkHandler(); + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } } } diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java deleted file mode 100644 index e4a813f5..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/ICustomPayloadPacket.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.resource.Identifier; - -public interface ICustomPayloadPacket { - - Identifier osl$networking$getChannel(); - - PacketByteBuf osl$networking$getData(); - -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index e06f34db..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -import net.minecraft.resource.Identifier; - -public interface INetworkHandler { - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(Identifier channel); - -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java deleted file mode 100644 index 54cf473b..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,85 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.client; - -import java.util.LinkedHashSet; -import java.util.Set; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientPlayNetworkHandler; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.impl.HandshakePayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -@Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin implements INetworkHandler { - - @Shadow @Final private Minecraft minecraft; - - /** - * Channels that the server is listening to. - */ - @Unique private Set serverChannels; - - @Inject( - method = "handleLogin", - at = @At( - value = "TAIL" - ) - ) - private void osl$networking$handleLogin(CallbackInfo ci) { - // send channel registration data as soon as login occurs - ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); - - ClientConnectionEvents.LOGIN.invoker().accept(minecraft); - } - - @Inject( - method = "onDisconnect", - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleDisconnect(CallbackInfo ci) { - ClientConnectionEvents.DISCONNECT.invoker().accept(minecraft); - serverChannels = null; - } - - @Inject( - method = "handleCustomPayload", - cancellable = true, - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) { - ci.cancel(); - } - } - - @Override - public boolean osl$networking$isPlayReady() { - return serverChannels != null; - } - - @Override - public void osl$networking$registerChannels(Set channels) { - serverChannels = new LinkedHashSet<>(channels); - } - - @Override - public boolean osl$networking$isRegisteredChannel(Identifier channel) { - return serverChannels != null && serverChannels.contains(channel); - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java deleted file mode 100644 index 5719c0a7..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/CustomPayloadC2SPacketMixin.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.resource.Identifier; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; - -@Mixin(CustomPayloadC2SPacket.class) -public class CustomPayloadC2SPacketMixin implements ICustomPayloadPacket { - - @Shadow private Identifier channel; - @Shadow private PacketByteBuf data; - - @Override - public Identifier osl$networking$getChannel() { - return channel; - } - - @Override - public PacketByteBuf osl$networking$getData() { - return data; - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java deleted file mode 100644 index ffa9c8d3..00000000 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/ServerPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,73 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import java.util.LinkedHashSet; -import java.util.Set; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.resource.Identifier; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -@Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { - - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; - - /** - * Channels that the client is listening to. - */ - @Unique private Set clientChannels; - - @Inject( - method = "onDisconnect", - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleDisconnect(CallbackInfo ci) { - ServerConnectionEvents.DISCONNECT.invoker().accept(server, player); - clientChannels = null; - } - - @Inject( - method = "handleCustomPayload", - cancellable = true, - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleCustomPayload(CustomPayloadC2SPacket packet, CallbackInfo ci) { - if (ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet)) { - ci.cancel(); - } - } - - @Override - public boolean osl$networking$isPlayReady() { - return clientChannels != null; - } - - @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); - } - - @Override - public boolean osl$networking$isRegisteredChannel(Identifier channel) { - return clientChannels != null && clientChannels.contains(channel); - } -} diff --git a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java index d7ff0a5c..647c0c11 100644 --- a/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ b/libraries/networking/networking-mc18w31a-mc1.14.4/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java @@ -11,229 +11,332 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.PacketUtils; import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.resource.Identifier; import net.minecraft.server.MinecraftServer; import net.minecraft.server.entity.living.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; import net.minecraft.world.dimension.DimensionType; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.PacketByteBufs; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteBufListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.impl.NetworkListener; -import net.ornithemc.osl.networking.impl.interfaces.mixin.ICustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.server.ServerPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; public final class ServerPlayNetworkingImpl { private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); + private static PacketFactory packetFactory; private static MinecraftServer server; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ServerPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up server custom payload packet factory when it was already set up!"); + } + + ServerPlayNetworkingImpl.packetFactory = factory; + } public static void setUp(MinecraftServer server) { if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); + throw new IllegalStateException("tried to set up server play networking when it was already set up!"); + } + if (ServerPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up server play networking when no custom payload packet factory was set up!"); } ServerPlayNetworkingImpl.server = server; + ServerPlayNetworkingImpl.thread = Thread.currentThread(); } public static void destroy(MinecraftServer server) { if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); + throw new IllegalStateException("tried to destroy server play networking when it was not set up!"); } ServerPlayNetworkingImpl.server = null; + ServerPlayNetworkingImpl.thread = null; } - public static final Map> LISTENERS = new LinkedHashMap<>(); + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); - public static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, false); - } - - public static void registerListenerAsync(Identifier channel, Supplier initializer, PayloadListener listener) { - registerListener(channel, initializer, listener, true); - } - - private static void registerListener(Identifier channel, Supplier initializer, PayloadListener listener, boolean async) { - registerListenerImpl(channel, (server, handler, player, data) -> { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + registerListenerInternal(channel, (context, buffer) -> { T payload = initializer.get(); - payload.read(data); + payload.read(buffer); - return listener.handle(server, handler, player, payload); - }, async); + listener.handle(context, payload); + }); } - public static void registerListener(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, false); + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + registerListenerInternal(channel, listener::handle); } - public static void registerListenerAsync(Identifier channel, ByteBufListener listener) { - registerListener(channel, listener, true); + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + registerListenerInternal(channel, (context, buffer) -> listener.handle(context, buffer.readByteArray())); } - private static void registerListener(Identifier channel, ByteBufListener listener, boolean async) { - registerListenerImpl(channel, listener::handle, async); - } + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); - private static void registerListenerImpl(Identifier channel, Listener listener, boolean async) { - LISTENERS.compute(channel, (key, value) -> { + if (settings == null || !settings.isServerbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not server-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); } - return new NetworkListener<>(listener, async); + return listener; }); } - public static void unregisterListener(Identifier channel) { - LISTENERS.remove(channel); + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); } - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadC2SPacket packet) { - ICustomPayloadPacket p = (ICustomPayloadPacket)packet; + public static boolean handlePacket(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; - Identifier channel = p.osl$networking$getChannel(); - NetworkListener listener = LISTENERS.get(channel); + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); if (listener != null) { - if (!listener.isAsync()) { - PacketUtils.ensureOnSameThread(packet, handler, server); - } + ChannelListener.Context ctx = new ChannelListener.Context(player); + PacketBuffer data = p.osl$networking$getData(); try { - return listener.get().handle(server, handler, player, p.osl$networking$getData()); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); - return true; + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) server).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); } + + return true; } return false; } + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, PacketBuffer data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; return handler != null && handler.osl$networking$isPlayReady(); } - public static boolean canSend(ServerPlayerEntity player, Identifier channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(channel); } - public static void send(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, payload); } } - public static void send(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, writer); } } - public static void send(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - if (canSend(player, channel)) { - doSend(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, buffer); } } - public static void send(Iterable players, Identifier channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, bytes); + } + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void send(DimensionType dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, payload); + } + + public static void send(DimensionType dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, writer); + } + + public static void send(DimensionType dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, buffer); + } + + public static void send(DimensionType dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, bytes); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, writer); } - public static void send(Iterable players, Identifier channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, buffer); } - public static void send(Iterable players, Identifier channel, PacketByteBuf data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); + public static void send(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, bytes); } - public static void send(DimensionType dimension, Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(player, channel, payload); } - public static void send(DimensionType dimension, Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(player, channel, writer); } - public static void send(DimensionType dimension, Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(player, channel, buffer); } - public static void send(Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(player, channel, bytes); } - public static void send(Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(players, channel, payload); } - public static void send(Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(players, channel, writer); } - public static void doSend(ServerPlayerEntity player, Identifier channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(players, channel, buffer); } - public static void doSend(ServerPlayerEntity player, Identifier channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(players, channel, bytes); } - public static void doSend(ServerPlayerEntity player, Identifier channel, PacketByteBuf data) { - sendPacket(player, makePacket(channel, data)); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, payload); } - public static void doSend(Iterable players, Identifier channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, writer); } - public static void doSend(Iterable players, Identifier channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, buffer); } - public static void doSend(Iterable players, Identifier channel, PacketByteBuf data) { - sendPacket(players, makePacket(channel, data)); + public static void sendNoCheck(DimensionType dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, bytes); } - public static void doSend(DimensionType dimension, Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(allPlayers(), channel, payload); } - public static void doSend(DimensionType dimension, Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(allPlayers(), channel, writer); } - public static void doSend(DimensionType dimension, Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(allPlayers(), channel, buffer); } - public static void doSend(Identifier channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(allPlayers(), channel, bytes); } - public static void doSend(Identifier channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(player, channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(player, channel, PacketBuffers.make(writer)); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(player, channel, buffer); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(player, channel, PacketBuffers.wrap(bytes)); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(players, channel, PacketBuffers.make(payload::write)); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(players, channel, PacketBuffers.make(writer)); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(players, channel, buffer); } - public static void doSend(Identifier channel, PacketByteBuf data) { - doSend(collectPlayers(p -> true), channel, data); + private static void sendInternal(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(players, channel, PacketBuffers.wrap(bytes)); + } + + private static Iterable allPlayers() { + return server.getPlayerManager().getAll(); } private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.getPlayerManager().getAll(), filter); + return collectPlayers(allPlayers(), filter); } private static Iterable collectPlayers(Iterable src, Predicate filter) { @@ -248,40 +351,59 @@ private static Iterable collectPlayers(Iterable makePacket(Identifier channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } + private static void sendPacket(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); - private static Packet makePacket(Identifier channel, IOConsumer writer) { - try { - return new CustomPayloadS2CPacket(channel, PacketByteBufs.make(writer)); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; + if (settings != null && settings.isClientbound()) { + player.networkHandler.sendPacket(packetFactory.create(channel, data)); } } - private static Packet makePacket(Identifier channel, PacketByteBuf data) { - return new CustomPayloadS2CPacket(channel, data); - } + private static void sendPacket(Iterable players, NamespacedIdentifier channel, PacketBuffer data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } + if (settings != null && settings.isClientbound()) { + Packet packet = packetFactory.create(channel, data); - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { for (ServerPlayerEntity player : players) { - sendPacket(player, packet); + player.networkHandler.sendPacket(packet); } } } - private interface Listener { + private interface ChannelListener { + + void handle(Context context, PacketBuffer buffer) throws IOException; + + class Context implements ServerPacketListener.Context { - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, PacketByteBuf data) throws IOException; + private final ServerPlayerEntity player; + Context(ServerPlayerEntity player) { + this.player = player; + } + + @Override + public MinecraftServer server() { + return server; + } + + @Override + public ServerPlayNetworkHandler networkHandler() { + return player.networkHandler; + } + + @Override + public ServerPlayerEntity player() { + return player; + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } } } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/build.gradle b/libraries/networking/networking-mca1.0.16-mca1.2.6/build.gradle index f7548915..a13b6e9a 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/build.gradle +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/build.gradle @@ -1,4 +1,4 @@ setUpModule(project, 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mcb1.4-1507-mcb1.7.3' + 'lifecycle-events-mcinf-20100630-1340-mca1.2.6' ) diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..332c2370 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1217 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.util.ByteProcessor; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + private static final int VAR_VALUE_BITS = 7; + private static final int VAR_VALUE_MASK = 1 << VAR_VALUE_BITS - 1; + private static final int VAR_PARITY_VALUE = 1 << VAR_VALUE_BITS; + private static final int VAR_INT_MAX_BYTES = 5; + private static final int VAR_LONG_MAX_BYTES = 10; + + final ByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + int value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_INT_MAX_BYTES) { + throw new RuntimeException("VarInt too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public long readVarLong() { + long value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_LONG_MAX_BYTES) { + throw new RuntimeException("VarLong too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public byte[] readByteArray() { + return this.readByteArray(this.readableBytes()); + } + + public byte[] readByteArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("ByteArray with size " + length + " is bigger than allowed " + maxLength); + } + + byte[] values = new byte[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readByte(); + } + + return values; + } + + public int[] readIntArray() { + return this.readIntArray(this.readableBytes()); + } + + public int[] readIntArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("IntArray with size " + length + " is bigger than allowed " + maxLength); + } + + int[] values = new int[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarInt(); + } + + return values; + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength * 4) { + throw new RuntimeException("The received encoded string buffer length is longer than maximum allowed (" + length + " > " + maxLength * 4 + ")"); + } + if (length < 0) { + throw new RuntimeException("The received encoded string buffer length is less than zero! Weird string!"); + } + + String s = this.toString(this.readerIndex(), length, StandardCharsets.UTF_8); + this.readerIndex(this.readerIndex() + length); + + if (s.length() > maxLength) { + throw new RuntimeException("The received string length is longer than maximum allowed (" + length + " > " + maxLength + ")"); + } + + return s; + } + + public > T readEnum(Class type) { + return type.getEnumConstants()[this.readVarInt()]; + } + + public Date readDate() { + return new Date(this.readLong()); + } + + public UUID readUuid() { + return new UUID( + this.readLong(), + this.readLong() + ); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public NbtCompound readNbtCompound() { + int readerIndex = this.readerIndex(); + byte firstByte = this.readByte(); + + if (firstByte == 0) { + return null; + } else { + this.readerIndex(readerIndex); + + try { + return NbtIo.read(new ByteBufInputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public ByteBuf writeVarInt(int value) { + while ((value & -128) != 0) { + this.writeByte(value & VAR_VALUE_MASK | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte(value); + + return this; + } + + public ByteBuf writeVarLong(long value) { + while ((value & -128) != 0) { + this.writeByte((int) (value & VAR_VALUE_MASK) | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte((int) value); + + return this; + } + + public ByteBuf writeByteArray(byte[] values) { + this.writeVarInt(values.length); + + for (byte value : values) { + this.writeByte(value); + } + + return this; + } + + public ByteBuf writeIntArray(int[] values) { + this.writeVarInt(values.length); + + for (int value : values) { + this.writeVarInt(value); + } + + return this; + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + if (bytes.length > maxLength) { + throw new RuntimeException("String too big (was " + bytes.length + " bytes encoded, max " + maxLength + ")"); + } + + this.writeByteArray(bytes); + + return this; + } + + public ByteBuf writeEnum(Enum value) { + this.writeVarInt(value.ordinal()); + + return this; + } + + public ByteBuf writeDate(Date date) { + this.writeLong(date.getTime()); + + return this; + } + + public ByteBuf writeUuid(UUID uuid) { + this.writeLong(uuid.getMostSignificantBits()); + this.writeLong(uuid.getLeastSignificantBits()); + + return this; + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + if (nbt == null) { + this.writeByte(0); + } else { + try { + NbtIo.write(nbt, new ByteBufOutputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return this; + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..a9e6ccc8 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,35 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java index f0963ed6..d5adcfb8 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java @@ -19,6 +19,10 @@ public class ClientConnectionEvents { * and until then data cannot safely be sent to the server. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * @@ -38,6 +42,10 @@ public class ClientConnectionEvents { * This marks the moment data can safely be sent to the server. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * @@ -54,6 +62,10 @@ public class ClientConnectionEvents { * This event is fired when the client disconnects from the server. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java new file mode 100644 index 00000000..558efde4 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java @@ -0,0 +1,46 @@ +package net.ornithemc.osl.networking.api.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ClientPacketListener { + + /** + * Receive incoming data from the server. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ClientPacketListener { + } + + @FunctionalInterface + interface Buffer extends ClientPacketListener { + } + + @FunctionalInterface + interface Bytes extends ClientPacketListener { + } + + interface Context { + + /** + * @return the current Minecraft game instance. + */ + Minecraft minecraft(); + + /** + * @return the network handler that received the packet. + */ + ClientNetworkHandler networkHandler(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java index e1594802..3b700508 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java @@ -1,50 +1,43 @@ package net.ornithemc.osl.networking.api.client; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; import java.util.function.Supplier; -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; public final class ClientPlayNetworking { /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be deserialized into a {@code CustomPayload} object of the given type. */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. */ - public static void registerListener(String channel, StreamListener listener) { + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { ClientPlayNetworkingImpl.registerListener(channel, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be given as a raw {@code byte[]}. */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); + public static void registerLegacyListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); } /** * Remove the listener registered to the given channel. */ - public static void unregisterListener(String channel) { + public static void unregisterListener(NamespacedIdentifier channel) { ClientPlayNetworkingImpl.unregisterListener(channel); } @@ -60,15 +53,15 @@ public static boolean isPlayReady() { * This method will return {@code false} if the client is not connected to a * server, or if the server has no listeners for the given channel. */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); + public static boolean isPlayReady(NamespacedIdentifier channel) { + return ClientPlayNetworkingImpl.isPlayReady(channel); } /** * Send a packet to the server through the given channel. The payload will * only be written if the channel is open. */ - public static void send(String channel, CustomPayload payload) { + public static void send(NamespacedIdentifier channel, PacketPayload payload) { ClientPlayNetworkingImpl.send(channel, payload); } @@ -76,15 +69,22 @@ public static void send(String channel, CustomPayload payload) { * Send a packet to the server through the given channel. The writer will * only be called if the channel is open. */ - public static void send(String channel, IOConsumer writer) { + public static void send(NamespacedIdentifier channel, IOConsumer writer) { ClientPlayNetworkingImpl.send(channel, writer); } /** * Send a packet to the server through the given channel. */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.send(channel, bytes); } /** @@ -93,8 +93,8 @@ public static void send(String channel, byte[] data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.sendNoCheck(channel, payload); } /** @@ -103,8 +103,8 @@ public static void doSend(String channel, CustomPayload payload) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, writer); } /** @@ -113,46 +113,17 @@ public static void doSend(String channel, IOConsumer writer) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the server. */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, buffer); } - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.sendNoCheck(channel, bytes); } } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Connections.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Connections.java new file mode 100644 index 00000000..836dd8ed --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Connections.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; + +public final class Connections { + + public static boolean checkAsyncHandling(Packet packet, PacketHandler listener) { + boolean handleAsync = packet instanceof CustomPayloadPacketAccess + && listener instanceof NetworkHandlerAccess + && ((NetworkHandlerAccess) listener).osl$networking$canRunOffMainThread(); + + if (handleAsync) { + packet.handle(listener); + } + + return handleAsync; + } +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java deleted file mode 100644 index 76c2850d..00000000 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; - -import net.minecraft.network.PacketHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public class CustomPayloadPacket extends Packet { - - public String channel; - public int size; - public byte[] data; - - public CustomPayloadPacket() { - } - - public CustomPayloadPacket(String channel, byte[] data) { - this.channel = channel; - this.data = data; - if (data != null) { - this.size = data.length; - if (this.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - } - - // the IOException has been stripped from the read/write methods - // by the obfuscator, thus we catch it and re-throw it as a - // runtime exception - it will be caught in Connection#read anyhow - - @Override - public void read(DataInputStream input) { - try { - this.channel = input.readUTF(); - this.size = input.readShort(); - if (this.size > 0 && this.size < Short.MAX_VALUE) { - this.data = new byte[this.size]; - input.readFully(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void write(DataOutputStream output) { - try { - output.writeUTF(this.channel); - output.writeShort(this.size); - if (this.data != null) { - output.write(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void handle(PacketHandler handler) { - if (handler instanceof INetworkHandler) { - ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); - } - } - - @Override - public int getSize() { - return 2 + this.channel.length() * 2 + 2 + this.size; - } -} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java index 3288d684..d09f7dbd 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -1,53 +1,57 @@ package net.ornithemc.osl.networking.impl; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.util.LinkedHashSet; import java.util.Set; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -public class HandshakePayload implements CustomPayload { +public class HandshakePayload implements PacketPayload { - public static final String CHANNEL = "OSL|Handshake"; + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); - public Set channels; + public byte protocol; + public Set channels; public HandshakePayload() { } - public HandshakePayload(Set channels) { - this.channels = channels; + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); } public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - throw new UnsupportedOperationException(); + return new HandshakePayload(ClientPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); } @Override - public void read(DataInputStream input) throws IOException { + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(input.readUTF()); - } + int channelCount = buffer.readInt(); + + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); } } @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); + buffer.writeInt(channels.size()); - for (String channel : channels) { - output.writeUTF(channel); + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); } } } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 7dfc242e..00000000 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor; - -public class Networking implements ModInitializer, ClientModInitializer { - - @Override - public void init() { - PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, CustomPayloadPacket.class); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - // send channel registration data as a response to receiving server channel registration data - ClientPlayNetworking.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..2a4f77aa --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface PacketFactory { + + Packet create(NamespacedIdentifier channel, byte[] data); + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..9c17038b --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + byte[] osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/LocalClientPlayerAccess.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/LocalClientPlayerAccess.java new file mode 100644 index 00000000..1364c7c5 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/LocalClientPlayerAccess.java @@ -0,0 +1,9 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.minecraft.client.network.handler.ClientNetworkHandler; + +public interface LocalClientPlayerAccess { + + ClientNetworkHandler osl$networking$getNetworkHandler(); + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..81b86b1f --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,17 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$canRunOffMainThread(); + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java index 29bc2aa0..7972bac3 100644 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java @@ -1,6 +1,5 @@ package net.ornithemc.osl.networking.impl.client; -import java.io.DataOutputStream; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; @@ -10,166 +9,240 @@ import org.apache.logging.log4j.Logger; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.mob.player.LocalClientPlayerEntity; import net.minecraft.client.network.handler.ClientNetworkHandler; import net.minecraft.network.packet.Packet; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.client.LocalClientPlayerEntityAccessor; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.client.ClientPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.LocalClientPlayerAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; public final class ClientPlayNetworkingImpl { private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); + private static PacketFactory packetFactory; private static Minecraft minecraft; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ClientPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up client custom payload packet factory when it was already set up!"); + } + + ClientPlayNetworkingImpl.packetFactory = factory; + } public static void setUp(Minecraft minecraft) { if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); + throw new IllegalStateException("tried to set up client play networking when it was already set up!"); + } + if (ClientPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up client play networking when no custom payload packet factory was set up!"); } ClientPlayNetworkingImpl.minecraft = minecraft; + ClientPlayNetworkingImpl.thread = Thread.currentThread(); } public static void destroy(Minecraft minecraft) { if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); + throw new IllegalStateException("tried to destroy client play networking when it was not set up!"); } ClientPlayNetworkingImpl.minecraft = null; + ClientPlayNetworkingImpl.thread = null; } - public static final Map LISTENERS = new LinkedHashMap<>(); + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + registerListenerInternal(channel, (context, bytes) -> { T payload = initializer.get(); - payload.read(DataStreams.input(data)); + payload.read(PacketBuffers.wrap(bytes)); - return listener.handle(minecraft, handler, payload); + listener.handle(context, payload); }); } - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + registerListenerInternal(channel, (context, bytes) -> listener.handle(context, PacketBuffers.wrap(bytes))); } - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + registerListenerInternal(channel, listener::handle); } - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isClientbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not client-bound - did you register it with the wrong settings?"); + } + CHANNEL_LISTENERS.compute(channel, (key, value) -> { if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); } return listener; }); } - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); } - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); + public static boolean handlePacket(Minecraft minecraft, ClientNetworkHandler handler, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(); + byte[] data = p.osl$networking$getData(); + try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) minecraft).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); } + + return true; } return false; } - private static ClientNetworkHandler getNetworkHandler() { - LocalClientPlayerEntity player = ((LocalClientPlayerEntity)minecraft.player); - return player != null ? ((LocalClientPlayerEntityAccessor)player).getNetworkHandler() : null; + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, byte[] data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + private static ClientNetworkHandler networkHandler() { + LocalClientPlayerAccess player = (LocalClientPlayerAccess) minecraft.player; + return player != null ? player.osl$networking$getNetworkHandler() : null; } public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)ClientPlayNetworkingImpl.getNetworkHandler(); + NetworkHandlerAccess handler = (NetworkHandlerAccess) networkHandler(); return handler != null && handler.osl$networking$isPlayReady(); } - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)ClientPlayNetworkingImpl.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); + public static boolean isPlayReady(NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess) networkHandler(); + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(channel)) { + sendInternal(channel, payload); + } } - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(channel)) { + sendInternal(channel, writer); } } - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(channel)) { + sendInternal(channel, buffer); } } - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); + public static void send(NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(channel)) { + sendInternal(channel, bytes); } } - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(channel, payload); } - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(channel, writer); } - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(channel, buffer); } - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(channel, bytes); } - private static Packet makePacket(String channel, IOConsumer writer) { + private static void sendInternal(NamespacedIdentifier channel, PacketPayload payload) { try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); + sendPacket(channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); } } - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); + private static void sendInternal(NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(NamespacedIdentifier channel, byte[] bytes) { + sendPacket(channel, bytes); } - private static void sendPacket(Packet packet) { - if (packet != null) { - ClientPlayNetworkingImpl.getNetworkHandler().sendPacket(packet); + private static void sendPacket(NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isServerbound()) { + networkHandler().sendPacket(packetFactory.create(channel, data)); } } - private interface Listener { + @FunctionalInterface + private interface ChannelListener { + + void handle(Context context, byte[] bytes) throws IOException; + + class Context implements ClientPacketListener.Context { + + @Override + public Minecraft minecraft() { + return minecraft; + } - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; + @Override + public ClientNetworkHandler networkHandler() { + return ClientPlayNetworkingImpl.networkHandler(); + } + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } } } diff --git a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityAccessor.java b/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityAccessor.java deleted file mode 100644 index d7c7b828..00000000 --- a/libraries/networking/networking-mca1.0.16-mca1.2.6/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/LocalClientPlayerEntityAccessor.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.entity.mob.player.LocalClientPlayerEntity; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -@Mixin(LocalClientPlayerEntity.class) -public interface LocalClientPlayerEntityAccessor { - - @Accessor("networkHandler") - default ClientNetworkHandler getNetworkHandler() { - throw new UnsupportedOperationException(); - } -} diff --git a/libraries/networking/networking-mc12w21a-mc13w39b/build.gradle b/libraries/networking/networking-mcb1.0-mc13w39b/build.gradle similarity index 100% rename from libraries/networking/networking-mc12w21a-mc13w39b/build.gradle rename to libraries/networking/networking-mcb1.0-mc13w39b/build.gradle diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/gradle.properties b/libraries/networking/networking-mcb1.0-mc13w39b/gradle.properties new file mode 100644 index 00000000..41bc8da8 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/gradle.properties @@ -0,0 +1,8 @@ +environment = * +min_mc_version = b1.0 +max_mc_version = 13w39b +mc_version_range = >=1.0.0-beta.0 <=1.7-alpha.13.39.b + +minecraft_version = 1.5.2 +feather_build = 1 +nests_build = 5 diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..332c2370 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1217 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.util.ByteProcessor; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + private static final int VAR_VALUE_BITS = 7; + private static final int VAR_VALUE_MASK = 1 << VAR_VALUE_BITS - 1; + private static final int VAR_PARITY_VALUE = 1 << VAR_VALUE_BITS; + private static final int VAR_INT_MAX_BYTES = 5; + private static final int VAR_LONG_MAX_BYTES = 10; + + final ByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + int value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_INT_MAX_BYTES) { + throw new RuntimeException("VarInt too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public long readVarLong() { + long value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_LONG_MAX_BYTES) { + throw new RuntimeException("VarLong too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public byte[] readByteArray() { + return this.readByteArray(this.readableBytes()); + } + + public byte[] readByteArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("ByteArray with size " + length + " is bigger than allowed " + maxLength); + } + + byte[] values = new byte[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readByte(); + } + + return values; + } + + public int[] readIntArray() { + return this.readIntArray(this.readableBytes()); + } + + public int[] readIntArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("IntArray with size " + length + " is bigger than allowed " + maxLength); + } + + int[] values = new int[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarInt(); + } + + return values; + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength * 4) { + throw new RuntimeException("The received encoded string buffer length is longer than maximum allowed (" + length + " > " + maxLength * 4 + ")"); + } + if (length < 0) { + throw new RuntimeException("The received encoded string buffer length is less than zero! Weird string!"); + } + + String s = this.toString(this.readerIndex(), length, StandardCharsets.UTF_8); + this.readerIndex(this.readerIndex() + length); + + if (s.length() > maxLength) { + throw new RuntimeException("The received string length is longer than maximum allowed (" + length + " > " + maxLength + ")"); + } + + return s; + } + + public > T readEnum(Class type) { + return type.getEnumConstants()[this.readVarInt()]; + } + + public Date readDate() { + return new Date(this.readLong()); + } + + public UUID readUuid() { + return new UUID( + this.readLong(), + this.readLong() + ); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public NbtCompound readNbtCompound() { + int readerIndex = this.readerIndex(); + byte firstByte = this.readByte(); + + if (firstByte == 0) { + return null; + } else { + this.readerIndex(readerIndex); + + try { + return NbtIo.read(new ByteBufInputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public ByteBuf writeVarInt(int value) { + while ((value & -128) != 0) { + this.writeByte(value & VAR_VALUE_MASK | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte(value); + + return this; + } + + public ByteBuf writeVarLong(long value) { + while ((value & -128) != 0) { + this.writeByte((int) (value & VAR_VALUE_MASK) | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte((int) value); + + return this; + } + + public ByteBuf writeByteArray(byte[] values) { + this.writeVarInt(values.length); + + for (byte value : values) { + this.writeByte(value); + } + + return this; + } + + public ByteBuf writeIntArray(int[] values) { + this.writeVarInt(values.length); + + for (int value : values) { + this.writeVarInt(value); + } + + return this; + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + if (bytes.length > maxLength) { + throw new RuntimeException("String too big (was " + bytes.length + " bytes encoded, max " + maxLength + ")"); + } + + this.writeByteArray(bytes); + + return this; + } + + public ByteBuf writeEnum(Enum value) { + this.writeVarInt(value.ordinal()); + + return this; + } + + public ByteBuf writeDate(Date date) { + this.writeLong(date.getTime()); + + return this; + } + + public ByteBuf writeUuid(UUID uuid) { + this.writeLong(uuid.getMostSignificantBits()); + this.writeLong(uuid.getLeastSignificantBits()); + + return this; + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + if (nbt == null) { + this.writeByte(0); + } else { + try { + NbtIo.write(nbt, new ByteBufOutputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return this; + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..a9e6ccc8 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,35 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java similarity index 100% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java rename to libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java new file mode 100644 index 00000000..558efde4 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPacketListener.java @@ -0,0 +1,46 @@ +package net.ornithemc.osl.networking.api.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ClientPacketListener { + + /** + * Receive incoming data from the server. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ClientPacketListener { + } + + @FunctionalInterface + interface Buffer extends ClientPacketListener { + } + + @FunctionalInterface + interface Bytes extends ClientPacketListener { + } + + interface Context { + + /** + * @return the current Minecraft game instance. + */ + Minecraft minecraft(); + + /** + * @return the network handler that received the packet. + */ + ClientNetworkHandler networkHandler(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java new file mode 100644 index 00000000..3b700508 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java @@ -0,0 +1,129 @@ +package net.ornithemc.osl.networking.api.client; + +import java.util.function.Supplier; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; + +public final class ClientPlayNetworking { + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be deserialized into a {@code CustomPayload} object of the given type. + */ + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. + */ + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be given as a raw {@code byte[]}. + */ + public static void registerLegacyListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + ClientPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Remove the listener registered to the given channel. + */ + public static void unregisterListener(NamespacedIdentifier channel) { + ClientPlayNetworkingImpl.unregisterListener(channel); + } + + /** + * Check whether the connection is ready for data to be sent to the server. + */ + public static boolean isPlayReady() { + return ClientPlayNetworkingImpl.isPlayReady(); + } + + /** + * Check whether the given channel is open for data to be sent through it. + * This method will return {@code false} if the client is not connected to a + * server, or if the server has no listeners for the given channel. + */ + public static boolean isPlayReady(NamespacedIdentifier channel) { + return ClientPlayNetworkingImpl.isPlayReady(channel); + } + + /** + * Send a packet to the server through the given channel. The payload will + * only be written if the channel is open. + */ + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.send(channel, payload); + } + + /** + * Send a packet to the server through the given channel. The writer will + * only be called if the channel is open. + */ + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.send(channel, writer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to the server through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.send(channel, bytes); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ClientPlayNetworkingImpl.sendNoCheck(channel, payload); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, writer); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ClientPlayNetworkingImpl.sendNoCheck(channel, buffer); + } + + /** + * Send a packet to the server through the given channel, without checking + * whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the server. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ClientPlayNetworkingImpl.sendNoCheck(channel, bytes); + } +} diff --git a/libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java similarity index 100% rename from libraries/networking/networking-mc12w18a-mc12w19a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java rename to libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java new file mode 100644 index 00000000..89fb46e9 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.api.server; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ServerPacketListener { + + /** + * Receive incoming data from the client. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ServerPacketListener { + } + + @FunctionalInterface + interface Buffer extends ServerPacketListener { + } + + @FunctionalInterface + interface Bytes extends ServerPacketListener { + } + + interface Context { + + /** + * @return the current MinecraftServer game instance. + */ + MinecraftServer server(); + + /** + * @return the network handler that received the packet. + */ + ServerPlayNetworkHandler networkHandler(); + + /** + * @return the player that received the packet. + */ + ServerPlayerEntity player(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java new file mode 100644 index 00000000..4f45df86 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java @@ -0,0 +1,345 @@ +package net.ornithemc.osl.networking.api.server; + +import java.util.function.Supplier; + +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public final class ServerPlayNetworking { + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be deserialized into a {@code CustomPayload} object of the given type. + */ + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. + */ + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be given as a raw {@code byte[]}. + */ + public static void registerLegacyListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Remove the listener registered to the given channel. + */ + public static void unregisterListener(NamespacedIdentifier channel) { + ServerPlayNetworkingImpl.unregisterListener(channel); + } + + /** + * Check whether the connection is ready for data to be sent to the client. + */ + public static boolean isPlayReady(ServerPlayerEntity player) { + return ServerPlayNetworkingImpl.isPlayReady(player); + } + + /** + * Check whether the given channel is ready for data to be sent through it. + * This method will return {@code false} if the client has no listeners for + * the given channel. + */ + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + return ServerPlayNetworkingImpl.isPlayReady(player, channel); + } + + /** + * Send a packet to the given player through the given channel. The payload + * will only be written if the channel is open. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(player, channel, payload); + } + + /** + * Send a packet to the given player through the given channel. The writer + * will only be called if the channel is open. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(player, channel, writer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel. The payload + * will only be written if the channel is open for at least one player. + */ + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(players, channel, payload); + } + + /** + * Send a packet to the given players through the given channel. The writer + * will only be called if the channel is open for at least one player. + */ + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(players, channel, writer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(players, channel, bytes); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel. The payload will only be written if the channel is open for at + * least one player. + */ + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(dimension, channel, payload); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel. The writer will only be called if the channel is open for at + * least one player. + */ + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(dimension, channel, writer); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel. + */ + public static void send(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(dimension, channel, buffer); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel. + */ + public static void send(int dimension, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(dimension, channel, bytes); + } + + /** + * Send a packet to all players through the given channel. The payload will + * only be written if the channel is open for at least one player. + */ + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(channel, payload); + } + + /** + * Send a packet to all players through the given channel. The writer will + * only be called if the channel is open for at least one player. + */ + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(channel, writer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(channel, bytes); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, payload); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, writer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, payload); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, writer); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, bytes); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel, without checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, payload); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel, without checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, writer); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel, without checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, buffer); + } + + /** + * Send a packet to the players in the given dimension through the given + * channel, without checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, bytes); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(channel, payload); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, writer); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, buffer); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(channel, bytes); + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Connections.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Connections.java new file mode 100644 index 00000000..836dd8ed --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/Connections.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; + +public final class Connections { + + public static boolean checkAsyncHandling(Packet packet, PacketHandler listener) { + boolean handleAsync = packet instanceof CustomPayloadPacketAccess + && listener instanceof NetworkHandlerAccess + && ((NetworkHandlerAccess) listener).osl$networking$canRunOffMainThread(); + + if (handleAsync) { + packet.handle(listener); + } + + return handleAsync; + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java new file mode 100644 index 00000000..a9bbf2ba --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -0,0 +1,62 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.IOException; +import java.util.LinkedHashSet; +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class HandshakePayload implements PacketPayload { + + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); + + public byte protocol; + public Set channels; + + public HandshakePayload() { + } + + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); + } + + public static HandshakePayload client() { + return new HandshakePayload(ClientPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); + } + + public static HandshakePayload server() { + return new HandshakePayload(ServerPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); + } + + @Override + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); + channels = new LinkedHashSet<>(); + + int channelCount = buffer.readInt(); + + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); + } + } + + @Override + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); + buffer.writeInt(channels.size()); + + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); + } + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..2a4f77aa --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface PacketFactory { + + Packet create(NamespacedIdentifier channel, byte[] data); + +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..9c17038b --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + byte[] osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..81b86b1f --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,17 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$canRunOffMainThread(); + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java new file mode 100644 index 00000000..1421579f --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java @@ -0,0 +1,242 @@ +package net.ornithemc.osl.networking.impl.client; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Supplier; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.handler.ClientNetworkHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.client.ClientPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +public final class ClientPlayNetworkingImpl { + + private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); + + private static PacketFactory packetFactory; + private static Minecraft minecraft; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ClientPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up client custom payload packet factory when it was already set up!"); + } + + ClientPlayNetworkingImpl.packetFactory = factory; + } + + public static void setUp(Minecraft minecraft) { + if (ClientPlayNetworkingImpl.minecraft == minecraft) { + throw new IllegalStateException("tried to set up client play networking when it was already set up!"); + } + if (ClientPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up client play networking when no custom payload packet factory was set up!"); + } + + ClientPlayNetworkingImpl.minecraft = minecraft; + ClientPlayNetworkingImpl.thread = Thread.currentThread(); + } + + public static void destroy(Minecraft minecraft) { + if (ClientPlayNetworkingImpl.minecraft != minecraft) { + throw new IllegalStateException("tried to destroy client play networking when it was not set up!"); + } + + ClientPlayNetworkingImpl.minecraft = null; + ClientPlayNetworkingImpl.thread = null; + } + + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); + + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ClientPacketListener.Payload listener) { + registerListenerInternal(channel, (context, bytes) -> { + T payload = initializer.get(); + payload.read(PacketBuffers.wrap(bytes)); + + listener.handle(context, payload); + }); + } + + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Buffer listener) { + registerListenerInternal(channel, (context, bytes) -> listener.handle(context, PacketBuffers.wrap(bytes))); + } + + public static void registerListener(NamespacedIdentifier channel, ClientPacketListener.Bytes listener) { + registerListenerInternal(channel, listener::handle); + } + + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isClientbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not client-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { + if (value != null) { + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); + } + + return listener; + }); + } + + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); + } + + public static boolean handlePacket(Minecraft minecraft, ClientNetworkHandler handler, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); + + if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(); + byte[] data = p.osl$networking$getData(); + + try { + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) minecraft).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); + } + + return true; + } + + return false; + } + + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, byte[] data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + public static boolean isPlayReady() { + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); + return handler != null && handler.osl$networking$isPlayReady(); + } + + public static boolean isPlayReady(NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)minecraft.getNetworkHandler(); + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(channel)) { + sendInternal(channel, payload); + } + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(channel)) { + sendInternal(channel, writer); + } + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(channel)) { + sendInternal(channel, buffer); + } + } + + public static void send(NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(channel)) { + sendInternal(channel, bytes); + } + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(channel, writer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(channel, buffer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(channel, bytes); + } + + private static void sendInternal(NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(NamespacedIdentifier channel, byte[] bytes) { + sendPacket(channel, bytes); + } + + private static void sendPacket(NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isServerbound()) { + minecraft.getNetworkHandler().sendPacket(packetFactory.create(channel, data)); + } + } + + @FunctionalInterface + private interface ChannelListener { + + void handle(Context context, byte[] bytes) throws IOException; + + class Context implements ClientPacketListener.Context { + + @Override + public Minecraft minecraft() { + return minecraft; + } + + @Override + public ClientNetworkHandler networkHandler() { + return minecraft.getNetworkHandler(); + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } + } +} diff --git a/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java new file mode 100644 index 00000000..40674912 --- /dev/null +++ b/libraries/networking/networking-mcb1.0-mc13w39b/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java @@ -0,0 +1,408 @@ +package net.ornithemc.osl.networking.impl.server; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.function.Supplier; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.network.packet.Packet; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.server.ServerPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +public final class ServerPlayNetworkingImpl { + + private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); + + private static PacketFactory packetFactory; + private static MinecraftServer server; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ServerPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up server custom payload packet factory when it was already set up!"); + } + + ServerPlayNetworkingImpl.packetFactory = factory; + } + + public static void setUp(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server == server) { + throw new IllegalStateException("tried to set up server play networking when it was already set up!"); + } + if (ServerPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up server play networking when no custom payload packet factory was set up!"); + } + + ServerPlayNetworkingImpl.server = server; + ServerPlayNetworkingImpl.thread = Thread.currentThread(); + } + + public static void destroy(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server != server) { + throw new IllegalStateException("tried to destroy server play networking when it was not set up!"); + } + + ServerPlayNetworkingImpl.server = null; + ServerPlayNetworkingImpl.thread = null; + } + + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); + + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + registerListenerInternal(channel, (context, bytes) -> { + T payload = initializer.get(); + payload.read(PacketBuffers.wrap(bytes)); + + listener.handle(context, payload); + }); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + registerListenerInternal(channel, (context, bytes) -> listener.handle(context, PacketBuffers.wrap(bytes))); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + registerListenerInternal(channel, listener::handle); + } + + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isServerbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not server-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { + if (value != null) { + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); + } + + return listener; + }); + } + + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); + } + + public static boolean handlePacket(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); + + if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(player); + byte[] data = p.osl$networking$getData(); + + try { + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) server).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); + } + + return true; + } + + return false; + } + + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, byte[] data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + public static boolean isPlayReady(ServerPlayerEntity player) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(); + } + + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, payload); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, writer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, buffer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, bytes); + } + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, payload); + } + + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, writer); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, buffer); + } + + public static void send(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, bytes); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(player, channel, payload); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(player, channel, writer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(player, channel, buffer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(player, channel, bytes); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(players, channel, payload); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(players, channel, writer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(players, channel, buffer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(players, channel, bytes); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, payload); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, writer); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, buffer); + } + + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, bytes); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(allPlayers(), channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(allPlayers(), channel, writer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(allPlayers(), channel, buffer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(allPlayers(), channel, bytes); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(player, channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(player, channel, bytes); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(players, channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(players, channel, bytes); + } + + private static Iterable allPlayers() { + return server.getPlayerManager().players; + } + + private static Iterable collectPlayers(Predicate filter) { + return collectPlayers(allPlayers(), filter); + } + + private static Iterable collectPlayers(Iterable src, Predicate filter) { + List players = new ArrayList<>(); + + for (ServerPlayerEntity player : src) { + if (filter.test(player)) { + players.add(player); + } + } + + return players; + } + + private static void sendPacket(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + player.networkHandler.sendPacket(packetFactory.create(channel, data)); + } + } + + private static void sendPacket(Iterable players, NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + Packet packet = packetFactory.create(channel, data); + + for (ServerPlayerEntity player : players) { + player.networkHandler.sendPacket(packet); + } + } + } + + private interface ChannelListener { + + void handle(Context context, byte[] bytes) throws IOException; + + class Context implements ServerPacketListener.Context { + + private final ServerPlayerEntity player; + + Context(ServerPlayerEntity player) { + this.player = player; + } + + @Override + public MinecraftServer server() { + return server; + } + + @Override + public ServerPlayNetworkHandler networkHandler() { + return player.networkHandler; + } + + @Override + public ServerPlayerEntity player() { + return player; + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } + } +} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/build.gradle b/libraries/networking/networking-mcb1.0-mcb1.4_01/build.gradle deleted file mode 100644 index f7548915..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mcb1.4-1507-mcb1.7.3' -) diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index f0963ed6..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,70 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index dbe85275..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 7eda8cca..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java deleted file mode 100644 index 76c2850d..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; - -import net.minecraft.network.PacketHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public class CustomPayloadPacket extends Packet { - - public String channel; - public int size; - public byte[] data; - - public CustomPayloadPacket() { - } - - public CustomPayloadPacket(String channel, byte[] data) { - this.channel = channel; - this.data = data; - if (data != null) { - this.size = data.length; - if (this.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - } - - // the IOException has been stripped from the read/write methods - // by the obfuscator, thus we catch it and re-throw it as a - // runtime exception - it will be caught in Connection#read anyhow - - @Override - public void read(DataInputStream input) { - try { - this.channel = input.readUTF(); - this.size = input.readShort(); - if (this.size > 0 && this.size < Short.MAX_VALUE) { - this.data = new byte[this.size]; - input.readFully(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void write(DataOutputStream output) { - try { - output.writeUTF(this.channel); - output.writeShort(this.size); - if (this.data != null) { - output.write(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void handle(PacketHandler handler) { - if (handler instanceof INetworkHandler) { - ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); - } - } - - @Override - public int getSize() { - return 2 + this.channel.length() * 2 + 2 + this.size; - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index bfe9ec89..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,54 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(input.readUTF()); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - output.writeUTF(channel); - } - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 3213aa87..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,58 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, CustomPayloadPacket.class); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - // send channel registration data as a response to receiving server channel registration data - ClientPlayNetworking.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index c4eb3d66..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,168 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java deleted file mode 100644 index d57902df..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerLoginNetworkHandlerMixin.java +++ /dev/null @@ -1,61 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.server; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -import net.minecraft.network.packet.HandshakePacket; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerLoginNetworkHandler; - -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.Constants; -import net.ornithemc.osl.networking.impl.HandshakePayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -@Mixin(ServerLoginNetworkHandler.class) -public class ServerLoginNetworkHandlerMixin { - - @Shadow @Final private MinecraftServer server; - - /** - * is the client also running OSL? - */ - @Unique private boolean ornithe; - - @Inject( - method = "handleHandshake", - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleHandshake(HandshakePacket packet, CallbackInfo ci) { - if (Constants.OSL_HANDSHAKE_KEY.equals(packet.key)) { - ornithe = true; - } - } - - @Inject( - method = "acceptLogin", - locals = LocalCapture.CAPTURE_FAILHARD, - at = @At( - value = "TAIL" - ) - ) - private void osl$networking$handleLogin(CallbackInfo ci, ServerPlayerEntity player) { - if (player != null) { - if (ornithe) { - // send channel registration data as soon as login occurs - ServerPlayNetworkingImpl.doSend(player, HandshakePayload.CHANNEL, HandshakePayload.server()); - } - - ServerConnectionEvents.LOGIN.invoker().accept(server, player); - } - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java deleted file mode 100644 index 11335ca1..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/mixin/server/ServerPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,64 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.server; - -import java.util.LinkedHashSet; -import java.util.Set; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -@Mixin(ServerPlayNetworkHandler.class) -public class ServerPlayNetworkHandlerMixin implements INetworkHandler { - - @Shadow @Final private MinecraftServer server; - @Shadow @Final private ServerPlayerEntity player; - - /** - * Channels that the client is listening to. - */ - @Unique private Set clientChannels; - - @Inject( - method = "onDisconnect", - at = @At( - value = "HEAD" - ) - ) - private void osl$networking$handleDisconnect(CallbackInfo ci) { - ServerConnectionEvents.DISCONNECT.invoker().accept(server, player); - clientChannels = null; - } - - @Override - public boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet) { - return ServerPlayNetworkingImpl.handle(server, (ServerPlayNetworkHandler)(Object)this, player, packet); - } - - @Override - public boolean osl$networking$isPlayReady() { - return clientChannels != null; - } - - @Override - public void osl$networking$registerChannels(Set channels) { - clientChannels = new LinkedHashSet<>(channels); - } - - @Override - public boolean osl$networking$isRegisteredChannel(String channel) { - return clientChannels != null && clientChannels.contains(channel); - } -} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 711978a2..00000000 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,269 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/build.gradle b/libraries/networking/networking-mcb1.5-mc11w48a/build.gradle deleted file mode 100644 index 4f17952b..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -setUpModule(project, - 'entrypoints-mcin-20091223-1459-mc1.5.2', - 'lifecycle-events-mcb1.8-pre1-201109081459-mc11w50a' -) diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java deleted file mode 100644 index f0963ed6..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientConnectionEvents.java +++ /dev/null @@ -1,70 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.util.function.Consumer; - -import net.minecraft.client.Minecraft; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the client side of a client-server connection. - */ -public class ClientConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.LOGIN.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.consumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.PLAY_READY.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.consumer(); - /** - * This event is fired when the client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ClientConnectionEvents.DISCONNECT.register(minecraft -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.consumer(); - -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java deleted file mode 100644 index e1594802..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/client/ClientPlayNetworking.java +++ /dev/null @@ -1,158 +0,0 @@ -package net.ornithemc.osl.networking.api.client; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; - -public final class ClientPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ClientPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ClientPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ClientPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the server. - */ - public static boolean isPlayReady() { - return ClientPlayNetworkingImpl.isPlayReady(); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client is not connected to a - * server, or if the server has no listeners for the given channel. - */ - public static boolean canSend(String channel) { - return ClientPlayNetworkingImpl.canSend(channel); - } - - /** - * Send a packet to the server through the given channel. The payload will - * only be written if the channel is open. - */ - public static void send(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to the server through the given channel. The writer will - * only be called if the channel is open. - */ - public static void send(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to the server through the given channel. - */ - public static void send(String channel, byte[] data) { - ClientPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, CustomPayload payload) { - ClientPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, IOConsumer writer) { - ClientPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to the server through the given channel, without checking - * whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the server. - */ - public static void doSend(String channel, byte[] data) { - ClientPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the server. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index dbe85275..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index 7eda8cca..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,321 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The payload will only be written if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. The writer will only be called if the channel is open for at - * least one player. - */ - public static void send(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel. - */ - public static void send(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); - } - - /** - * Send a packet to the players in the given dimension through the given - * channel, without checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(int dimension, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(dimension, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 18cff165..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - return new HandshakePayload(ClientPlayNetworkingImpl.LISTENERS.keySet()); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(Packet.readString(input, Channels.MAX_LENGTH)); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - Packet.writeString(channel, output); - } - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index 682a1906..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,58 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.client.ClientConnectionEvents; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer { - - @Override - public void init() { - PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, true, true, CustomPayloadPacket.class); - } - - @Override - public void initClient() { - MinecraftClientEvents.START.register(minecraft -> { - ClientPlayNetworkingImpl.setUp(minecraft); - }); - MinecraftClientEvents.STOP.register(minecraft -> { - ClientPlayNetworkingImpl.destroy(minecraft); - }); - ClientPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> { - // send channel registration data as a response to receiving server channel registration data - ClientPlayNetworking.doSend(HandshakePayload.CHANNEL, HandshakePayload.client()); - - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft); - - return true; - }); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java deleted file mode 100644 index c4eb3d66..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/client/ClientPlayNetworkingImpl.java +++ /dev/null @@ -1,168 +0,0 @@ -package net.ornithemc.osl.networking.impl.client; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.handler.ClientNetworkHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.client.ClientPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ClientPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Client Play Networking"); - - private static Minecraft minecraft; - - public static void setUp(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft == minecraft) { - throw new IllegalStateException("tried to set up client networking when it was already set up!"); - } - - ClientPlayNetworkingImpl.minecraft = minecraft; - } - - public static void destroy(Minecraft minecraft) { - if (ClientPlayNetworkingImpl.minecraft != minecraft) { - throw new IllegalStateException("tried to destroy client networking when it was not set up!"); - } - - ClientPlayNetworkingImpl.minecraft = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(minecraft, handler, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (minecraft, handler, data) -> { - return listener.handle(minecraft, handler, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(Minecraft minecraft, ClientNetworkHandler handler, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(minecraft, handler, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady() { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(String channel) { - INetworkHandler handler = (INetworkHandler)minecraft.getNetworkHandler(); - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(String channel, CustomPayload payload) { - if (canSend(channel)) { - doSend(channel, payload); - } - } - - public static void send(String channel, IOConsumer writer) { - if (canSend(channel)) { - doSend(channel, writer); - } - } - - public static void send(String channel, byte[] data) { - if (canSend(channel)) { - doSend(channel, data); - } - } - - public static void doSend(String channel, CustomPayload payload) { - sendPacket(makePacket(channel, payload)); - } - - public static void doSend(String channel, IOConsumer writer) { - sendPacket(makePacket(channel, writer)); - } - - public static void doSend(String channel, byte[] data) { - sendPacket(makePacket(channel, data)); - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(Packet packet) { - if (packet != null) { - minecraft.getNetworkHandler().sendPacket(packet); - } - } - - private interface Listener { - - boolean handle(Minecraft minecraft, ClientNetworkHandler handler, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 711978a2..00000000 --- a/libraries/networking/networking-mcb1.5-mc11w48a/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,269 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); - } - - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); - } - - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); - } - - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); - } - - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/build.gradle b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/build.gradle similarity index 100% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/build.gradle rename to libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/build.gradle diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/gradle.properties b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/gradle.properties similarity index 63% rename from libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/gradle.properties rename to libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/gradle.properties index ba01cbea..cf5879ed 100644 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/gradle.properties +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/gradle.properties @@ -1,7 +1,7 @@ environment = server -min_mc_version = server-a0.1.2_01 +min_mc_version = server-a0.1.0 max_mc_version = server-a0.2.1 -mc_version_range = >=1.0.0-alpha.1.2.1 <=1.0.0-alpha.2.1 +mc_version_range = >=1.0.0-alpha.1.0 <=1.0.0-alpha.2.1 minecraft_version = server-a0.2.1 feather_build = 1 diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..332c2370 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1217 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.util.ByteProcessor; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + private static final int VAR_VALUE_BITS = 7; + private static final int VAR_VALUE_MASK = 1 << VAR_VALUE_BITS - 1; + private static final int VAR_PARITY_VALUE = 1 << VAR_VALUE_BITS; + private static final int VAR_INT_MAX_BYTES = 5; + private static final int VAR_LONG_MAX_BYTES = 10; + + final ByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + int value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_INT_MAX_BYTES) { + throw new RuntimeException("VarInt too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public long readVarLong() { + long value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_LONG_MAX_BYTES) { + throw new RuntimeException("VarLong too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public byte[] readByteArray() { + return this.readByteArray(this.readableBytes()); + } + + public byte[] readByteArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("ByteArray with size " + length + " is bigger than allowed " + maxLength); + } + + byte[] values = new byte[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readByte(); + } + + return values; + } + + public int[] readIntArray() { + return this.readIntArray(this.readableBytes()); + } + + public int[] readIntArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("IntArray with size " + length + " is bigger than allowed " + maxLength); + } + + int[] values = new int[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarInt(); + } + + return values; + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength * 4) { + throw new RuntimeException("The received encoded string buffer length is longer than maximum allowed (" + length + " > " + maxLength * 4 + ")"); + } + if (length < 0) { + throw new RuntimeException("The received encoded string buffer length is less than zero! Weird string!"); + } + + String s = this.toString(this.readerIndex(), length, StandardCharsets.UTF_8); + this.readerIndex(this.readerIndex() + length); + + if (s.length() > maxLength) { + throw new RuntimeException("The received string length is longer than maximum allowed (" + length + " > " + maxLength + ")"); + } + + return s; + } + + public > T readEnum(Class type) { + return type.getEnumConstants()[this.readVarInt()]; + } + + public Date readDate() { + return new Date(this.readLong()); + } + + public UUID readUuid() { + return new UUID( + this.readLong(), + this.readLong() + ); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public NbtCompound readNbtCompound() { + int readerIndex = this.readerIndex(); + byte firstByte = this.readByte(); + + if (firstByte == 0) { + return null; + } else { + this.readerIndex(readerIndex); + + try { + return NbtIo.read(new ByteBufInputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public ByteBuf writeVarInt(int value) { + while ((value & -128) != 0) { + this.writeByte(value & VAR_VALUE_MASK | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte(value); + + return this; + } + + public ByteBuf writeVarLong(long value) { + while ((value & -128) != 0) { + this.writeByte((int) (value & VAR_VALUE_MASK) | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte((int) value); + + return this; + } + + public ByteBuf writeByteArray(byte[] values) { + this.writeVarInt(values.length); + + for (byte value : values) { + this.writeByte(value); + } + + return this; + } + + public ByteBuf writeIntArray(int[] values) { + this.writeVarInt(values.length); + + for (int value : values) { + this.writeVarInt(value); + } + + return this; + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + if (bytes.length > maxLength) { + throw new RuntimeException("String too big (was " + bytes.length + " bytes encoded, max " + maxLength + ")"); + } + + this.writeByteArray(bytes); + + return this; + } + + public ByteBuf writeEnum(Enum value) { + this.writeVarInt(value.ordinal()); + + return this; + } + + public ByteBuf writeDate(Date date) { + this.writeLong(date.getTime()); + + return this; + } + + public ByteBuf writeUuid(UUID uuid) { + this.writeLong(uuid.getMostSignificantBits()); + this.writeLong(uuid.getLeastSignificantBits()); + + return this; + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + if (nbt == null) { + this.writeByte(0); + } else { + try { + NbtIo.write(nbt, new ByteBufOutputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return this; + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..a9e6ccc8 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,35 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java similarity index 96% rename from libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java rename to libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java index e248c524..9d33bccf 100644 --- a/libraries/networking/networking-mc13w41a-mc14w20b/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java @@ -3,7 +3,7 @@ import java.util.function.BiConsumer; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.living.player.ServerPlayerEntity; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.ornithemc.osl.core.api.events.Event; diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java new file mode 100644 index 00000000..89fb46e9 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.api.server; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ServerPacketListener { + + /** + * Receive incoming data from the client. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ServerPacketListener { + } + + @FunctionalInterface + interface Buffer extends ServerPacketListener { + } + + @FunctionalInterface + interface Bytes extends ServerPacketListener { + } + + interface Context { + + /** + * @return the current MinecraftServer game instance. + */ + MinecraftServer server(); + + /** + * @return the network handler that received the packet. + */ + ServerPlayNetworkHandler networkHandler(); + + /** + * @return the player that received the packet. + */ + ServerPlayerEntity player(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java new file mode 100644 index 00000000..0b6bc7ad --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java @@ -0,0 +1,271 @@ +package net.ornithemc.osl.networking.api.server; + +import java.util.function.Supplier; + +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public final class ServerPlayNetworking { + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be deserialized into a {@code CustomPayload} object of the given type. + */ + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. + */ + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Register a listener to receive data from the server through the given channel. + * The data will be given as a raw {@code byte[]}. + */ + public static void registerLegacyListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); + } + + /** + * Remove the listener registered to the given channel. + */ + public static void unregisterListener(NamespacedIdentifier channel) { + ServerPlayNetworkingImpl.unregisterListener(channel); + } + + /** + * Check whether the connection is ready for data to be sent to the client. + */ + public static boolean isPlayReady(ServerPlayerEntity player) { + return ServerPlayNetworkingImpl.isPlayReady(player); + } + + /** + * Check whether the given channel is ready for data to be sent through it. + * This method will return {@code false} if the client has no listeners for + * the given channel. + */ + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + return ServerPlayNetworkingImpl.isPlayReady(player, channel); + } + + /** + * Send a packet to the given player through the given channel. The payload + * will only be written if the channel is open. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(player, channel, payload); + } + + /** + * Send a packet to the given player through the given channel. The writer + * will only be called if the channel is open. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(player, channel, writer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel. The payload + * will only be written if the channel is open for at least one player. + */ + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(players, channel, payload); + } + + /** + * Send a packet to the given players through the given channel. The writer + * will only be called if the channel is open for at least one player. + */ + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(players, channel, writer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(players, channel, bytes); + } + + /** + * Send a packet to all players through the given channel. The payload will + * only be written if the channel is open for at least one player. + */ + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.send(channel, payload); + } + + /** + * Send a packet to all players through the given channel. The writer will + * only be called if the channel is open for at least one player. + */ + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.send(channel, writer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(channel, bytes); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, payload); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, writer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, payload); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, writer); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, bytes); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(channel, payload); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, writer); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, buffer); + } + + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(channel, bytes); + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Connections.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Connections.java new file mode 100644 index 00000000..836dd8ed --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Connections.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; + +public final class Connections { + + public static boolean checkAsyncHandling(Packet packet, PacketHandler listener) { + boolean handleAsync = packet instanceof CustomPayloadPacketAccess + && listener instanceof NetworkHandlerAccess + && ((NetworkHandlerAccess) listener).osl$networking$canRunOffMainThread(); + + if (handleAsync) { + packet.handle(listener); + } + + return handleAsync; + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java new file mode 100644 index 00000000..c07137a5 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -0,0 +1,57 @@ +package net.ornithemc.osl.networking.impl; + +import java.io.IOException; +import java.util.LinkedHashSet; +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; + +public class HandshakePayload implements PacketPayload { + + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); + + public byte protocol; + public Set channels; + + public HandshakePayload() { + } + + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); + } + + public static HandshakePayload server() { + return new HandshakePayload(ServerPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); + } + + @Override + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); + channels = new LinkedHashSet<>(); + + int channelCount = buffer.readInt(); + + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); + } + } + + @Override + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); + buffer.writeInt(channels.size()); + + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); + } + } +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..2a4f77aa --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface PacketFactory { + + Packet create(NamespacedIdentifier channel, byte[] data); + +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..9c17038b --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + byte[] osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..81b86b1f --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,17 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$canRunOffMainThread(); + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java new file mode 100644 index 00000000..218a1e67 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java @@ -0,0 +1,376 @@ +package net.ornithemc.osl.networking.impl.server; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.function.Supplier; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.network.packet.Packet; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.function.IOConsumer; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.server.ServerPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; + +public final class ServerPlayNetworkingImpl { + + private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); + + private static PacketFactory packetFactory; + private static MinecraftServer server; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ServerPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up server custom payload packet factory when it was already set up!"); + } + + ServerPlayNetworkingImpl.packetFactory = factory; + } + + public static void setUp(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server == server) { + throw new IllegalStateException("tried to set up server play networking when it was already set up!"); + } + if (ServerPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up server play networking when no custom payload packet factory was set up!"); + } + + ServerPlayNetworkingImpl.server = server; + ServerPlayNetworkingImpl.thread = Thread.currentThread(); + } + + public static void destroy(MinecraftServer server) { + if (ServerPlayNetworkingImpl.server != server) { + throw new IllegalStateException("tried to destroy server play networking when it was not set up!"); + } + + ServerPlayNetworkingImpl.server = null; + ServerPlayNetworkingImpl.thread = null; + } + + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); + + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + registerListenerInternal(channel, (context, bytes) -> { + T payload = initializer.get(); + payload.read(PacketBuffers.wrap(bytes)); + + listener.handle(context, payload); + }); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + registerListenerInternal(channel, (context, bytes) -> listener.handle(context, PacketBuffers.wrap(bytes))); + } + + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + registerListenerInternal(channel, listener::handle); + } + + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isServerbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not server-bound - did you register it with the wrong settings?"); + } + + CHANNEL_LISTENERS.compute(channel, (key, value) -> { + if (value != null) { + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); + } + + return listener; + }); + } + + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); + } + + public static boolean handlePacket(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); + + if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(player); + byte[] data = p.osl$networking$getData(); + + try { + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) server).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); + } + + return true; + } + + return false; + } + + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, byte[] data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + + public static boolean isPlayReady(ServerPlayerEntity player) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(); + } + + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(channel); + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, payload); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, writer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, buffer); + } + } + + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, bytes); + } + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(player, channel, payload); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(player, channel, writer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(player, channel, buffer); + } + + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(player, channel, bytes); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(players, channel, payload); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(players, channel, writer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(players, channel, buffer); + } + + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(players, channel, bytes); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(allPlayers(), channel, payload); + } + + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(allPlayers(), channel, writer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(allPlayers(), channel, buffer); + } + + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(allPlayers(), channel, bytes); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(player, channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(player, channel, bytes); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(players, channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(players, channel, bytes); + } + + private static Iterable allPlayers() { + return server.playerManager.players; + } + + private static Iterable collectPlayers(Predicate filter) { + return collectPlayers(allPlayers(), filter); + } + + private static Iterable collectPlayers(Iterable src, Predicate filter) { + List players = new ArrayList<>(); + + for (ServerPlayerEntity player : src) { + if (filter.test(player)) { + players.add(player); + } + } + + return players; + } + + private static void sendPacket(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + player.networkHandler.sendPacket(packetFactory.create(channel, data)); + } + } + + private static void sendPacket(Iterable players, NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings != null && settings.isClientbound()) { + Packet packet = packetFactory.create(channel, data); + + for (ServerPlayerEntity player : players) { + player.networkHandler.sendPacket(packet); + } + } + } + + private interface ChannelListener { + + void handle(Context context, byte[] bytes) throws IOException; + + class Context implements ServerPacketListener.Context { + + private final ServerPlayerEntity player; + + Context(ServerPlayerEntity player) { + this.player = player; + } + + @Override + public MinecraftServer server() { + return server; + } + + @Override + public ServerPlayNetworkHandler networkHandler() { + return player.networkHandler; + } + + @Override + public ServerPlayerEntity player() { + return player; + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } + } +} diff --git a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json similarity index 76% rename from libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json rename to libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json index e2526e75..f1faf16d 100644 --- a/libraries/networking/networking-mcb1.0-mcb1.4_01/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking/networking-mcserver-a0.1.0-mcserver-a0.2.1/src/main/resources/osl.networking.mixins.json @@ -4,13 +4,13 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ - "common.PacketAccessor" + "common.PacketAccessor", + "common.PacketMixin" ], "client": [ - "client.ClientNetworkHandlerMixin", - "client.HandshakePacketMixin" ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java deleted file mode 100644 index dbe85275..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.util.function.BiConsumer; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; - -import net.ornithemc.osl.core.api.events.Event; - -/** - * Events related to the server side of a client-server connection. - */ -public class ServerConnectionEvents { - - /** - * This event is fired after a successful login occurs. - * - *

- * Note that channel registration happens after login, - * and until then data cannot safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.LOGIN.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> LOGIN = Event.biConsumer(); - /** - * This event is fired after login, once channel registration is complete. - * - *

- * This marks the moment data can safely be sent to the client. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.PLAY_READY.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> PLAY_READY = Event.biConsumer(); - /** - * This event is fired when a client disconnects from the server. - * - *

- * Callbacks to this event should be registered in your mod's entrypoint, - * and can be done as follows: - * - *

-	 * {@code
-	 * ServerConnectionEvents.DISCONNECT.register((server, player) -> {
-	 * 	...
-	 * });
-	 * }
-	 * 
- */ - public static final Event> DISCONNECT = Event.biConsumer(); - -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java deleted file mode 100644 index f32f6e17..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ /dev/null @@ -1,265 +0,0 @@ -package net.ornithemc.osl.networking.api.server; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.function.Supplier; - -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public final class ServerPlayNetworking { - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListener(String channel, StreamListener listener) { - ServerPlayNetworkingImpl.registerListener(channel, listener); - } - - /** - * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. - */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); - } - - /** - * Remove the listener registered to the given channel. - */ - public static void unregisterListener(String channel) { - ServerPlayNetworkingImpl.unregisterListener(channel); - } - - /** - * Check whether the connection is ready for data to be sent to the client. - */ - public static boolean isPlayReady(ServerPlayerEntity player) { - return ServerPlayNetworkingImpl.isPlayReady(player); - } - - /** - * Check whether the given channel is open for data to be sent through it. - * This method will return {@code false} if the client has no listeners for - * the given channel. - */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); - } - - /** - * Send a packet to the given player through the given channel. The payload - * will only be written if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel. The writer - * will only be called if the channel is open. - */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel. - */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel. The payload - * will only be written if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel. The writer - * will only be called if the channel is open for at least one player. - */ - public static void send(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel. - */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); - } - - /** - * Send a packet to all players through the given channel. The payload will - * only be written if the channel is open for at least one player. - */ - public static void send(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.send(channel, payload); - } - - /** - * Send a packet to all players through the given channel. The writer will - * only be called if the channel is open for at least one player. - */ - public static void send(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.send(channel, writer); - } - - /** - * Send a packet to all players through the given channel. - */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); - } - - /** - * Send a packet to the given player through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); - } - - /** - * Send a packet to the given players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(channel, payload); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); - } - - /** - * Send a packet to all players through the given channel, without - * checking whether it is open. - * USE WITH CAUTION. Careless use of this method could lead to packet and log - * spam on the client. - */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - - } - - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - - } - - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java deleted file mode 100644 index 76c2850d..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; - -import net.minecraft.network.PacketHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public class CustomPayloadPacket extends Packet { - - public String channel; - public int size; - public byte[] data; - - public CustomPayloadPacket() { - } - - public CustomPayloadPacket(String channel, byte[] data) { - this.channel = channel; - this.data = data; - if (data != null) { - this.size = data.length; - if (this.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - } - - // the IOException has been stripped from the read/write methods - // by the obfuscator, thus we catch it and re-throw it as a - // runtime exception - it will be caught in Connection#read anyhow - - @Override - public void read(DataInputStream input) { - try { - this.channel = input.readUTF(); - this.size = input.readShort(); - if (this.size > 0 && this.size < Short.MAX_VALUE) { - this.data = new byte[this.size]; - input.readFully(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void write(DataOutputStream output) { - try { - output.writeUTF(this.channel); - output.writeShort(this.size); - if (this.data != null) { - output.write(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void handle(PacketHandler handler) { - if (handler instanceof INetworkHandler) { - ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); - } - } - - @Override - public int getSize() { - return 2 + this.channel.length() * 2 + 2 + this.size; - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java deleted file mode 100644 index 8fc8931f..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.LinkedHashSet; -import java.util.Set; - -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class HandshakePayload implements CustomPayload { - - public static final String CHANNEL = "OSL|Handshake"; - - public Set channels; - - public HandshakePayload() { - } - - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - throw new UnsupportedOperationException(); - } - - public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); - } - - @Override - public void read(DataInputStream input) throws IOException { - channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(input.readUTF()); - } - } - } - - @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); - - for (String channel : channels) { - output.writeUTF(channel); - } - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index d3c0e85d..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,34 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ServerModInitializer { - - @Override - public void init() { - PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, CustomPayloadPacket.class); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java deleted file mode 100644 index 295ead13..00000000 --- a/libraries/networking/networking-mcserver-a0.1.2_01-mcserver-a0.2.1/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ /dev/null @@ -1,245 +0,0 @@ -package net.ornithemc.osl.networking.impl.server; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import net.minecraft.network.packet.Packet; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public final class ServerPlayNetworkingImpl { - - private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); - - private static MinecraftServer server; - - public static void setUp(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); - } - - ServerPlayNetworkingImpl.server = server; - } - - public static void destroy(MinecraftServer server) { - if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); - } - - ServerPlayNetworkingImpl.server = null; - } - - public static final Map LISTENERS = new LinkedHashMap<>(); - - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - T payload = initializer.get(); - payload.read(DataStreams.input(data)); - - return listener.handle(server, handler, player, payload); - }); - } - - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); - } - - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); - } - - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); - - if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); - } - - return listener; - }); - } - - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); - } - - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); - - if (listener != null) { - try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; - } - } - - return false; - } - - public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isPlayReady(); - } - - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); - } - - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); - } - } - - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); - } - } - - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); - } - } - - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); - } - - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); - } - - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); - } - - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); - } - - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); - } - - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); - } - - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); - } - - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); - } - - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); - } - - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); - } - - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); - } - - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); - } - - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); - } - - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); - } - - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); - } - - @SuppressWarnings("unchecked") // thanks proguard - private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); - } - - private static Iterable collectPlayers(Iterable src, Predicate filter) { - List players = new ArrayList<>(); - - for (ServerPlayerEntity player : src) { - if (filter.test(player)) { - players.add(player); - } - } - - return players; - } - - private static Packet makePacket(String channel, CustomPayload payload) { - return makePacket(channel, payload::write); - } - - private static Packet makePacket(String channel, IOConsumer writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; - } - } - - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } - - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } - - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { - for (ServerPlayerEntity player : players) { - sendPacket(player, packet); - } - } - } - - private interface Listener { - - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - - } -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java deleted file mode 100644 index 237d7e67..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/CustomPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public interface CustomPayload { - - void read(DataInputStream input) throws IOException; - - void write(DataOutputStream output) throws IOException; - -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java deleted file mode 100644 index f68e894c..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/DataStreams.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.ornithemc.osl.networking.api; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import net.ornithemc.osl.core.api.util.function.IOConsumer; - -public final class DataStreams { - - public static DataInputStream input(byte[] bytes) { - return new DataInputStream(new ByteArrayInputStream(bytes == null ? new byte[0] : bytes)); - } - - public static ByteArrayOutputStream output(IOConsumer writer) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream os = new DataOutputStream(bos); - writer.accept(os); - return bos; - } -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java new file mode 100644 index 00000000..332c2370 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffer.java @@ -0,0 +1,1217 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.channels.GatheringByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Date; +import java.util.UUID; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.util.ByteProcessor; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; + +public class PacketBuffer extends ByteBuf { + + private static final int VAR_VALUE_BITS = 7; + private static final int VAR_VALUE_MASK = 1 << VAR_VALUE_BITS - 1; + private static final int VAR_PARITY_VALUE = 1 << VAR_VALUE_BITS; + private static final int VAR_INT_MAX_BYTES = 5; + private static final int VAR_LONG_MAX_BYTES = 10; + + final ByteBuf delegate; + + public PacketBuffer(ByteBuf delegate) { + this.delegate = delegate; + } + + public int readVarInt() { + int value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_INT_MAX_BYTES) { + throw new RuntimeException("VarInt too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public long readVarLong() { + long value = 0; + + byte bytes = 0; + byte nextByte = 0; + + do { + nextByte = this.readByte(); + value |= (nextByte & VAR_VALUE_MASK) << bytes++ * VAR_VALUE_BITS; + + if (bytes > VAR_LONG_MAX_BYTES) { + throw new RuntimeException("VarLong too big"); + } + } while ((nextByte & VAR_PARITY_VALUE) == VAR_PARITY_VALUE); + + return value; + } + + public byte[] readByteArray() { + return this.readByteArray(this.readableBytes()); + } + + public byte[] readByteArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("ByteArray with size " + length + " is bigger than allowed " + maxLength); + } + + byte[] values = new byte[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readByte(); + } + + return values; + } + + public int[] readIntArray() { + return this.readIntArray(this.readableBytes()); + } + + public int[] readIntArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new RuntimeException("IntArray with size " + length + " is bigger than allowed " + maxLength); + } + + int[] values = new int[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarInt(); + } + + return values; + } + + public long[] readLongArray() { + return this.readLongArray(this.readableBytes() / 8); + } + + public long[] readLongArray(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength) { + throw new IllegalStateException("LongArray with size " + length + " is bigger than allowed " + maxLength); + } + + long[] values = new long[length]; + + for (int i = 0; i < length; i++) { + values[i] = this.readVarLong(); + } + + return values; + } + + public String readString() { + return this.readString(Short.MAX_VALUE); + } + + public String readString(int maxLength) { + int length = this.readVarInt(); + + if (length > maxLength * 4) { + throw new RuntimeException("The received encoded string buffer length is longer than maximum allowed (" + length + " > " + maxLength * 4 + ")"); + } + if (length < 0) { + throw new RuntimeException("The received encoded string buffer length is less than zero! Weird string!"); + } + + String s = this.toString(this.readerIndex(), length, StandardCharsets.UTF_8); + this.readerIndex(this.readerIndex() + length); + + if (s.length() > maxLength) { + throw new RuntimeException("The received string length is longer than maximum allowed (" + length + " > " + maxLength + ")"); + } + + return s; + } + + public > T readEnum(Class type) { + return type.getEnumConstants()[this.readVarInt()]; + } + + public Date readDate() { + return new Date(this.readLong()); + } + + public UUID readUuid() { + return new UUID( + this.readLong(), + this.readLong() + ); + } + + public NamespacedIdentifier readNamespacedIdentifier() { + return NamespacedIdentifiers.parse(this.readString()); + } + + public NbtCompound readNbtCompound() { + int readerIndex = this.readerIndex(); + byte firstByte = this.readByte(); + + if (firstByte == 0) { + return null; + } else { + this.readerIndex(readerIndex); + + try { + return NbtIo.read(new ByteBufInputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + public ByteBuf writeVarInt(int value) { + while ((value & -128) != 0) { + this.writeByte(value & VAR_VALUE_MASK | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte(value); + + return this; + } + + public ByteBuf writeVarLong(long value) { + while ((value & -128) != 0) { + this.writeByte((int) (value & VAR_VALUE_MASK) | VAR_PARITY_VALUE); + value >>>= VAR_VALUE_BITS; + } + + this.writeByte((int) value); + + return this; + } + + public ByteBuf writeByteArray(byte[] values) { + this.writeVarInt(values.length); + + for (byte value : values) { + this.writeByte(value); + } + + return this; + } + + public ByteBuf writeIntArray(int[] values) { + this.writeVarInt(values.length); + + for (int value : values) { + this.writeVarInt(value); + } + + return this; + } + + public ByteBuf writeLongArray(long[] values) { + this.writeVarInt(values.length); + + for (long value : values) { + this.writeVarLong(value); + } + + return this; + } + + public ByteBuf writeString(String s) { + return this.writeString(s, Short.MAX_VALUE); + } + + public ByteBuf writeString(String s, int maxLength) { + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + if (bytes.length > maxLength) { + throw new RuntimeException("String too big (was " + bytes.length + " bytes encoded, max " + maxLength + ")"); + } + + this.writeByteArray(bytes); + + return this; + } + + public ByteBuf writeEnum(Enum value) { + this.writeVarInt(value.ordinal()); + + return this; + } + + public ByteBuf writeDate(Date date) { + this.writeLong(date.getTime()); + + return this; + } + + public ByteBuf writeUuid(UUID uuid) { + this.writeLong(uuid.getMostSignificantBits()); + this.writeLong(uuid.getLeastSignificantBits()); + + return this; + } + + public ByteBuf writeNamespacedIdentifier(NamespacedIdentifier id) { + return this.writeString(id.toString()); + } + + public ByteBuf writeNbtCompound(NbtCompound nbt) { + if (nbt == null) { + this.writeByte(0); + } else { + try { + NbtIo.write(nbt, new ByteBufOutputStream(this)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return this; + } + + @Override + public int capacity() { + return this.delegate.capacity(); + } + + @Override + public ByteBuf capacity(int newCapacity) { + return this.delegate.capacity(newCapacity); + } + + @Override + public int maxCapacity() { + return this.delegate.maxCapacity(); + } + + @Override + public ByteBufAllocator alloc() { + return this.delegate.alloc(); + } + + @Override + public ByteOrder order() { + return this.delegate.order(); + } + + @Override + public ByteBuf order(ByteOrder order) { + return this.delegate.order(order); + } + + @Override + public ByteBuf unwrap() { + return this.delegate.unwrap(); + } + + @Override + public boolean isDirect() { + return this.delegate.isDirect(); + } + + @Override + public boolean isReadOnly() { + return this.delegate.isReadOnly(); + } + + @Override + public ByteBuf asReadOnly() { + return this.delegate.asReadOnly(); + } + + @Override + public int readerIndex() { + return this.delegate.readerIndex(); + } + + @Override + public ByteBuf readerIndex(int readerIndex) { + return this.delegate.readerIndex(readerIndex); + } + + @Override + public int writerIndex() { + return this.delegate.writerIndex(); + } + + @Override + public ByteBuf writerIndex(int writerIndex) { + return this.delegate.writerIndex(writerIndex); + } + + @Override + public ByteBuf setIndex(int readerIndex, int writerIndex) { + return this.delegate.setIndex(readerIndex, writerIndex); + } + + @Override + public int readableBytes() { + return this.delegate.readableBytes(); + } + + @Override + public int writableBytes() { + return this.delegate.writableBytes(); + } + + @Override + public int maxWritableBytes() { + return this.delegate.maxWritableBytes(); + } + + @Override + public boolean isReadable() { + return this.delegate.isReadable(); + } + + @Override + public boolean isReadable(int size) { + return this.delegate.isReadable(size); + } + + @Override + public boolean isWritable() { + return this.delegate.isWritable(); + } + + @Override + public boolean isWritable(int size) { + return this.delegate.isWritable(size); + } + + @Override + public ByteBuf clear() { + return this.delegate.clear(); + } + + @Override + public ByteBuf markReaderIndex() { + return this.delegate.markReaderIndex(); + } + + @Override + public ByteBuf resetReaderIndex() { + return this.delegate.resetReaderIndex(); + } + + @Override + public ByteBuf markWriterIndex() { + return this.delegate.markWriterIndex(); + } + + @Override + public ByteBuf resetWriterIndex() { + return this.delegate.resetWriterIndex(); + } + + @Override + public ByteBuf discardReadBytes() { + return this.delegate.discardReadBytes(); + } + + @Override + public ByteBuf discardSomeReadBytes() { + return this.delegate.discardSomeReadBytes(); + } + + @Override + public ByteBuf ensureWritable(int minWritableBytes) { + return this.delegate.ensureWritable(minWritableBytes); + } + + @Override + public int ensureWritable(int minWritableBytes, boolean force) { + return this.delegate.ensureWritable(minWritableBytes, force); + } + + @Override + public boolean getBoolean(int index) { + return this.delegate.getBoolean(index); + } + + @Override + public byte getByte(int index) { + return this.delegate.getByte(index); + } + + @Override + public short getUnsignedByte(int index) { + return this.delegate.getUnsignedByte(index); + } + + @Override + public short getShort(int index) { + return this.delegate.getShort(index); + } + + @Override + public short getShortLE(int index) { + return this.delegate.getShortLE(index); + } + + @Override + public int getUnsignedShort(int index) { + return this.delegate.getUnsignedShort(index); + } + + @Override + public int getUnsignedShortLE(int index) { + return this.delegate.getUnsignedShortLE(index); + } + + @Override + public int getMedium(int index) { + return this.delegate.getMedium(index); + } + + @Override + public int getMediumLE(int index) { + return this.delegate.getMediumLE(index); + } + + @Override + public int getUnsignedMedium(int index) { + return this.delegate.getUnsignedMedium(index); + } + + @Override + public int getUnsignedMediumLE(int index) { + return this.delegate.getUnsignedMediumLE(index); + } + + @Override + public int getInt(int index) { + return this.delegate.getInt(index); + } + + @Override + public int getIntLE(int index) { + return this.delegate.getIntLE(index); + } + + @Override + public long getUnsignedInt(int index) { + return this.delegate.getUnsignedInt(index); + } + + @Override + public long getUnsignedIntLE(int index) { + return this.delegate.getUnsignedIntLE(index); + } + + @Override + public long getLong(int index) { + return this.delegate.getLong(index); + } + + @Override + public long getLongLE(int index) { + return this.delegate.getLongLE(index); + } + + @Override + public char getChar(int index) { + return this.delegate.getChar(index); + } + + @Override + public float getFloat(int index) { + return this.delegate.getFloat(index); + } + + @Override + public double getDouble(int index) { + return this.delegate.getDouble(index); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int length) { + return this.delegate.getBytes(index, dst, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { + return this.delegate.getBytes(index, dst, dstIndex, length); + } + + @Override + public ByteBuf getBytes(int index, ByteBuffer dst) { + return this.delegate.getBytes(index, dst); + } + + @Override + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { + return this.delegate.getBytes(index, out, length); + } + + @Override + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { + return this.delegate.getBytes(index, out, position, length); + } + + @Override + public CharSequence getCharSequence(int index, int length, Charset charset) { + return this.delegate.getCharSequence(index, length, charset); + } + + @Override + public ByteBuf setBoolean(int index, boolean value) { + return this.delegate.setBoolean(index, value); + } + + @Override + public ByteBuf setByte(int index, int value) { + return this.delegate.setByte(index, value); + } + + @Override + public ByteBuf setShort(int index, int value) { + return this.delegate.setShort(index, value); + } + + @Override + public ByteBuf setShortLE(int index, int value) { + return this.delegate.setShortLE(index, value); + } + + @Override + public ByteBuf setMedium(int index, int value) { + return this.delegate.setMedium(index, value); + } + + @Override + public ByteBuf setMediumLE(int index, int value) { + return this.delegate.setMediumLE(index, value); + } + + @Override + public ByteBuf setInt(int index, int value) { + return this.delegate.setInt(index, value); + } + + @Override + public ByteBuf setIntLE(int index, int value) { + return this.delegate.setIntLE(index, value); + } + + @Override + public ByteBuf setLong(int index, long value) { + return this.delegate.setLong(index, value); + } + + @Override + public ByteBuf setLongLE(int index, long value) { + return this.delegate.setLongLE(index, value); + } + + @Override + public ByteBuf setChar(int index, int value) { + return this.delegate.setChar(index, value); + } + + @Override + public ByteBuf setFloat(int index, float value) { + return this.delegate.setFloat(index, value); + } + + @Override + public ByteBuf setDouble(int index, double value) { + return this.delegate.setDouble(index, value); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int length) { + return this.delegate.setBytes(index, src, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, byte[] src) { + return this.delegate.setBytes(index, src); + } + + @Override + public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { + return this.delegate.setBytes(index, src, srcIndex, length); + } + + @Override + public ByteBuf setBytes(int index, ByteBuffer src) { + return this.delegate.setBytes(index, src); + } + + @Override + public int setBytes(int index, InputStream in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { + return this.delegate.setBytes(index, in, length); + } + + @Override + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { + return this.delegate.setBytes(index, in, position, length); + } + + @Override + public ByteBuf setZero(int index, int length) { + return this.delegate.setZero(index, length); + } + + @Override + public int setCharSequence(int index, CharSequence sequence, Charset charset) { + return this.delegate.setCharSequence(index, sequence, charset); + } + + @Override + public boolean readBoolean() { + return this.delegate.readBoolean(); + } + + @Override + public byte readByte() { + return this.delegate.readByte(); + } + + @Override + public short readUnsignedByte() { + return this.delegate.readUnsignedByte(); + } + + @Override + public short readShort() { + return this.delegate.readShort(); + } + + @Override + public short readShortLE() { + return this.delegate.readShortLE(); + } + + @Override + public int readUnsignedShort() { + return this.delegate.readUnsignedShort(); + } + + @Override + public int readUnsignedShortLE() { + return this.delegate.readUnsignedShortLE(); + } + + @Override + public int readMedium() { + return this.delegate.readMedium(); + } + + @Override + public int readMediumLE() { + return this.delegate.readMediumLE(); + } + + @Override + public int readUnsignedMedium() { + return this.delegate.readUnsignedMedium(); + } + + @Override + public int readUnsignedMediumLE() { + return this.delegate.readUnsignedMediumLE(); + } + + @Override + public int readInt() { + return this.delegate.readInt(); + } + + @Override + public int readIntLE() { + return this.delegate.readIntLE(); + } + + @Override + public long readUnsignedInt() { + return this.delegate.readUnsignedInt(); + } + + @Override + public long readUnsignedIntLE() { + return this.delegate.readUnsignedIntLE(); + } + + @Override + public long readLong() { + return this.delegate.readLong(); + } + + @Override + public long readLongLE() { + return this.delegate.readLongLE(); + } + + @Override + public char readChar() { + return this.delegate.readChar(); + } + + @Override + public float readFloat() { + return this.delegate.readFloat(); + } + + @Override + public double readDouble() { + return this.delegate.readDouble(); + } + + @Override + public ByteBuf readBytes(int length) { + return this.delegate.readBytes(length); + } + + @Override + public ByteBuf readSlice(int length) { + return this.delegate.readSlice(length); + } + + @Override + public ByteBuf readRetainedSlice(int length) { + return this.delegate.readRetainedSlice(length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int length) { + return this.delegate.readBytes(dst, length); + } + + @Override + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(byte[] dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { + return this.delegate.readBytes(dst, dstIndex, length); + } + + @Override + public ByteBuf readBytes(ByteBuffer dst) { + return this.delegate.readBytes(dst); + } + + @Override + public ByteBuf readBytes(OutputStream out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public int readBytes(GatheringByteChannel out, int length) throws IOException { + return this.delegate.readBytes(out, length); + } + + @Override + public CharSequence readCharSequence(int length, Charset charset) { + return this.delegate.readCharSequence(length, charset); + } + + @Override + public int readBytes(FileChannel out, long position, int length) throws IOException { + return this.delegate.readBytes(out, position, length); + } + + @Override + public ByteBuf skipBytes(int length) { + return this.delegate.skipBytes(length); + } + + @Override + public ByteBuf writeBoolean(boolean value) { + return this.delegate.writeBoolean(value); + } + + @Override + public ByteBuf writeByte(int value) { + return this.delegate.writeByte(value); + } + + @Override + public ByteBuf writeShort(int value) { + return this.delegate.writeShort(value); + } + + @Override + public ByteBuf writeShortLE(int value) { + return this.delegate.writeShortLE(value); + } + + @Override + public ByteBuf writeMedium(int value) { + return this.delegate.writeMedium(value); + } + + @Override + public ByteBuf writeMediumLE(int value) { + return this.delegate.writeMediumLE(value); + } + + @Override + public ByteBuf writeInt(int value) { + return this.delegate.writeInt(value); + } + + @Override + public ByteBuf writeIntLE(int value) { + return this.delegate.writeIntLE(value); + } + + @Override + public ByteBuf writeLong(long value) { + return this.delegate.writeLong(value); + } + + @Override + public ByteBuf writeLongLE(long value) { + return this.delegate.writeLongLE(value); + } + + @Override + public ByteBuf writeChar(int value) { + return this.delegate.writeChar(value); + } + + @Override + public ByteBuf writeFloat(float value) { + return this.delegate.writeFloat(value); + } + + @Override + public ByteBuf writeDouble(double value) { + return this.delegate.writeDouble(value); + } + + @Override + public ByteBuf writeBytes(ByteBuf src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int length) { + return this.delegate.writeBytes(src, length); + } + + @Override + public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(byte[] src) { + return this.delegate.writeBytes(src); + } + + @Override + public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + return this.delegate.writeBytes(src, srcIndex, length); + } + + @Override + public ByteBuf writeBytes(ByteBuffer src) { + return this.delegate.writeBytes(src); + } + + @Override + public int writeBytes(InputStream in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(ScatteringByteChannel in, int length) throws IOException { + return this.delegate.writeBytes(in, length); + } + + @Override + public int writeBytes(FileChannel in, long position, int length) throws IOException { + return this.delegate.writeBytes(in, position, length); + } + + @Override + public ByteBuf writeZero(int length) { + return this.delegate.writeZero(length); + } + + @Override + public int writeCharSequence(CharSequence sequence, Charset charset) { + return this.delegate.writeCharSequence(sequence, charset); + } + + @Override + public int indexOf(int fromIndex, int toIndex, byte value) { + return this.delegate.indexOf(fromIndex, toIndex, value); + } + + @Override + public int bytesBefore(byte value) { + return this.delegate.bytesBefore(value); + } + + @Override + public int bytesBefore(int length, byte value) { + return this.delegate.bytesBefore(length, value); + } + + @Override + public int bytesBefore(int index, int length, byte value) { + return this.delegate.bytesBefore(index, length, value); + } + + @Override + public int forEachByte(ByteProcessor processor) { + return this.delegate.forEachByte(processor); + } + + @Override + public int forEachByte(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByte(index, length, processor); + } + + @Override + public int forEachByteDesc(ByteProcessor processor) { + return this.delegate.forEachByteDesc(processor); + } + + @Override + public int forEachByteDesc(int index, int length, ByteProcessor processor) { + return this.delegate.forEachByteDesc(index, length, processor); + } + + @Override + public ByteBuf copy() { + return this.delegate.copy(); + } + + @Override + public ByteBuf copy(int index, int length) { + return this.delegate.copy(index, length); + } + + @Override + public ByteBuf slice() { + return this.delegate.slice(); + } + + @Override + public ByteBuf retainedSlice() { + return this.delegate.retainedSlice(); + } + + @Override + public ByteBuf slice(int index, int length) { + return this.delegate.slice(index, length); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return this.delegate.retainedSlice(index, length); + } + + @Override + public ByteBuf duplicate() { + return this.delegate.duplicate(); + } + + @Override + public ByteBuf retainedDuplicate() { + return this.delegate.retainedDuplicate(); + } + + @Override + public int nioBufferCount() { + return this.delegate.nioBufferCount(); + } + + @Override + public ByteBuffer nioBuffer() { + return this.delegate.nioBuffer(); + } + + @Override + public ByteBuffer nioBuffer(int index, int length) { + return this.delegate.nioBuffer(index, length); + } + + @Override + public ByteBuffer internalNioBuffer(int index, int length) { + return this.delegate.internalNioBuffer(index, length); + } + + @Override + public ByteBuffer[] nioBuffers() { + return this.delegate.nioBuffers(); + } + + @Override + public ByteBuffer[] nioBuffers(int index, int length) { + return this.delegate.nioBuffers(index, length); + } + + @Override + public boolean hasArray() { + return this.delegate.hasArray(); + } + + @Override + public byte[] array() { + return this.delegate.array(); + } + + @Override + public int arrayOffset() { + return this.delegate.arrayOffset(); + } + + @Override + public boolean hasMemoryAddress() { + return this.delegate.hasMemoryAddress(); + } + + @Override + public long memoryAddress() { + return this.delegate.memoryAddress(); + } + + @Override + public String toString(Charset charset) { + return this.delegate.toString(charset); + } + + @Override + public String toString(int index, int length, Charset charset) { + return this.delegate.toString(index, length, charset); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } + + @Override + public boolean equals(Object o) { + return this.delegate.equals(o); + } + + @Override + public int compareTo(ByteBuf o) { + return this.delegate.compareTo(o); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + @Override + public ByteBuf retain(int increment) { + return this.delegate.retain(increment); + } + + @Override + public ByteBuf retain() { + return this.delegate.retain(); + } + + @Override + public ByteBuf touch() { + return this.delegate.touch(); + } + + @Override + public ByteBuf touch(Object hint) { + return this.delegate.touch(hint); + } + + @Override + public int refCnt() { + return this.delegate.refCnt(); + } + + @Override + public boolean release() { + return this.delegate.release(); + } + + @Override + public boolean release(int decrement) { + return this.delegate.release(decrement); + } +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java new file mode 100644 index 00000000..a9e6ccc8 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketBuffers.java @@ -0,0 +1,35 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import net.ornithemc.osl.core.api.util.function.IOConsumer; + +public final class PacketBuffers { + + public static PacketBuffer make() { + return wrapped(Unpooled.buffer()); + } + + public static PacketBuffer make(IOConsumer writer) throws IOException { + PacketBuffer buffer = make(); + writer.accept(buffer); + return buffer; + } + + public static PacketBuffer wrap(byte[] bytes) { + return wrapped(Unpooled.wrappedBuffer(bytes)); + } + + public static byte[] unwrap(PacketBuffer buffer) { + byte[] bytes = new byte[buffer.writerIndex()]; + buffer.getBytes(0, bytes); + return bytes; + } + + public static PacketBuffer wrapped(ByteBuf buffer) { + return new PacketBuffer(buffer); + } +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java new file mode 100644 index 00000000..7f7835e7 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/PacketPayload.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.api; + +import java.io.IOException; + +public interface PacketPayload { + + void read(PacketBuffer buffer) throws IOException; + + void write(PacketBuffer buffer) throws IOException; + +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java index dbe85275..9d33bccf 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerConnectionEvents.java @@ -20,6 +20,10 @@ public class ServerConnectionEvents { * and until then data cannot safely be sent to the client. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * @@ -39,6 +43,10 @@ public class ServerConnectionEvents { * This marks the moment data can safely be sent to the client. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * @@ -55,6 +63,10 @@ public class ServerConnectionEvents { * This event is fired when a client disconnects from the server. * *

+ * This applies to connections to dedicated servers as + * well as connections to integrated servers. + * + *

* Callbacks to this event should be registered in your mod's entrypoint, * and can be done as follows: * diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java new file mode 100644 index 00000000..89fb46e9 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPacketListener.java @@ -0,0 +1,52 @@ +package net.ornithemc.osl.networking.api.server; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.entity.mob.player.ServerPlayerEntity; +import net.minecraft.server.network.handler.ServerPlayNetworkHandler; + +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; + +public interface ServerPacketListener { + + /** + * Receive incoming data from the client. + */ + void handle(Context ctx, T data); + + @FunctionalInterface + interface Payload extends ServerPacketListener { + } + + @FunctionalInterface + interface Buffer extends ServerPacketListener { + } + + @FunctionalInterface + interface Bytes extends ServerPacketListener { + } + + interface Context { + + /** + * @return the current MinecraftServer game instance. + */ + MinecraftServer server(); + + /** + * @return the network handler that received the packet. + */ + ServerPlayNetworkHandler networkHandler(); + + /** + * @return the player that received the packet. + */ + ServerPlayerEntity player(); + + /** + * Ensure the packet listener is running on the main thread. + */ + void ensureOnMainThread(); + + } +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java index 7eda8cca..4f45df86 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/api/server/ServerPlayNetworking.java @@ -1,51 +1,45 @@ package net.ornithemc.osl.networking.api.server; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; import java.util.function.Supplier; -import net.minecraft.server.MinecraftServer; import net.minecraft.server.entity.mob.player.ServerPlayerEntity; -import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; public final class ServerPlayNetworking { /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be deserialized into a {@code CustomPayload} object of the given type. */ - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { ServerPlayNetworkingImpl.registerListener(channel, initializer, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be wrapped in a {@link PacketBuffer} from which it can be read. */ - public static void registerListener(String channel, StreamListener listener) { + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Register a listener to receive data from the server through the given channel. - * This listener will only be called from the main thread. - * A channel can be any String of length {@value net.ornithemc.osl.networking.api.Channels#MAX_LENGTH} or less. + * The data will be given as a raw {@code byte[]}. */ - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - ServerPlayNetworkingImpl.registerListenerRaw(channel, listener); + public static void registerLegacyListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + ServerPlayNetworkingImpl.registerListener(channel, listener); } /** * Remove the listener registered to the given channel. */ - public static void unregisterListener(String channel) { + public static void unregisterListener(NamespacedIdentifier channel) { ServerPlayNetworkingImpl.unregisterListener(channel); } @@ -57,19 +51,19 @@ public static boolean isPlayReady(ServerPlayerEntity player) { } /** - * Check whether the given channel is open for data to be sent through it. + * Check whether the given channel is ready for data to be sent through it. * This method will return {@code false} if the client has no listeners for * the given channel. */ - public static boolean canSend(ServerPlayerEntity player, String channel) { - return ServerPlayNetworkingImpl.canSend(player, channel); + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + return ServerPlayNetworkingImpl.isPlayReady(player, channel); } /** * Send a packet to the given player through the given channel. The payload * will only be written if the channel is open. */ - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(player, channel, payload); } @@ -77,22 +71,29 @@ public static void send(ServerPlayerEntity player, String channel, CustomPayload * Send a packet to the given player through the given channel. The writer * will only be called if the channel is open. */ - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(player, channel, writer); } /** * Send a packet to the given player through the given channel. */ - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel. + */ + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(player, channel, bytes); } /** * Send a packet to the given players through the given channel. The payload * will only be written if the channel is open for at least one player. */ - public static void send(Iterable players, String channel, CustomPayload payload) { + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(players, channel, payload); } @@ -100,15 +101,22 @@ public static void send(Iterable players, String channel, Cu * Send a packet to the given players through the given channel. The writer * will only be called if the channel is open for at least one player. */ - public static void send(Iterable players, String channel, IOConsumer writer) { + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(players, channel, writer); } /** * Send a packet to the given players through the given channel. */ - public static void send(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.send(players, channel, data); + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(players, channel, buffer); + } + + /** + * Send a packet to the given players through the given channel. + */ + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(players, channel, bytes); } /** @@ -116,7 +124,7 @@ public static void send(Iterable players, String channel, by * channel. The payload will only be written if the channel is open for at * least one player. */ - public static void send(int dimension, String channel, CustomPayload payload) { + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { ServerPlayNetworkingImpl.send(dimension, channel, payload); } @@ -125,7 +133,7 @@ public static void send(int dimension, String channel, CustomPayload payload) { * channel. The writer will only be called if the channel is open for at * least one player. */ - public static void send(int dimension, String channel, IOConsumer writer) { + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(dimension, channel, writer); } @@ -133,15 +141,23 @@ public static void send(int dimension, String channel, IOConsumer writer) { + public static void send(NamespacedIdentifier channel, IOConsumer writer) { ServerPlayNetworkingImpl.send(channel, writer); } /** * Send a packet to all players through the given channel. */ - public static void send(String channel, byte[] data) { - ServerPlayNetworkingImpl.send(channel, data); + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.send(channel, buffer); + } + + /** + * Send a packet to all players through the given channel. + */ + public static void send(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.send(channel, bytes); } /** @@ -166,8 +189,8 @@ public static void send(String channel, byte[] data) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(player, channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, payload); } /** @@ -176,8 +199,8 @@ public static void doSend(ServerPlayerEntity player, String channel, CustomPaylo * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(player, channel, writer); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, writer); } /** @@ -186,8 +209,28 @@ public static void doSend(ServerPlayerEntity player, String channel, IOConsumer< * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(player, channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, buffer); + } + + /** + * Send a packet to the given player through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(player, channel, bytes); + } + + /** + * Send a packet to the given players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, payload); } /** @@ -196,8 +239,8 @@ public static void doSend(ServerPlayerEntity player, String channel, byte[] data * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(players, channel, payload); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, writer); } /** @@ -206,8 +249,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(players, channel, writer); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, buffer); } /** @@ -216,8 +259,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(Iterable players, String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(players, channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(players, channel, bytes); } /** @@ -226,8 +269,8 @@ public static void doSend(Iterable players, String channel, * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(int dimension, String channel, CustomPayload payload) { - ServerPlayNetworkingImpl.doSend(dimension, channel, payload); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, payload); } /** @@ -236,8 +279,8 @@ public static void doSend(int dimension, String channel, CustomPayload payload) * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(dimension, channel, writer); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(dimension, channel, writer); } /** @@ -246,18 +289,18 @@ public static void doSend(int dimension, String channel, IOConsumer writer) { - ServerPlayNetworkingImpl.doSend(channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + ServerPlayNetworkingImpl.sendNoCheck(channel, payload); } /** @@ -276,46 +319,27 @@ public static void doSend(String channel, IOConsumer writer) { * USE WITH CAUTION. Careless use of this method could lead to packet and log * spam on the client. */ - public static void doSend(String channel, byte[] data) { - ServerPlayNetworkingImpl.doSend(channel, data); - } - - public interface PayloadListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, T payload) throws IOException; - + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, writer); } - public interface StreamListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, DataInputStream data) throws IOException; - + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + ServerPlayNetworkingImpl.sendNoCheck(channel, buffer); } - public interface ByteArrayListener { - - /** - * Receive incoming data from the client. - * - * @return - * Whether the data is consumed. Should only return {@code false} if the - * data is completely ignored. - */ - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; - + /** + * Send a packet to all players through the given channel, without + * checking whether it is open. + * USE WITH CAUTION. Careless use of this method could lead to packet and log + * spam on the client. + */ + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + ServerPlayNetworkingImpl.sendNoCheck(channel, bytes); } } diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Connections.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Connections.java new file mode 100644 index 00000000..836dd8ed --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Connections.java @@ -0,0 +1,22 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.PacketHandler; +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; + +public final class Connections { + + public static boolean checkAsyncHandling(Packet packet, PacketHandler listener) { + boolean handleAsync = packet instanceof CustomPayloadPacketAccess + && listener instanceof NetworkHandlerAccess + && ((NetworkHandlerAccess) listener).osl$networking$canRunOffMainThread(); + + if (handleAsync) { + packet.handle(listener); + } + + return handleAsync; + } +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java deleted file mode 100644 index 76c2850d..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/CustomPayloadPacket.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; - -import net.minecraft.network.PacketHandler; -import net.minecraft.network.packet.Packet; - -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; - -public class CustomPayloadPacket extends Packet { - - public String channel; - public int size; - public byte[] data; - - public CustomPayloadPacket() { - } - - public CustomPayloadPacket(String channel, byte[] data) { - this.channel = channel; - this.data = data; - if (data != null) { - this.size = data.length; - if (this.size > Short.MAX_VALUE) { - throw new IllegalArgumentException("Payload may not be larger than 32k"); - } - } - } - - // the IOException has been stripped from the read/write methods - // by the obfuscator, thus we catch it and re-throw it as a - // runtime exception - it will be caught in Connection#read anyhow - - @Override - public void read(DataInputStream input) { - try { - this.channel = input.readUTF(); - this.size = input.readShort(); - if (this.size > 0 && this.size < Short.MAX_VALUE) { - this.data = new byte[this.size]; - input.readFully(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void write(DataOutputStream output) { - try { - output.writeUTF(this.channel); - output.writeShort(this.size); - if (this.data != null) { - output.write(this.data); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @Override - public void handle(PacketHandler handler) { - if (handler instanceof INetworkHandler) { - ((INetworkHandler)handler).osl$networking$handleCustomPayload(this); - } - } - - @Override - public int getSize() { - return 2 + this.channel.length() * 2 + 2 + this.size; - } -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java index 8fc8931f..c07137a5 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/HandshakePayload.java @@ -1,53 +1,57 @@ package net.ornithemc.osl.networking.impl; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.util.LinkedHashSet; import java.util.Set; -import net.ornithemc.osl.networking.api.CustomPayload; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; +import net.ornithemc.osl.networking.api.ChannelRegistry; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketPayload; import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; -public class HandshakePayload implements CustomPayload { +public class HandshakePayload implements PacketPayload { - public static final String CHANNEL = "OSL|Handshake"; + public static final NamespacedIdentifier CHANNEL = ChannelRegistry.register(Constants.OSL_HANDSHAKE_CHANNEL); - public Set channels; + public byte protocol; + public Set channels; public HandshakePayload() { } - public HandshakePayload(Set channels) { - this.channels = channels; - } - - public static HandshakePayload client() { - throw new UnsupportedOperationException(); + public HandshakePayload(Set channels) { + this.protocol = Constants.OSL_HANDSHAKE_PROTOCOL; + // we allow registering listeners on channels that do not conform to OSL spec + // but payloads sent over these channels aren't sent via OSL so we can ignore + // them for the OSL handshake. + this.channels = ChannelIdentifiers.dropInvalid(channels); } public static HandshakePayload server() { - return new HandshakePayload(ServerPlayNetworkingImpl.LISTENERS.keySet()); + return new HandshakePayload(ServerPlayNetworkingImpl.CHANNEL_LISTENERS.keySet()); } @Override - public void read(DataInputStream input) throws IOException { + public void read(PacketBuffer buffer) throws IOException { + protocol = buffer.readByte(); channels = new LinkedHashSet<>(); - int channelCount = input.readInt(); - if (channelCount > 0) { - for (int i = 0; i < channelCount; i++) { - channels.add(input.readUTF()); - } + int channelCount = buffer.readInt(); + + for (int i = 0; i < channelCount; i++) { + channels.add(buffer.readNamespacedIdentifier()); } } @Override - public void write(DataOutputStream output) throws IOException { - output.writeInt(channels.size()); + public void write(PacketBuffer buffer) throws IOException { + buffer.writeByte(protocol); + buffer.writeInt(channels.size()); - for (String channel : channels) { - output.writeUTF(channel); + for (NamespacedIdentifier channel : channels) { + buffer.writeNamespacedIdentifier(channel); } } } diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java deleted file mode 100644 index d3c0e85d..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/Networking.java +++ /dev/null @@ -1,34 +0,0 @@ -package net.ornithemc.osl.networking.impl; - -import net.ornithemc.osl.entrypoints.api.ModInitializer; -import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer; -import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents; -import net.ornithemc.osl.networking.api.server.ServerConnectionEvents; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; -import net.ornithemc.osl.networking.impl.mixin.common.PacketAccessor; -import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl; - -public class Networking implements ModInitializer, ServerModInitializer { - - @Override - public void init() { - PacketAccessor.register(Constants.CUSTOM_PAYLOAD_PACKET_ID, CustomPayloadPacket.class); - } - - @Override - public void initServer() { - MinecraftServerEvents.START.register(server -> { - ServerPlayNetworkingImpl.setUp(server); - }); - MinecraftServerEvents.STOP.register(server -> { - ServerPlayNetworkingImpl.destroy(server); - }); - ServerPlayNetworking.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> { - ((INetworkHandler)handler).osl$networking$registerChannels(payload.channels); - ServerConnectionEvents.PLAY_READY.invoker().accept(server, player); - - return true; - }); - } -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java new file mode 100644 index 00000000..2a4f77aa --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/PacketFactory.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl; + +import net.minecraft.network.packet.Packet; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface PacketFactory { + + Packet create(NamespacedIdentifier channel, byte[] data); + +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java new file mode 100644 index 00000000..9c17038b --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/CustomPayloadPacketAccess.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl.access; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface CustomPayloadPacketAccess { + + NamespacedIdentifier osl$networking$getChannel(); + + byte[] osl$networking$getData(); + +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java new file mode 100644 index 00000000..81b86b1f --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/NetworkHandlerAccess.java @@ -0,0 +1,17 @@ +package net.ornithemc.osl.networking.impl.access; + +import java.util.Set; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public interface NetworkHandlerAccess { + + boolean osl$networking$canRunOffMainThread(); + + boolean osl$networking$isPlayReady(); + + boolean osl$networking$isPlayReady(NamespacedIdentifier channel); + + void osl$networking$registerChannels(Set channels); + +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java new file mode 100644 index 00000000..bff1ba39 --- /dev/null +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/access/TaskRunnerAccess.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.networking.impl.access; + +public interface TaskRunnerAccess { + + boolean osl$networking$submit(Runnable task); + +} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java deleted file mode 100644 index 420bdcda..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/interfaces/mixin/INetworkHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.ornithemc.osl.networking.impl.interfaces.mixin; - -import java.util.Set; - -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; - -public interface INetworkHandler { - - boolean osl$networking$handleCustomPayload(CustomPayloadPacket packet); - - boolean osl$networking$isPlayReady(); - - void osl$networking$registerChannels(Set channels); - - boolean osl$networking$isRegisteredChannel(String channel); - -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java deleted file mode 100644 index ccc94437..00000000 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/mixin/common/PacketAccessor.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.ornithemc.osl.networking.impl.mixin.common; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Invoker; - -import net.minecraft.network.packet.Packet; - -@Mixin(Packet.class) -public interface PacketAccessor { - - @Invoker("register") - public static void register(int id, Class type) { - throw new AssertionError(); - } -} diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java index 711978a2..fd016d8b 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/java/net/ornithemc/osl/networking/impl/server/ServerPlayNetworkingImpl.java @@ -1,6 +1,5 @@ package net.ornithemc.osl.networking.impl.server; -import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -17,205 +16,326 @@ import net.minecraft.server.entity.mob.player.ServerPlayerEntity; import net.minecraft.server.network.handler.ServerPlayNetworkHandler; +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; import net.ornithemc.osl.core.api.util.function.IOConsumer; -import net.ornithemc.osl.networking.api.Channels; -import net.ornithemc.osl.networking.api.CustomPayload; -import net.ornithemc.osl.networking.api.DataStreams; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.ByteArrayListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.PayloadListener; -import net.ornithemc.osl.networking.api.server.ServerPlayNetworking.StreamListener; -import net.ornithemc.osl.networking.impl.CustomPayloadPacket; -import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler; +import net.ornithemc.osl.networking.api.PacketBuffer; +import net.ornithemc.osl.networking.api.PacketBuffers; +import net.ornithemc.osl.networking.api.PacketPayload; +import net.ornithemc.osl.networking.api.server.ServerPacketListener; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; +import net.ornithemc.osl.networking.impl.ChannelSettings; +import net.ornithemc.osl.networking.impl.NotOnMainThreadException; +import net.ornithemc.osl.networking.impl.PacketFactory; +import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess; +import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess; +import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess; public final class ServerPlayNetworkingImpl { private static final Logger LOGGER = LogManager.getLogger("OSL|Server Play Networking"); + private static PacketFactory packetFactory; private static MinecraftServer server; + private static Thread thread; + + public static void setUpPacketFactory(PacketFactory factory) { + if (ServerPlayNetworkingImpl.packetFactory != null) { + throw new IllegalStateException("tried to set up server custom payload packet factory when it was already set up!"); + } + + ServerPlayNetworkingImpl.packetFactory = factory; + } public static void setUp(MinecraftServer server) { if (ServerPlayNetworkingImpl.server == server) { - throw new IllegalStateException("tried to set up server networking when it was already set up!"); + throw new IllegalStateException("tried to set up server play networking when it was already set up!"); + } + if (ServerPlayNetworkingImpl.packetFactory == null) { + throw new IllegalStateException("tried to set up server play networking when no custom payload packet factory was set up!"); } ServerPlayNetworkingImpl.server = server; + ServerPlayNetworkingImpl.thread = Thread.currentThread(); } public static void destroy(MinecraftServer server) { if (ServerPlayNetworkingImpl.server != server) { - throw new IllegalStateException("tried to destroy server networking when it was not set up!"); + throw new IllegalStateException("tried to destroy server play networking when it was not set up!"); } ServerPlayNetworkingImpl.server = null; + ServerPlayNetworkingImpl.thread = null; } - public static final Map LISTENERS = new LinkedHashMap<>(); + public static final Map CHANNEL_LISTENERS = new LinkedHashMap<>(); - public static void registerListener(String channel, Supplier initializer, PayloadListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { + public static void registerListener(NamespacedIdentifier channel, Supplier initializer, ServerPacketListener.Payload listener) { + registerListenerInternal(channel, (context, bytes) -> { T payload = initializer.get(); - payload.read(DataStreams.input(data)); + payload.read(PacketBuffers.wrap(bytes)); - return listener.handle(server, handler, player, payload); + listener.handle(context, payload); }); } - public static void registerListener(String channel, StreamListener listener) { - registerListenerImpl(channel, (server, handler, player, data) -> { - return listener.handle(server, handler, player, DataStreams.input(data)); - }); + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Buffer listener) { + registerListenerInternal(channel, (context, bytes) -> listener.handle(context, PacketBuffers.wrap(bytes))); } - public static void registerListenerRaw(String channel, ByteArrayListener listener) { - registerListenerImpl(channel, listener::handle); + public static void registerListener(NamespacedIdentifier channel, ServerPacketListener.Bytes listener) { + registerListenerInternal(channel, listener::handle); } - private static void registerListenerImpl(String channel, Listener listener) { - LISTENERS.compute(channel, (key, value) -> { - Channels.validate(channel); + private static void registerListenerInternal(NamespacedIdentifier channel, ChannelListener listener) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); + + if (settings == null || !settings.isServerbound()) { + throw new IllegalArgumentException("channel \'" + channel + "\' is not server-bound - did you register it with the wrong settings?"); + } + CHANNEL_LISTENERS.compute(channel, (key, value) -> { if (value != null) { - throw new IllegalStateException("there is already a listener on channel \'" + channel + "\'"); + throw new IllegalArgumentException("there is already a listener on channel \'" + channel + "\'"); } return listener; }); } - public static void unregisterListener(String channel) { - LISTENERS.remove(channel); + public static void unregisterListener(NamespacedIdentifier channel) { + CHANNEL_LISTENERS.remove(channel); } - public static boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, CustomPayloadPacket packet) { - Listener listener = LISTENERS.get(packet.channel); + public static boolean handlePacket(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, Packet packet) { + CustomPayloadPacketAccess p = (CustomPayloadPacketAccess)packet; + + NamespacedIdentifier channel = p.osl$networking$getChannel(); + ChannelListener listener = CHANNEL_LISTENERS.get(channel); if (listener != null) { + ChannelListener.Context ctx = new ChannelListener.Context(player); + byte[] data = p.osl$networking$getData(); + try { - return listener.handle(server, handler, player, packet.data); - } catch (IOException e) { - LOGGER.warn("error handling custom payload on channel \'" + packet.channel + "\'", e); - return true; + handlePayload(channel, listener, ctx, data); + } catch (NotOnMainThreadException e) { + ((TaskRunnerAccess) server).osl$networking$submit(() -> handlePayload(channel, listener, ctx, data)); } + + return true; } return false; } + private static void handlePayload(NamespacedIdentifier channel, ChannelListener listener, ChannelListener.Context ctx, byte[] data) { + try { + listener.handle(ctx, data); + } catch (IOException e) { + LOGGER.warn("error handling custom payload on channel \'" + channel + "\'", e); + } + } + public static boolean isPlayReady(ServerPlayerEntity player) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; return handler != null && handler.osl$networking$isPlayReady(); } - public static boolean canSend(ServerPlayerEntity player, String channel) { - INetworkHandler handler = (INetworkHandler)player.networkHandler; - return handler != null && handler.osl$networking$isRegisteredChannel(channel); + public static boolean isPlayReady(ServerPlayerEntity player, NamespacedIdentifier channel) { + NetworkHandlerAccess handler = (NetworkHandlerAccess)player.networkHandler; + return handler != null && handler.osl$networking$isPlayReady(channel); } - public static void send(ServerPlayerEntity player, String channel, CustomPayload payload) { - if (canSend(player, channel)) { - doSend(player, channel, payload); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, payload); } } - public static void send(ServerPlayerEntity player, String channel, IOConsumer writer) { - if (canSend(player, channel)) { - doSend(player, channel, writer); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, writer); } } - public static void send(ServerPlayerEntity player, String channel, byte[] data) { - if (canSend(player, channel)) { - doSend(player, channel, data); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, buffer); } } - public static void send(Iterable players, String channel, CustomPayload payload) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, payload)); + public static void send(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + if (isPlayReady(player, channel)) { + sendInternal(player, channel, bytes); + } + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, buffer); + } + + public static void send(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(players, p -> isPlayReady(p, channel)), channel, bytes); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, payload); + } + + public static void send(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)), channel, writer); + } + + public static void send(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, buffer); + } + + public static void send(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension && isPlayReady(p, channel)),channel, bytes); + } + + public static void send(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, payload); + } + + public static void send(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, writer); + } + + public static void send(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, buffer); } - public static void send(Iterable players, String channel, IOConsumer writer) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, writer)); + public static void send(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> isPlayReady(p, channel)), channel, bytes); } - public static void send(Iterable players, String channel, byte[] data) { - sendPacket(collectPlayers(players, p -> canSend(p, channel)), makePacket(channel, data)); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(player, channel, payload); } - public static void send(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, payload); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(player, channel, writer); } - public static void send(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)), channel, writer); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(player, channel, buffer); } - public static void send(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension && canSend(p, channel)),channel, data); + public static void sendNoCheck(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(player, channel, bytes); } - public static void send(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, payload); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(players, channel, payload); } - public static void send(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, writer); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(players, channel, writer); } - public static void send(String channel, byte[] data) { - doSend(collectPlayers(p -> canSend(p, channel)), channel, data); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(players, channel, buffer); } - public static void doSend(ServerPlayerEntity player, String channel, CustomPayload payload) { - sendPacket(player, makePacket(channel, payload)); + public static void sendNoCheck(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(players, channel, bytes); } - public static void doSend(ServerPlayerEntity player, String channel, IOConsumer writer) { - sendPacket(player, makePacket(channel, writer)); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, payload); } - public static void doSend(ServerPlayerEntity player, String channel, byte[] data) { - sendPacket(player, makePacket(channel, data)); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(collectPlayers(p -> p.dimension == dimension), channel, writer); } - public static void doSend(Iterable players, String channel, CustomPayload payload) { - sendPacket(players, makePacket(channel, payload)); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, buffer); } - public static void doSend(Iterable players, String channel, IOConsumer writer) { - sendPacket(players, makePacket(channel, writer)); + public static void sendNoCheck(int dimension, NamespacedIdentifier channel, byte[] bytes) { + sendInternal(collectPlayers(p -> p.dimension == dimension),channel, bytes); } - public static void doSend(Iterable players, String channel, byte[] data) { - sendPacket(players, makePacket(channel, data)); + public static void sendNoCheck(NamespacedIdentifier channel, PacketPayload payload) { + sendInternal(allPlayers(), channel, payload); } - public static void doSend(int dimension, String channel, CustomPayload payload) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, payload); + public static void sendNoCheck(NamespacedIdentifier channel, IOConsumer writer) { + sendInternal(allPlayers(), channel, writer); } - public static void doSend(int dimension, String channel, IOConsumer writer) { - doSend(collectPlayers(p -> p.dimension == dimension), channel, writer); + public static void sendNoCheck(NamespacedIdentifier channel, PacketBuffer buffer) { + sendInternal(allPlayers(), channel, buffer); } - public static void doSend(int dimension, String channel, byte[] data) { - doSend(collectPlayers(p -> p.dimension == dimension),channel, data); + public static void sendNoCheck(NamespacedIdentifier channel, byte[] bytes) { + sendInternal(allPlayers(), channel, bytes); } - public static void doSend(String channel, CustomPayload payload) { - doSend(collectPlayers(p -> true), channel, payload); + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(player, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(player, channel, PacketBuffers.unwrap(buffer)); + } + + private static void sendInternal(ServerPlayerEntity player, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(player, channel, bytes); + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketPayload payload) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(payload::write))); + } catch (IOException e) { + LOGGER.warn("error writing packet payload to channel \'" + channel + "\'", e); + } + } + + private static void sendInternal(Iterable players, NamespacedIdentifier channel, IOConsumer writer) { + try { + sendPacket(players, channel, PacketBuffers.unwrap(PacketBuffers.make(writer))); + } catch (IOException e) { + LOGGER.warn("error writing buffer to channel \'" + channel + "\'", e); + } } - public static void doSend(String channel, IOConsumer writer) { - doSend(collectPlayers(p -> true), channel, writer); + private static void sendInternal(Iterable players, NamespacedIdentifier channel, PacketBuffer buffer) { + sendPacket(players, channel, PacketBuffers.unwrap(buffer)); } - public static void doSend(String channel, byte[] data) { - doSend(collectPlayers(p -> true), channel, data); + private static void sendInternal(Iterable players, NamespacedIdentifier channel, byte[] bytes) { + sendPacket(players, channel, bytes); + } + + private static Iterable allPlayers() { + return server.playerManager.players; } - @SuppressWarnings("unchecked") // thanks proguard private static Iterable collectPlayers(Predicate filter) { - return collectPlayers(server.playerManager.players, filter); + return collectPlayers(allPlayers(), filter); } private static Iterable collectPlayers(Iterable src, Predicate filter) { @@ -230,40 +350,59 @@ private static Iterable collectPlayers(Iterable writer) { - try { - return new CustomPayloadPacket(channel, DataStreams.output(writer).toByteArray()); - } catch (IOException e) { - LOGGER.warn("error writing custom payload to channel \'" + channel + "\'", e); - return null; + if (settings != null && settings.isClientbound()) { + player.networkHandler.sendPacket(packetFactory.create(channel, data)); } } - private static Packet makePacket(String channel, byte[] data) { - return new CustomPayloadPacket(channel, data); - } + private static void sendPacket(Iterable players, NamespacedIdentifier channel, byte[] data) { + ChannelSettings settings = ChannelRegistryImpl.getSettings(channel); - private static void sendPacket(ServerPlayerEntity player, Packet packet) { - if (packet != null) { - player.networkHandler.sendPacket(packet); - } - } + if (settings != null && settings.isClientbound()) { + Packet packet = packetFactory.create(channel, data); - private static void sendPacket(Iterable players, Packet packet) { - if (packet != null) { for (ServerPlayerEntity player : players) { - sendPacket(player, packet); + player.networkHandler.sendPacket(packet); } } } - private interface Listener { + private interface ChannelListener { + + void handle(Context context, byte[] bytes) throws IOException; - boolean handle(MinecraftServer server, ServerPlayNetworkHandler handler, ServerPlayerEntity player, byte[] data) throws IOException; + class Context implements ServerPacketListener.Context { + private final ServerPlayerEntity player; + + Context(ServerPlayerEntity player) { + this.player = player; + } + + @Override + public MinecraftServer server() { + return server; + } + + @Override + public ServerPlayNetworkHandler networkHandler() { + return player.networkHandler; + } + + @Override + public ServerPlayerEntity player() { + return player; + } + + @Override + public void ensureOnMainThread() { + if (Thread.currentThread() != thread) { + throw NotOnMainThreadException.INSTANCE; + } + } + } } } diff --git a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json index 2cfc21d4..f1faf16d 100644 --- a/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json +++ b/libraries/networking/networking-mcserver-a0.2.2-mcserver-a0.2.8/src/main/resources/osl.networking.mixins.json @@ -4,11 +4,13 @@ "package": "net.ornithemc.osl.networking.impl.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ - "common.PacketAccessor" + "common.PacketAccessor", + "common.PacketMixin" ], "client": [ ], "server": [ + "server.MinecraftServerMixin", "server.ServerLoginNetworkHandlerMixin", "server.ServerPlayNetworkHandlerMixin" ], diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelIdentifiers.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelIdentifiers.java new file mode 100644 index 00000000..55358b7d --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelIdentifiers.java @@ -0,0 +1,108 @@ +package net.ornithemc.osl.networking.api; + +import java.util.Set; +import java.util.stream.Collectors; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.api.util.NamespacedIdentifiers; +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierException; +import net.ornithemc.osl.networking.impl.ChannelIdentifierException; + +/** + * Utility methods for creating and validating channel identifiers. + */ +public final class ChannelIdentifiers { + + /** + * The default namespace of channel identifiers. + * It is recommended to use a custom namespace for your own identifiers. + */ + public static final String DEFAULT_NAMESPACE = NamespacedIdentifiers.DEFAULT_NAMESPACE; + + /** + * The maximum length of a channel identifier's namespace string. + */ + public static final int MAX_LENGTH_NAMESPACE = Byte.MAX_VALUE; + /** + * The maximum length of a channel identifier's identifier string. + */ + public static final int MAX_LENGTH_IDENTIFIER = Byte.MAX_VALUE; + + /** + * Construct and validate a channel identifier with the default namespace and the given identifier. + */ + public static NamespacedIdentifier from(String identifier) { + return from(DEFAULT_NAMESPACE, identifier); + } + + /** + * Construct and validate a channel identifier from the given namespace and identifier. + */ + public static NamespacedIdentifier from(String namespace, String identifier) { + return NamespacedIdentifiers.from( + validateNamespace(namespace), + validateIdentifier(identifier) + ); + } + + /** + * Check whether the given channel identifier is valid, or throw an exception. + */ + public static NamespacedIdentifier validate(NamespacedIdentifier id) { + try { + validateNamespace(id.namespace()); + validateIdentifier(id.identifier()); + + return id; + } catch (ChannelIdentifierException e) { + throw ChannelIdentifierException.invalid(id, e); + } + } + + /** + * Check that the given namespace is valid for a channel identifier. + */ + public static String validateNamespace(String namespace) { + if (namespace == null || namespace.isEmpty()) { + throw ChannelIdentifierException.invalidNamespace(namespace, "null or empty"); + } + if (namespace.length() > MAX_LENGTH_NAMESPACE) { + throw ChannelIdentifierException.invalidNamespace(namespace, "length " + namespace.length() + " is greater than maximum allowed " + MAX_LENGTH_NAMESPACE); + } + if (!namespace.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9'))) { + throw ChannelIdentifierException.invalidNamespace(namespace, "contains illegal characters - only [a-z0-9-._] are allowed"); + } + + return NamespacedIdentifiers.validateNamespace(namespace); + } + + /** + * Check that the given identifier is valid for a channel identifier. + */ + public static String validateIdentifier(String identifier) { + if (identifier == null || identifier.isEmpty()) { + throw ChannelIdentifierException.invalidIdentifier(identifier, "null or empty"); + } + if (identifier.length() > MAX_LENGTH_IDENTIFIER) { + throw ChannelIdentifierException.invalidIdentifier(identifier, "length " + identifier.length() + " is greater than maximum allowed " + MAX_LENGTH_IDENTIFIER); + } + if (!identifier.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || chr == '/' || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9'))) { + throw ChannelIdentifierException.invalidIdentifier(identifier, "contains illegal characters - only [a-z0-9-._/] are allowed"); + } + + return NamespacedIdentifiers.validateIdentifier(identifier); + } + + public static Set dropInvalid(Set channels) { + return channels + .stream() + .filter(channel -> { + try { + return ChannelIdentifiers.validate(channel) != null; + } catch (ChannelIdentifierException | NamespacedIdentifierException e) { + return false; + } + }) + .collect(Collectors.toSet()); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelRegistry.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelRegistry.java new file mode 100644 index 00000000..d499e4b8 --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/ChannelRegistry.java @@ -0,0 +1,36 @@ +package net.ornithemc.osl.networking.api; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.impl.ChannelRegistryImpl; + +public final class ChannelRegistry { + + /** + * Register a channel with default settings. This means the channel will accept + * both client-bound and server-bound packets. For finer control over channel + * settings, see {@linkplain #register(NamespacedIdentifier, boolean, boolean)}. + * + * @return the registered channel. + */ + public static NamespacedIdentifier register(NamespacedIdentifier channel) { + return ChannelRegistryImpl.register(channel); + } + + /** + * Register a channel with the given settings. + *

+ * A channel should be marked as client-bound if the client is expected to + * receive and handle packets sent over that channel. + *
+ * A channel should be marked as server-bound if the server is expected to + * receive and handle packets sent over that channel. + *

+ * A channel that has not been registered can not have any listeners and is + * considered 'closed'. + * + * @return the registered channel. + */ + public static NamespacedIdentifier register(NamespacedIdentifier channel, boolean clientbound, boolean serverbound) { + return ChannelRegistryImpl.register(channel, clientbound, serverbound); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/Channels.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/Channels.java deleted file mode 100644 index ec3246a4..00000000 --- a/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/Channels.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.ornithemc.osl.networking.api; - -public class Channels { - - /** - * The maximum String length for a custom payload channel. - */ - public static final int MAX_LENGTH = 20; - - /** - * Check that the given String channel is valid, or throw an exception. - */ - public static String validate(String channel) { - if (channel.length() > MAX_LENGTH) { - throw new RuntimeException("channel \'" + channel + "\' is too long (length " + channel.length() + "/allowed " + MAX_LENGTH + ")"); - } - - return channel; - } -} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/StringChannelIdentifierParser.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/StringChannelIdentifierParser.java new file mode 100644 index 00000000..a795afc5 --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/api/StringChannelIdentifierParser.java @@ -0,0 +1,71 @@ +package net.ornithemc.osl.networking.api; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierImpl; +import net.ornithemc.osl.networking.impl.ChannelIdentifierException; +import net.ornithemc.osl.networking.impl.ChannelIdentifierParseException; + +/** + * Utility methods for converting channel identifiers from and to {@link String}s. + */ +public final class StringChannelIdentifierParser { + + /** + * The maximum allowed length for the {@code String} representation of a channel identifier. + */ + public static final int MAX_LENGTH = ChannelIdentifiers.MAX_LENGTH_NAMESPACE + 1 + ChannelIdentifiers.MAX_LENGTH_IDENTIFIER; + + /** + * Convert the given {@code String} to a channel identifier. + * The returned identifier may be invalid. + * + * @return the channel identifier represented by the {@code String}, + * which may or may not be a valid identifier. + */ + public static NamespacedIdentifier fromString(String s) { + int i = s.indexOf('|'); + + if (i < 1) { + // allow null namespaces to support channel ids that do not conform + // to OSL spec - MC did not enforce a strict spec before 1.13 + return new NamespacedIdentifierImpl(null, s); + } else { + return new NamespacedIdentifierImpl(s.substring(0, i), s.substring(i + 1)); + } + } + + /** + * Convert the given {@code String} to a channel identifier. + * The returned channel identifier is always valid. If no valid channel + * identifier can be parsed from the given string, an exception is + * thrown. + * + * @return the channel identifier represented by the {@code String}. + * @throws ChannelIdentifierParseException + * if no valid channel identifier can be parsed from the given {@code String}. + */ + public static NamespacedIdentifier fromStringOrThrow(String s) { + int i = s.indexOf('|'); + + try { + if (i < 0) { + return ChannelIdentifiers.from(s); + } else if (i > 0) { + return ChannelIdentifiers.from(s.substring(0, i), s.substring(i + 1)); + } else { + throw ChannelIdentifierParseException.invalid(s, "badly formatted"); + } + } catch (ChannelIdentifierException e) { + throw ChannelIdentifierParseException.invalid(s, e); + } + } + + /** + * Convert the given {@code NamespacedIdentifier} to its {@code String} representation. + */ + public static String toString(NamespacedIdentifier id) { + return id.namespace() == null + ? id.identifier() + : id.namespace() + "|" + id.identifier(); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierException.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierException.java new file mode 100644 index 00000000..aef3307c --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierException.java @@ -0,0 +1,31 @@ +package net.ornithemc.osl.networking.impl; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +@SuppressWarnings("serial") +public class ChannelIdentifierException extends RuntimeException { + + private ChannelIdentifierException(String message) { + super(message); + } + + private ChannelIdentifierException(String message, Throwable cause) { + super(message, cause); + } + + public static ChannelIdentifierException invalid(NamespacedIdentifier id, Throwable cause) { + return new ChannelIdentifierException("\'" + id + "\' is not a valid channel identifier", cause); + } + + public static ChannelIdentifierException invalid(NamespacedIdentifier id, String reason) { + return new ChannelIdentifierException("\'" + id + "\' is not a valid channel identifier: " + reason); + } + + public static ChannelIdentifierException invalidNamespace(String namespace, String reason) { + return new ChannelIdentifierException("\'" + namespace + "\' is not a valid namespace: " + reason); + } + + public static ChannelIdentifierException invalidIdentifier(String identifier, String reason) { + return new ChannelIdentifierException("\'" + identifier + "\' is not a valid identifier: " + reason); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierParseException.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierParseException.java new file mode 100644 index 00000000..4566ca55 --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelIdentifierParseException.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.networking.impl; + +@SuppressWarnings("serial") +public class ChannelIdentifierParseException extends RuntimeException { + + private ChannelIdentifierParseException(String message) { + super(message); + } + + private ChannelIdentifierParseException(String message, Throwable cause) { + super(message, cause); + } + + public static ChannelIdentifierParseException invalid(String id, Throwable cause) { + return new ChannelIdentifierParseException("unable to parse channel identifier from \'" + id + "\'", cause); + } + + public static ChannelIdentifierParseException invalid(String id, String reason) { + return new ChannelIdentifierParseException("unable to parse channel identifier from \'" + id + "\': " + reason); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelRegistryImpl.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelRegistryImpl.java new file mode 100644 index 00000000..59f19d2b --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelRegistryImpl.java @@ -0,0 +1,39 @@ +package net.ornithemc.osl.networking.impl; + +import java.util.HashMap; +import java.util.Map; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; + +public final class ChannelRegistryImpl { + + private static final Map SETTINGS = new HashMap<>(); + + public static NamespacedIdentifier register(NamespacedIdentifier channel) { + return register(channel, new ChannelSettings()); + } + + public static NamespacedIdentifier register(NamespacedIdentifier channel, boolean clientbound, boolean serverbound) { + return register(channel, new ChannelSettings(clientbound, serverbound)); + } + + public static NamespacedIdentifier register(NamespacedIdentifier channel, ChannelSettings settings) { + SETTINGS.compute(channel, (key, value) -> { + if (value != null && !value.is(settings)) { + throw new IllegalArgumentException("channel \'" + channel + "\' was already registered with different settings!"); + } + + return settings; + }); + + return channel; + } + + public static boolean contains(NamespacedIdentifier channel) { + return SETTINGS.containsKey(channel); + } + + public static ChannelSettings getSettings(NamespacedIdentifier channel) { + return SETTINGS.get(channel); + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelSettings.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelSettings.java new file mode 100644 index 00000000..6c976148 --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/ChannelSettings.java @@ -0,0 +1,31 @@ +package net.ornithemc.osl.networking.impl; + +public final class ChannelSettings { + + private final boolean clientbound; + private final boolean serverbound; + + public ChannelSettings() { + this(true, true); + } + + public ChannelSettings(boolean clientbound, boolean serverbound) { + this.clientbound = clientbound; + this.serverbound = serverbound; + } + + public boolean is(ChannelSettings o) { + if (this == o) { + return true; + } + return clientbound == o.clientbound && serverbound == o.serverbound; + } + + public boolean isClientbound() { + return clientbound; + } + + public boolean isServerbound() { + return serverbound; + } +} diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/Constants.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/Constants.java index 7d75b887..5ae8b598 100644 --- a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/Constants.java +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/Constants.java @@ -1,6 +1,9 @@ package net.ornithemc.osl.networking.impl; -public class Constants { +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.networking.api.ChannelIdentifiers; + +public final class Constants { /** * The packet id for custom payloads, matches that @@ -12,5 +15,14 @@ public class Constants { * It needs to be an invalid Minecraft username. */ public static final String OSL_HANDSHAKE_KEY = "\0OrnitheMC"; + /** + * A number that describes the OSL handshake protocol, in case + * the data that is sent is expanded, reduced, or reorganized. + */ + public static final byte OSL_HANDSHAKE_PROTOCOL = 1; + /** + * The channel identifier for OSL's handshake payload. + */ + public static final NamespacedIdentifier OSL_HANDSHAKE_CHANNEL = ChannelIdentifiers.from("osl", "handshake"); } diff --git a/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/NotOnMainThreadException.java b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/NotOnMainThreadException.java new file mode 100644 index 00000000..83693fcc --- /dev/null +++ b/libraries/networking/src/main/java/net/ornithemc/osl/networking/impl/NotOnMainThreadException.java @@ -0,0 +1,11 @@ +package net.ornithemc.osl.networking.impl; + +@SuppressWarnings("serial") +public final class NotOnMainThreadException extends RuntimeException { + + public static final NotOnMainThreadException INSTANCE = new NotOnMainThreadException(); + + private NotOnMainThreadException() { + super(); + } +} diff --git a/libraries/networking/src/main/resources/fabric.mod.json b/libraries/networking/src/main/resources/fabric.mod.json index 16417e6f..7652b91d 100644 --- a/libraries/networking/src/main/resources/fabric.mod.json +++ b/libraries/networking/src/main/resources/fabric.mod.json @@ -17,18 +17,8 @@ "icon": "assets/ornithe-standard-libraries/networking/icon.png", "environment": "${environment}", "entrypoints": { - "init": [ - "net.ornithemc.osl.networking.impl.Networking" - ], - "client-init": [ - "net.ornithemc.osl.networking.impl.Networking" - ], - "server-init": [ - "net.ornithemc.osl.networking.impl.Networking" - ] }, "mixins": [ - "osl.networking.mixins.json" ], "depends": { "fabricloader": ">=0.16.0", diff --git a/settings.gradle b/settings.gradle index cb6f2ac3..f28aa83d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,5 @@ pluginManagement { repositories { - mavenLocal() maven { name = 'Fabric' url = 'https://maven.fabricmc.net/' @@ -63,20 +62,28 @@ include ':libraries:lifecycle-events:lifecycle-events-mc19w04a-mc1.14.4' include ':libraries:networking' include ':libraries:networking:networking-mca1.0.16-mca1.2.6' -include ':libraries:networking:networking-mcserver-a0.1.2_01-mcserver-a0.2.1' +include ':libraries:networking:networking-mcserver-a0.1.0-mcserver-a0.2.1' include ':libraries:networking:networking-mcserver-a0.2.2-mcserver-a0.2.8' -include ':libraries:networking:networking-mcb1.0-mcb1.4_01' -include ':libraries:networking:networking-mcb1.5-mc11w48a' -include ':libraries:networking:networking-mc11w49a-mc12w16a' -include ':libraries:networking:networking-mc12w17a-mc12w17a' -include ':libraries:networking:networking-mc12w18a-mc12w19a' -include ':libraries:networking:networking-mc12w21a-mc13w39b' -include ':libraries:networking:networking-mc13w41a-mc14w20b' -include ':libraries:networking:networking-mc14w21a-mc14w30c' -include ':libraries:networking:networking-mc14w31a-mc1.13-pre2' -include ':libraries:networking:networking-mc1.13-pre4-mc18w30b' +include ':libraries:networking:networking-mcb1.0-mc13w39b' +include ':libraries:networking:networking-mc13w41a-mc18w30b' include ':libraries:networking:networking-mc18w31a-mc1.14.4' +include ':libraries:networking-impl' +include ':libraries:networking-impl:networking-impl-mca1.0.16-mca1.2.6' +include ':libraries:networking-impl:networking-impl-mcserver-a0.1.2_01-mcserver-a0.2.8' +include ':libraries:networking-impl:networking-impl-mcb1.0-mcb1.4_01' +include ':libraries:networking-impl:networking-impl-mcb1.5-mc11w48a' +include ':libraries:networking-impl:networking-impl-mc11w49a-mc12w16a' +include ':libraries:networking-impl:networking-impl-mc12w17a-mc12w17a' +include ':libraries:networking-impl:networking-impl-mc12w18a-mc12w19a' +include ':libraries:networking-impl:networking-impl-mc12w21a-mc13w39b' +include ':libraries:networking-impl:networking-impl-mc13w41a-mc14w20b' +include ':libraries:networking-impl:networking-impl-mc14w21a-mc14w30c' +include ':libraries:networking-impl:networking-impl-mc14w31a-mc1.13-pre2' +include ':libraries:networking-impl:networking-impl-mc1.13-pre3-mc1.13-pre3' +include ':libraries:networking-impl:networking-impl-mc1.13-pre4-mc1.13.2' +include ':libraries:networking-impl:networking-impl-mc18w43a-mc1.14.4' + include ':libraries:resource-loader' include ':libraries:resource-loader:resource-loader-mca1.2.2-1624-mc11w48a' include ':libraries:resource-loader:resource-loader-mc11w49a-mc1.2.5'