diff --git a/.gitignore b/.gitignore index ba9af14..12f8644 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,25 @@ -__* -*.bak -*.xlsx - -/releases -/test - -/.gradle -/.settings -/bin -/build -/gradle -/run - -/.classpath -/.project -/gradlew -/gradlew.bat -/forge*.zip - -/build.properties -/locations.gradle - -/todo.txt -/changes.txt -/changelog.txt +# eclipse +bin +*.launch +.settings +.metadata +.classpath +.project + +# idea +out +*.ipr +*.iws +*.iml +.idea + +# gradle +build +.gradle + +# other +eclipse +run + +# Files from Forge MDK +forge*changelog.txt diff --git a/build.gradle b/build.gradle index 34f9f85..a9ab862 100644 --- a/build.gradle +++ b/build.gradle @@ -1,367 +1,98 @@ -ext.forgeversion = "1.7.10-10.13.3.1428-1.7.10" -group = "com.xcompwiz.lookingglass" // http://maven.apache.org/guides/mini/guide-naming-conventions.html -ext.archivesBaseName = "lookingglass" -ext.nameCased = "LookingGlass" -ext.curseId = "230541" // project url is http://minecraft.curseforge.com/mc-mods/230541-lookingglass/ -ext.curseReleaseType = "release" //The release type must be either 'alpha', 'beta', or 'release' -ext.curseIncludeAPI = true -ext.curseIncludeDev = true - -// define some stuff. hereafter referenced as project.varName -ext.configFile = file "build.properties" -ext.smallChangelog = file("changes.txt") -ext.releaseChangelog = file("changelog.txt") -ext.publishChangelog = file("changes.txt") -ext.userHome = System.properties["user.home"] - -// -------------------------- -// End local config -// -------------------------- - -//Configure these through locations.gradle -ext.jarDeploys = [] -ext.devDeploys = [] -ext.APIDeploys = [] -ext.changelogDeploys = [] - buildscript { - repositories { - mavenCentral() - maven { - name = "forge" - url = "http://files.minecraftforge.net/maven" - } - maven { - name = "sonatype" - url = "https://oss.sonatype.org/content/repositories/snapshots/" - } - } - dependencies { - classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT' - } -} - -repositories { - mavenLocal() - mavenCentral() - ivy { - name 'Forge FS legacy' - artifactPattern "http://files.minecraftforge.net/[module]/[module]-dev-[revision].[ext]" - } - maven { - name 'ForgeFS' - url 'http://files.minecraftforge.net/maven' - } - maven { - name 'MinecraftS3' - url 'http://s3.amazonaws.com/Minecraft.Download/libraries' - } -} - -apply plugin: 'curseforge' -apply plugin: 'forge' - -if (file('locations.gradle').exists()) { - apply from: 'locations.gradle' -} - -if (file('dependencies.gradle').exists()) { - apply from: 'dependencies.gradle' + repositories { + maven { url = 'https://maven.minecraftforge.net/' } + mavenCentral() + } + dependencies { + classpath 'net.minecraftforge.gradle:ForgeGradle:3.+' + } } + +apply plugin: 'net.minecraftforge.gradle' +// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. +apply plugin: 'eclipse' +apply plugin: 'maven-publish' -configFile.withReader { - // read config. it shall from now on be referenced as simply config or as project.config - def prop = new Properties() - prop.load(it) - ext.config = new ConfigSlurper().parse prop -} +version = '1.0' +group = 'com.xcompwiz.lookingglass' // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = 'lookingglass' -version = "${config.version.super}.${config.version.major}.${config.version.minor}.${config.version.build}" +sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly. -// Setup the forge minecraft plugin data. Specify the preferred forge/minecraft version here minecraft { - version = project.forgeversion - runDir = "../run/client" - - replace "@VERSION@", project.version -} - -processResources -{ - // this will ensure that this task is redone when the versions change. - inputs.property "version", {"${project.version}"} - inputs.property "mcversion", {project.minecraft.version} - - // replace stuff in mcmod.info, nothing else - from(sourceSets.main.resources.srcDirs) { - include 'mcmod.info' - - // replace version and mcversion - expand 'version':"${-> project.version}", 'mcversion':project.minecraft.version - } - - // copy everything else, thats not the mcmod.info - from(sourceSets.main.resources.srcDirs) { - exclude 'mcmod.info' - } -} - -// this sets our output jar to have a 'tag' of 'universal' on it -// It also adds the minecraft version in a custom version name -// The result is files named --.jar -jar { - classifier = project.version - version = project.minecraft.version - includeEmptyDirs = false - from sourceSets.api.output -} - -// ------------- -// extra jars -// ------------- - -// because the normal output has been made to be obfuscated -task devJar(type: Jar) { - from sourceSets.main.output - from sourceSets.api.output - from sourceSets.api.allSource - classifier = 'dev' - version = "${project.minecraft.version}-${project.version}" -} - -task apiJar(type: Jar) { - from sourceSets.api.output - from sourceSets.api.allSource - classifier = 'api' - version = "${project.minecraft.version}-${project.version}" -} - -task srcJar(type: Jar) { - from sourceSets.main.java - from sourceSets.api.java - classifier = 'src' - version = "${project.minecraft.version}-${project.version}" -} - -build.dependsOn apiJar, devJar - -// specify artifacts to be uploaded -artifacts { - // the default jar is already here by default - archives devJar - archives apiJar -} - -// --------------------------- -// Changelog and Deployments -// --------------------------- + mappings channel: 'snapshot', version: '20171003-1.12' -task("updateChangelog") { - inputs.file project.smallChangelog - inputs.file project.releaseChangelog - outputs.file project.smallChangelog - outputs.file project.releaseChangelog + runs { + client { + workingDirectory project.file('run') - doLast { - def small = project.smallChangelog - def release = project.releaseChangelog - - release.text = "[${project.version}]\n${small.text}\n${release.text}" - small.text = "" - } -} - -task("markChangelog") { - inputs.file project.releaseChangelog - outputs.file project.releaseChangelog - - doLast { - def release = project.releaseChangelog - release.text = "[RELEASE]\n${release.text}" - } - mustRunAfter updateChangelog -} - -project.tasks.curse.dependsOn "markChangelog" - -task "deploy" // creates it for later -task "deployjar" // creates it for later - -project.tasks.deploy.dependsOn project.tasks.deployjar - -project.tasks.deployjar.dependsOn project.tasks.reobf -project.tasks.deployjar.dependsOn project.tasks.devJar -project.tasks.deployjar.dependsOn project.tasks.apiJar - -jarDeploys.each { name, dir -> - def deployer = task("deploy-${name}", type: Copy) { - //from project.configurations.archives // jars, API jars, whatever - - from {project.tasks.jar.getArchivePath()} // just release jar - into dir - dependsOn "reobf" - } - project.tasks.deployjar.dependsOn deployer - - def deleter = task("clean-${name}") << { - fileTree(dir()).each { file -> - if (!file.isDirectory() && file.name.startsWith(project.archivesBaseName)) - file.delete() - } - } - deployer.dependsOn deleter -} - -devDeploys.each { name, dir -> - def deployer = task("deploy-devjar-${name}", type: Copy) { - from {project.tasks.devJar.getArchivePath()} // the dev jar - into dir - dependsOn "devJar" - mustRunAfter "deployjar" - } - project.tasks.deploy.dependsOn deployer - - def deleter = task("clean-devjar-${name}") << { - fileTree(dir()).each { file -> - if (!file.isDirectory() && file.name.startsWith(project.archivesBaseName) && file.name.endsWith("${project.devJar.classifier}.jar")) - file.delete() - } - } - deleter.mustRunAfter "devJar" - deployer.dependsOn deleter -} - -APIDeploys.each { name, dir -> - def deployer = task("deploy-apijar-${name}", type: Copy) { - from {project.tasks.apiJar.getArchivePath()} // the API - into dir - dependsOn "apiJar" - mustRunAfter "deployjar" - } - project.tasks.deploy.dependsOn deployer + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + property 'forge.logging.console.level', 'debug' + } - def deleter = task("clean-apijar-${name}") << { - fileTree(dir()).each { file -> - if (!file.isDirectory() && file.name.startsWith(project.archivesBaseName) && file.name.endsWith("${project.apiJar.classifier}.jar")) - file.delete() - } - } - deleter.mustRunAfter "apiJar" - deployer.dependsOn deleter + server { + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + property 'forge.logging.console.level', 'debug' + } + } } -changelogDeploys.each { name, dir -> - def deployer = task("deploy-changelog-${name}", type: Copy) { - from project.releaseChangelog - into dir - mustRunAfter "updateChangelog" - } - project.tasks.deploy.dependsOn deployer -} +dependencies { + // Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed + // that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied. + // The userdev artifact is a special name and will get all sorts of transformations applied to it. + minecraft 'net.minecraftforge:forge:1.12.2-14.23.5.2859' -// ---------------------- -// Incrementer handling -// ---------------------- + // You may put jars on which you depend on in ./libs or you may define them like so.. + // compile "some.group:artifact:version:classifier" + // compile "some.group:artifact:version" -import java.text.DecimalFormat -def formatter = new DecimalFormat('00') -def formatters = [new DecimalFormat('0'), new DecimalFormat('0'), new DecimalFormat('0'), new DecimalFormat('00')] + // Real examples + // compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env + // compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env -// increment tasks -def types = ["super", "major", "minor", "build"] -types.eachWithIndex { type, index -> - def incrementer = task("increment-${type}").doLast { - // increment - int newNum = (config.version[type.toLowerCase()].toString().toInteger()) + 1 - config.version[type.toLowerCase()] = formatters[index].format(newNum) - // set lower #'s to 0 - types.eachWithIndex { type2, index2 -> - if (index2 > index) { - config.version[type2.toLowerCase()] = formatters[index2].format(0) - } - } + // The 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime. + // provided 'com.mod-buildcraft:buildcraft:6.0.8:dev' - // write back to the file - configFile.withWriter { - config.toProperties().store(it, "") - } - project.version = "${config.version.super}.${config.version.major}.${config.version.minor}.${config.version.build}" - jar.classifier = "${project.version}" - devJar.version = "${project.minecraft.version}-${project.version}" - apiJar.version = "${project.minecraft.version}-${project.version}" - srcJar.version = "${project.minecraft.version}-${project.version}" - project.tasks.sourceMainJava.replace "@VERSION@", project.version - } + // These dependencies get remapped to your current MCP mappings + // deobf 'com.mod-buildcraft:buildcraft:6.0.8:dev' - task("deploy-$type") { - dependsOn incrementer, project.tasks.updateChangelog, project.tasks.deploy - group = "Mystcraft" - description = "Increments ${type.toLowerCase()} by 1 and the deploys the artifacts" - } + // For more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html - project.tasks.sourceMainJava.mustRunAfter incrementer - project.tasks.updateChangelog.mustRunAfter incrementer - project.tasks.deploy.mustRunAfter incrementer - project.tasks.uploadArchives.mustRunAfter incrementer - project.tasks.processResources.mustRunAfter incrementer - project.tasks.compileApiJava.mustRunAfter incrementer } -curse { - apiKey = config.curseforge_key // saved in my properties file. http://minecraft.curseforge.com/my-api-tokens - projectId = project.curseId - releaseType = project.curseReleaseType //The release type must be either 'alpha', 'beta', or 'release' - - changelog = project.publishChangelog.text - - // the default obfuscated jar is uploaded by default - // artifact = project.file("some/jar/to/upload.jar") - if (project.curseIncludeAPI) { - additionalArtifact {project.tasks.apiJar.getArchivePath()} - } - if (project.curseIncludeDev) { - additionalArtifact {project.tasks.devJar.getArchivePath()} - } - - // allows you to add extra compatible MC versions. The one specified in the minecraft{} block is used by default. - // addGameVersion "1.7.1" - // addGameversion "1.7.0", "1.7.4" -} - -// deployment stuff - -configurations { deployerJars } - -dependencies { deployerJars "org.apache.maven.wagon:wagon-ssh:2.2" } - -uploadArchives { +// Example for how to get properties into the manifest for reading by the runtime.. +jar { + manifest { + attributes([ + "Specification-Title": "examplemod", + "Specification-Vendor": "examplemodsareus", + "Specification-Version": "1", // We are version 1 of ourselves + "Implementation-Title": project.name, + "Implementation-Version": "${version}", + "Implementation-Vendor" :"examplemodsareus", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } +} + +// Example configuration to allow publishing using the maven-publish task +// This is the preferred method to reobfuscate your jar file +jar.finalizedBy('reobfJar') +// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing +//publish.dependsOn('reobfJar') + +publishing { + publications { + mavenJava(MavenPublication) { + artifact jar + } + } repositories { - mavenDeployer { - configuration = configurations.deployerJars - - repository(url: config.mavenurl) { - authentication(userName: config.mavenuser, password: config.mavenpass) - } - - pom { - groupId = project.group - version = "${-> project.version}" - artifactId = project.archivesBaseName - project { - name project.archivesBaseName - packaging 'jar' - description project.nameCased - //url project.url //'https://github.com/XCompWiz/...' - - developers { - developer { - id 'XCompWiz' - name 'XCompWiz' - roles { role 'developer' } - } - } - } - } + maven { + url "file:///${project.projectDir}/mcmodsrepo" } - } -} \ No newline at end of file + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..e057fa9 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,7 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false + +version=1.0 +mc_version=1.12.2 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7a3265e Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..949819d --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/api/java/com/xcompwiz/lookingglass/api/APIUndefined.java b/src/api/java/com/xcompwiz/lookingglass/api/APIUndefined.java deleted file mode 100644 index 6ce8e17..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/APIUndefined.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.xcompwiz.lookingglass.api; - -/** - * Thrown in the case of an API name being unrecognized (ex. requesting 'zymbol-1') - * @author xcompwiz - */ -public class APIUndefined extends Exception { - - /** - * Generated Serial UID - */ - private static final long serialVersionUID = 7033833326135545759L; - - public APIUndefined(String string) { - super(string); - } -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java b/src/api/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java deleted file mode 100644 index cc45033..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.xcompwiz.lookingglass.api; - -/** - * Thrown in the case of an API version having been removed entirely and no longer supported. This can be interpreted as the request being for below the minimum - * supported version for the API. - * @author xcompwiz - */ -public class APIVersionRemoved extends Exception { - - /** - * Generated Serial UID - */ - private static final long serialVersionUID = -7702376017254522430L; - - public APIVersionRemoved(String string) { - super(string); - } -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java b/src/api/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java deleted file mode 100644 index 2c08a87..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.xcompwiz.lookingglass.api; - -/** - * Thrown in the case of an API version not being available (ex. requesting 'symbol-4096') - * @author xcompwiz - */ -public class APIVersionUndefined extends Exception { - - /** - * Generated Serial UID - */ - private static final long serialVersionUID = -6195164554156957083L; - - public APIVersionUndefined(String string) { - super(string); - } -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java b/src/api/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java deleted file mode 100644 index 557e28b..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.xcompwiz.lookingglass.api; - -import net.minecraft.util.ChunkCoordinates; - -import com.xcompwiz.lookingglass.api.view.IWorldView; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * @deprecated This interface will be removed in a future version. You should switch to the IWorldViewAPI2. - * @author xcompwiz - */ -@Deprecated -public interface IWorldViewAPI { - - /** - * Creates a world viewer object which will handle the rendering and retrieval of the remote location. Can return null. - * @param dimid The target dimension - * @param coords The coordinates of the target location. If null, world spawn is used. - * @param width Texture resolution width - * @param height Texture resolution height - * @return A IWorldView object for your use or null if something goes wrong. - */ - @SideOnly(Side.CLIENT) - IWorldView createWorldView(Integer dimid, ChunkCoordinates coords, int width, int height); -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java b/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java deleted file mode 100644 index 21d7f5b..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java +++ /dev/null @@ -1,184 +0,0 @@ -package com.xcompwiz.lookingglass.api.animator; - -import net.minecraft.block.Block; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.world.IBlockAccess; - -import com.xcompwiz.lookingglass.api.view.IViewCamera; - -/** - * This is a standard sample implementation of a camera animator. It simply uses the target location as a LookAt target and does a fly pivot around it. It can - * be extended for more control, if desired. - * @author xcompwiz - */ -public class CameraAnimatorPivot implements ICameraAnimator { - /** This is a list of recommended, preset offsets that the animator will choose from. It's use can be overridden via function. */ - private static final int[][] presets = { { 2, 5 }, { 5, 9 }, { 9, 15 }, { 1, 3 }, { 2, 1 }, { 0, 2 } }; - /** This is a pair used when the animator cannot find a good path from the presets. It's use can be overridden via function. */ - private static final int[] defaults = { 1, 3 }; - - private final IViewCamera camera; - private ChunkCoordinates target; - - private boolean positionSet = false; - - private int xCenter; - private int yCenter; - private int zCenter; - - private int yUp = 0; - private int radius = 0; - private float pitch = 0; - - public CameraAnimatorPivot(IViewCamera camera) { - this.camera = camera; - } - - @Override - public void setTarget(ChunkCoordinates target) { - this.target = target; - positionSet = false; - } - - @Override - public void update(long dt) { - if (camera == null) return; - camera.addRotations(dt*0.1F, 0); - camera.setPitch(-pitch); - - double x = Math.cos(Math.toRadians(camera.getYaw() + 90)) * radius; - double z = Math.sin(Math.toRadians(camera.getYaw() + 90)) * radius; - camera.setLocation(xCenter + 0.5 - x, yCenter - 0.5 + yUp, zCenter + 0.5 - z); - } - - @Override - public void refresh() { - if (camera == null) return; - if (target == null) return; - if (!positionSet) this.checkCameraY(); - - int chunkX = xCenter >> 4; - int chunkY = yCenter >> 4; - int chunkZ = zCenter >> 4; - - int[][] presets = this.getPresets(); - - for (int i = 0; i < presets.length; ++i) { - if (checkPath(presets[i][0], presets[i][1], chunkX, chunkY, chunkZ)) { - yUp = presets[i][0]; - radius = presets[i][1]; - pitch = (float) Math.toDegrees(Math.atan(((double) -yUp) / radius)); - return; - } - } - int[] defaults = this.getDefaults(); - yUp = defaults[0]; - radius = defaults[1]; - pitch = (float) Math.toDegrees(Math.atan(((double) -yUp) / radius)); - } - - /** - * Should the selection from the offsets fail, the pair of values in this array will be used instead. - * @return A pair of numbers up and distance in an array of length 2 - */ - public int[] getDefaults() { - return defaults; - } - - /** - * Overriding his function allows you specify your own offset presets. - * @return An array of integer pairs up and distance from which to select the first functional pair - */ - public int[][] getPresets() { - return presets; - } - - /** - * @author Ken Butler/shadowking97 - */ - private boolean checkPath(int up, int distance, int chunkX, int chunkY, int chunkZ) { - if ((yCenter & 15) > 15 - up) { - if (isAboveNullLayer(chunkX, chunkY, chunkZ)) return false; - if ((xCenter & 15) < distance) { - if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ)) return false; - if ((zCenter & 15) < distance) { - if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ - 1)) return false; - if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; - } else if ((zCenter & 15) > 15 - distance) { - if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ + 1)) return false; - if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; - } - } else if ((xCenter & 15) > 15 - distance) { - if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ)) return false; - if ((zCenter & 15) < distance) { - if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ - 1)) return false; - if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; - } else if ((zCenter & 15) > 15 - distance) { - if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ + 1)) return false; - if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; - } - } else { - if ((zCenter & 15) < distance) { - if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; - } else if ((zCenter & 15) > 15 - distance) { - if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; - } - } - } - for (int j = -distance; j <= distance; ++j) { - for (int k = -distance; k <= distance; ++k) { - if (!camera.getBlockData().isAirBlock(xCenter + j, yCenter + up, zCenter + k)) return false; - } - } - return true; - } - - /** - * @author Ken Butler/shadowking97 - */ - private boolean isAboveNullLayer(int x, int y, int z) { - if (y + 1 > 15) return true; - int x2 = x << 4; - int z2 = z << 4; - int y2 = (y << 4) + 15; - int yl = (y + 1) << 4; - for (int i = 0; i < 15; i++) - for (int j = 0; j < 15; j++) - if (!isBlockNormalCube(camera.getBlockData(), x2 + i, y2, z2 + i)) return false; - if (camera.chunkLevelsExist(x, z, yl, yl + 15)) return true; - return false; - } - - private boolean isBlockNormalCube(IBlockAccess blockData, int x, int y, int z) { - Block block = blockData.getBlock(x, y, z); - return block.isNormalCube(blockData, x, y, z); - } - - private void checkCameraY() { - int x = target.posX; - int y = target.posY; - int z = target.posZ; - int yBackup = y; - if (camera.chunkExists(x, z)) { - if (camera.getBlockData().getBlock(x, y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) { - while (y > 0 && camera.getBlockData().getBlock(x, --y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) - ; - if (y == 0) y = yBackup; - else y += 2; - } else { - while (y < 256 && !camera.getBlockData().getBlock(x, ++y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) - ; - if (y == 256) y = yBackup; - else ++y; - } - this.setCenterPoint(x, y, z); - } - } - - private void setCenterPoint(int x, int y, int z) { - xCenter = x; - yCenter = y - 1; - zCenter = z; - positionSet = true; - } -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java b/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java deleted file mode 100644 index 29506ff..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.xcompwiz.lookingglass.api.animator; - -import net.minecraft.entity.Entity; -import net.minecraft.util.ChunkCoordinates; - -import com.xcompwiz.lookingglass.api.view.IViewCamera; - -/** - * This is a badly approximated "portal render" animator, which makes the camera move based on what direction the player is from some defined point. It doesn't - * do quite what it was meant to, but it's neat enough looking, so I'll leave it be. It does make a view look more "alive" or 3D, so if you just want a slightly - * animated view that doesn't look like you could walk through it, then this works great. It produces an effect more akin to Harry Potter's portraits than it - * does to Portal's portals. - * @author xcompwiz - */ -public class CameraAnimatorPlayer implements ICameraAnimator { - // This is the camera object we are animating - private final IViewCamera camera; - - // The entity was are using as our reference point, such as our portrait view. - private Entity reference; - // The entity we are facing. Expected to be the client side player, but could be anything, really. - private Entity player; - // The point we are looking at in block coordinates. - private ChunkCoordinates target; - - private boolean updateY; - private float accum; - - /** - * - * @param camera - * @param reference The entity was are using as our reference point, such as our portrait view. - * @param player The entity we are facing. Expected to be the client side player, but could be anything, really. - */ - public CameraAnimatorPlayer(IViewCamera camera, Entity reference, Entity player) { - this.camera = camera; - this.reference = reference; - this.player = player; - } - - /** - * Sets the point we are looking at using block coordinates. - */ - @Override - public void setTarget(ChunkCoordinates target) { - this.target = new ChunkCoordinates(target); - } - - @Override - public void refresh() { - updateY = true; - } - - @Override - public void update(long dt) { - // This animator is incomplete and broken. It's a rough approximation I made at 4AM one night. - // However, it's also pretty cool looking, so I'm not going to bother fixing it. :P - // Note: Needs base yaw and pitch of view - if (reference.worldObj.provider.dimensionId != player.worldObj.provider.dimensionId) return; - - // A standard accumulator trick to force periodic rechecks of the y position. Probably superfluous. - // Ticks every 10 seconds - if ((accum += dt) >= 10000) { - updateY = true; - accum -= 10000; - } - if (updateY) updateTargetPosition(); - double dx = player.posX - reference.posX; - double dy = player.posY - (reference.posY + player.yOffset); - double dz = player.posZ - reference.posZ; - double length = Math.sqrt(dx * dx + dz * dz + dy * dy); //TODO: Needs Go Faster - float yaw = -(float) Math.atan2(dx, dz); - yaw *= 180 / Math.PI; - float pitch = (float) Math.asin(dy / length); - pitch *= 180 / Math.PI; - camera.setLocation(target.posX, target.posY, target.posZ); - camera.setYaw(yaw); - camera.setPitch(pitch); - } - - private void updateTargetPosition() { - updateY = false; - int x = target.posX; - int y = target.posY; - int z = target.posZ; - if (!camera.chunkExists(x, z)) { - if (camera.getBlockData().getBlock(x, y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) { - while (y > 0 && camera.getBlockData().getBlock(x, --y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) - ; - if (y == 0) y = target.posY; - else y += 2; - } else { - while (y < 256 && !camera.getBlockData().getBlock(x, ++y, z).getBlocksMovement(camera.getBlockData(), x, y, z)) - ; - if (y == 256) y = target.posY; - else ++y; - } - target.posY = y; - } - } - -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java b/src/api/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java deleted file mode 100644 index 7409b72..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.xcompwiz.lookingglass.api.animator; - -import net.minecraft.util.ChunkCoordinates; - -public interface ICameraAnimator { - - /** - * Sets the look-at target (in block coordinates) - * @param target The block target - */ - void setTarget(ChunkCoordinates target); - - /** - * Allows the animator to refresh/reboot its settings - */ - void refresh(); - - /** - * Tick! - * @param dt Delta time in milliseconds since last render tick - */ - void update(long dt); - -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java b/src/api/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java deleted file mode 100644 index 8968abc..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.xcompwiz.lookingglass.api.event; - -import net.minecraft.entity.player.EntityPlayerMP; -import cpw.mods.fml.common.eventhandler.Event; - -/** - * Called on the server side to allow mods to send dimension information to clients - * @author xcompwiz - */ -public class ClientWorldInfoEvent extends Event { - - public final int dim; - public final EntityPlayerMP player; - - public ClientWorldInfoEvent(int dim, EntityPlayerMP player) { - this.dim = dim; - this.player = player; - } - -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java b/src/api/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java deleted file mode 100644 index 5731883..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.xcompwiz.lookingglass.api.hook; - -import net.minecraft.util.ChunkCoordinates; - -import com.xcompwiz.lookingglass.api.view.IWorldView; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * Available via "view-2" from the API provider - * @author xcompwiz - */ -public interface WorldViewAPI2 { - - /** - * Creates a world viewer object which will handle the rendering and retrieval of the remote location. Can return null. - * @param dimid The target dimension - * @param coords The coordinates of the target location. If null, world spawn is used. - * @param width Texture resolution width - * @param height Texture resolution height - * @return A IWorldView object for your use or null if something goes wrong. - */ - @SideOnly(Side.CLIENT) - IWorldView createWorldView(Integer dimid, ChunkCoordinates coords, int width, int height); - - /** - * This function is available should you wish to explicitly have the world view clean up its framebuffer. You should not use a view after calling this on - * the view. - * @param worldview The view to clean up (effectively "destroy") - */ - void cleanupWorldView(IWorldView worldview); -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/package-info.java b/src/api/java/com/xcompwiz/lookingglass/api/package-info.java deleted file mode 100644 index 918c79e..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/package-info.java +++ /dev/null @@ -1,9 +0,0 @@ -/** - * This package contains API for LookingGlass. You should never directly reference anything outside of this package (and it's sub-packages) when interacting - * with LookingGlass. - */ -@API(owner = "LookingGlass", apiVersion = "1.0", provides = "LookingGlass|API") -package com.xcompwiz.lookingglass.api; - -import cpw.mods.fml.common.API; - diff --git a/src/api/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java b/src/api/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java deleted file mode 100644 index b07e373..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.xcompwiz.lookingglass.api.view; - -import net.minecraft.world.IBlockAccess; - -public interface IViewCamera { - - /** - * Adds par1*0.15 to the entity's yaw, and *subtracts* par2*0.15 from the pitch. Clamps pitch from -90 to 90. Both arguments in degrees. - * @param yaw The yaw to be added - * @param pitch The pitch to be subtracted - */ - public void addRotations(float yaw, int pitch); - - /** - * Sets the yaw rotation of the camera - * @param f The camera's new yaw in degrees - */ - public void setYaw(float f); - - /** - * @return The camera's yaw in degrees - */ - public float getYaw(); - - /** - * Sets the pitch rotation of the camera - * @param f The camera's new pitch in degrees - */ - public void setPitch(float f); - - /** - * @return The camera's pitch in degrees - */ - public float getPitch(); - - /** - * Sets the position of the camera - * @param x X Coordinate (Block space) - * @param y Y Coordinate (Block space) - * @param z Z Coordinate (Block space) - */ - public void setLocation(double x, double y, double z); - - /** - * @return The camera's X coordinate (Block space) - */ - public double getX(); - - /** - * @return The camera's Y coordinate (Block space) - */ - public double getY(); - - /** - * @return The camera's Z coordinate (Block space) - */ - public double getZ(); - - /** - * This is provided to allow for checking for air blocks and similar. Technically, it is a WorldClient object, but it is provided as a IBlockAccess to - * discourage modifying the world. Modifying the world object would break things for everyone, so please don't do that. Should it become an issue, this will - * be replaced with something that isn't a WorldClient, so be wary of casting it. - * @return A read-only reference to the world which the camera inhabits - */ - public IBlockAccess getBlockData(); - - /** - * An easy check for if a chunk exists in the local data copy - * @param x The X coordinate in block space - * @param z The Z coordinate in block space - * @return True if the chunk at those coordinates is available locally - */ - public boolean chunkExists(int x, int z); - - /** - * Since LookingGlass utilizes partial chunk loading to minimize data traffic and storage, it's useful to be able to check if certain levels of a chunk are - * loaded locally. - * @param x The X coordinate in block space - * @param z The Z coordinate in block space - * @param yl1 The lower (closer to 0) y coordinate of the levels to check for in block space - * @param yl2 The larger (farther from 0) y coordinate of the levels to check for in block space - * @return True if the levels are loaded locally - */ - public boolean chunkLevelsExist(int x, int z, int yl1, int yl2); -} diff --git a/src/api/java/com/xcompwiz/lookingglass/api/view/IWorldView.java b/src/api/java/com/xcompwiz/lookingglass/api/view/IWorldView.java deleted file mode 100644 index 09a7732..0000000 --- a/src/api/java/com/xcompwiz/lookingglass/api/view/IWorldView.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.xcompwiz.lookingglass.api.view; - -import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; - -public interface IWorldView { - - /** - * @return The OpenGL texture id for rendering the view - */ - public int getTexture(); - - /** - * This needs to be called to request LookingGlass rerender the view to the texture - */ - public void markDirty(); - - /** - * This will be activated once the view has chunks and is ready to render - * @return True if the view has been rendered to the texture. - */ - public boolean isReady(); - - /** - * Sets the animator object for the camera. This will be updated before each render frame. - * @param animator - */ - public void setAnimator(ICameraAnimator animator); - - /** - * Returns the view's camera object. This allows you to create animators for it and adjust its location locally. - * @return the entity object from which rendering is handled - */ - public IViewCamera getCamera(); - - /** - * @deprecated This function no longer does anything and will be removed in an upcoming version. - */ - @Deprecated - public void grab(); - - /** - * @deprecated This function no longer does anything and will be removed in an upcoming version. - */ - @Deprecated - public boolean release(); - -} diff --git a/src/main/java/com/xcompwiz/lookingglass/LookingGlass.java b/src/main/java/com/xcompwiz/lookingglass/LookingGlass.java index 3019b25..b90346c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/LookingGlass.java +++ b/src/main/java/com/xcompwiz/lookingglass/LookingGlass.java @@ -1,12 +1,5 @@ package com.xcompwiz.lookingglass; -import java.io.File; - -import net.minecraft.command.ServerCommandManager; -import net.minecraft.server.MinecraftServer; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.config.Configuration; - import com.google.common.collect.ImmutableList; import com.xcompwiz.lookingglass.apiimpl.APIProviderImpl; import com.xcompwiz.lookingglass.command.CommandCreateView; @@ -15,103 +8,102 @@ import com.xcompwiz.lookingglass.imc.IMCHandler; import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; -import com.xcompwiz.lookingglass.network.packet.PacketChunkInfo; -import com.xcompwiz.lookingglass.network.packet.PacketCloseView; -import com.xcompwiz.lookingglass.network.packet.PacketCreateView; -import com.xcompwiz.lookingglass.network.packet.PacketRequestChunk; -import com.xcompwiz.lookingglass.network.packet.PacketRequestTE; -import com.xcompwiz.lookingglass.network.packet.PacketRequestWorldInfo; -import com.xcompwiz.lookingglass.network.packet.PacketTileEntityNBT; -import com.xcompwiz.lookingglass.network.packet.PacketWorldInfo; +import com.xcompwiz.lookingglass.network.packet.*; import com.xcompwiz.lookingglass.proxyworld.LookingGlassEventHandler; import com.xcompwiz.lookingglass.proxyworld.ModConfigs; +import net.minecraft.command.ServerCommandManager; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.EventHandler; +import net.minecraftforge.fml.common.SidedProxy; +import net.minecraftforge.fml.common.event.*; +import net.minecraftforge.fml.common.network.NetworkRegistry; +import org.apache.logging.log4j.Logger; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.Mod; -import cpw.mods.fml.common.Mod.EventHandler; -import cpw.mods.fml.common.Mod.Instance; -import cpw.mods.fml.common.SidedProxy; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLInterModComms.IMCEvent; -import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerStartingEvent; -import cpw.mods.fml.common.event.FMLServerStoppedEvent; -import cpw.mods.fml.common.network.NetworkRegistry; -import cpw.mods.fml.common.registry.EntityRegistry; +import java.io.File; @Mod(modid = LookingGlass.MODID, name = "LookingGlass", version = LookingGlass.VERSION) public class LookingGlass { - public static final String MODID = "LookingGlass"; - public static final String VERSION = "@VERSION@"; - - @Instance(LookingGlass.MODID) - public static LookingGlass instance; - - @SidedProxy(clientSide = "com.xcompwiz.lookingglass.client.ClientProxy", serverSide = "com.xcompwiz.lookingglass.core.CommonProxy") - public static CommonProxy sidedProxy; - - @EventHandler - public void preinit(FMLPreInitializationEvent event) { - //Initialize the packet handling - LookingGlassPacketManager.registerPacketHandler(new PacketCreateView(), (byte) 10); - LookingGlassPacketManager.registerPacketHandler(new PacketCloseView(), (byte) 11); - LookingGlassPacketManager.registerPacketHandler(new PacketWorldInfo(), (byte) 100); - LookingGlassPacketManager.registerPacketHandler(new PacketChunkInfo(), (byte) 101); - LookingGlassPacketManager.registerPacketHandler(new PacketTileEntityNBT(), (byte) 102); - LookingGlassPacketManager.registerPacketHandler(new PacketRequestWorldInfo(), (byte) 200); - LookingGlassPacketManager.registerPacketHandler(new PacketRequestChunk(), (byte) 201); - LookingGlassPacketManager.registerPacketHandler(new PacketRequestTE(), (byte) 202); - - LookingGlassPacketManager.bus = NetworkRegistry.INSTANCE.newEventDrivenChannel(LookingGlassPacketManager.CHANNEL); - LookingGlassPacketManager.bus.register(new LookingGlassPacketManager()); - - // Load our basic configs - ModConfigs.loadConfigs(new Configuration(event.getSuggestedConfigurationFile())); - - // Here we use the recommended config file to establish a good place to put a log file for any proxy world error logs. Used primarily to log the full errors when ticking or rendering proxy worlds. - File configroot = event.getSuggestedConfigurationFile().getParentFile(); - // Main tick handler. Handles FML events. - FMLCommonHandler.instance().bus().register(new LookingGlassEventHandler(new File(configroot.getParentFile(), "logs/proxyworlds.log"))); - // Forge event handler - MinecraftForge.EVENT_BUS.register(new LookingGlassForgeEventHandler()); - - // Initialize the API provider system. Beware, this way be dragons. - APIProviderImpl.init(); - } - - @EventHandler - public void init(FMLInitializationEvent event) { - // Our one and only entity. - EntityRegistry.registerModEntity(com.xcompwiz.lookingglass.entity.EntityPortal.class, "lookingglass.portal", 216, this, 64, 10, false); - - sidedProxy.init(); - } - - @EventHandler - public void handleIMC(IMCEvent event) { - // Catch IMC messages and send them off to our IMC handler - ImmutableList messages = event.getMessages(); - IMCHandler.process(messages); - } - - @EventHandler - public void postinit(FMLPostInitializationEvent event) {} - - @EventHandler - public void serverStart(FMLServerStartingEvent event) { - MinecraftServer mcserver = event.getServer(); - // Register commands - ((ServerCommandManager) mcserver.getCommandManager()).registerCommand(new CommandCreateView()); - // Start up the packet dispatcher we use for throttled data to client. - ServerPacketDispatcher.getInstance().start(); //Note: This might need to be preceded by a force init of the ServerPacketDispatcher. Doesn't seem to currently have any issues, though. - } - - @EventHandler - public void serverStop(FMLServerStoppedEvent event) { - // Shutdown our throttled packet dispatcher - ServerPacketDispatcher.getInstance().halt(); - ServerPacketDispatcher.shutdown(); - } + public static final String MODID = "lookingglass"; + public static final String VERSION = "@VERSION@"; + + @Mod.Instance(LookingGlass.MODID) + public static LookingGlass instance; + + @SidedProxy( + clientSide = "com.xcompwiz.lookingglass.client.ClientProxy", + serverSide = "com.xcompwiz.lookingglass.core.CommonProxy" + ) + public static CommonProxy sidedProxy; + + private static Logger logger; + public static Logger logger() { + return logger; + } + + @EventHandler + public void preInit(FMLPreInitializationEvent event) { + logger = event.getModLog(); + + LookingGlassPacketManager.registerPacketHandler(new PacketCreateView(), (byte) 10); + LookingGlassPacketManager.registerPacketHandler(new PacketCloseView(), (byte) 11); + LookingGlassPacketManager.registerPacketHandler(new PacketWorldInfo(), (byte) 100); + LookingGlassPacketManager.registerPacketHandler(new PacketChunkInfo(), (byte) 101); + LookingGlassPacketManager.registerPacketHandler(new PacketTileEntityNBT(), (byte) 102); + LookingGlassPacketManager.registerPacketHandler(new PacketRequestWorldInfo(), (byte) 200); + LookingGlassPacketManager.registerPacketHandler(new PacketRequestChunk(), (byte) 201); + LookingGlassPacketManager.registerPacketHandler(new PacketRequestTE(), (byte) 202); + + LookingGlassPacketManager.bus = NetworkRegistry.INSTANCE.newEventDrivenChannel(LookingGlassPacketManager.CHANNEL); + LookingGlassPacketManager.bus.register(new LookingGlassPacketManager()); + + ModConfigs.loadConfig(new Configuration(event.getSuggestedConfigurationFile())); + + File configRoot = event.getSuggestedConfigurationFile().getParentFile(); + MinecraftForge.EVENT_BUS.register(new LookingGlassEventHandler(new File(configRoot.getParentFile(), "logs/proxyworlds.log"))); + MinecraftForge.EVENT_BUS.register(new LookingGlassForgeEventHandler()); + + APIProviderImpl.init(); + } + + @EventHandler + public void init(FMLInitializationEvent event) { + sidedProxy.init(); + } + + @EventHandler + public void handleIMC(FMLInterModComms.IMCEvent event) { + ImmutableList messages = event.getMessages(); + IMCHandler.process(messages); + } + + @EventHandler + public void postInit(FMLPostInitializationEvent event) { + + } + + @EventHandler + public void serverStart(FMLServerStartingEvent event) { + MinecraftServer server = event.getServer(); + ((ServerCommandManager)server.getCommandManager()).registerCommand(new CommandCreateView()); + ServerPacketDispatcher.getInstance().start(); + } + + @EventHandler + public void serverStarted(FMLServerStartedEvent event) { + if (ModConfigs.forceLoadAllWorlds) { + for (int dim : DimensionManager.getIDs()) { + DimensionManager.keepDimensionLoaded(dim, true); + } + } + } + + @EventHandler + public void serverStop(FMLServerStoppedEvent event) { + ServerPacketDispatcher.getInstance().halt(); + ServerPacketDispatcher.shutdown(); + } } diff --git a/src/api/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java b/src/main/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java similarity index 62% rename from src/api/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java rename to src/main/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java index 74d1c20..dd55b37 100644 --- a/src/api/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java +++ b/src/main/java/com/xcompwiz/lookingglass/api/APIInstanceProvider.java @@ -1,50 +1,50 @@ -package com.xcompwiz.lookingglass.api; - -import java.util.Map; -import java.util.Set; - -/** - * The purpose of this interface is simply to provide instances of the API interfaces. Request an instance via IMC (see below). This interface supports - * providing multiple versions of an API, so you can request ex. 'symbol-1' and always get the same interface, enabling the API to move forward without breaking - * compatibility. The provider instance provided to you, as well as any API interface instances created by it, will belong to the mod which requested the - * provider. - * @author xcompwiz - */ -public interface APIInstanceProvider { - - /** - * Returns a constructed version of the requested API. If the API requested doesn't exist then an exception will be thrown. Be wary when attempting to cast - * the returned instance, as, if you try using an interface not included in the class path, you may get missing definition crashes. It is wiser to, after - * verifying success, pass the object to another class dedicated to handling the casting. - * @param api The name of the API and version desired, formatted as ex. "symbol-1". - * @return The requested API instance as an Object. If you get an object, it is guaranteed to be of the requested API and version. - */ - public Object getAPIInstance(String api) throws APIUndefined, APIVersionUndefined, APIVersionRemoved; - - /** - * Returns a collection of all available APIs by name and what versions are supported in this environment. - * @return A map of API names and their available versions - */ - public Map> getAvailableAPIs(); - -//@formatter:off - /* Example Usage - In order to get an instance of this class, send an IMC message to LookingGlass with the key "API" and a string value which is a static method (with classpath) - which takes an instance of APIInstanceProvider as it's only param. - Example: FMLInterModComms.sendMessage("LookingGlass", "API", "com.xcompwiz.newmod.integration.lookingglass.register"); - - public static void register(APIInstanceProvider provider) { - try { - Object apiinst = provider.getAPIInstance("awesomeAPI-3"); - OtherClass.apiGet(apiinst); //At this point, we've got an object of the right interface. - } catch (APIUndefined e) { - // The API we requested doesn't exist. Give up with a nice log message. - } catch (APIVersionUndefined e) { - // The API we requested exists, but the version we wanted is missing in the local environment. We can try falling back to an older version. - } catch (APIVersionRemoved e) { - // The API we requested exists, but the version we wanted has been removed and is no longer supported. Better update. - } - } - */ -//formatter:on -} +package com.xcompwiz.lookingglass.api; + +import java.util.Map; +import java.util.Set; + +/** + * The purpose of this interface is simply to provide instances of the API interfaces. Request an instance via IMC (see below). This interface supports + * providing multiple versions of an API, so you can request ex. 'symbol-1' and always get the same interface, enabling the API to move forward without breaking + * compatibility. The provider instance provided to you, as well as any API interface instances created by it, will belong to the mod which requested the + * provider. + * @author xcompwiz + */ +public interface APIInstanceProvider { + + /** + * Returns a constructed version of the requested API. If the API requested doesn't exist then an exception will be thrown. Be wary when attempting to cast + * the returned instance, as, if you try using an interface not included in the class path, you may get missing definition crashes. It is wiser to, after + * verifying success, pass the object to another class dedicated to handling the casting. + * @param api The name of the API and version desired, formatted as ex. "symbol-1". + * @return The requested API instance as an Object. If you get an object, it is guaranteed to be of the requested API and version. + */ + public Object getAPIInstance(String api) throws APIUndefined, APIVersionUndefined, APIVersionRemoved; + + /** + * Returns a collection of all available APIs by name and what versions are supported in this environment. + * @return A map of API names and their available versions + */ + public Map> getAvailableAPIs(); + + //@formatter:off + /* Example Usage + In order to get an instance of this class, send an IMC message to LookingGlass with the key "API" and a string value which is a static method (with classpath) + which takes an instance of APIInstanceProvider as it's only param. + Example: FMLInterModComms.sendMessage("LookingGlass", "API", "com.xcompwiz.newmod.integration.lookingglass.register"); + + public static void register(APIInstanceProvider provider) { + try { + Object apiinst = provider.getAPIInstance("awesomeAPI-3"); + OtherClass.apiGet(apiinst); //At this point, we've got an object of the right interface. + } catch (APIUndefined e) { + // The API we requested doesn't exist. Give up with a nice log message. + } catch (APIVersionUndefined e) { + // The API we requested exists, but the version we wanted is missing in the local environment. We can try falling back to an older version. + } catch (APIVersionRemoved e) { + // The API we requested exists, but the version we wanted has been removed and is no longer supported. Better update. + } + } + */ +//formatter:on +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/APIUndefined.java b/src/main/java/com/xcompwiz/lookingglass/api/APIUndefined.java new file mode 100644 index 0000000..f53ce53 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/APIUndefined.java @@ -0,0 +1,9 @@ +package com.xcompwiz.lookingglass.api; + +public class APIUndefined extends Exception { + private static final long serialVersionUID = 7033833326135545759L; + + public APIUndefined(String message) { + super(message); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java b/src/main/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java new file mode 100644 index 0000000..b49bfde --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/APIVersionRemoved.java @@ -0,0 +1,9 @@ +package com.xcompwiz.lookingglass.api; + +public class APIVersionRemoved extends Exception { + private static final long serialVersionUID = -7702376017254522430L; + + public APIVersionRemoved(String message) { + super(message); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java b/src/main/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java new file mode 100644 index 0000000..aa7f89b --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/APIVersionUndefined.java @@ -0,0 +1,9 @@ +package com.xcompwiz.lookingglass.api; + +public class APIVersionUndefined extends Exception { + private static final long serialVersionUID = -6195164554156957083L; + + public APIVersionUndefined(String message) { + super(message); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java b/src/main/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java new file mode 100644 index 0000000..d056b9c --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/IWorldViewAPI.java @@ -0,0 +1,12 @@ +package com.xcompwiz.lookingglass.api; + +import com.xcompwiz.lookingglass.api.view.IWorldView; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@Deprecated +public interface IWorldViewAPI { + @SideOnly(Side.CLIENT) + IWorldView createWorldView(Integer dimensionID, BlockPos pos, int width, int height); +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java b/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java new file mode 100644 index 0000000..7ed81b0 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPivot.java @@ -0,0 +1,166 @@ +package com.xcompwiz.lookingglass.api.animator; + +import com.xcompwiz.lookingglass.api.view.IViewCamera; +import net.minecraft.block.state.IBlockState; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; + +public class CameraAnimatorPivot implements ICameraAnimator { + private static final int[][] presets = {{2, 5}, {5, 9}, {9, 15}, {1, 3}, {2, 1}, {0, 2}}; + private static final int[] defaults = {1, 3}; + + private final IViewCamera camera; + private BlockPos target; + + private boolean positionSet = false; + + private int xCenter, yCenter, zCenter; + private int yUp = 0; + private int radius = 0; + private float pitch = 0.0F; + + private final BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(); + + public CameraAnimatorPivot(IViewCamera camera) { + this.camera = camera; + } + + @Override + public void setTarget(BlockPos target) { + this.target = target; + this.positionSet = false; + } + + @Override + public void update(long dt) { + if (this.camera == null) return; + this.camera.addRotations(dt*0.1F, 0); + this.camera.setPitch(-this.pitch); + + double x = Math.cos(Math.toRadians(this.camera.getYaw() + 90)) * this.radius; + double z = Math.sin(Math.toRadians(this.camera.getYaw() + 90)) * this.radius; + this.camera.setLocation(this.xCenter + 0.5 - x, this.yCenter - 0.5 + this.yUp, this.zCenter + 0.5 - z); + } + + @Override + public void refresh() { + if (this.camera == null) return; + if (this.target == null) return; + if (!this.positionSet) this.checkCameraY(); + + int chunkX = this.xCenter >> 4; + int chunkY = this.yCenter >> 4; + int chunkZ = this.zCenter >> 4; + + int[][] presets = this.getPresets(); + for (int i = 0; i < presets.length; i++) { + if (this.checkPath(presets[i][0], presets[i][1], chunkX, chunkY, chunkZ)) { + this.yUp = presets[i][0]; + this.radius = presets[i][1]; + this.pitch = (float) Math.toDegrees(Math.atan((double) -this.yUp / this.radius)); + return; + } + } + int[] defaults = this.getDefaults(); + this.yUp = defaults[0]; + this.radius = defaults[1]; + this.pitch = (float) Math.toDegrees(Math.atan((double) -this.yUp / this.radius)); + } + + public int[] getDefaults() { + return defaults; + } + + public int[][] getPresets() { + return presets; + } + + private boolean checkPath(int up, int distance, int chunkX, int chunkY, int chunkZ) { + if ((this.yCenter & 15) > 15 - up) { + if (isAboveNullLayer(chunkX, chunkY, chunkZ)) return false; + if ((this.xCenter & 15) < distance) { + if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ)) return false; + if ((this.zCenter & 15) < distance) { + if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ - 1)) return false; + if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; + } else if ((this.zCenter & 15) > 15 - distance) { + if (isAboveNullLayer(chunkX - 1, chunkY, chunkZ + 1)) return false; + if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; + } + } else if ((this.xCenter & 15) > 15 - distance) { + if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ)) return false; + if ((this.zCenter & 15) < distance) { + if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ - 1)) return false; + if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; + } else if ((this.zCenter & 15) > 15 - distance) { + if (isAboveNullLayer(chunkX + 1, chunkY, chunkZ + 1)) return false; + if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; + } + } else { + if ((this.zCenter & 15) < distance) { + if (isAboveNullLayer(chunkX, chunkY, chunkZ - 1)) return false; + } else if ((this.zCenter & 15) > 15 - distance) { + if (isAboveNullLayer(chunkX, chunkY, chunkZ + 1)) return false; + } + } + } + for (int j = -distance; j <= distance; ++j) { + for (int k = -distance; k <= distance; ++k) { + if (!this.camera.getBlockData() + .isAirBlock(this.mutable.setPos(this.xCenter + j, this.yCenter + up, this.zCenter + k))) { + return false; + } + } + } + return true; + } + + private boolean isAboveNullLayer(int x, int y, int z) { + if (y + 1 > 15) return true; + int x2 = x << 4; + int z2 = z << 4; + int y2 = (y << 4) + 15; + int yl = (y + 1) << 4; + for (int i = 0; i < 15; i++) + for (int j = 0; j < 15; j++) + if (!this.isBlockNormalCube(this.camera.getBlockData(), this.mutable.setPos(x2 + i, y2, z2 + i))) return false; + return this.camera.chunkLevelsExist(x, z, yl, yl + 15); + } + + private boolean isBlockNormalCube(IBlockAccess world, BlockPos pos) { + IBlockState state = world.getBlockState(pos); + return state.isNormalCube(); + } + + private void checkCameraY() { + int x = this.target.getX(); + int y = this.target.getY(); + int z = this.target.getZ(); + int yBackup = y; + if (this.camera.chunkExists(x, z)) { + IBlockAccess world = this.camera.getBlockData(); + IBlockState state = world.getBlockState(this.mutable.setPos(x, y, z)); + if (state.getCollisionBoundingBox(world, this.mutable) != null) { + do { + state = world.getBlockState(this.mutable.setPos(x, --y, z)); + } while (y > 0 && state.getCollisionBoundingBox(world, this.mutable) != null); + if (y == 0) y = yBackup; + else y += 2; + } else { + do { + state = world.getBlockState(this.mutable.setPos(x, ++y, z)); + } while (y < 256 && state.getCollisionBoundingBox(world, this.mutable) == null); + if (y == 256) y = yBackup; + else ++y; + } + this.setCenterPoint(x, y, z); + } + } + + private void setCenterPoint(int x, int y, int z) { + this.xCenter = x; + this.yCenter = y; + this.zCenter = z; + this.positionSet = true; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java b/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java new file mode 100644 index 0000000..f49e099 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/animator/CameraAnimatorPlayer.java @@ -0,0 +1,81 @@ +package com.xcompwiz.lookingglass.api.animator; + +import com.xcompwiz.lookingglass.api.view.IViewCamera; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; + +public class CameraAnimatorPlayer implements ICameraAnimator { + private final IViewCamera camera; + + private Entity reference; + private Entity player; + private BlockPos target; + private boolean updateY; + private float accumulate; + + public CameraAnimatorPlayer(IViewCamera camera, Entity reference, Entity player) { + this.camera = camera; + this.reference = reference; + this.player = player; + } + + @Override + public void setTarget(BlockPos target) { + this.target = target.toImmutable(); + } + + @Override + public void refresh() { + this.updateY = true; + } + + @Override + public void update(long dt) { + if (this.reference.world.provider.getDimension() != this.player.world.provider.getDimension()) return; + + if ((this.accumulate += dt) >= 10000) { + this.updateY = true; + this.accumulate -= 10000; + } + if (this.updateY) this.updateTargetPosition(); + double dx = this.player.posX - this.reference.posY; + double dy = this.player.posY - (this.reference.posY + this.player.getYOffset()); + double dz = this.player.posZ - this.reference.posZ; + double length = Math.sqrt(dx * dx + dy * dy + dz * dz); + float yaw = -(float) Math.atan2(dx, dz); + yaw *= (float) (180 / Math.PI); + float pitch = (float) Math.asin(dy / length); + pitch *= (float) (180 / Math.PI); + this.camera.setLocation(this.target.getX(), this.target.getY(), this.target.getZ()); + this.camera.setYaw(yaw); + this.camera.setPitch(pitch); + } + + private void updateTargetPosition() { + this.updateY = false; + int x = this.target.getX(); + int y = this.target.getY(); + int z = this.target.getZ(); + if (!this.camera.chunkExists(x, z)) { + BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(this.target); + IBlockAccess world = this.camera.getBlockData(); + IBlockState state = world.getBlockState(this.target); + if (state.getCollisionBoundingBox(world, mutable) != null) { + do { + state = world.getBlockState(mutable.setPos(x, --y, z)); + } while (y > 0 && state.getCollisionBoundingBox(world, mutable) != null); + if (y == 0) y = this.target.getY(); + else y += 2; + } else { + do { + state = world.getBlockState(mutable.setPos(x, ++y, z)); + } while (y < 256 && state.getCollisionBoundingBox(world, mutable) == null); + if (y == 256) y = this.target.getY(); + else ++y; + } + this.target = new BlockPos(x, y, z); + } + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java b/src/main/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java new file mode 100644 index 0000000..c4b1210 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/animator/ICameraAnimator.java @@ -0,0 +1,9 @@ +package com.xcompwiz.lookingglass.api.animator; + +import net.minecraft.util.math.BlockPos; + +public interface ICameraAnimator { + void setTarget(BlockPos target); + void refresh(); + void update(long dt); +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java b/src/main/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java new file mode 100644 index 0000000..f683f34 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/event/ClientWorldInfoEvent.java @@ -0,0 +1,14 @@ +package com.xcompwiz.lookingglass.api.event; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.fml.common.eventhandler.Event; + +public class ClientWorldInfoEvent extends Event { + public final int dimension; + public final EntityPlayerMP player; + + public ClientWorldInfoEvent(int dimension, EntityPlayerMP player) { + this.dimension = dimension; + this.player = player; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java b/src/main/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java new file mode 100644 index 0000000..20d962f --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/hook/WorldViewAPI2.java @@ -0,0 +1,30 @@ +package com.xcompwiz.lookingglass.api.hook; + +import com.xcompwiz.lookingglass.api.view.IWorldView; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * Available via "view-2" from the API provider + * @author xcompwiz + */ +public interface WorldViewAPI2 { + /** + * Creates a world viewer object which will handle the rendering and retrieval of the remote location. Can return null. + * @param dimensionID The target dimension + * @param pos The coordinates of the target location. If null, world spawn is used. + * @param width Texture resolution width + * @param height Texture resolution height + * @return A IWorldView object for your use or null if something goes wrong. + */ + @SideOnly(Side.CLIENT) + IWorldView createWorldView(Integer dimensionID, BlockPos pos, int width, int height); + + /** + * This function is available should you wish to explicitly have the world view clean up its framebuffer. You should not use a view after calling this on + * the view. + * @param view The view to clean up (effectively "destroy") + */ + void cleanupWorldView(IWorldView view); +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java b/src/main/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java new file mode 100644 index 0000000..0bce26f --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/view/IViewCamera.java @@ -0,0 +1,20 @@ +package com.xcompwiz.lookingglass.api.view; + +import net.minecraft.world.IBlockAccess; + +public interface IViewCamera { + void addRotations(float yaw, int pitch); + void setYaw(float f); + float getYaw(); + void setPitch(float f); + float getPitch(); + + void setLocation(double x, double y, double z); + double getX(); + double getY(); + double getZ(); + + IBlockAccess getBlockData(); + boolean chunkExists(int x, int z); + boolean chunkLevelsExist(int x, int z, int yl1, int yl2); +} diff --git a/src/main/java/com/xcompwiz/lookingglass/api/view/IWorldView.java b/src/main/java/com/xcompwiz/lookingglass/api/view/IWorldView.java new file mode 100644 index 0000000..8eeb1f0 --- /dev/null +++ b/src/main/java/com/xcompwiz/lookingglass/api/view/IWorldView.java @@ -0,0 +1,15 @@ +package com.xcompwiz.lookingglass.api.view; + +import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; + +public interface IWorldView { + int getTexture(); + void markDirty(); + boolean isReady(); + void setAnimator(ICameraAnimator animator); + IViewCamera getCamera(); + @Deprecated + void grab(); + @Deprecated + boolean release(); +} diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIProviderImpl.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIProviderImpl.java index 8ca9d47..b3003a3 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIProviderImpl.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIProviderImpl.java @@ -1,134 +1,89 @@ -package com.xcompwiz.lookingglass.apiimpl; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import com.xcompwiz.lookingglass.api.APIInstanceProvider; -import com.xcompwiz.lookingglass.api.APIUndefined; -import com.xcompwiz.lookingglass.api.APIVersionRemoved; -import com.xcompwiz.lookingglass.api.APIVersionUndefined; -import com.xcompwiz.lookingglass.log.LoggerUtils; - -/** - * The implementation of the API provider interface. Instances of this class are given to mods requesting an API provider and bound to that mod's name. The - * class also functions as the registration manager for what APIs we have available. - */ -public class APIProviderImpl implements APIInstanceProvider { - private String modname; - - public APIProviderImpl(String modname) { - this.modname = modname; - } - - public String getOwnerMod() { - return modname; - } - - private HashMap instances = new HashMap(); - - // See parent/javadoc for doc - @Override - public Object getAPIInstance(String api) throws APIUndefined, APIVersionUndefined, APIVersionRemoved { - Object ret = instances.get(api); - // First, we check if the API has already been constructed up for this provider - if (ret != null) return ret; - // Get the id and version from the passed in arg - String[] splitName = api.split("-"); - // If we can't get a name and version then we throw APIUndefined. - if (splitName.length != 2) throw new APIUndefined(api); - String apiname = splitName[0]; - int version = Integer.parseInt(splitName[1]); - // Ask the magical constructor to provide us an instance of the API for the specified version. - ret = constructAPIWrapper(modname, apiname, version); - instances.put(api, ret); - return ret; - } - - private static Map> apiCtors; - private static Map> apiVersions; - private static Map> apiVersions_immutable_sets; - private static Map> apiVersions_immutable; - - /** - * This init function sets up the wrapper constructors for the different APIs and versions of APIs we support. - */ - public static void init() { - // Skip if already initialized - if (apiCtors != null) return; - apiCtors = new HashMap>(); - apiVersions = new HashMap>(); - // Immutable views to the internal stuff to allow for the getAvailableAPIs functionality without breaking containment - apiVersions_immutable_sets = new HashMap>(); - apiVersions_immutable = Collections.unmodifiableMap(apiVersions_immutable_sets); - - // Register the APIs we support - registerAPI("view", 1, new WrapperBuilder(LookingGlassAPIWrapper.class)); - registerAPI("view", 2, new WrapperBuilder(LookingGlassAPI2Wrapper.class)); - // Note that removed API versions should be registered as null. - } - - private static void registerAPI(String apiname, int version, WrapperBuilder builder) { - getVersions(apiname).add(version); - getCtors(apiname).put(version, builder); - } - - private static Map getCtors(String apiname) { - Map ctors = apiCtors.get(apiname); - if (ctors == null) { - ctors = new HashMap(); - apiCtors.put(apiname, ctors); - } - return ctors; - } - - private static Set getVersions(String apiname) { - Set versions = apiVersions.get(apiname); - if (versions == null) { - versions = new HashSet(); - apiVersions.put(apiname, versions); - apiVersions_immutable_sets.put(apiname, Collections.unmodifiableSet(versions)); - } - return versions; - } - - /** - * ** This function is voodoo.**
This is the function which actually calls the builders to produce the wrappers which implement the version of the requested API. - * @param owner The name of the mod wanting the API - * @param apiname The name of the API wanted - * @param version The version of the API wanted - * @return An object which is an instance of the interface matching the API version - * @throws APIUndefined The API requested doesn't exist - * @throws APIVersionUndefined The API requested exists, but the version requested is missing in the local environment - * @throws APIVersionRemoved The API requested exists, but the version requested has been removed and is no longer supported - */ - private static Object constructAPIWrapper(String owner, String apiname, int version) throws APIUndefined, APIVersionUndefined, APIVersionRemoved { - // First, check to make sure we initialized before - if (apiCtors == null) throw new RuntimeException("Something is broken. The LookingGlass API Provider hasn't constructed properly."); - // Get the builders for the API we want - Map ctors = apiCtors.get(apiname); - // If there are no builders, then the API doesn't exist - if (ctors == null) throw new APIUndefined(apiname); - // If the builders collection doesn't have an entry for the version we wanted, it never existed - if (!ctors.containsKey(version)) throw new APIVersionUndefined(apiname + "-" + version); - // Get the builder entry - WrapperBuilder ctor = ctors.get(version); - // If the builder is null, then the API has been removed. - if (ctor == null) throw new APIVersionRemoved(apiname + "-" + version); - // Now, the magic itself. Use the builder to produce an instance of the API - try { - return ctor.newInstance(owner); // Poof! - } catch (Exception e) { - // If there are any problems then we need to report them. Theoretically there shouldn't be, but one never knows. - LoggerUtils.error("Caught an exception while building an API wrapper. Go kick XCompWiz."); - throw new RuntimeException("Caught an exception while building an API wrapper. Go kick XCompWiz.", e); - } - } - - @Override - public Map> getAvailableAPIs() { - return apiVersions_immutable; - } -} +package com.xcompwiz.lookingglass.apiimpl; + +import com.xcompwiz.lookingglass.api.APIInstanceProvider; +import com.xcompwiz.lookingglass.api.APIUndefined; +import com.xcompwiz.lookingglass.api.APIVersionRemoved; +import com.xcompwiz.lookingglass.api.APIVersionUndefined; + +import java.util.*; + +public class APIProviderImpl implements APIInstanceProvider { + private final String modname; + + public APIProviderImpl(String modname) { + this.modname = modname; + } + + public String getOwnerMod() { + return this.modname; + } + + private final HashMap instances = new HashMap<>(); + + @Override + public Object getAPIInstance(String api) throws APIUndefined, APIVersionUndefined, APIVersionRemoved { + Object ret = this.instances.get(api); + if (ret != null) return ret; + String[] splitName = api.split("-"); + if (splitName.length != 2) throw new APIUndefined(api); + String apiName = splitName[0]; + int version = Integer.parseInt(splitName[1]); + ret = constructAPIWrapper(this.modname, apiName, version); + instances.put(api, ret); + return ret; + } + + private static Map> apiCtors; + private static Map> apiVersions; + private static Map> apiVersionsImmutableSets; + private static Map> apiVersionsImmutable; + + public static void init() { + if (apiCtors != null) return; + apiCtors = new HashMap<>(); + apiVersions = new HashMap<>(); + apiVersionsImmutableSets = new HashMap<>(); + apiVersionsImmutable = Collections.unmodifiableMap(apiVersions); + + registerAPI("view", 1, new WrapperBuilder(LookingGlassAPIWrapper.class)); + registerAPI("view", 2, new WrapperBuilder(LookingGlassAPI2Wrapper.class)); + } + + private static void registerAPI(String apiName, int version, WrapperBuilder builder) { + getVersions(apiName).add(version); + getCtors(apiName).put(version, builder); + } + + private static Map getCtors(String apiName) { + return apiCtors.computeIfAbsent(apiName, k -> new HashMap<>()); + } + + private static Set getVersions(String apiName) { + Set versions = apiVersions.get(apiName); + if (versions == null) { + versions = new HashSet<>(); + apiVersions.put(apiName, versions); + apiVersionsImmutableSets.put(apiName, Collections.unmodifiableSet(versions)); + } + return versions; + } + + private static Object constructAPIWrapper(String owner, String apiName, int version) throws APIUndefined, APIVersionUndefined, APIVersionRemoved { + if (apiCtors == null) throw new RuntimeException("Something is broken. The LookingGlass API hasn't constructed properly."); + Map ctors = apiCtors.get(apiName); + if (ctors == null) throw new APIUndefined(apiName); + if (!ctors.containsKey(version)) throw new APIVersionUndefined(apiName + "-" + version); + WrapperBuilder ctor = ctors.get(version); + if (ctor == null) throw new APIVersionRemoved(apiName + "-" + version); + try { + return ctor.newInstance(owner); + } catch (Exception e) { + throw new RuntimeException("Caught an exception while building an API wrapper. Go kick XCompWiz. Or Siepert, for that matter.", e); + } + } + + @Override + public Map> getAvailableAPIs() { + return apiVersionsImmutable; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIWrapper.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIWrapper.java index 9b2e1e0..f2b55e2 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIWrapper.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/APIWrapper.java @@ -1,14 +1,13 @@ -package com.xcompwiz.lookingglass.apiimpl; - -public class APIWrapper { - private String modname; - - public APIWrapper(String modname) { - this.modname = modname; - } - - public String getOwnerMod() { - return modname; - } - -} +package com.xcompwiz.lookingglass.apiimpl; + +public class APIWrapper { + private final String modname; + + public APIWrapper(String modname) { + this.modname = modname; + } + + public String getOwnerMod() { + return this.modname; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/InternalAPI.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/InternalAPI.java index e11a8f9..1decc4c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/InternalAPI.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/InternalAPI.java @@ -1,24 +1,13 @@ -package com.xcompwiz.lookingglass.apiimpl; - -import java.util.HashMap; - -import com.xcompwiz.lookingglass.api.APIInstanceProvider; - -/** - * This class simply manages the construction and tracking of the API provider instances. The Mystcraft version was enormously more complex and did lots more, - * but I simplified it here. The class is intentionally not named APIInstanceProviderProvider.... - */ -public class InternalAPI { - - private static HashMap instances = new HashMap(); - - public synchronized static APIInstanceProvider getAPIProviderInstance(String modname) { - APIInstanceProvider instance = instances.get(modname); - if (instance == null) { - instance = new APIProviderImpl(modname); - instances.put(modname, instance); - } - return instance; - } - -} +package com.xcompwiz.lookingglass.apiimpl; + +import com.xcompwiz.lookingglass.api.APIInstanceProvider; + +import java.util.HashMap; + +public class InternalAPI { + private static final HashMap instances = new HashMap<>(); + + public synchronized static APIInstanceProvider getAPIProviderInstance(String modname) { + return instances.computeIfAbsent(modname, APIProviderImpl::new); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPI2Wrapper.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPI2Wrapper.java index f7ae4e8..59e6abb 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPI2Wrapper.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPI2Wrapper.java @@ -1,35 +1,32 @@ package com.xcompwiz.lookingglass.apiimpl; -import net.minecraft.util.ChunkCoordinates; - import com.xcompwiz.lookingglass.api.hook.WorldViewAPI2; import com.xcompwiz.lookingglass.api.view.IWorldView; import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; import com.xcompwiz.lookingglass.client.proxyworld.WorldView; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; /** * This is the API wrapper (instance) class for the WorldView API at version 2. * @author xcompwiz */ public class LookingGlassAPI2Wrapper extends APIWrapper implements WorldViewAPI2 { + public LookingGlassAPI2Wrapper(String modname) { + super(modname); + } - public LookingGlassAPI2Wrapper(String modname) { - super(modname); - } - - @Override - @SideOnly(Side.CLIENT) - public IWorldView createWorldView(Integer dimid, ChunkCoordinates spawn, int width, int height) { - return ProxyWorldManager.createWorldView(dimid, (spawn != null ? new ChunkCoordinates(spawn) : null), width, height); - } + @SideOnly(Side.CLIENT) + @Override + public IWorldView createWorldView(Integer dimensionID, BlockPos pos, int width, int height) { + return ProxyWorldManager.createWorldView(dimensionID, pos.toImmutable(), width, height); + } - @Override - public void cleanupWorldView(IWorldView worldview) { - if (worldview == null) return; - if (!(worldview instanceof WorldView)) throw new RuntimeException("[%s] is misusing the LookingGlass API. Cannot cleanup custom IWorldView objects."); - ProxyWorldManager.destroyWorldView((WorldView) worldview); - } + @Override + public void cleanupWorldView(IWorldView view) { + if (view == null) return; + if (!(view instanceof WorldView)) throw new RuntimeException("[%s] is misusing the LookingGlass API. Cannot cleanup custom IWorldView objects."); + ProxyWorldManager.destroyWorldView((WorldView) view); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPIWrapper.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPIWrapper.java index ad63cea..0f2e97e 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPIWrapper.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/LookingGlassAPIWrapper.java @@ -1,28 +1,25 @@ -package com.xcompwiz.lookingglass.apiimpl; - -import net.minecraft.util.ChunkCoordinates; - -import com.xcompwiz.lookingglass.api.IWorldViewAPI; -import com.xcompwiz.lookingglass.api.view.IWorldView; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * This is the API wrapper (instance) class for the WorldView API at version 1. - * @author xcompwiz - */ -@SuppressWarnings("deprecation") -public class LookingGlassAPIWrapper extends APIWrapper implements IWorldViewAPI { - - public LookingGlassAPIWrapper(String modname) { - super(modname); - } - - @Override - @SideOnly(Side.CLIENT) - public IWorldView createWorldView(Integer dimid, ChunkCoordinates spawn, int width, int height) { - return ProxyWorldManager.createWorldView(dimid, (spawn != null ? new ChunkCoordinates(spawn) : null), width, height); - } -} +package com.xcompwiz.lookingglass.apiimpl; + +import com.xcompwiz.lookingglass.api.IWorldViewAPI; +import com.xcompwiz.lookingglass.api.view.IWorldView; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * This is the API wrapper (instance) class for the WorldView API at version 1. + * @author xcompwiz + */ +@SuppressWarnings("deprecation") +public class LookingGlassAPIWrapper extends APIWrapper implements IWorldViewAPI { + public LookingGlassAPIWrapper(String modname) { + super(modname); + } + + @SideOnly(Side.CLIENT) + @Override + public IWorldView createWorldView(Integer dimensionID, BlockPos pos, int width, int height) { + return ProxyWorldManager.createWorldView(dimensionID, pos.toImmutable(), width, height); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/apiimpl/WrapperBuilder.java b/src/main/java/com/xcompwiz/lookingglass/apiimpl/WrapperBuilder.java index 3354c90..aa570ea 100644 --- a/src/main/java/com/xcompwiz/lookingglass/apiimpl/WrapperBuilder.java +++ b/src/main/java/com/xcompwiz/lookingglass/apiimpl/WrapperBuilder.java @@ -1,31 +1,20 @@ -package com.xcompwiz.lookingglass.apiimpl; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -/** - * This is a bit of a magic class. We use it to build API instances (API wrappers). It requires that the target class have a constructor which takes a string. - * It passes the API instance "owner" to the constructor when building the wrapper instance. Basically a cheap and easy class factory. - */ -public class WrapperBuilder { - - private final Constructor itemCtor; - - public WrapperBuilder(Class clazz) { - try { - itemCtor = clazz.getConstructor(String.class); - } catch (Exception e) { - throw new RuntimeException("LookingGlass has derped.", e); - } - } - - /** - * Called by the APIProviderImpl to construct the API wrapper passed to it on its construction. - * @param owner - * @return The instance - */ - public Object newInstance(String owner) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - return itemCtor.newInstance(owner); - } - -} +package com.xcompwiz.lookingglass.apiimpl; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +public class WrapperBuilder { + private final Constructor itemCtor; + + public WrapperBuilder(Class clazz) { + try { + this.itemCtor = clazz.getConstructor(String.class); + } catch (Exception e) { + throw new RuntimeException("LookingGlass has derped.", e); + } + } + + public Object newInstance(String owner) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { + return this.itemCtor.newInstance(owner); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/client/ClientProxy.java b/src/main/java/com/xcompwiz/lookingglass/client/ClientProxy.java index 4e6a3b2..760e95f 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/ClientProxy.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/ClientProxy.java @@ -1,31 +1,20 @@ -package com.xcompwiz.lookingglass.client; - -import net.minecraft.client.renderer.entity.Render; -import net.minecraft.client.renderer.entity.RenderManager; - -import com.xcompwiz.lookingglass.client.render.RenderPortal; -import com.xcompwiz.lookingglass.core.CommonProxy; -import com.xcompwiz.lookingglass.entity.EntityPortal; - -import cpw.mods.fml.client.registry.RenderingRegistry; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * Our faithful proxy class. Allows for running code differently dependent on whether we are client- or server-side. - */ -@SideOnly(Side.CLIENT) -public class ClientProxy extends CommonProxy { - - /** - * Run during mod init. - */ - @Override - public void init() { - // We register the portal renderer here - Render render; - render = new RenderPortal(); - render.setRenderManager(RenderManager.instance); - RenderingRegistry.registerEntityRenderingHandler(EntityPortal.class, render); - } -} +package com.xcompwiz.lookingglass.client; + +import com.xcompwiz.lookingglass.client.render.RenderPortal; +import com.xcompwiz.lookingglass.core.CommonProxy; +import com.xcompwiz.lookingglass.entity.EntityPortal; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.entity.Render; +import net.minecraftforge.fml.client.registry.RenderingRegistry; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class ClientProxy extends CommonProxy { + @Override + public void init() { + Render render; + render = new RenderPortal(Minecraft.getMinecraft().getRenderManager()); + RenderingRegistry.registerEntityRenderingHandler(EntityPortal.class, render); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorld.java b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorld.java index 6a6e367..bff6d1c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorld.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorld.java @@ -1,28 +1,83 @@ package com.xcompwiz.lookingglass.client.proxyworld; +import mcp.MethodsReturnNonnullByDefault; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; -import net.minecraft.client.particle.EntityFireworkStarterFX; +import net.minecraft.client.particle.ParticleFirework; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvent; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.GameType; import net.minecraft.world.WorldSettings; -import net.minecraft.world.WorldSettings.GameType; import net.minecraft.world.WorldType; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; -// FIXME: AAHH! Fake world classes! EXTERMINATE! +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +@SideOnly(Side.CLIENT) +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault public class ProxyWorld extends WorldClient { - public ProxyWorld(int dimensionID) { - super(Minecraft.getMinecraft().getNetHandler(), new WorldSettings(0L, GameType.SURVIVAL, true, false, WorldType.DEFAULT), dimensionID, Minecraft.getMinecraft().gameSettings.difficulty, Minecraft.getMinecraft().theWorld.theProfiler); - } - - // TODO: In order to eliminate this class we may need an event in this function to allow canceling/redirecting sounds - @Override - public void playSound(double par1, double par3, double par5, String par7Str, float par8, float par9, boolean par10) {} - - // TODO: In order to eliminate this class we need to create a redirection wrapper class for the mc.effectRenderer which does this for all views. - @Override - public void makeFireworks(double par1, double par3, double par5, double par7, double par9, double par11, NBTTagCompound par13NBTTagCompound) { - for (WorldView activeview : ProxyWorldManager.getWorldViews(this.provider.dimensionId)) { - activeview.getEffectRenderer().addEffect(new EntityFireworkStarterFX(this, par1, par3, par5, par7, par9, par11, activeview.getEffectRenderer(), par13NBTTagCompound)); - } - } + public ProxyWorld(int dimensionID) { + super(Minecraft.getMinecraft().getConnection(), + new WorldSettings(0L, GameType.SURVIVAL, true, false, WorldType.DEFAULT), + dimensionID, + Minecraft.getMinecraft().gameSettings.difficulty, + Minecraft.getMinecraft().world.profiler + ); + } + + @Override + public void playSound(double x, double y, double z, + SoundEvent soundIn, SoundCategory category, float volume, float pitch, boolean distanceDelay) { + + } + + @Override + public void playSound(@Nullable EntityPlayer player, BlockPos pos, + SoundEvent soundIn, SoundCategory category, float volume, float pitch) { + + } + + @Override + public void playSound(BlockPos pos, + SoundEvent soundIn, SoundCategory category, float volume, float pitch, boolean distanceDelay) { + + } + + @Override + public void playSound(@Nullable EntityPlayer player, double x, double y, double z, + SoundEvent soundIn, SoundCategory category, float volume, float pitch) { + + } + + @Override + public void playBroadcastSound(int id, BlockPos pos, int data) { + + } + + @Override + public void playEvent(int type, BlockPos pos, int data) { + + } + + @Override + public void playEvent(@Nullable EntityPlayer player, int type, BlockPos pos, int data) { + + } + + @Override + public void makeFireworks(double x, double y, double z, double motionX, double motionY, double motionZ, @Nullable NBTTagCompound compound) { + for (WorldView activeView : ProxyWorldManager.getWorldViews(this.provider.getDimension())) { + activeView.getEffectRenderer().addEffect( + new ParticleFirework.Starter( + this, x, y, z, motionX, motionY, motionZ, activeView.getEffectRenderer(), compound + ) + ); + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorldManager.java b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorldManager.java index 8534135..6c4e8af 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorldManager.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ProxyWorldManager.java @@ -1,144 +1,118 @@ -package com.xcompwiz.lookingglass.client.proxyworld; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.WeakHashMap; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.WorldClient; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.util.ChunkCoordinates; -import net.minecraftforge.common.DimensionManager; - -import com.xcompwiz.lookingglass.client.render.FrameBufferContainer; -import com.xcompwiz.lookingglass.entity.EntityCamera; -import com.xcompwiz.lookingglass.log.LoggerUtils; -import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; -import com.xcompwiz.lookingglass.network.packet.PacketCreateView; -import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -@SideOnly(Side.CLIENT) -public class ProxyWorldManager { - private static Map proxyworlds = new HashMap(); - private static Collection proxyworldset = Collections.unmodifiableCollection(proxyworlds.values()); - /** We actually populate this with weak sets. This allows for the world views to be freed without us needing to do anything. */ - private static Map> worldviewsets = new HashMap>(); - - /** - * This is a complex bit. As we want to reuse the current client world when rendering, if possible, we need to handle when that world changes. We could - * simply destroy all the views pointing to the existing proxy world, but that would be annoying to mods using the API. Instead, we replace our proxy world - * with the new client world. This should only be called by LookingGlass, and only from the handling of the client world change detection. - * @param world The new client world - */ - public static void handleWorldChange(WorldClient world) { - if (ModConfigs.disabled) return; - if (world == null) return; - int dimid = world.provider.dimensionId; - if (!proxyworlds.containsKey(dimid)) return; //BEST CASE! We don't have to do anything! - proxyworlds.put(dimid, world); - Collection worldviews = worldviewsets.get(dimid); - for (WorldView view : worldviews) { - // Handle the change on the view object - view.replaceWorldObject(world); - } - } - - public static synchronized void detectFreedWorldViews() { - FrameBufferContainer.detectFreedWorldViews(); - //TODO: closeViewConnection(worldviewID); - HashSet emptyLists = new HashSet(); - for (Map.Entry> entry : worldviewsets.entrySet()) { - if (entry.getValue().isEmpty()) emptyLists.add(entry.getKey()); - } - for (Integer dimId : emptyLists) { - unloadProxyWorld(dimId); - } - } - - public static synchronized WorldClient getProxyworld(int dimid) { - if (ModConfigs.disabled) return null; - WorldClient proxyworld = proxyworlds.get(dimid); - if (proxyworld == null) { - if (!DimensionManager.isDimensionRegistered(dimid)) return null; - // We really don't want to be doing this during a render cycle - if (Minecraft.getMinecraft().thePlayer instanceof EntityCamera) return null; //TODO: This check probably needs to be altered - WorldClient theWorld = Minecraft.getMinecraft().theWorld; - if (theWorld != null && theWorld.provider.dimensionId == dimid) proxyworld = theWorld; - if (proxyworld == null) proxyworld = new ProxyWorld(dimid); - proxyworlds.put(dimid, proxyworld); - worldviewsets.put(dimid, Collections.newSetFromMap(new WeakHashMap())); - } - return proxyworld; - } - - private static void unloadProxyWorld(int dimId) { - Collection set = worldviewsets.remove(dimId); - if (set != null && set.size() > 0) LoggerUtils.warn("Unloading ProxyWorld with live views"); - WorldClient proxyworld = proxyworlds.remove(dimId); - WorldClient theWorld = Minecraft.getMinecraft().theWorld; - if (theWorld != null && theWorld == proxyworld) return; - if (proxyworld != null) net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.world.WorldEvent.Unload(proxyworld)); - } - - public static void clearProxyworlds() { - while (!proxyworlds.isEmpty()) { - unloadProxyWorld(proxyworlds.keySet().iterator().next()); - } - } - - public static Collection getProxyworlds() { - return proxyworldset; - } - - public static Collection getWorldViews(int dimid) { - Collection set = worldviewsets.get(dimid); - if (set == null) return Collections.emptySet(); - return Collections.unmodifiableCollection(set); - } - - public static WorldView createWorldView(int dimid, ChunkCoordinates spawn, int width, int height) { - if (ModConfigs.disabled) return null; - if (!DimensionManager.isDimensionRegistered(dimid)) return null; - - WorldClient proxyworld = ProxyWorldManager.getProxyworld(dimid); - if (proxyworld == null) return null; - - Collection worldviews = worldviewsets.get(dimid); - if (worldviews == null) return null; - - WorldView view = new WorldView(proxyworld, spawn, width, height); - - // Initialize the view rendering system - Minecraft mc = Minecraft.getMinecraft(); - EntityLivingBase backup = mc.renderViewEntity; - mc.renderViewEntity = view.camera; - view.getRenderGlobal().setWorldAndLoadRenderers(proxyworld); - mc.renderViewEntity = backup; - - // Inform the server of the new view - LookingGlassPacketManager.bus.sendToServer(PacketCreateView.createPacket(view)); - worldviews.add(view); - return view; - } - - //TODO: private static void closeViewConnection(long worldviewID) { - //LookingGlassPacketManager.bus.sendToServer(PacketCloseView.createPacket(worldviewID)); - //} - - /** - * Handles explicit shutdown of a world view. Tells the view to clean itself up and removes it from the tracked world views here (encouraging the world to unload). - * @param view The view to kill - */ - public static void destroyWorldView(WorldView view) { - Collection set = worldviewsets.get(view.getWorldObj().provider.dimensionId); - if (set != null) set.remove(view); - //TODO: closeViewConnection(worldviewID); - view.cleanup(); - } -} +package com.xcompwiz.lookingglass.client.proxyworld; + +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.render.FrameBufferContainer; +import com.xcompwiz.lookingglass.entity.EntityCamera; +import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; +import com.xcompwiz.lookingglass.network.packet.PacketCreateView; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import java.util.*; + +@SideOnly(Side.CLIENT) +public class ProxyWorldManager { + private static Map proxyWorlds = new HashMap<>(); + private static Collection proxyWorldSet = Collections.unmodifiableCollection(proxyWorlds.values()); + private static Map> worldViewSets = new HashMap<>(); + + public static void handleWorldChange(WorldClient world) { + if (ModConfigs.disabled) return; + if (world == null) return; + int dimID = world.provider.getDimension(); + if (!proxyWorlds.containsKey(dimID)) return; + proxyWorlds.put(dimID, world); + Collection worldViews = worldViewSets.get(dimID); + for (WorldView view : worldViews) { + view.replaceWorld(world); + } + } + + public static synchronized void detectFreedWorldViews() { + FrameBufferContainer.detectFreedWorldViews(); + HashSet emptyLists = new HashSet<>(); + for (Map.Entry> entry : worldViewSets.entrySet()) { + if (entry.getValue().isEmpty()) emptyLists.add(entry.getKey()); + } + for (Integer dimID : emptyLists) { + unloadProxyWorld(dimID); + } + } + + public static synchronized WorldClient getProxyWorld(int dimID) { + if (ModConfigs.disabled) return null; + WorldClient proxyWorld = proxyWorlds.get(dimID); + if (proxyWorld == null) { + if (!DimensionManager.isDimensionRegistered(dimID)) return null; + if (Minecraft.getMinecraft().player instanceof EntityCamera) return null; + WorldClient world = Minecraft.getMinecraft().world; + if (world != null && world.provider.getDimension() == dimID) proxyWorld = world; + if (proxyWorld == null) proxyWorld = new ProxyWorld(dimID); + proxyWorlds.put(dimID, proxyWorld); + worldViewSets.put(dimID, Collections.newSetFromMap(new WeakHashMap<>())); + } + return proxyWorld; + } + + private static synchronized void unloadProxyWorld(int dimID) { + Collection set = worldViewSets.remove(dimID); + if (set != null && !set.isEmpty()) LookingGlass.logger().warn("Unloading ProxyWorld with live views"); + WorldClient proxyWorld = proxyWorlds.remove(dimID); + WorldClient world = Minecraft.getMinecraft().world; + if (world != null && world == proxyWorld) return; + if (proxyWorld != null) MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(proxyWorld)); + } + + public static void clearProxyWorlds() { + while (!proxyWorlds.isEmpty()) { + unloadProxyWorld(proxyWorlds.keySet().iterator().next()); + } + } + + public static Collection getProxyWorlds() { + return proxyWorldSet; + } + + public static Collection getWorldViews(int dimID) { + Collection set = worldViewSets.get(dimID); + if (set == null) return Collections.emptySet(); + return set; + } + + public static synchronized WorldView createWorldView(int dimID, BlockPos spawn, int width, int height) { + if (ModConfigs.disabled) return null; + if (!DimensionManager.isDimensionRegistered(dimID)) return null; + + WorldClient proxyWorld = ProxyWorldManager.getProxyWorld(dimID); + if (proxyWorld == null) return null; + + Collection worldViews = worldViewSets.get(dimID); + if (worldViews == null) return null; + + WorldView view = new WorldView(proxyWorld, spawn, width, height); + + Minecraft mc = Minecraft.getMinecraft(); + Entity backup = mc.getRenderViewEntity(); + mc.setRenderViewEntity(view.camera); + view.getRenderGlobal().setWorldAndLoadRenderers(proxyWorld); + mc.setRenderViewEntity(backup); + + LookingGlassPacketManager.bus.sendToServer(PacketCreateView.createPacket(view)); + worldViews.add(view); + return view; + } + + public static synchronized void destroyWorldView(WorldView view) { + Collection set = worldViewSets.get(view.getWorld().provider.getDimension()); + if (set != null) set.remove(view); + view.cleanup(); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ViewCameraImpl.java b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ViewCameraImpl.java index 2c9c5c7..7faef3c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ViewCameraImpl.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/ViewCameraImpl.java @@ -1,77 +1,77 @@ package com.xcompwiz.lookingglass.client.proxyworld; -import net.minecraft.world.IBlockAccess; - import com.xcompwiz.lookingglass.api.view.IViewCamera; import com.xcompwiz.lookingglass.entity.EntityCamera; +import net.minecraft.world.IBlockAccess; public class ViewCameraImpl implements IViewCamera { - private EntityCamera camera; - - public ViewCameraImpl(EntityCamera camera) { - this.camera = camera; - } - - @Override - public void addRotations(float yaw, int pitch) { - this.camera.setAngles(yaw, pitch); - } - - @Override - public void setYaw(float f) { - this.camera.prevRotationYaw = f; - this.camera.rotationYaw = f; - } - - @Override - public float getYaw() { - return this.camera.rotationYaw; - } - - @Override - public void setPitch(float f) { - this.camera.prevRotationPitch = f; - this.camera.rotationPitch = f; - } - - @Override - public float getPitch() { - return this.camera.rotationPitch; - } - - @Override - public void setLocation(double x, double y, double z) { - this.camera.setLocationAndAngles(x, y, z, this.camera.rotationYaw, this.camera.rotationPitch); - } - - @Override - public double getX() { - return this.camera.posX; - } - - @Override - public double getY() { - return this.camera.posY; - } - - @Override - public double getZ() { - return this.camera.posZ; - } - - @Override - public IBlockAccess getBlockData() { - return this.camera.worldObj; - } - - @Override - public boolean chunkExists(int x, int z) { - return !camera.worldObj.getChunkFromBlockCoords(x, z).isEmpty(); - } - - @Override - public boolean chunkLevelsExist(int x, int z, int yl1, int yl2) { - return !camera.worldObj.getChunkFromBlockCoords(x, z).getAreLevelsEmpty(yl1, yl2); - } - + private EntityCamera camera; + + public ViewCameraImpl(EntityCamera camera) { + this.camera = camera; + } + + @Override + public void addRotations(float yaw, int pitch) { + this.camera.rotationYaw = (this.camera.rotationYaw + yaw) % 360.0F; + this.camera.rotationPitch -= pitch; + this.camera.rotationPitch = Math.max(-90, Math.min(90, this.camera.rotationPitch)); + this.camera.prevRotationYaw = this.camera.rotationYaw; + this.camera.prevRotationPitch = this.camera.rotationPitch; + } + + @Override + public void setYaw(float f) { + this.camera.prevRotationYaw = this.camera.rotationYaw = f; + } + + @Override + public float getYaw() { + return this.camera.rotationYaw; + } + + @Override + public void setPitch(float f) { + this.camera.prevRotationPitch = this.camera.rotationPitch = f; + } + + @Override + public float getPitch() { + return this.camera.rotationPitch; + } + + @Override + public void setLocation(double x, double y, double z) { + this.camera.setLocationAndAngles(x, y, z, this.camera.rotationYaw, this.camera.rotationPitch); + } + + @Override + public double getX() { + return this.camera.posX; + } + + @Override + public double getY() { + return this.camera.posY; + } + + @Override + public double getZ() { + return this.camera.posZ; + } + + @Override + public IBlockAccess getBlockData() { + return this.camera.world; + } + + @Override + public boolean chunkExists(int x, int z) { + return !this.camera.world.getChunkFromChunkCoords(x >> 4, z >> 4).isEmpty(); + } + + @Override + public boolean chunkLevelsExist(int x, int z, int yl1, int yl2) { + return !this.camera.world.getChunkFromChunkCoords(x >> 4, z >> 4).isEmptyBetween(yl1, yl2); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/WorldView.java b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/WorldView.java index 852d35d..327547c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/WorldView.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/proxyworld/WorldView.java @@ -1,150 +1,139 @@ -package com.xcompwiz.lookingglass.client.proxyworld; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.WorldClient; -import net.minecraft.client.particle.EffectRenderer; -import net.minecraft.client.renderer.RenderGlobal; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.util.MathHelper; - -import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; -import com.xcompwiz.lookingglass.api.view.IViewCamera; -import com.xcompwiz.lookingglass.api.view.IWorldView; -import com.xcompwiz.lookingglass.client.render.FrameBufferContainer; -import com.xcompwiz.lookingglass.entity.EntityCamera; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -@SideOnly(Side.CLIENT) -public class WorldView implements IWorldView { - private WorldClient worldObj; - public final ChunkCoordinates coords; - public final EntityCamera camera; - public final IViewCamera camerawrapper; - - public final int width; - public final int height; - - private boolean update; - private boolean ready; - private boolean hasChunks; - private long last_render_time = -1; - - private RenderGlobal renderGlobal; - private EffectRenderer effectRenderer; - - private FrameBufferContainer fbo; - - public WorldView(WorldClient worldObj, ChunkCoordinates coords, int width, int height) { - this.width = width; - this.height = height; - this.worldObj = worldObj; - this.coords = coords; - this.camera = new EntityCamera(worldObj, coords); - this.camerawrapper = new ViewCameraImpl(camera); - this.renderGlobal = new RenderGlobal(Minecraft.getMinecraft()); - this.effectRenderer = new EffectRenderer(worldObj, Minecraft.getMinecraft().getTextureManager()); - // Technically speaking, this is poor practice as it leaks a reference to the view before it's done constructing. - this.fbo = FrameBufferContainer.createNewFramebuffer(this, width, height); - } - - /** - * Explicitly shuts down the view. Informs the frame buffer manager that we don't want our framebuffer anymore (so it can be cleaned up for certain on the - * next cleanup pass) and kills our fbo reference. The view is no longer usable after this is called. - */ - public void cleanup() { - this.fbo = null; - FrameBufferContainer.removeWorldView(this); - } - - @Override - public boolean isReady() { - return fbo == null ? false : ready; - } - - public boolean hasChunks() { - return fbo == null ? false : hasChunks; - } - - @Override - public void markDirty() { - update = true; - } - - public boolean markClean() { - if (fbo == null) return false; - ready = true; - boolean temp = update; - update = false; - return temp; - } - - public int getFramebuffer() { - return fbo == null ? 0 : fbo.getFramebuffer(); - } - - public RenderGlobal getRenderGlobal() { - return this.renderGlobal; - } - - public EffectRenderer getEffectRenderer() { - return this.effectRenderer; - } - - @Override - public int getTexture() { - return fbo == null ? 0 : fbo.getTexture(); - } - - @Override - public void grab() {} - - @Override - public boolean release() { - return false; - } - - public void onChunkReceived(int cx, int cz) { - this.hasChunks = true; - int cam_cx = MathHelper.floor_double(this.camera.posX) >> 4; - int cam_cz = MathHelper.floor_double(this.camera.posZ) >> 4; - if (cam_cx >= cx - 1 && cam_cx <= cx + 1 && cam_cz > cz - 1 && cam_cz < cz + 1) this.camera.refreshAnimator(); - } - - public void updateWorldSpawn(ChunkCoordinates cc) { - this.camera.updateWorldSpawn(cc); - } - - public void startRender(long renderT) { - if (this.last_render_time > 0) this.camera.tick(renderT - this.last_render_time); - this.last_render_time = renderT; - } - - @Override - public void setAnimator(ICameraAnimator animator) { - this.camera.setAnimator(animator); - } - - @Override - public IViewCamera getCamera() { - return camerawrapper; - } - - /** - * This is a really complex bit. As we want to reuse the current client world when rendering, if possible, we need to handle when that world changes. We - * could simply destroy all the views pointing to the existing proxy world, but that would be annoying to mods using the API. Instead, we replace our proxy - * world with the new client world. This should only be called by LookingGlass, and only from the handling of the client world change detection. - * @param world The new world - */ - public void replaceWorldObject(WorldClient world) { - this.worldObj = world; - this.camera.worldObj = world; - this.effectRenderer.clearEffects(world); - this.renderGlobal.setWorldAndLoadRenderers(world); - } - - public WorldClient getWorldObj() { - return this.worldObj; - } -} +package com.xcompwiz.lookingglass.client.proxyworld; + +import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; +import com.xcompwiz.lookingglass.api.view.IViewCamera; +import com.xcompwiz.lookingglass.api.view.IWorldView; +import com.xcompwiz.lookingglass.client.render.FrameBufferContainer; +import com.xcompwiz.lookingglass.entity.EntityCamera; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.particle.ParticleManager; +import net.minecraft.client.renderer.RenderGlobal; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class WorldView implements IWorldView { + private WorldClient world; + public final BlockPos pos; + public final EntityCamera camera; + public final IViewCamera cameraWrapper; + + public final int width; + public final int height; + + private boolean update; + private boolean ready; + private boolean hasChunks; + private long lastRenderTime = -1L; + + private RenderGlobal renderGlobal; + private ParticleManager effectRenderer; + + private FrameBufferContainer fbo; + + public WorldView(WorldClient world, BlockPos pos, int width, int height) { + this.width = width; + this.height = height; + this.world = world; + this.pos = pos; + this.camera = new EntityCamera(this.world, this.pos); + this.cameraWrapper = new ViewCameraImpl(this.camera); + this.renderGlobal = new RenderGlobal(Minecraft.getMinecraft()); + this.effectRenderer = new ParticleManager(this.world, Minecraft.getMinecraft().getTextureManager()); + this.fbo = FrameBufferContainer.createNewFramebuffer(this, this.width, this.height); + } + + public void cleanup() { + this.fbo = null; + FrameBufferContainer.removeWorldView(this); + } + + @Override + public boolean isReady() { + return this.fbo != null && this.ready; + } + + public boolean hasChunks() { + return this.fbo != null && this.hasChunks; + } + + @Override + public void markDirty() { + this.update = true; + } + + public boolean markClean() { + if (this.fbo == null) return false; + this.ready = true; + boolean temp = this.update; + this.update = false; + return temp; + } + + public int getFramebuffer() { + return this.fbo == null ? 0 : this.fbo.getFramebuffer(); + } + + public RenderGlobal getRenderGlobal() { + return this.renderGlobal; + } + + public ParticleManager getEffectRenderer() { + return this.effectRenderer; + } + + @Override + public int getTexture() { + return this.fbo == null ? 0 : this.fbo.getTexture(); + } + + @Override + public void grab() { + + } + + @Override + public boolean release() { + return false; + } + + public void onChunkReceived(int cx, int cz) { + this.hasChunks = true; + int camCX = MathHelper.floor(this.camera.posX) >> 4; + int camCZ = MathHelper.floor(this.camera.posZ) >> 4; + if (camCX >= cx - 1 && camCX <= cx + 1 && camCZ > cz - 1 && camCZ < cz + 1) this.camera.refreshAnimator(); + } + + public void updateWorldSpawn(BlockPos pos) { + this.camera.updateWorldSpawn(pos); + } + + public void startRender(long renderT) { + if (this.lastRenderTime > 0) this.camera.tick(renderT - this.lastRenderTime); + this.lastRenderTime = renderT; + } + + @Override + public void setAnimator(ICameraAnimator animator) { + this.camera.setAnimator(animator); + } + + @Override + public IViewCamera getCamera() { + return this.cameraWrapper; + } + + public void replaceWorld(WorldClient world) { + this.world = world; + this.camera.setWorld(world); + this.effectRenderer.clearEffects(world); + this.renderGlobal.setWorldAndLoadRenderers(world); + } + + public WorldClient getWorld() { + return this.world; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/client/render/FrameBufferContainer.java b/src/main/java/com/xcompwiz/lookingglass/client/render/FrameBufferContainer.java index a47af8b..b1d20cc 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/render/FrameBufferContainer.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/render/FrameBufferContainer.java @@ -1,118 +1,113 @@ package com.xcompwiz.lookingglass.client.render; -import java.util.Collection; -import java.util.HashSet; -import java.util.concurrent.ConcurrentMap; - -import net.minecraftforge.client.MinecraftForgeClient; - +import com.google.common.collect.MapMaker; +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.proxyworld.WorldView; import org.lwjgl.opengl.EXTFramebufferObject; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL14; -import com.google.common.collect.MapMaker; -import com.xcompwiz.lookingglass.client.proxyworld.WorldView; -import com.xcompwiz.lookingglass.log.LoggerUtils; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.HashSet; +import java.util.concurrent.ConcurrentMap; public class FrameBufferContainer { - /** - * Using this map we can detect which FBOs should be freed. The map will delete any entry where the world view object is garbage collected. It unfortunately - * can't detect that the world view is otherwise leaked, though, just when it's gone. - */ - private static ConcurrentMap weakfbomap = new MapMaker().weakKeys(). makeMap(); - private static Collection framebuffers = new HashSet(); - - public static FrameBufferContainer createNewFramebuffer(WorldView view, int width, int height) { - FrameBufferContainer fbo = new FrameBufferContainer(width, height); - weakfbomap.put(view, fbo); - framebuffers.add(fbo); - return fbo; - } - - public static void removeWorldView(WorldView view) { - weakfbomap.remove(view); - } - - public static void clearAll() { - for (FrameBufferContainer fbo : framebuffers) { - fbo.release(); - } - framebuffers.clear(); - } - - public static synchronized void detectFreedWorldViews() { - Collection unpairedFBOs = new HashSet(framebuffers); - unpairedFBOs.removeAll(weakfbomap.values()); - if (unpairedFBOs.isEmpty()) return; - LoggerUtils.info("Freeing %d loose framebuffers from expired world views", unpairedFBOs.size()); - for (FrameBufferContainer fbo : unpairedFBOs) { - fbo.release(); - } - framebuffers.removeAll(unpairedFBOs); - } - - public final int width; - public final int height; - - private int framebuffer; - private int depthBuffer; - private int texture; - - private FrameBufferContainer(int width, int height) { - this.width = width; - this.height = height; - allocateFrameBuffer(); - } - - private void release() { - freeFrameBuffer(); - } - - public int getFramebuffer() { - return framebuffer; - } - - public int getTexture() { - return texture; - } - - // Always clean up your allocations - private synchronized void freeFrameBuffer() { - try { - if (this.texture != 0) GL11.glDeleteTextures(this.texture); - this.texture = 0; - if (depthBuffer != 0) EXTFramebufferObject.glDeleteRenderbuffersEXT(depthBuffer); - depthBuffer = 0; - if (this.framebuffer != 0) EXTFramebufferObject.glDeleteFramebuffersEXT(this.framebuffer); - this.framebuffer = 0; - } catch (Exception e) { - // Just in case, we make sure we don't crash. Because crashing is bad. - LoggerUtils.error("Error while cleaning up a world view frame buffer."); - } - } - - // This method builds the frame buffer and texture references - private void allocateFrameBuffer() { - if (this.framebuffer != 0) return; - - this.framebuffer = EXTFramebufferObject.glGenFramebuffersEXT(); //Release via: EXTFramebufferObject.glDeleteFramebuffersEXT(framebuffer); - this.depthBuffer = EXTFramebufferObject.glGenRenderbuffersEXT(); //Release via: EXTFramebufferObject.glDeleteRenderbuffersEXT(depthBuffer); - - EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, this.framebuffer); - - EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthBuffer); - if (MinecraftForgeClient.getStencilBits() == 0) EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24, width, height); - else EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, org.lwjgl.opengl.EXTPackedDepthStencil.GL_DEPTH24_STENCIL8_EXT, width, height); - - EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthBuffer); - if (MinecraftForgeClient.getStencilBits() != 0) EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_STENCIL_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthBuffer); - - this.texture = GL11.glGenTextures(); //Release via: GL11.glDeleteTextures(colorTexture); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.texture); - GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); - GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_INT, (java.nio.ByteBuffer) null); - EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, this.texture, 0); - - EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0); - } + private static ConcurrentMap weakFBOMap = new MapMaker().weakKeys().makeMap(); + private static Collection framebuffers = new HashSet<>(); + + public static FrameBufferContainer createNewFramebuffer(WorldView view, int width, int height) { + FrameBufferContainer fbo = new FrameBufferContainer(width, height); + weakFBOMap.put(view, fbo); + framebuffers.add(fbo); + return fbo; + } + + public static void removeWorldView(WorldView view) { + weakFBOMap.remove(view); + } + + public static void clearAll() { + for (FrameBufferContainer fbo : framebuffers) { + fbo.release(); + } + framebuffers.clear(); + } + + public static synchronized void detectFreedWorldViews() { + Collection unpairedFBOs = new HashSet<>(framebuffers); + unpairedFBOs.removeAll(weakFBOMap.values()); + if (unpairedFBOs.isEmpty()) return; + LookingGlass.logger().info("Freeing {} loose framebuffers from expired world views", unpairedFBOs.size()); + for (FrameBufferContainer fbo : unpairedFBOs) { + fbo.release(); + } + framebuffers.removeAll(unpairedFBOs); + } + + public final int width; + public final int height; + + private int framebuffer; + private int depthBuffer; + private int texture; + + private FrameBufferContainer(int width, int height) { + this.width = width; + this.height = height; + this.allocateFramebuffer(); + } + + private void release() { + this.freeFramebuffer(); + } + + public int getFramebuffer() { + return this.framebuffer; + } + + public int getTexture() { + return this.texture; + } + + private synchronized void freeFramebuffer() { + try { + if (this.texture != 0) GL11.glDeleteTextures(this.texture); + this.texture = 0; + if (this.depthBuffer != 0) EXTFramebufferObject.glDeleteRenderbuffersEXT(this.depthBuffer); + this.depthBuffer = 0; + if (this.framebuffer != 0) EXTFramebufferObject.glDeleteFramebuffersEXT(this.framebuffer); + this.framebuffer = 0; + } catch (Exception e) { + LookingGlass.logger().error("Error while cleaning up a world view framebuffer."); + } + } + + private void allocateFramebuffer() { + if (this.framebuffer != 0) return; + + this.framebuffer = EXTFramebufferObject.glGenFramebuffersEXT(); + this.depthBuffer = EXTFramebufferObject.glGenRenderbuffersEXT(); + + EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, this.framebuffer); + + EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, this.depthBuffer); + //Assuming stencil bits are available as MinecraftForgeClient.getStencilBits() doesn't exist anymore + EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24, + this.width, this.height); + + EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, + EXTFramebufferObject.GL_RENDERBUFFER_EXT, this.depthBuffer); + //EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_STENCIL_ATTACHMENT_EXT, + // EXTFramebufferObject.GL_RENDERBUFFER_EXT, this.depthBuffer); + + this.texture = GL11.glGenTextures(); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.texture); + GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, this.width, this.height, + 0, GL11.GL_RGBA, GL11.GL_INT, (ByteBuffer) null); + EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, this.texture, 0); + + EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/client/render/RenderPortal.java b/src/main/java/com/xcompwiz/lookingglass/client/render/RenderPortal.java index a7e56e8..25306ce 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/render/RenderPortal.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/render/RenderPortal.java @@ -1,69 +1,84 @@ -package com.xcompwiz.lookingglass.client.render; - -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.entity.Render; -import net.minecraft.entity.Entity; -import net.minecraft.util.ResourceLocation; - -import org.lwjgl.opengl.GL11; - -import com.xcompwiz.lookingglass.api.view.IWorldView; -import com.xcompwiz.lookingglass.entity.EntityPortal; - -public class RenderPortal extends Render { - - @Override - public void doRender(Entity entity, double d, double d1, double d2, float f, float f1) { - if (!(entity instanceof EntityPortal)) return; - EntityPortal portal = (EntityPortal) entity; - IWorldView activeview = portal.getActiveView(); - if (activeview == null) return; - - int texture = activeview.getTexture(); - if (texture == 0) return; - - int width = 2; - int height = 3; - double left = -width / 2.; - double top = 0; - - activeview.markDirty(); - GL11.glDisable(GL11.GL_ALPHA_TEST); - GL11.glDisable(GL11.GL_LIGHTING); - - GL11.glPushMatrix(); - GL11.glTranslatef((float) d, (float) d1, (float) d2); - - GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture); - Tessellator tessellator = Tessellator.instance; - tessellator.setColorRGBA_F(1, 1, 1, 1); - tessellator.startDrawingQuads(); - tessellator.addVertexWithUV(left, top, 0.0D, 0.0D, 0.0D); //inc=bl out; inc=bl down - tessellator.addVertexWithUV(width + left, top, 0.0D, 1.0D, 0.0D); //dc=br out; inc=br down - tessellator.addVertexWithUV(width + left, height + top, 0.0D, 1.0D, 1.0D); //dec=tr out; dec=tr up - tessellator.addVertexWithUV(left, height + top, 0.0D, 0.0D, 1.0D); //inc=lt out; dec=tl up - tessellator.draw(); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); - //XXX: Make the back of the portals a little nicer - tessellator.setColorRGBA_F(0, 0, 1, 1); - tessellator.startDrawingQuads(); - tessellator.addVertexWithUV(left, height + top, 0.0D, 0.0D, 1.0D); - tessellator.addVertexWithUV(width + left, height + top, 0.0D, 1.0D, 1.0D); - tessellator.addVertexWithUV(width + left, top, 0.0D, 1.0D, 0.0D); - tessellator.addVertexWithUV(left, top, 0.0D, 0.0D, 0.0D); - tessellator.draw(); - GL11.glPopMatrix(); - - GL11.glEnable(GL11.GL_LIGHTING); - GL11.glEnable(GL11.GL_ALPHA_TEST); - } - - @Override - protected void bindEntityTexture(Entity entity) {} - - @Override - protected ResourceLocation getEntityTexture(Entity entity) { - return null; - } - -} +package com.xcompwiz.lookingglass.client.render; + +import com.xcompwiz.lookingglass.api.view.IWorldView; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorld; +import com.xcompwiz.lookingglass.entity.EntityPortal; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.entity.Render; +import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import org.lwjgl.opengl.GL11; + +import javax.annotation.Nullable; + +@SideOnly(Side.CLIENT) +public class RenderPortal extends Render { + public RenderPortal(RenderManager renderManager) { + super(renderManager); + } + + @Override + public void doRender(EntityPortal entity, double x, double y, double z, float entityYaw, float partialTicks) { + if (entity.world instanceof ProxyWorld && ModConfigs.disableRenderInRenderPortal) return; //Render-in-render? No! (maybe) + IWorldView activeView = entity.getActiveView(); + if (activeView == null) return; + + int texture = activeView.getTexture(); + if (texture == 0) return; + + int width = 2; + int height = 3; + double left = -width / 2.0; + double top = 0.0; + + activeView.markDirty(); + GlStateManager.disableAlpha(); + GlStateManager.disableLighting(); + + GlStateManager.pushMatrix(); + GlStateManager.translate(x, y, z); + + GlStateManager.bindTexture(texture); + GlStateManager.color(1, 1, 1); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder builder = tessellator.getBuffer(); + builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX); + builder.pos(left, top, 0.0).tex(0.0, 0.0).endVertex(); + builder.pos(width + left, top, 0.0).tex(1.0, 0.0).endVertex(); + builder.pos(width + left, height + top, 0.0).tex(1.0, 1.0).endVertex(); + builder.pos(left, height + top, 0.0).tex(0.0, 1.0).endVertex(); + tessellator.draw(); + + GlStateManager.bindTexture(0); + GlStateManager.color(0, 0, 1); + builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION); + builder.pos(left, height + top, 0.0).endVertex(); + builder.pos(width + left, height + top, 0.0).endVertex(); + builder.pos(width + left, top, 0.0).endVertex(); + builder.pos(left, top, 0.0).endVertex(); + tessellator.draw(); + + GlStateManager.color(1, 1, 1); + GlStateManager.popMatrix(); + + GlStateManager.enableLighting(); + GlStateManager.enableAlpha(); + } + + @Override + protected boolean bindEntityTexture(EntityPortal entity) { + return false; + } + + @Nullable + @Override + protected ResourceLocation getEntityTexture(EntityPortal entity) { + return null; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/client/render/RenderUtils.java b/src/main/java/com/xcompwiz/lookingglass/client/render/RenderUtils.java index 9547709..b976c72 100644 --- a/src/main/java/com/xcompwiz/lookingglass/client/render/RenderUtils.java +++ b/src/main/java/com/xcompwiz/lookingglass/client/render/RenderUtils.java @@ -1,87 +1,78 @@ -package com.xcompwiz.lookingglass.client.render; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.EntityRenderer; -import net.minecraft.client.renderer.Tessellator; - -import org.lwjgl.opengl.EXTFramebufferObject; -import org.lwjgl.opengl.GL11; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -public class RenderUtils { - - @SideOnly(Side.CLIENT) - public final static void renderWorldToTexture(float renderTime, int framebuffer, int width, int height) { - if (framebuffer == 0) return; - Minecraft mc = Minecraft.getMinecraft(); - if (mc.skipRenderWorld) return; - EntityRenderer entityRenderer = mc.entityRenderer; - - //Backup current render settings - int heightBackup = mc.displayHeight; - int widthBackup = mc.displayWidth; - - int thirdPersonBackup = mc.gameSettings.thirdPersonView; - boolean hideGuiBackup = mc.gameSettings.hideGUI; - int particleBackup = mc.gameSettings.particleSetting; - boolean anaglyphBackup = mc.gameSettings.anaglyph; - int renderDistanceBackup = mc.gameSettings.renderDistanceChunks; - float FOVbackup = mc.gameSettings.fovSetting; - - //Render world - try { - //Set all of the render setting to work on the proxy world - mc.displayHeight = height; - mc.displayWidth = width; - - //TODO: params (FOV, Particle setting, renderDistance) - mc.gameSettings.thirdPersonView = 0; - mc.gameSettings.hideGUI = true; - //mc.gameSettings.particleSetting = ; - mc.gameSettings.anaglyph = false; - //mc.gameSettings.renderDistanceChunks = ; - //mc.gameSettings.fovSetting = ; - - //Set gl options - GL11.glViewport(0, 0, mc.displayWidth, mc.displayHeight); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); - EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, framebuffer); - GL11.glClearColor(1.0f, 0.0f, 0.0f, 0.5f); - GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); - - int i1 = mc.gameSettings.limitFramerate; - if (mc.isFramerateLimitBelowMax()) { - entityRenderer.renderWorld(renderTime, (1000000000 / i1)); - } else { - entityRenderer.renderWorld(renderTime, 0L); - } - } catch (Exception e) { - try { - //Clean up the tessellator, just in case. - Tessellator.instance.draw(); - } catch (Exception e2) { - //It might throw an exception, but that just means we didn't need to clean it up (this time) - } - throw new RuntimeException("Error while rendering proxy world", e); - } finally { - GL11.glEnable(GL11.GL_TEXTURE_2D); - EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0); - - GL11.glViewport(0, 0, widthBackup, heightBackup); - GL11.glLoadIdentity(); - - mc.gameSettings.thirdPersonView = thirdPersonBackup; - mc.gameSettings.hideGUI = hideGuiBackup; - mc.gameSettings.particleSetting = particleBackup; - mc.gameSettings.anaglyph = anaglyphBackup; - mc.gameSettings.renderDistanceChunks = renderDistanceBackup; - mc.gameSettings.fovSetting = FOVbackup; - - mc.displayHeight = heightBackup; - mc.displayWidth = widthBackup; - } - } - -} +package com.xcompwiz.lookingglass.client.render; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.Tessellator; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import org.lwjgl.opengl.EXTFramebufferObject; +import org.lwjgl.opengl.GL11; + +public class RenderUtils { + @SideOnly(Side.CLIENT) + public static void renderWorldToTexture(float renderTime, int framebuffer, int width, int height) { + if (framebuffer == 0) return; + Minecraft mc = Minecraft.getMinecraft(); + if (mc.skipRenderWorld) return; + EntityRenderer entityRenderer = mc.entityRenderer; + + + int heightBackup = mc.displayHeight; + int widthBackup = mc.displayWidth; + + int thirdPersonBackup = mc.gameSettings.thirdPersonView; + boolean hideGuiBackup = mc.gameSettings.hideGUI; + int particleBackup = mc.gameSettings.particleSetting; + boolean anaglyphBackup = mc.gameSettings.anaglyph; + int renderDistanceBackup = mc.gameSettings.renderDistanceChunks; + float FOVBackup = mc.gameSettings.fovSetting; + + try { + mc.displayWidth = width; + mc.displayHeight = height; + + mc.gameSettings.thirdPersonView = 0; + mc.gameSettings.hideGUI = true; + //mc.gameSettings.particleSetting = ; + mc.gameSettings.anaglyph = false; + //mc.gameSettings.renderDistanceChunks = ; + //mc.gameSettings.fovSetting = ; + + //Set gl options + GlStateManager.viewport(0, 0, mc.displayWidth, mc.displayHeight); + GlStateManager.bindTexture(0); + EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, framebuffer); + GlStateManager.clearColor(1.0F, 0.0F, 0.0F, 0.5F); + GlStateManager.clear(GL11.GL_COLOR_BUFFER_BIT); + + int i1 = mc.gameSettings.limitFramerate; + if (mc.isFramerateLimitBelowMax()) { + entityRenderer.renderWorld(renderTime, (1000000000 / i1)); + } else { + entityRenderer.renderWorld(renderTime, 0L); + } + } catch (Exception e) { + try { + Tessellator.getInstance().draw(); + } catch (Exception ignored) {} + throw new RuntimeException("Error rendering proxy world", e); + } finally { + GL11.glEnable(GL11.GL_TEXTURE_2D); + EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0); + + GL11.glViewport(0, 0, widthBackup, heightBackup); + GL11.glLoadIdentity(); + + mc.gameSettings.thirdPersonView = thirdPersonBackup; + mc.gameSettings.hideGUI = hideGuiBackup; + mc.gameSettings.particleSetting = particleBackup; + mc.gameSettings.anaglyph = anaglyphBackup; + mc.gameSettings.renderDistanceChunks = renderDistanceBackup; + mc.gameSettings.fovSetting = FOVBackup; + + mc.displayHeight = heightBackup; + mc.displayWidth = widthBackup; + } + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/command/CommandBaseAdv.java b/src/main/java/com/xcompwiz/lookingglass/command/CommandBaseAdv.java index 9fa3681..b441966 100644 --- a/src/main/java/com/xcompwiz/lookingglass/command/CommandBaseAdv.java +++ b/src/main/java/com/xcompwiz/lookingglass/command/CommandBaseAdv.java @@ -1,108 +1,52 @@ -package com.xcompwiz.lookingglass.command; - -import java.util.Random; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.command.NumberInvalidException; -import net.minecraft.command.PlayerNotFoundException; -import net.minecraft.command.PlayerSelector; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.world.World; - -public abstract class CommandBaseAdv extends CommandBase { - - public void sendToAdmins(ICommandSender agent, String text, Object[] objects) { - func_152373_a(agent, this, text, objects); - } - - public static EntityPlayerMP getTargetPlayer(ICommandSender sender, String target) { - EntityPlayerMP entityplayermp = PlayerSelector.matchOnePlayer(sender, target); - - if (entityplayermp == null) { - entityplayermp = MinecraftServer.getServer().getConfigurationManager().func_152612_a(target); - } - if (entityplayermp == null) { throw new PlayerNotFoundException(); } - return entityplayermp; - } - - public static Integer getSenderDimension(ICommandSender sender) { - World w = sender.getEntityWorld(); - if (w == null) throw new CommandException("You must specify a dimension to use this command from the commandline"); - return w.provider.dimensionId; - } - - /** - * Returns the given ICommandSender as a EntityPlayer or throw an exception. - */ - public static TileEntity getCommandSenderAsTileEntity(ICommandSender sender) { - try { - World world = sender.getEntityWorld(); - ChunkCoordinates coords = sender.getPlayerCoordinates(); - return world.getTileEntity(coords.posX, coords.posY, coords.posZ); - } catch (Exception e) { - throw new CommandException("Could not get tile entity"); - } - } - - public static double handleRelativeNumber(ICommandSender sender, double origin, String arg) { - return handleRelativeNumber(sender, origin, arg, -30000000, 30000000); - } - - public static double handleRelativeNumber(ICommandSender par1ICommandSender, double origin, String arg, int min, int max) { - boolean relative = arg.startsWith("~"); - boolean random = arg.startsWith("?"); - if (random) relative = true; - double d1 = relative ? origin : 0.0D; - - if (!relative || arg.length() > 1) { - boolean flag1 = arg.contains("."); - - if (relative) { - arg = arg.substring(1); - } - - double d2 = parseDouble(par1ICommandSender, arg); - if (random) { - Random rand = new Random(); - d1 += (rand.nextDouble() * 2 - 1) * d2; - } else { - d1 += d2; - } - - if (!flag1 && !relative) { - d1 += 0.5D; - } - } - - if (min != 0 || max != 0) { - if (d1 < min) { throw new NumberInvalidException("commands.generic.double.tooSmall", new Object[] { Double.valueOf(d1), Integer.valueOf(min) }); } - - if (d1 > max) { throw new NumberInvalidException("commands.generic.double.tooBig", new Object[] { Double.valueOf(d1), Integer.valueOf(max) }); } - } - - return d1; - } - - /** - * Returns the player for a username as an Entity or throws an exception. - */ - public static Entity parsePlayerByName(String name) throws PlayerNotFoundException { - EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(name); - if (player != null) { return player; } - throw new PlayerNotFoundException("lookingglass.commands.generic.player.notfound", new Object[] { name }); - } - - public static float parseFloat(ICommandSender par0ICommandSender, String par1Str) { - try { - return Float.parseFloat(par1Str); - } catch (NumberFormatException numberformatexception) { - throw new NumberInvalidException("commands.generic.num.invalid", new Object[] { par1Str }); - } - } -} +package com.xcompwiz.lookingglass.command; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.command.NumberInvalidException; +import net.minecraft.world.World; + +import java.util.Random; + +public abstract class CommandBaseAdv extends CommandBase { + public static Integer getSenderDimension(ICommandSender sender) { + World world = sender.getEntityWorld(); + return world.provider.getDimension(); + } + + public static double handleRelativeNumber(double origin, String arg) throws CommandException { + return handleRelativeNumber(origin, arg, -30000000, 30000000); + } + public static double handleRelativeNumber(double origin, String arg, int min, int max) throws CommandException { + boolean random = arg.startsWith("?"); + boolean relative = random || arg.startsWith("~"); + double d1 = relative ? origin : 0.0; + + if (!relative || arg.length() > 1) { + boolean flag1 = arg.contains("."); + + if (relative) { + arg = arg.substring(1); + } + + double d2 = parseDouble(arg); + if (random) { + Random rnd = new Random(); + d1 += (rnd.nextDouble() * 2 - 1) * d2; + } else { + d1 += d2; + } + + if (!flag1 && !relative) { + d1 += 0.5; + } + } + + if (min != 0 || max != 0) { + if (d1 < min) throw new NumberInvalidException("commands.generic.double.tooSmall", d1, min); + if (d1 > max) throw new NumberInvalidException("commands.generic.double.tooBig", d1, max); + } + + return d1; + } +} \ No newline at end of file diff --git a/src/main/java/com/xcompwiz/lookingglass/command/CommandCreateView.java b/src/main/java/com/xcompwiz/lookingglass/command/CommandCreateView.java index f38fc65..cd86dd7 100644 --- a/src/main/java/com/xcompwiz/lookingglass/command/CommandCreateView.java +++ b/src/main/java/com/xcompwiz/lookingglass/command/CommandCreateView.java @@ -1,63 +1,55 @@ -package com.xcompwiz.lookingglass.command; - -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.Entity; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.world.WorldServer; -import net.minecraftforge.common.DimensionManager; - -import com.xcompwiz.lookingglass.entity.EntityPortal; - -public class CommandCreateView extends CommandBaseAdv { - @Override - public String getCommandName() { - return "lg-viewdim"; - } - - @Override - public String getCommandUsage(ICommandSender par1ICommandSender) { - return "/" + this.getCommandName() + " targetdim [dim, x, y, z]"; - } - - @Override - public void processCommand(ICommandSender agent, String[] args) { - int targetdim = 0; - Integer dim = null; - ChunkCoordinates coords = null; - - //XXX: Set Coordinates of view location? - if (args.length > 0) { - String sTarget = args[0]; - targetdim = parseInt(agent, sTarget); - } else { - throw new WrongUsageException("Could not parse command."); - } - if (args.length > 4) { - dim = parseInt(agent, args[1]); - Entity caller = null; - try { - caller = getCommandSenderAsPlayer(agent); - } catch (Exception e) { - } - int x = (int) handleRelativeNumber(agent, (caller != null ? caller.posX : 0), args[2]); - int y = (int) handleRelativeNumber(agent, (caller != null ? caller.posY : 0), args[3], 0, 0); - int z = (int) handleRelativeNumber(agent, (caller != null ? caller.posZ : 0), args[4]); - coords = new ChunkCoordinates(x, y, z); - } - if (coords == null) { - dim = getSenderDimension(agent); - coords = agent.getPlayerCoordinates(); - } - if (coords == null) throw new WrongUsageException("Location Required"); - - WorldServer worldObj = DimensionManager.getWorld(dim); - if (worldObj == null) { throw new CommandException("The target world is not loaded"); } - - EntityPortal portal = new EntityPortal(worldObj, targetdim, coords.posX, coords.posY, coords.posZ); - worldObj.spawnEntityInWorld(portal); - - sendToAdmins(agent, "A window to dimension " + targetdim + " has been created.", new Object[0]); - } -} +package com.xcompwiz.lookingglass.command; + +import com.xcompwiz.lookingglass.entity.EntityPortal; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.command.WrongUsageException; +import net.minecraft.entity.Entity; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.DimensionManager; + +public class CommandCreateView extends CommandBaseAdv { + @Override + public String getName() { + return "lg-viewdim"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "/" + this.getName() + " [dimension] [x y z]"; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + int targetDim = 0; + Integer dim = null; + BlockPos pos = null; + + if (args.length > 0) { + String sTarget = args[0]; + targetDim = parseInt(sTarget); + } else { + throw new WrongUsageException("A target dimension is required!"); + } + if (args.length > 4) { + dim = parseInt(args[1]); + Entity caller = sender.getCommandSenderEntity(); + int x = (int) handleRelativeNumber((caller != null ? caller.posX : 0.0), args[2]); + int y = (int) handleRelativeNumber((caller != null ? caller.posY : 0.0), args[3], 0, 0); + int z = (int) handleRelativeNumber((caller != null ? caller.posZ : 0.0), args[4]); + pos = new BlockPos(x, y, z); + } + if (pos == null) { + dim = getSenderDimension(sender); + pos = sender.getPosition(); + } + + WorldServer world = DimensionManager.getWorld(dim); + if (world == null) throw new CommandException("lookingglass.commands.generic.world.notloaded"); + + EntityPortal portal = new EntityPortal(world, targetDim, pos.getX(), pos.getY(), pos.getZ()); + world.spawnEntity(portal); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/core/CommonProxy.java b/src/main/java/com/xcompwiz/lookingglass/core/CommonProxy.java index 5a0d984..1393215 100644 --- a/src/main/java/com/xcompwiz/lookingglass/core/CommonProxy.java +++ b/src/main/java/com/xcompwiz/lookingglass/core/CommonProxy.java @@ -1,5 +1,5 @@ -package com.xcompwiz.lookingglass.core; - -public class CommonProxy { - public void init() {} -} +package com.xcompwiz.lookingglass.core; + +public class CommonProxy { + public void init() {} +} diff --git a/src/main/java/com/xcompwiz/lookingglass/core/LookingGlassForgeEventHandler.java b/src/main/java/com/xcompwiz/lookingglass/core/LookingGlassForgeEventHandler.java index 35ce34e..13abeb6 100644 --- a/src/main/java/com/xcompwiz/lookingglass/core/LookingGlassForgeEventHandler.java +++ b/src/main/java/com/xcompwiz/lookingglass/core/LookingGlassForgeEventHandler.java @@ -1,43 +1,49 @@ package com.xcompwiz.lookingglass.core; -import java.util.List; - +import com.xcompwiz.lookingglass.entity.EntityPortal; import net.minecraft.entity.Entity; +import net.minecraft.util.ClassInheritanceMultiMap; import net.minecraft.world.chunk.Chunk; +import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.world.ChunkEvent; import net.minecraftforge.event.world.WorldEvent; - -import com.xcompwiz.lookingglass.entity.EntityPortal; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.registry.EntityEntry; +import net.minecraftforge.fml.common.registry.EntityEntryBuilder; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; public class LookingGlassForgeEventHandler { + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onChunkUnload(ChunkEvent.Unload event) { + if (!event.getWorld().isRemote) return; + Chunk chunk = event.getChunk(); + synchronized (chunk.getEntityLists()) { + for (int i = 0; i < chunk.getEntityLists().length; i++) { + ClassInheritanceMultiMap list = chunk.getEntityLists()[i]; + for (Entity entity : list) { + if (entity instanceof EntityPortal) ((EntityPortal) entity).releaseActiveView(); + } + } + } + } - @SideOnly(Side.CLIENT) - @SubscribeEvent - public void onChunkUnload(ChunkEvent.Unload event) { - if (!event.world.isRemote) return; - Chunk chunk = event.getChunk(); - // When we unload a chunk client side, we want to make sure that any view entities clean up. Not strictly necessary, but a good practice. - // I don't trust vanilla to unload entity references quickly/correctly/completely. - for (int i = 0; i < chunk.entityLists.length; ++i) { - List list = chunk.entityLists[i]; - for (Entity entity : list) { - if (entity instanceof EntityPortal) ((EntityPortal) entity).releaseActiveView(); - } - } - } + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onWorldUnload(WorldEvent.Unload event) { + if (!event.getWorld().isRemote) return; + synchronized (event.getWorld().getLoadedEntityList()) { + for (Entity entity : event.getWorld().getLoadedEntityList()) { + if (entity instanceof EntityPortal) ((EntityPortal) entity).releaseActiveView(); + } + } + } - @SideOnly(Side.CLIENT) - @SubscribeEvent - public void onWorldUnload(WorldEvent.Unload event) { - if (!event.world.isRemote) return; - // When we unload a world client side, we want to make sure that any view entities clean up. Not strictly necessary, but a good practice. - // I don't trust vanilla to unload entity references quickly/correctly/completely. - for (Object entity : event.world.loadedEntityList) { - if (entity instanceof EntityPortal) ((EntityPortal) entity).releaseActiveView(); - } - } + @SubscribeEvent + public void registerEntities(RegistryEvent.Register event) { + event.getRegistry().register(EntityEntryBuilder.create().entity(EntityPortal.class) + .factory(EntityPortal::new).id("lookingglass:portal", 216).name("lookingglass.portal") + .tracker(64, 10, false).build()); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/entity/EntityCamera.java b/src/main/java/com/xcompwiz/lookingglass/entity/EntityCamera.java index bb1b2af..e57618f 100644 --- a/src/main/java/com/xcompwiz/lookingglass/entity/EntityCamera.java +++ b/src/main/java/com/xcompwiz/lookingglass/entity/EntityCamera.java @@ -1,398 +1,297 @@ package com.xcompwiz.lookingglass.entity; -import net.minecraft.block.material.Material; +import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; +import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.MoverType; import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.effect.EntityLightningBolt; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.potion.Potion; -import net.minecraft.potion.PotionEffect; -import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.DamageSource; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import com.xcompwiz.lookingglass.api.animator.ICameraAnimator; +public class EntityCamera extends EntityPlayerSP { + private ICameraAnimator animator; + private BlockPos target; + private boolean defaultSpawn = false; + private float fovMultiplier = 1.0F; + + public EntityCamera(World world, BlockPos spawn) { + super(Minecraft.getMinecraft(), world, Minecraft.getMinecraft().getConnection(), null, null); + this.target = spawn; + if (this.target == null) { + this.defaultSpawn = true; + BlockPos pos = world.provider.getSpawnPoint(); + int y = this.updateTargetPosition(pos); + this.target = new BlockPos(pos.getX(), y, pos.getZ()); + } + this.setPosition(this.target.getX(), this.target.getY(), this.target.getZ()); + } + + public void setAnimator(ICameraAnimator animator) { + this.animator = animator; + if (this.animator != null) this.animator.setTarget(this.target); + } + + @Override + protected void applyEntityAttributes() { + super.applyEntityAttributes(); + this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(1.0); + this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.0); + } + + public void updateWorldSpawn(BlockPos spawn) { + if (this.defaultSpawn) { + int y = this.updateTargetPosition(spawn); + spawn = new BlockPos(spawn.getX(), y, spawn.getZ()); + this.setPositionAndUpdate(spawn.getX(), spawn.getY(), spawn.getZ()); + if (this.animator != null) this.animator.setTarget(spawn); + this.refreshAnimator(); + } + } + + private int updateTargetPosition(BlockPos target) { + int x = target.getX(); + int y = target.getY(); + int z = target.getZ(); + if (!this.world.getChunkFromBlockCoords(target).isEmpty()) { + BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(); + IBlockState state = this.world.getBlockState(target); + if (state.getCollisionBoundingBox(this.world, target) != null) { + do { + state = this.world.getBlockState(mutable.setPos(x, --y, z)); + } while (y > 0 && state.getCollisionBoundingBox(this.world, mutable) != null); + if (y == 0) y = target.getY(); + else ++y; + } else { + do { + state = this.world.getBlockState(mutable.setPos(x, ++y, z)); + } while (y < 256 && state.getCollisionBoundingBox(this.world, mutable) == null); + if (y == 256) y = target.getY(); + } + return y; + } + return target.getY(); + } + + public void refreshAnimator() { + if (this.animator != null) this.animator.refresh(); + } + + public void tick(long dt) { + if (this.animator != null) this.animator.update(dt); + } + + @Override + public float getFovModifier() { + return this.fovMultiplier; + } + + public void setFOVMult(float mult) { + this.fovMultiplier = mult; + } + + @Override + public void onEntityUpdate() { + + } + + @Override + public void onLivingUpdate() { + + } + + @Override + public void onUpdate() { + + } + + @Override + protected int getExperiencePoints(EntityPlayer player) { + return 0; + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + return compound; + } + + @Override + public void writeEntityToNBT(NBTTagCompound compound) { + + } + + @Override + public boolean writeToNBTAtomically(NBTTagCompound compound) { + return false; + } + + @Override + public boolean writeToNBTOptional(NBTTagCompound compound) { + return false; + } + + @Override + public void readFromNBT(NBTTagCompound compound) { + + } + + @Override + public void readEntityFromNBT(NBTTagCompound compound) { + + } + + @Override + public boolean doesEntityNotTriggerPressurePlate() { + return true; + } -/** - * Our camera entity. This is made a player so that we can replace the player client-side when doing rendering. - * At the bottom of the class we create a bunch of method stubs to override higher level logic, so that our "player" doesn't act like one. - */ -public class EntityCamera extends EntityClientPlayerMP { - - private ICameraAnimator animator; - private ChunkCoordinates target; - private boolean defaultSpawn = false; - - private float fovmultiplier = 1; - - public EntityCamera(World worldObj, ChunkCoordinates spawn) { - super(Minecraft.getMinecraft(), worldObj, Minecraft.getMinecraft().getSession(), null, null); - this.target = spawn; - if (target == null) { - defaultSpawn = true; - ChunkCoordinates cc = worldObj.provider.getSpawnPoint(); - int y = updateTargetPosition(cc); - target = new ChunkCoordinates(cc.posX, y, cc.posZ); - } - this.setPositionAndUpdate(target.posX, target.posY, target.posZ); - } - - public void setAnimator(ICameraAnimator animator) { - this.animator = animator; - if (this.animator != null) this.animator.setTarget(target); - } - - @Override - protected void applyEntityAttributes() { - super.applyEntityAttributes(); - getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(1); - getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.0D); - } - - public void updateWorldSpawn(ChunkCoordinates cc) { - if (defaultSpawn) { - int y = updateTargetPosition(cc); - target = new ChunkCoordinates(cc.posX, y, cc.posZ); - this.setPositionAndUpdate(target.posX, target.posY, target.posZ); - if (animator != null) animator.setTarget(cc); - this.refreshAnimator(); - } - } - - private int updateTargetPosition(ChunkCoordinates target) { - int x = target.posX; - int y = target.posY; - int z = target.posZ; - if (!this.worldObj.getChunkFromBlockCoords(x, z).isEmpty()) { - if (this.worldObj.getBlock(x, y, z).getBlocksMovement(this.worldObj, x, y, z)) { - while (y > 0 && this.worldObj.getBlock(x, --y, z).getBlocksMovement(this.worldObj, x, y, z)) - ; - if (y == 0) y = target.posY; - else ++y; - } else { - while (y < 256 && !this.worldObj.getBlock(x, ++y, z).getBlocksMovement(this.worldObj, x, y, z)) - ; - if (y == 256) y = target.posY; - } - return y; - } - return target.posY; - } - - public void refreshAnimator() { - if (this.animator != null) animator.refresh(); - } - - public void tick(long dt) { - if (this.animator != null) animator.update(dt); - } - - @Override - public float getFOVMultiplier() { - return fovmultiplier; - } - - public void setFOVMult(float fovmult) { - fovmultiplier = fovmult; - } - - /* - * POSSIBLY UNNECESSARY CODE TO PREVENT OTHER CODE FROM RUNNING - */ - @Override - public void onEntityUpdate() {} - - @Override - public void onLivingUpdate() {} - - @Override - public void onUpdate() {} - - @Override - protected int getExperiencePoints(EntityPlayer par1EntityPlayer) { - return 0; - } - - @Override - protected boolean isAIEnabled() { - return false; - } - - @Override - public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) {} - - @Override - public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) {} - - @Override - public void setAIMoveSpeed(float par1) {} - - @Override - protected void updateAITasks() {} - - @Override - public ItemStack getHeldItem() { - return null; - } - - @Override - public ItemStack getEquipmentInSlot(int par1) { - return null; - } - - @Override - public void setCurrentItemOrArmor(int par1, ItemStack par2ItemStack) {} - - @Override - public ItemStack[] getLastActiveItems() { - return null; - } - - @Override - protected void dropEquipment(boolean par1, int par2) {} - - @Override - protected void fall(float par1) {} - - @Override - protected void updateFallState(double par1, boolean par3) {} - - @Override - protected void onDeathUpdate() { - this.setDead(); - } - - @Override - public EntityLivingBase getAITarget() { - return null; - } - - @Override - public void setRevengeTarget(EntityLivingBase par1) {} - - @Override - public EntityLivingBase getLastAttacker() { - return null; - } + @Override + protected void collideWithEntity(Entity entityIn) { - @Override - public void setLastAttacker(Entity par1) {} + } - @Override - protected void updatePotionEffects() {} + @Override + protected void collideWithNearbyEntities() { - @Override - public void clearActivePotions() {} + } - @Override - public boolean isPotionActive(int par1) { - return false; - } + @Override + public boolean shouldRenderInPass(int pass) { + return false; + } - @Override - public boolean isPotionActive(Potion par1) { - return false; - } + @Override + public void travel(float strafe, float vertical, float forward) { - @Override - public PotionEffect getActivePotionEffect(Potion par1) { - return null; - } + } - @Override - public void addPotionEffect(PotionEffect par1) {} + @Override + public boolean isEntityInvulnerable(DamageSource source) { + return true; + } - @Override - public boolean isPotionApplicable(PotionEffect par1) { - return false; - } + @Override + public void onStruckByLightning(EntityLightningBolt lightningBolt) { - @Override - public boolean isEntityUndead() { - return false; - } + } - @Override - public void removePotionEffectClient(int par1) {} + @Override + public boolean isInvisible() { + return true; + } - @Override - public void removePotionEffect(int par1) {} + @Override + public boolean isInvisibleToPlayer(EntityPlayer player) { + return true; + } - @Override - protected void onNewPotionEffect(PotionEffect par1) {} + @Override + public boolean getIsInvulnerable() { + return true; + } - @Override - protected void onChangedPotionEffect(PotionEffect par1, boolean par2) {} + @Override + public boolean isSneaking() { + return false; + } - @Override - protected void onFinishedPotionEffect(PotionEffect par1) {} + @Override + public boolean isRiding() { + return false; + } - @Override - public void heal(float par1) {} + @Override + public boolean isBurning() { + return false; + } - @Override - public boolean attackEntityFrom(DamageSource par1, float par2) { - return false; - } + @Override + public void applyEntityCollision(Entity entityIn) { - @Override - public void renderBrokenItemStack(ItemStack par1) {} + } - @Override - public void onDeath(DamageSource par1) { - this.worldObj.setEntityState(this, (byte) 3); - } + @Override + public int getBrightnessForRender() { + return 0; + } - @Override - public void knockBack(Entity par1Entity, float par2, double par3, double par5) {} + @Override + public float getBrightness() { + return 0.0F; + } - @Override - public boolean isOnLadder() { - return false; - } + @Override + protected void handleJumpLava() { - @Override - public int getTotalArmorValue() { - return 0; - } + } - @Override - protected float applyArmorCalculations(DamageSource par1DamageSource, float par2) { - return par2; - } + @Override + protected void handleJumpWater() { - @Override - protected float applyPotionDamageCalculations(DamageSource par1DamageSource, float par2) { - return par2; - } + } - @Override - protected void damageEntity(DamageSource par1, float par2) {} + @Override + public boolean handleWaterMovement() { + return false; + } - @Override - public void swingItem() {} + @Override + public void move(MoverType type, double x, double y, double z) { - @Override - protected void updateArmSwingProgress() {} + } - @Override - public void setSprinting(boolean par1) {} + @Override + public void moveRelative(float strafe, float up, float forward, float friction) { - @Override - protected float getSoundVolume() { - return 0F; - } + } - @Override - public void dismountEntity(Entity par1Entity) {} + @Override + public void moveToBlockPosAndAngles(BlockPos pos, float rotationYawIn, float rotationPitchIn) { - @Override - public void moveEntityWithHeading(float par1, float par2) {} + } - @Override - public void updateRidden() {} + @Override + protected boolean canBeRidden(Entity entityIn) { + return false; + } - @Override - public void setJumping(boolean par1) {} - - @Override - public void onItemPickup(Entity par1Entity, int par2) {} + @Override + protected boolean canDropLoot() { + return false; + } - @Override - public boolean canEntityBeSeen(Entity par1Entity) { - return false; - } - - @Override - public boolean canBeCollidedWith() { - return false; - } + @Override + protected boolean canFitPassenger(Entity passenger) { + return false; + } - @Override - public boolean canBePushed() { - return false; - } + @Override + protected boolean canTriggerWalking() { + return false; + } - @Override - protected boolean canTriggerWalking() { - return false; - } + @Override + public boolean canAttackPlayer(EntityPlayer other) { + return false; + } - @Override - public boolean handleWaterMovement() { - return false; - } - - @Override - public boolean isInsideOfMaterial(Material par1Material) { - return false; - } - - @Override - public boolean handleLavaMovement() { - return false; - } - - @Override - public void moveFlying(float par1, float par2, float par3) {} - - @Override - public float getBrightness(float par1) { - return 0; - } - - @Override - public void applyEntityCollision(Entity par1Entity) {} - - @Override - public boolean isBurning() { - return false; - } - - @Override - public boolean isRiding() { - return false; - } - - @Override - public boolean isSneaking() { - return false; - } - - @Override - public boolean isInvisible() { - return true; - } + @Override + public boolean canBeAttackedWithItem() { + return false; + } - @Override - public void onStruckByLightning(EntityLightningBolt par1) {} - - @Override - public boolean isEntityInvulnerable() { - return true; - } + @Override + public boolean canBeCollidedWith() { + return false; + } - @Override - public void travelToDimension(int par1) {} - - @Override - public boolean shouldRenderInPass(int pass) { - return false; - } - - @Override - protected void collideWithEntity(Entity par1Entity) {} - - @Override - protected void collideWithNearbyEntities() {} - - @Override - public boolean doesEntityNotTriggerPressurePlate() { - return true; - } + @Override + public boolean canBeHitWithPotion() { + return false; + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/entity/EntityPortal.java b/src/main/java/com/xcompwiz/lookingglass/entity/EntityPortal.java index a17936d..4f441f3 100644 --- a/src/main/java/com/xcompwiz/lookingglass/entity/EntityPortal.java +++ b/src/main/java/com/xcompwiz/lookingglass/entity/EntityPortal.java @@ -1,105 +1,102 @@ -package com.xcompwiz.lookingglass.entity; - -import net.minecraft.client.Minecraft; -import net.minecraft.entity.Entity; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.world.World; - -import com.xcompwiz.lookingglass.api.animator.CameraAnimatorPlayer; -import com.xcompwiz.lookingglass.api.view.IWorldView; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; -import com.xcompwiz.lookingglass.client.proxyworld.WorldView; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * Despite it's name, this isn't so much a doorway or window as it is a moving picture. More Harry Potter's portraits than Portal's portals. (Man I wish the - * best example of portal rendering in games wasn't called Portal.... So hard to reference sanely.) - */ -public class EntityPortal extends Entity { - // We store the dimension ID we point at in the dataWatcher at this index. - private static final int targetID = 20; - - // How long the window has to live. Functions as a countdown timer. - private long lifetime = 1000L; - - @SideOnly(Side.CLIENT) - private IWorldView activeview; - - public EntityPortal(World world) { - super(world); - dataWatcher.addObject(targetID, Integer.valueOf(0)); - } - - public EntityPortal(World world, int targetdim, int posX, int posY, int posZ) { - this(world); - this.setTarget(targetdim); - this.setPosition(posX, posY, posZ); - } - - /** Puts the dim id target in the datawatcher. */ - private void setTarget(int targetdim) { - dataWatcher.updateObject(targetID, targetdim); - //XXX: Technically speaking, it might be wise to design this so that it can change targets, but that's not needed for this class. - // If it was, we'd have this function kill any active views when the target changed, causing it to open a new view for the new target. - } - - /** Gets the target dimension id */ - private int getTarget() { - return dataWatcher.getWatchableObjectInt(targetID); - } - - @Override - protected void entityInit() {} - - @Override - @SideOnly(Side.CLIENT) - public void setDead() { - super.setDead(); - releaseActiveView(); - } - - @Override - public void onUpdate() { - // Countdown to die - --lifetime; - if (lifetime <= 0) { - this.setDead(); - return; - } - super.onUpdate(); - } - - @SideOnly(Side.CLIENT) - public IWorldView getActiveView() { - if (!worldObj.isRemote) return null; - if (activeview == null) { - activeview = ProxyWorldManager.createWorldView(getTarget(), null, 160, 240); - if (activeview != null) { - // We set the player animator on our portrait. This makes the view move a little depending on how the user looks at it. Not quite a replacement for portal rendering, but cool looking anyway. - activeview.setAnimator(new CameraAnimatorPlayer(activeview.getCamera(), this, Minecraft.getMinecraft().thePlayer)); - } - } - return activeview; - } - - @SideOnly(Side.CLIENT) - public void releaseActiveView() { - if (activeview != null) ProxyWorldManager.destroyWorldView((WorldView) activeview); - activeview = null; - } - - @Override - protected void readEntityFromNBT(NBTTagCompound nbt) { - setTarget(nbt.getInteger("Dimension")); - lifetime = nbt.getLong("lifetime"); - } - - @Override - protected void writeEntityToNBT(NBTTagCompound nbt) { - nbt.setInteger("Dimension", getTarget()); - nbt.setLong("lifetime", lifetime); - } - -} +package com.xcompwiz.lookingglass.entity; + +import com.xcompwiz.lookingglass.api.animator.CameraAnimatorPivot; +import com.xcompwiz.lookingglass.api.animator.CameraAnimatorPlayer; +import com.xcompwiz.lookingglass.api.view.IWorldView; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import com.xcompwiz.lookingglass.client.proxyworld.WorldView; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.datasync.DataParameter; +import net.minecraft.network.datasync.DataSerializers; +import net.minecraft.network.datasync.EntityDataManager; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class EntityPortal extends Entity { + public static final DataParameter TARGET_DIMENSION = EntityDataManager.createKey(EntityPortal.class, DataSerializers.VARINT); + + private long lifetime = 1000L; + + @SideOnly(Side.CLIENT) + private IWorldView activeView; + + public EntityPortal(World world) { + super(world); + this.dataManager.register(TARGET_DIMENSION, 0); + } + public EntityPortal(World world, int dimension, int posX, int posY, int posZ) { + this(world); + this.setTarget(dimension); + this.setPosition(posX, posY, posZ); + } + + private void setTarget(int dimension) { + this.dataManager.set(TARGET_DIMENSION, dimension); + } + + private int getTarget() { + return this.dataManager.get(TARGET_DIMENSION); + } + + @Override + protected void entityInit() { + + } + + @SideOnly(Side.CLIENT) + @Override + public void setDead() { + super.setDead(); + this.releaseActiveView(); + } + + @Override + public void onUpdate() { + --this.lifetime; + if (this.lifetime <= 0) { + this.setDead(); + return; + } + super.onUpdate(); + } + + @SideOnly(Side.CLIENT) + public IWorldView getActiveView() { + if (!this.world.isRemote) return null; + if (this.activeView == null) { + this.activeView = ProxyWorldManager.createWorldView(this.getTarget(), null, 160, 240); + if (this.activeView != null) { + if (ModConfigs.alternativePortal) { + this.activeView.setAnimator(new CameraAnimatorPlayer(this.activeView.getCamera(), + this, Minecraft.getMinecraft().player)); + } else { + this.activeView.setAnimator(new CameraAnimatorPivot(this.activeView.getCamera())); + } + } + } + return this.activeView; + } + + @SideOnly(Side.CLIENT) + public void releaseActiveView() { + if (this.activeView != null) ProxyWorldManager.destroyWorldView((WorldView) this.activeView); + this.activeView = null; + } + + @Override + protected void writeEntityToNBT(NBTTagCompound nbt) { + //I do not agree with the capitalization but compatibility with 1.7.10 is more important than my opinion + nbt.setInteger("Dimension", this.getTarget()); + nbt.setLong("lifetime", this.lifetime); + } + + @Override + protected void readEntityFromNBT(NBTTagCompound nbt) { + this.setTarget(nbt.getInteger("Dimension")); + this.lifetime = nbt.getLong("lifetime"); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/imc/IMCAPIRegister.java b/src/main/java/com/xcompwiz/lookingglass/imc/IMCAPIRegister.java index 48b3a3b..535d49b 100644 --- a/src/main/java/com/xcompwiz/lookingglass/imc/IMCAPIRegister.java +++ b/src/main/java/com/xcompwiz/lookingglass/imc/IMCAPIRegister.java @@ -1,53 +1,44 @@ -package com.xcompwiz.lookingglass.imc; - -import java.lang.reflect.Method; - -import com.xcompwiz.lookingglass.api.APIInstanceProvider; -import com.xcompwiz.lookingglass.apiimpl.InternalAPI; -import com.xcompwiz.lookingglass.imc.IMCHandler.IMCProcessor; -import com.xcompwiz.lookingglass.log.LoggerUtils; - -import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; - -public class IMCAPIRegister implements IMCProcessor { - - @Override - public void process(IMCMessage message) { - if (!message.isStringMessage()) return; - LoggerUtils.info(String.format("Receiving API registration request from [%s] for method %s", message.getSender(), message.getStringValue())); - callbackRegistration(message.getStringValue(), message.getSender()); - } - - /** - * This handles the classpath and method location and calling for API IMC calls Based on some code from WAILA. - * @param method The method (prefixed by classname) to call - * @param modname The name of the mod which made the request - */ - public static void callbackRegistration(String method, String modname) { - String[] splitName = method.split("\\."); - String methodName = splitName[splitName.length - 1]; - String className = method.substring(0, method.length() - methodName.length() - 1); - - APIInstanceProvider providerinst = InternalAPI.getAPIProviderInstance(modname); - if (providerinst == null) { - LoggerUtils.error(String.format("Could not initialize API provider instance for %s", modname)); - return; - } - - LoggerUtils.info(String.format("Trying to call (reflection) %s %s", className, methodName)); - - try { - Class reflectClass = Class.forName(className); - Method reflectMethod = reflectClass.getDeclaredMethod(methodName, APIInstanceProvider.class); - reflectMethod.invoke(null, providerinst); - LoggerUtils.info(String.format("API provided to %s", modname)); - } catch (ClassNotFoundException e) { - LoggerUtils.error(String.format("Could not find class %s", className)); - } catch (NoSuchMethodException e) { - LoggerUtils.error(String.format("Could not find method %s", methodName)); - } catch (Exception e) { - LoggerUtils.error(String.format("Exception while calling the method %s.%s", className, methodName)); - e.printStackTrace(); - } - } -} +package com.xcompwiz.lookingglass.imc; + +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.api.APIInstanceProvider; +import com.xcompwiz.lookingglass.apiimpl.InternalAPI; +import net.minecraftforge.fml.common.event.FMLInterModComms; + +import java.lang.reflect.Method; + +public class IMCAPIRegister implements IMCHandler.IMCProcessor { + @Override + public void process(FMLInterModComms.IMCMessage message) { + if (!message.isStringMessage()) return; + LookingGlass.logger().info("Receiving API registration request from [{}] for method {}", message.getSender(), message.getStringValue()); + callbackRegistration(message.getStringValue(), message.getSender()); + } + + public static void callbackRegistration(String method, String modname) { + String[] splitName = method.split("\\."); + String methodName = splitName[splitName.length - 1]; + String className = method.substring(0, method.length() - methodName.length() - 1); + + APIInstanceProvider provider = InternalAPI.getAPIProviderInstance(modname); + if (provider == null) { + LookingGlass.logger().error("Could not initialize API provider instance for {}", modname); + return; + } + LookingGlass.logger().info("Trying to call (reflection) {} {}", className, methodName); + + try { + Class reflectClass = Class.forName(className); + Method reflectMethod = reflectClass.getDeclaredMethod(methodName, APIInstanceProvider.class); + reflectMethod.invoke(null, provider); + LookingGlass.logger().info("API provided to {}", modname); + } catch (ClassNotFoundException e) { + LookingGlass.logger().error("Could not find class {}", className); + } catch (NoSuchMethodException e) { + LookingGlass.logger().error("Could not find method {}", methodName); + } catch (Exception e) { + LookingGlass.logger().error("Exception while calling the method {}.{}", className, methodName); + e.printStackTrace(System.err); + } + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/imc/IMCHandler.java b/src/main/java/com/xcompwiz/lookingglass/imc/IMCHandler.java index 3b5a8e8..6685460 100644 --- a/src/main/java/com/xcompwiz/lookingglass/imc/IMCHandler.java +++ b/src/main/java/com/xcompwiz/lookingglass/imc/IMCHandler.java @@ -1,41 +1,42 @@ -package com.xcompwiz.lookingglass.imc; - -import java.util.HashMap; -import java.util.Map; - -import com.google.common.collect.ImmutableList; -import com.xcompwiz.lookingglass.log.LoggerUtils; - -import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; - -public class IMCHandler { - public interface IMCProcessor { - public void process(IMCMessage message); - } - - private static Map processors = new HashMap(); - - static { - registerProcessor("api", new IMCAPIRegister()); - } - - private static void registerProcessor(String key, IMCProcessor processor) { - processors.put(key.toLowerCase(), processor); - } - - public static void process(ImmutableList messages) { - for (IMCMessage message : messages) { - String key = message.key.toLowerCase(); - IMCProcessor process = processors.get(key); - if (process == null) { - LoggerUtils.error("IMC message '%s' from [%s] unrecognized", key, message.getSender()); - } - try { - process.process(message); - } catch (Exception e) { - LoggerUtils.error("Failed to process IMC message '%s' from [%s]", key, message.getSender()); - e.printStackTrace(); - } - } - } -} +package com.xcompwiz.lookingglass.imc; + +import com.google.common.collect.ImmutableList; +import com.xcompwiz.lookingglass.LookingGlass; +import net.minecraftforge.fml.common.event.FMLInterModComms; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class IMCHandler { + interface IMCProcessor { + void process(FMLInterModComms.IMCMessage message); + } + + private static final Map processors = new HashMap<>(); + + static { + registerProcessor("API", new IMCAPIRegister()); + } + + private static void registerProcessor(String key, IMCProcessor processor) { + processors.put(key, processor); + } + + public static void process(ImmutableList messages) { + for (FMLInterModComms.IMCMessage message : messages) { + String key = message.key.toLowerCase(Locale.ENGLISH); + IMCProcessor process = processors.get(key); + if (process == null) { + LookingGlass.logger().error("IMC message '{}' from [{}] unrecognized", key, message.getSender()); + continue; + } + try { + process.process(message); + } catch (Exception e) { + LookingGlass.logger().error("Failed to process IMC message '{}' from [{}]", key, message.getSender()); + e.printStackTrace(System.err); + } + } + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/log/LoggerUtils.java b/src/main/java/com/xcompwiz/lookingglass/log/LoggerUtils.java deleted file mode 100644 index 5d025dd..0000000 --- a/src/main/java/com/xcompwiz/lookingglass/log/LoggerUtils.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.xcompwiz.lookingglass.log; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import com.xcompwiz.lookingglass.LookingGlass; - -public final class LoggerUtils { - private static Logger log = null; - - /** - * Configure the logger - */ - private static void configureLogging() { - log = LogManager.getLogger(LookingGlass.MODID); - } - - public static void log(Level level, String message, Object... params) { - if (log == null) { - configureLogging(); - } - if (message == null) { - log.log(level, "Attempted to log null message."); - } else { - try { - message = String.format(message, params); - } catch (Exception e) { - } - log.log(level, message); - } - } - - public static void info(String message, Object... params) { - log(Level.INFO, message, params); - } - - public static void warn(String message, Object... params) { - log(Level.WARN, message, params); - } - - public static void error(String message, Object... params) { - log(Level.ERROR, message, params); - } - - public static void debug(String message, Object... params) { - //NOPE - } -} diff --git a/src/main/java/com/xcompwiz/lookingglass/network/LookingGlassPacketManager.java b/src/main/java/com/xcompwiz/lookingglass/network/LookingGlassPacketManager.java index d8453c1..9fbeff8 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/LookingGlassPacketManager.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/LookingGlassPacketManager.java @@ -1,87 +1,75 @@ -package com.xcompwiz.lookingglass.network; - -import io.netty.buffer.ByteBuf; - -import java.util.HashMap; - -import net.minecraft.client.Minecraft; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.NetHandlerPlayServer; -import net.minecraft.network.NetworkManager; - -import com.xcompwiz.lookingglass.log.LoggerUtils; -import com.xcompwiz.lookingglass.network.packet.PacketHandlerBase; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.network.FMLEventChannel; -import cpw.mods.fml.common.network.FMLNetworkEvent.ClientCustomPacketEvent; -import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent; -import cpw.mods.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -public class LookingGlassPacketManager { - - public static final String CHANNEL = "lookingglass"; - public static FMLEventChannel bus; - - private static HashMap packethandlers = new HashMap(); - private static HashMap, Byte> idmap = new HashMap, Byte>(); - - /** - * Register a new packet handler to the manager. We use pre-defined packet ids to avoid mismatched packet ids across client-server communications. - * @param handler The packet handler to register - * @param id The id to which the handler should be bound - */ - public static void registerPacketHandler(PacketHandlerBase handler, byte id) { - if (packethandlers.get(id) != null) { throw new RuntimeException("Multiple id registrations for packet type on " + CHANNEL + " channel"); } - packethandlers.put(id, handler); - idmap.put(handler.getClass(), id); - } - - public static byte getId(PacketHandlerBase handler) { - return getId(handler.getClass()); - } - - public static byte getId(Class handlerclass) { - if (!idmap.containsKey(handlerclass)) throw new RuntimeException("Attempted to get id for unregistered network message handler."); - return idmap.get(handlerclass); - } - - @SubscribeEvent - @SideOnly(Side.CLIENT) - public void onPacketData(ClientCustomPacketEvent event) { - FMLProxyPacket pkt = event.packet; - - onPacketData(event.manager, pkt, Minecraft.getMinecraft().thePlayer); - } - - @SubscribeEvent - public void onPacketData(ServerCustomPacketEvent event) { - FMLProxyPacket pkt = event.packet; - - onPacketData(event.manager, pkt, ((NetHandlerPlayServer) event.handler).playerEntity); - } - - public void onPacketData(NetworkManager manager, FMLProxyPacket packet, EntityPlayer player) { - try { - if (packet == null || packet.payload() == null) { throw new RuntimeException("Empty packet sent to " + CHANNEL + " channel"); } - ByteBuf data = packet.payload(); - byte type = data.readByte(); - - try { - PacketHandlerBase handler = packethandlers.get(type); - if (handler == null) { throw new RuntimeException("Unrecognized packet sent to " + CHANNEL + " channel"); } - handler.handle(data, player); - } catch (Exception e) { - LoggerUtils.warn("PacketHandler: Failed to handle packet type " + type); - LoggerUtils.warn(e.toString()); - e.printStackTrace(); - } - } catch (Exception e) { - LoggerUtils.warn("PacketHandler: Failed to read packet"); - LoggerUtils.warn(e.toString()); - e.printStackTrace(); - } - } -} +package com.xcompwiz.lookingglass.network; + +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.network.packet.PacketHandlerBase; +import io.netty.buffer.ByteBuf; +import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap; +import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ByteMap; +import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.NetworkManager; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.network.FMLEventChannel; +import net.minecraftforge.fml.common.network.FMLNetworkEvent; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class LookingGlassPacketManager { + public static final String CHANNEL = "lookingglass"; + public static FMLEventChannel bus; + + private static final Byte2ObjectMap handlers = new Byte2ObjectOpenHashMap<>(); + private static final Object2ByteMap> idMap = new Object2ByteOpenHashMap<>(); + + public static void registerPacketHandler(PacketHandlerBase handler, byte id) { + if (handlers.get(id) != null) throw new RuntimeException("Multiple id registrations for packet type on " + CHANNEL + "channel"); + handlers.put(id, handler); + idMap.put(handler.getClass(), id); + } + + public static byte getId(PacketHandlerBase handler) { + return getId(handler.getClass()); + } + public static byte getId(Class clazz) { + if (!idMap.containsKey(clazz)) throw new RuntimeException("Attempted to get id for unregistered network message handler"); + return idMap.get(clazz); + } + + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onPacketData(FMLNetworkEvent.ClientCustomPacketEvent event) { + FMLProxyPacket packet = event.getPacket(); + this.onPacketData(event.getManager(), packet, Minecraft.getMinecraft().player); + } + + @SubscribeEvent + public void onPacketData(FMLNetworkEvent.ServerCustomPacketEvent event) { + FMLProxyPacket packet = event.getPacket(); + this.onPacketData(event.getManager(), packet, ((NetHandlerPlayServer)event.getHandler()).player); + } + + public void onPacketData(NetworkManager manager, FMLProxyPacket packet, EntityPlayer player) { + try { + if (packet == null || packet.payload() == null) throw new RuntimeException("Empty packet sent to " + CHANNEL + " channel"); + ByteBuf data = packet.payload(); + byte type = data.readByte(); + try { + PacketHandlerBase handler = handlers.get(type); + if (handler == null) throw new RuntimeException("Unrecognized packet sent to " + CHANNEL + " channel"); + handler.handle(data, player); + } catch (Exception e) { + LookingGlass.logger().warn("PacketHandler: Failed handle packet type {}", type); + LookingGlass.logger().warn(e.toString()); + e.printStackTrace(System.err); + } + } catch (Exception e) { + LookingGlass.logger().warn("PacketHandler: Failed to read packet"); + LookingGlass.logger().warn(e.toString()); + e.printStackTrace(System.err); + } + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/network/PacketHolder.java b/src/main/java/com/xcompwiz/lookingglass/network/PacketHolder.java index d69a451..846e0a0 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/PacketHolder.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/PacketHolder.java @@ -2,30 +2,26 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; -/** - * @author Ken Butler/shadowking97 - */ -// TODO: This class doesn't need to exist, it's just a (Player, Packet) tuple public class PacketHolder { - EntityPlayer player; - FMLProxyPacket packet; + final EntityPlayer player; + final FMLProxyPacket packet; - public PacketHolder(EntityPlayer player, FMLProxyPacket packet) { - this.player = player; - this.packet = packet; - } + public PacketHolder(EntityPlayer player, FMLProxyPacket packet) { + this.player = player; + this.packet = packet; + } - public boolean belongsToPlayer(EntityPlayer p) { - return player.equals(p); - } + public boolean belongsToPlayer(EntityPlayer player) { + return this.player.equals(player); + } - public int sendPacket() { - if (packet != null) { - LookingGlassPacketManager.bus.sendTo(packet, (EntityPlayerMP) player); - return packet.payload().writerIndex(); - } - return 0; - } + public int sendPacket() { + if (this.packet != null) { + LookingGlassPacketManager.bus.sendTo(this.packet, (EntityPlayerMP) this.player); + return this.packet.payload().writerIndex(); + } + return 0; + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/ServerPacketDispatcher.java b/src/main/java/com/xcompwiz/lookingglass/network/ServerPacketDispatcher.java index 971736c..7ca526c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/ServerPacketDispatcher.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/ServerPacketDispatcher.java @@ -1,93 +1,87 @@ package com.xcompwiz.lookingglass.network; -import java.util.LinkedList; -import java.util.List; - -import net.minecraft.entity.player.EntityPlayer; - import com.xcompwiz.lookingglass.proxyworld.ModConfigs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import java.util.LinkedList; +import java.util.List; -/** - * This class is a variant o nthe vanilla server packet dispatcher. We use it so that we can send data to cleitns in a limited (throttled) manner. This allows - * server admins to limit how much bandwidth LookingGlass consumes on a server. - */ public class ServerPacketDispatcher extends Thread { + private static ServerPacketDispatcher instance; - private static ServerPacketDispatcher instance; - - private List packets; - private boolean isRunning = true; + private final List packets; + private boolean isRunning = true; - private ServerPacketDispatcher() { - packets = new LinkedList(); - } + private ServerPacketDispatcher() { + this.packets = new LinkedList<>(); + } - public static ServerPacketDispatcher getInstance() { - if (instance == null) instance = new ServerPacketDispatcher(); - return instance; - } + public static ServerPacketDispatcher getInstance() { + if (instance == null) instance = new ServerPacketDispatcher(); + return instance; + } - public static void shutdown() { - if (instance != null) instance.halt(); - instance = null; - } + public static void shutdown() { + if (instance != null) instance.halt(); + instance = null; + } - public void addPacket(EntityPlayer player, FMLProxyPacket packet) { - synchronized (this) { - packets.add(new PacketHolder(player, packet)); - this.notify(); - } - } + public void addPacket(EntityPlayer player, FMLProxyPacket packet) { + synchronized (this) { + this.packets.add(new PacketHolder(player, packet)); + this.notify(); + } + } - public void removeAllPacketsOf(EntityPlayer player) { - synchronized (this) { - for (int j = 0; j < packets.size(); ++j) { - if (packets.get(j).belongsToPlayer(player)) { - packets.remove(--j); - } - } - } - } + public void removeAllPacketsOf(EntityPlayer player) { + synchronized (this) { + for (int i = 0; i < this.packets.size(); i++) { + if (this.packets.get(i).belongsToPlayer(player)) { + this.packets.remove(i); + --i; + } + } + } + } - public void tick() { - int byteLimit = ModConfigs.dataRate; - for (int bytes = 0; bytes < byteLimit && !packets.isEmpty();) { - PacketHolder p = packets.get(0); - bytes += p.sendPacket(); - packets.remove(0); - } - } + public void tick() { + int byteLimit = ModConfigs.dataRate; + for (int bytes = 0; bytes < byteLimit && !this.packets.isEmpty();) { + PacketHolder holder = this.packets.get(0); + bytes += holder.sendPacket(); + this.packets.remove(0); + } + } - public void halt() { - synchronized (this) { - isRunning = false; - packets.clear(); - } - } + public void halt() { + synchronized (this) { + this.isRunning = false; + this.packets.clear(); + } + } - @Override - public void run() { - while (isRunning) { - if (packets.size() > 0) { - try { - synchronized (this) { - tick(); - this.wait(20); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - } else { - try { - synchronized (this) { - this.wait(1000); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - } + @Override + public void run() { + while (this.isRunning) { + if (!this.packets.isEmpty()) { + try { + synchronized (this) { + this.tick(); + this.wait(20); + } + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + } else { + try { + synchronized (this) { + this.wait(1000); + } + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + } + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketChunkInfo.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketChunkInfo.java index 40aec1e..23d4f1e 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketChunkInfo.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketChunkInfo.java @@ -1,258 +1,195 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import com.xcompwiz.lookingglass.client.proxyworld.WorldView; +import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; import io.netty.buffer.ByteBuf; - -import java.util.concurrent.Semaphore; -import java.util.zip.DataFormatException; -import java.util.zip.Deflater; -import java.util.zip.Inflater; - +import net.minecraft.block.state.IBlockState; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.play.server.S21PacketChunkData; -import net.minecraft.network.play.server.S21PacketChunkData.Extracted; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.WorldProviderSurface; import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.NibbleArray; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; -import com.xcompwiz.lookingglass.client.proxyworld.WorldView; -import com.xcompwiz.lookingglass.log.LoggerUtils; -import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import java.util.concurrent.Semaphore; +import java.util.zip.Deflater; /** - * Based on code from Ken Butler/shadowking97 + * Chunk serialization changed between 1.7.10 and 1.12.2, so I had to freestyle here. I hope it doesn't break... */ public class PacketChunkInfo extends PacketHandlerBase { - private static byte[] inflatearray; - private static byte[] dataarray; - private static Semaphore deflateGate = new Semaphore(1); - - private static int deflate(byte[] chunkData, byte[] compressedChunkData) { - Deflater deflater = new Deflater(-1); - if (compressedChunkData == null) return 0; - int bytesize = 0; - try { - deflater.setInput(chunkData, 0, chunkData.length); - deflater.finish(); - bytesize = deflater.deflate(compressedChunkData); - } finally { - deflater.end(); - } - return bytesize; - } - - public static FMLProxyPacket createPacket(Chunk chunk, boolean includeinit, int subid, int dim) { - int xPos = chunk.xPosition; - int zPos = chunk.zPosition; - Extracted extracted = getMapChunkData(chunk, includeinit, subid); - int yMSBPos = extracted.field_150281_c; - int yPos = extracted.field_150280_b; - byte[] chunkData = extracted.field_150282_a; - - deflateGate.acquireUninterruptibly(); - byte[] compressedChunkData = new byte[chunkData.length]; - int len = deflate(chunkData, compressedChunkData); - deflateGate.release(); - - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); - - data.writeInt(dim); - data.writeInt(xPos); - data.writeInt(zPos); - data.writeBoolean(includeinit); - data.writeShort((short) (yPos & 65535)); - data.writeShort((short) (yMSBPos & 65535)); - data.writeInt(len); - data.writeInt(chunkData.length); - data.ensureWritable(len); - data.writeBytes(compressedChunkData, 0, len); - - return buildPacket(data); - } - - @Override - public void handle(ByteBuf in, EntityPlayer player) { - int dim = in.readInt(); - int xPos = in.readInt(); - int zPos = in.readInt(); - boolean reqinit = in.readBoolean(); - short yPos = in.readShort(); - short yMSBPos = in.readShort(); - int compressedsize = in.readInt(); - int uncompressedsize = in.readInt(); - byte[] chunkData = inflateChunkData(in, compressedsize, uncompressedsize); - - if (chunkData == null) { - LookingGlassPacketManager.bus.sendToServer(PacketRequestChunk.createPacket(xPos, yPos, zPos, dim)); - LoggerUtils.error("Chunk decompression failed: %d\t:\t%d\t\t%d : %d\n", yMSBPos, yPos, compressedsize, uncompressedsize); - return; - } - handle(player, chunkData, dim, xPos, zPos, reqinit, yPos, yMSBPos); - } - - public void handle(EntityPlayer player, byte[] chunkData, int dim, int xPos, int zPos, boolean reqinit, short yPos, short yMSBPos) { - WorldClient proxyworld = ProxyWorldManager.getProxyworld(dim); - if (proxyworld == null) return; - if (proxyworld.provider.dimensionId != dim) return; - - //TODO: Test to see if this first part is even necessary - Chunk chunk = proxyworld.getChunkProvider().provideChunk(xPos, zPos); - if (reqinit && (chunk == null || chunk.isEmpty())) { - if (yPos == 0) { - proxyworld.doPreChunk(xPos, zPos, false); - return; - } - proxyworld.doPreChunk(xPos, zPos, true); - } - // End possible removal section - proxyworld.invalidateBlockReceiveRegion(xPos << 4, 0, zPos << 4, (xPos << 4) + 15, 256, (zPos << 4) + 15); - chunk = proxyworld.getChunkFromChunkCoords(xPos, zPos); - if (reqinit && (chunk == null || chunk.isEmpty())) { - proxyworld.doPreChunk(xPos, zPos, true); - chunk = proxyworld.getChunkFromChunkCoords(xPos, zPos); - } - if (chunk != null) { - chunk.fillChunk(chunkData, yPos, yMSBPos, reqinit); - receivedChunk(proxyworld, xPos, zPos); - if (!reqinit || !(proxyworld.provider instanceof WorldProviderSurface)) { - chunk.resetRelightChecks(); - } - } - } - - public void receivedChunk(WorldClient worldObj, int cx, int cz) { - worldObj.markBlockRangeForRenderUpdate(cx << 4, 0, cz << 4, (cx << 4) + 15, 256, (cz << 4) + 15); - Chunk c = worldObj.getChunkFromChunkCoords(cx, cz); - if (c == null || c.isEmpty()) return; - - for (WorldView activeview : ProxyWorldManager.getWorldViews(worldObj.provider.dimensionId)) { - activeview.onChunkReceived(cx, cz); - } - - int x = (cx << 4); - int z = (cz << 4); - for (int y = 0; y < worldObj.getActualHeight(); y += 16) { - if (c.getAreLevelsEmpty(y, y)) continue; - for (int x2 = 0; x2 < 16; ++x2) { - for (int z2 = 0; z2 < 16; ++z2) { - for (int y2 = 0; y2 < 16; ++y2) { - int lx = x + x2; - int ly = y + y2; - int lz = z + z2; - if (worldObj.getBlock(lx, ly, lz).hasTileEntity(worldObj.getBlockMetadata(lx, ly, lz))) { - LookingGlassPacketManager.bus.sendToServer(PacketRequestTE.createPacket(lx, ly, lz, worldObj.provider.dimensionId)); - } - } - } - } - } - } - - private byte[] inflateChunkData(ByteBuf in, int compressedsize, int uncompressedsize) { - if (inflatearray == null || inflatearray.length < compressedsize) { - inflatearray = new byte[compressedsize]; - } - in.readBytes(inflatearray, 0, compressedsize); - byte[] chunkData = new byte[uncompressedsize]; - Inflater inflater = new Inflater(); - inflater.setInput(inflatearray, 0, compressedsize); - - try { - inflater.inflate(chunkData); - } catch (DataFormatException e) { - return null; - } finally { - inflater.end(); - } - return chunkData; - } - - public static Extracted getMapChunkData(Chunk chunk, boolean includeinit, int subid) { - int j = 0; - ExtendedBlockStorage[] aextendedblockstorage = chunk.getBlockStorageArray(); - int k = 0; - S21PacketChunkData.Extracted extracted = new S21PacketChunkData.Extracted(); - if (dataarray == null || dataarray.length < 196864) { - dataarray = new byte[196864]; - } - byte[] abyte = dataarray; - - if (includeinit) { - chunk.sendUpdates = true; - } - - int l; - - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && (subid & 1 << l) != 0) { - extracted.field_150280_b |= 1 << l; - - if (aextendedblockstorage[l].getBlockMSBArray() != null) { - extracted.field_150281_c |= 1 << l; - ++k; - } - } - } - - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && (subid & 1 << l) != 0) { - byte[] abyte1 = aextendedblockstorage[l].getBlockLSBArray(); - System.arraycopy(abyte1, 0, abyte, j, abyte1.length); - j += abyte1.length; - } - } - - NibbleArray nibblearray; - - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && (subid & 1 << l) != 0) { - nibblearray = aextendedblockstorage[l].getMetadataArray(); - System.arraycopy(nibblearray.data, 0, abyte, j, nibblearray.data.length); - j += nibblearray.data.length; - } - } - - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && (subid & 1 << l) != 0) { - nibblearray = aextendedblockstorage[l].getBlocklightArray(); - System.arraycopy(nibblearray.data, 0, abyte, j, nibblearray.data.length); - j += nibblearray.data.length; - } - } - - if (!chunk.worldObj.provider.hasNoSky) { - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && (subid & 1 << l) != 0) { - nibblearray = aextendedblockstorage[l].getSkylightArray(); - System.arraycopy(nibblearray.data, 0, abyte, j, nibblearray.data.length); - j += nibblearray.data.length; - } - } - } - - if (k > 0) { - for (l = 0; l < aextendedblockstorage.length; ++l) { - if (aextendedblockstorage[l] != null && (!includeinit || !aextendedblockstorage[l].isEmpty()) && aextendedblockstorage[l].getBlockMSBArray() != null && (subid & 1 << l) != 0) { - nibblearray = aextendedblockstorage[l].getBlockMSBArray(); - System.arraycopy(nibblearray.data, 0, abyte, j, nibblearray.data.length); - j += nibblearray.data.length; - } - } - } - - if (includeinit) { - byte[] abyte2 = chunk.getBiomeArray(); - System.arraycopy(abyte2, 0, abyte, j, abyte2.length); - j += abyte2.length; - } - - extracted.field_150282_a = new byte[j]; - System.arraycopy(abyte, 0, extracted.field_150282_a, 0, j); - return extracted; - } + private static byte[] inflateArray; + private static byte[] dataArray; + private static final Semaphore deflateGate = new Semaphore(1); + + private static int deflate(byte[] chunkData, byte[] compressedChunkData) { + if (compressedChunkData == null) return 0; + Deflater deflater = new Deflater(-1); + int byteSize = 0; + try { + deflater.setInput(chunkData, 0, chunkData.length); + deflater.finish(); + byteSize = deflater.deflate(compressedChunkData); + } finally { + deflater.end(); + } + return byteSize; + } + + public static FMLProxyPacket createPacket(Chunk chunk, boolean includeInit, int subID, int dimension) { + LookingGlass.logger().debug("Sending chunk info"); + int x = chunk.x; + int z = chunk.z; + + PacketBuffer buffer = new PacketBuffer(PacketHandlerBase.createDataBuffer(PacketChunkInfo.class)); + int blockMask = 0; + int blockLightMask = 0; + int skyLightMask = 0; + ExtendedBlockStorage[] storages = chunk.getBlockStorageArray(); + for (int l = 0; l < storages.length; l++) { + ExtendedBlockStorage storage = storages[l]; + int m = (1 << l); + if (storage != null && (!includeInit || !storage.isEmpty()) && (subID & m) != 0) { + blockMask |= m; + blockLightMask |= m; + } + } + if (chunk.getWorld().provider.hasSkyLight()) { + for (int l = 0; l < storages.length; l++) { + ExtendedBlockStorage storage = storages[l]; + int m = (1 << l); + if (storage != null && (!includeInit || !storage.isEmpty()) && (subID & m) != 0) { + skyLightMask |= m; + } + } + } + + buffer.writeInt(dimension); + buffer.writeInt(x); + buffer.writeInt(z); + buffer.writeBoolean(includeInit); //basically "include biomes" here + buffer.writeInt(blockMask); + buffer.writeInt(blockLightMask); + buffer.writeInt(skyLightMask); + if (includeInit) { + buffer.writeBytes(chunk.getBiomeArray()); + } + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((blockMask & m) != 0) { + storages[l].getData().write(buffer); + } + } + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((blockLightMask & m) != 0) { + buffer.writeBytes(storages[l].getBlockLight().getData()); + } + } + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((skyLightMask & m) != 0) { + buffer.writeBytes(storages[l].getSkyLight().getData()); + } + } + + return buildPacket(buffer); + } + + @Override + public void handle(ByteBuf data, EntityPlayer player) { + LookingGlass.logger().debug("Received chunk info"); + int dimension = data.readInt(); + int x = data.readInt(); + int z = data.readInt(); + boolean includeInit = data.readBoolean(); + int blockMask = data.readInt(); + int blockLightMask = data.readInt(); + int skyLightMask = data.readInt(); + + WorldClient proxyWorld = ProxyWorldManager.getProxyWorld(dimension); + if (proxyWorld == null) return; + if (proxyWorld.provider.getDimension() != dimension) return; + + Chunk chunk = proxyWorld.getChunkProvider().getLoadedChunk(x, z); + if (includeInit && (chunk == null || chunk.isEmpty())) { + proxyWorld.doPreChunk(x, z, true); + } + proxyWorld.invalidateBlockReceiveRegion(x << 4, 0, z << 4, (x << 4) + 15, 256, (z << 4) + 15); + chunk = proxyWorld.getChunkFromChunkCoords(x, z); + if (chunk != null) { + if (includeInit) { + data.readBytes(chunk.getBiomeArray()); + } + boolean flag = chunk.getWorld().provider.hasSkyLight(); + ExtendedBlockStorage[] storages = chunk.getBlockStorageArray(); + PacketBuffer buffer = new PacketBuffer(data); //wrap + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((blockMask & m) != 0) { + if (storages[l] == null) storages[l] = new ExtendedBlockStorage(l, flag); + ExtendedBlockStorage storage = storages[l]; + storage.getData().read(buffer); + } + } + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((blockLightMask & m) != 0) { + if (storages[l] == null) storages[l] = new ExtendedBlockStorage(l, flag); + ExtendedBlockStorage storage = storages[l]; + buffer.readBytes(storage.getBlockLight().getData()); + } + } + for (int l = 0; l < storages.length; l++) { + int m = 1 << l; + if ((skyLightMask & m) != 0) { + if (storages[l] == null) storages[l] = new ExtendedBlockStorage(l, flag); + ExtendedBlockStorage storage = storages[l]; + buffer.readBytes(storage.getSkyLight().getData()); + } + } + + this.receivedChunk(proxyWorld, x, z); + if (!includeInit || !(proxyWorld.provider instanceof WorldProviderSurface)) { + chunk.resetRelightChecks(); + } + } + } + + public void receivedChunk(WorldClient world, int cx, int cz) { + world.markBlockRangeForRenderUpdate(cx << 4, 0, cz << 4, (cx << 4) + 15, 256, (cz << 4) + 15); + Chunk c = world.getChunkFromChunkCoords(cx, cz); + if (c == null || c.isEmpty()) { + LookingGlass.logger().debug("Ignoring chunk at {} {}", cx, cz); + return; + } + + for (WorldView activeView : ProxyWorldManager.getWorldViews(world.provider.getDimension())) { + activeView.onChunkReceived(cx, cz); + } + + BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(); + int x = cx << 4; + int z = cz << 4; + for (int y = 0; y < world.getActualHeight(); y += 16) { + if (c.isEmptyBetween(y, y)) continue; + for (int x2 = 0; x2 < 16; x2++) { + for (int z2 = 0; z2 < 16; z2++) { + for (int y2 = 0; y2 < 16; y2++) { + int lx = x + x2; + int ly = y + y2; + int lz = z + z2; + IBlockState state = world.getBlockState(mutable.setPos(lx, ly, lz)); + if (state.getBlock().hasTileEntity(state)) { + LookingGlassPacketManager.bus.sendToServer(PacketRequestTE.createPacket(lx, ly, lz, world.provider.getDimension())); + } + } + } + } + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCloseView.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCloseView.java index 5dbafab..8cc8c2f 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCloseView.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCloseView.java @@ -1,28 +1,21 @@ package com.xcompwiz.lookingglass.network.packet; -import io.netty.buffer.ByteBuf; -import net.minecraft.entity.player.EntityPlayer; - import com.xcompwiz.lookingglass.client.proxyworld.WorldView; import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; public class PacketCloseView extends PacketHandlerBase { - @SideOnly(Side.CLIENT) - public static FMLProxyPacket createPacket(WorldView worldview) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); - - return buildPacket(data); - } - - @Override - public void handle(ByteBuf data, EntityPlayer player) { - if (ModConfigs.disabled) return; + @SideOnly(Side.CLIENT) + public static FMLProxyPacket createPacket(WorldView view) { + return buildPacket(createDataBuffer(PacketCloseView.class)); + } - //TODO: make closing viewpoint aware. See PacketCreateView - } + @Override + public void handle(ByteBuf data, EntityPlayer player) { + if (ModConfigs.disabled) return; + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCreateView.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCreateView.java index 3e85563..7fbd18e 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCreateView.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketCreateView.java @@ -1,80 +1,75 @@ package com.xcompwiz.lookingglass.network.packet; -import io.netty.buffer.ByteBuf; -import net.minecraft.client.Minecraft; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.world.WorldServer; -import net.minecraftforge.common.DimensionManager; - +import com.xcompwiz.lookingglass.LookingGlass; import com.xcompwiz.lookingglass.api.event.ClientWorldInfoEvent; import com.xcompwiz.lookingglass.client.proxyworld.WorldView; import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; import com.xcompwiz.lookingglass.proxyworld.ChunkFinder; import com.xcompwiz.lookingglass.proxyworld.ChunkFinderManager; import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; public class PacketCreateView extends PacketHandlerBase { - @SideOnly(Side.CLIENT) - public static FMLProxyPacket createPacket(WorldView worldview) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); + @SideOnly(Side.CLIENT) + public static FMLProxyPacket createPacket(WorldView view) { + ByteBuf data = createDataBuffer(PacketCreateView.class); - int x = 0; - int y = -1; - int z = 0; - if (worldview.coords != null) { - x = worldview.coords.posX >> 4; - y = worldview.coords.posY >> 4; - z = worldview.coords.posZ >> 4; - } + int x = 0; + int y = -1; + int z = 0; + if (view.pos != null) { + x = view.pos.getX() >> 4; + y = view.pos.getY() >> 4; + z = view.pos.getZ() >> 4; + } - data.writeInt(worldview.getWorldObj().provider.dimensionId); - data.writeInt(x); - data.writeInt(y); - data.writeInt(z); - data.writeByte(Math.min(ModConfigs.renderDistance, Minecraft.getMinecraft().gameSettings.renderDistanceChunks)); + data.writeInt(view.getWorld().provider.getDimension()); + data.writeInt(x); + data.writeInt(y); + data.writeInt(z); + data.writeByte(Math.min(ModConfigs.renderDistance, Minecraft.getMinecraft().gameSettings.renderDistanceChunks)); - return buildPacket(data); - } + return buildPacket(data); + } - @Override - public void handle(ByteBuf data, EntityPlayer player) { - if (ModConfigs.disabled) return; - int dim = data.readInt(); - int xPos = data.readInt(); - int yPos = data.readInt(); - int zPos = data.readInt(); - byte renderDistance = data.readByte(); + @Override + public void handle(ByteBuf data, EntityPlayer player) { + if (ModConfigs.disabled) return; + LookingGlass.logger().debug("Received view request"); + int dimension = data.readInt(); + int xPos = data.readInt(); + int yPos = data.readInt(); + int zPos = data.readInt(); + byte renderDistance = data.readByte(); - if (!DimensionManager.isDimensionRegistered(dim)) return; - WorldServer world = MinecraftServer.getServer().worldServerForDimension(dim); - if (world == null) return; - int x; - int y; - int z; - if (yPos < 0) { - ChunkCoordinates c = world.getSpawnPoint(); - x = c.posX >> 4; - y = c.posY >> 4; - z = c.posZ >> 4; - } else { - x = xPos; - y = yPos; - z = zPos; - } - if (renderDistance > ModConfigs.renderDistance) renderDistance = ModConfigs.renderDistance; - ChunkFinderManager.instance.addFinder(new ChunkFinder(new ChunkCoordinates(x, y, z), dim, world.getChunkProvider(), player, renderDistance)); - //TODO: Add to tracking list. Send time/data updates at intervals. Keep in mind to catch player disconnects when tracking clients. - //Register ChunkFinder, and support change of finder location. - //TODO: This is a repeat of the handling of PacketRequestWorldInfo - net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new ClientWorldInfoEvent(dim, (EntityPlayerMP) player)); - LookingGlassPacketManager.bus.sendTo(PacketWorldInfo.createPacket(dim), (EntityPlayerMP) player); - } + if (!DimensionManager.isDimensionRegistered(dimension)) return; + WorldServer world = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dimension); + if (world == null) return; + int x, y, z; + if (yPos < 0) { + BlockPos pos = world.getSpawnPoint(); + x = pos.getX() >> 4; + y = pos.getY() >> 4; + z = pos.getZ() >> 4; + } else { + x = xPos; + y = yPos; + z = zPos; + } + if (renderDistance > ModConfigs.renderDistance) renderDistance = ModConfigs.renderDistance; + ChunkFinderManager.instance.addFinder(new ChunkFinder(new BlockPos(x, y, z), dimension, world.getChunkProvider(), player, renderDistance)); + MinecraftForge.EVENT_BUS.post(new ClientWorldInfoEvent(dimension, (EntityPlayerMP) player)); + LookingGlassPacketManager.bus.sendTo(PacketWorldInfo.createPacket(dimension), (EntityPlayerMP) player); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketHandlerBase.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketHandlerBase.java index e982ba3..063aa72 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketHandlerBase.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketHandlerBase.java @@ -1,37 +1,43 @@ -package com.xcompwiz.lookingglass.network.packet; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import net.minecraft.entity.player.EntityPlayer; - -import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; - -/** - * This class is the parent of the packet handling classes for our network communication. Mostly contains helper functions. - */ -public abstract class PacketHandlerBase { - - /** - * Called by our packet manager to process packet data - */ - public abstract void handle(ByteBuf data, EntityPlayer player); - - /** - * Used by the progeny of this class in order to produce and prepare the buffer for packet data. Includes writing the correct packet id for the packet. - */ - public static ByteBuf createDataBuffer(Class handlerclass) { - ByteBuf data = Unpooled.buffer(); - data.writeByte(LookingGlassPacketManager.getId(handlerclass)); - return data; - } - - /** - * Used by the progeny of this class in order to produce a packet object from the data buffer. Automatically uses our packet channel so that the manager on - * the other side will receive the packet. - */ - protected static FMLProxyPacket buildPacket(ByteBuf payload) { - return new FMLProxyPacket(payload, LookingGlassPacketManager.CHANNEL); - } -} +package com.xcompwiz.lookingglass.network.packet; + +import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; + +/** + * This class is the parent of the packet handling classes for our network communication. Mostly contains helper functions. + */ +public abstract class PacketHandlerBase { + /** + * Called by our packet manager to process packet data + */ + public abstract void handle(ByteBuf data, EntityPlayer player); + + /** + * Used by the progeny of this class in order to produce and prepare the buffer for packet data. Includes writing the correct packet id for the packet. + */ + public static ByteBuf createDataBuffer(Class clazz) { + ByteBuf data = Unpooled.buffer(); + data.writeByte(LookingGlassPacketManager.getId(clazz)); + return data; + } + + /** + * Used by the progeny of this class in order to produce a packet object from the data buffer. Automatically uses our packet channel so that the manager on + * the other side will receive the packet. + */ + protected static FMLProxyPacket buildPacket(ByteBuf payload) { + return new FMLProxyPacket(new PacketBuffer(payload), LookingGlassPacketManager.CHANNEL); + } + + /** + * Used by the progeny of this class in order to produce a packet object from the data buffer. Automatically uses our packet channel so that the manager on + * the other side will receive the packet. Uses the packet buffer directly instead of a wrapped ByteBuf. + */ + protected static FMLProxyPacket buildPacket(PacketBuffer payload) { + return new FMLProxyPacket(payload, LookingGlassPacketManager.CHANNEL); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestChunk.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestChunk.java index 7211087..01af272 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestChunk.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestChunk.java @@ -1,43 +1,39 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; +import com.xcompwiz.lookingglass.proxyworld.ChunkFinder; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.server.MinecraftServer; import net.minecraft.world.WorldServer; import net.minecraft.world.chunk.Chunk; import net.minecraftforge.common.DimensionManager; - -import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; -import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; public class PacketRequestChunk extends PacketHandlerBase { - public static FMLProxyPacket createPacket(int xPos, int yPos, int zPos, int dim) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); - - data.writeInt(dim); - data.writeInt(xPos); - data.writeInt(yPos); - data.writeInt(zPos); - - return buildPacket(data); - } - - @Override - public void handle(ByteBuf data, EntityPlayer player) { - if (ModConfigs.disabled) return; - int dim = data.readInt(); - int xPos = data.readInt(); - int yPos = data.readInt(); - int zPos = data.readInt(); - - if (!DimensionManager.isDimensionRegistered(dim)) return; - WorldServer world = MinecraftServer.getServer().worldServerForDimension(dim); - if (world == null) return; - Chunk chunk = world.getChunkFromChunkCoords(xPos, zPos); - if (!chunk.isChunkLoaded) chunk = world.getChunkProvider().loadChunk(xPos, zPos); - ServerPacketDispatcher.getInstance().addPacket(player, PacketChunkInfo.createPacket(chunk, true, yPos, dim)); - } + public static FMLProxyPacket createPacket(int x, int y, int z, int dimension) { + ByteBuf data = createDataBuffer(PacketRequestChunk.class); + + data.writeInt(dimension); + data.writeInt(x).writeInt(y).writeInt(z); + + return buildPacket(data); + } + + @Override + public void handle(ByteBuf data, EntityPlayer player) { + if (ModConfigs.disabled) return; + int dimension = data.readInt(); + int x = data.readInt(); + int y = data.readInt(); + int z = data.readInt(); + + if (!DimensionManager.isDimensionRegistered(dimension)) return; + WorldServer world = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dimension); + if (world == null) return; + Chunk chunk = world.getChunkFromChunkCoords(x, z); + if (!chunk.isLoaded()) chunk = ChunkFinder.loadChunk(world.getChunkProvider(), x, z); + ServerPacketDispatcher.getInstance().addPacket(player, PacketChunkInfo.createPacket(chunk, true, y, dimension)); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestTE.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestTE.java index 7aa20bf..5417e22 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestTE.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestTE.java @@ -1,51 +1,42 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; - -import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; -import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; public class PacketRequestTE extends PacketHandlerBase { - public static FMLProxyPacket createPacket(int xPos, int yPos, int zPos, int dim) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); - - data.writeInt(dim); - data.writeInt(xPos); - data.writeInt(yPos); - data.writeInt(zPos); - - return buildPacket(data); - } - - @Override - public void handle(ByteBuf data, EntityPlayer player) { - if (ModConfigs.disabled) return; - int dim = data.readInt(); - int xPos = data.readInt(); - int yPos = data.readInt(); - int zPos = data.readInt(); - - if (!DimensionManager.isDimensionRegistered(dim)) return; - WorldServer world = MinecraftServer.getServer().worldServerForDimension(dim); - if (world == null) return; - TileEntity tile = world.getTileEntity(xPos, yPos, zPos); - if (tile != null) { - //FIXME: This is currently a very "forceful" method of doing this, and not technically guaranteed to produce correct results - // This would be much better handled by using the getDescriptionPacket method and wrapping that packet in a LookingGlass - // packet to control delivery timing, allowing for processing the packet while the correct target world is the active world - // This idea requires that that system be in place, though, so until then this hack will hopefully hold. - NBTTagCompound tag = new NBTTagCompound(); - tile.writeToNBT(tag); - ServerPacketDispatcher.getInstance().addPacket(player, PacketTileEntityNBT.createPacket(xPos, yPos, zPos, tag, dim)); - } - } + public static FMLProxyPacket createPacket(int x, int y, int z, int dimension) { + ByteBuf data = createDataBuffer(PacketRequestTE.class); + + data.writeInt(dimension); + data.writeInt(x).writeInt(y).writeInt(z); + + return buildPacket(data); + } + + @Override + public void handle(ByteBuf data, EntityPlayer player) { + if (ModConfigs.disabled) return; + + int dimension = data.readInt(); + BlockPos pos = new BlockPos(data.readInt(), data.readInt(), data.readInt()); + + if (!DimensionManager.isDimensionRegistered(dimension)) return; + WorldServer world = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dimension); + if (world == null) return; + TileEntity te = world.getTileEntity(pos); + if (te != null) { + NBTTagCompound nbt = new NBTTagCompound(); + te.writeToNBT(nbt); + ServerPacketDispatcher.getInstance().addPacket(player, PacketTileEntityNBT.createPacket(pos.getX(), pos.getY(), pos.getZ(), nbt, dimension)); + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestWorldInfo.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestWorldInfo.java index 8fbc9ec..457f8b7 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestWorldInfo.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketRequestWorldInfo.java @@ -1,36 +1,34 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.api.event.ClientWorldInfoEvent; +import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; +import com.xcompwiz.lookingglass.proxyworld.ModConfigs; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraftforge.common.DimensionManager; - -import com.xcompwiz.lookingglass.api.event.ClientWorldInfoEvent; -import com.xcompwiz.lookingglass.network.LookingGlassPacketManager; -import com.xcompwiz.lookingglass.proxyworld.ModConfigs; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; public class PacketRequestWorldInfo extends PacketHandlerBase { - @SideOnly(Side.CLIENT) - public static FMLProxyPacket createPacket(int xPos, int yPos, int zPos, int dim) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); + @SideOnly(Side.CLIENT) + public static FMLProxyPacket createPacket(int x, int y, int z, int dimension) { + ByteBuf data = createDataBuffer(PacketRequestWorldInfo.class); - data.writeInt(dim); + data.writeInt(dimension); - return buildPacket(data); - } + return buildPacket(data); + } - @Override - public void handle(ByteBuf data, EntityPlayer player) { - if (ModConfigs.disabled) return; - int dim = data.readInt(); + @Override + public void handle(ByteBuf data, EntityPlayer player) { + if (ModConfigs.disabled) return; + int dimension = data.readInt(); - if (!DimensionManager.isDimensionRegistered(dim)) return; - net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new ClientWorldInfoEvent(dim, (EntityPlayerMP) player)); - LookingGlassPacketManager.bus.sendTo(PacketWorldInfo.createPacket(dim), (EntityPlayerMP) player); - } + if (!DimensionManager.isDimensionRegistered(dimension)) return; + MinecraftForge.EVENT_BUS.post(new ClientWorldInfoEvent(dimension, (EntityPlayerMP) player)); + LookingGlassPacketManager.bus.sendTo(PacketWorldInfo.createPacket(dimension), (EntityPlayerMP) player); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketTileEntityNBT.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketTileEntityNBT.java index e3f985e..ede1c48 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketTileEntityNBT.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketTileEntityNBT.java @@ -1,60 +1,50 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; import io.netty.buffer.ByteBuf; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.fml.common.network.ByteBufUtils; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; - -import cpw.mods.fml.common.network.ByteBufUtils; -import cpw.mods.fml.common.network.internal.FMLProxyPacket; - -/** - * Based on code from Ken Butler/shadowking97 - */ public class PacketTileEntityNBT extends PacketHandlerBase { - - public static FMLProxyPacket createPacket(int xPos, int yPos, int zPos, NBTTagCompound nbt, int dim) { - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); - - data.writeInt(dim); - data.writeInt(xPos); - data.writeInt(yPos); - data.writeInt(zPos); - ByteBufUtils.writeTag(data, nbt); - - return buildPacket(data); - } - - @Override - public void handle(ByteBuf data, EntityPlayer player) { - int dimension = data.readInt(); - int xPos = data.readInt(); - int yPos = data.readInt(); - int zPos = data.readInt(); - NBTTagCompound nbt = ByteBufUtils.readTag(data); - - WorldClient proxyworld = ProxyWorldManager.getProxyworld(dimension); - if (proxyworld == null) return; - if (proxyworld.provider.dimensionId != dimension) return; - if (proxyworld.blockExists(xPos, yPos, zPos)) { - TileEntity tileentity = proxyworld.getTileEntity(xPos, yPos, zPos); - - if (tileentity != null) { - tileentity.readFromNBT(nbt); - } else { - //Create tile entity from data - tileentity = TileEntity.createAndLoadEntity(nbt); - if (tileentity != null) { - proxyworld.addTileEntity(tileentity); - } - } - proxyworld.markTileEntityChunkModified(xPos, yPos, zPos, tileentity); - proxyworld.setTileEntity(xPos, yPos, zPos, tileentity); - proxyworld.markBlockForUpdate(xPos, yPos, zPos); - } - } + public static FMLProxyPacket createPacket(int x, int y, int z, NBTTagCompound nbt, int dimension) { + ByteBuf data =createDataBuffer(PacketTileEntityNBT.class); + + data.writeInt(dimension); + data.writeInt(x).writeInt(y).writeInt(z); + ByteBufUtils.writeTag(data, nbt); + + return buildPacket(data); + } + + @Override + public void handle(ByteBuf data, EntityPlayer player) { + int dimension = data.readInt(); + BlockPos pos = new BlockPos(data.readInt(), data.readInt(), data.readInt()); + NBTTagCompound nbt = ByteBufUtils.readTag(data); + + WorldClient proxyWorld = ProxyWorldManager.getProxyWorld(dimension); + if (proxyWorld == null) return; + if (proxyWorld.provider.getDimension() != dimension) return; + if (proxyWorld.isBlockLoaded(pos)) { + TileEntity te = proxyWorld.getTileEntity(pos); + + if (te != null) { + te.validate(); + te.readFromNBT(nbt); + } else { + te = TileEntity.create(proxyWorld, nbt); + if (te != null) { + te.validate(); + proxyWorld.addTileEntity(te); + } + } + + proxyWorld.setTileEntity(pos, te); + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketWorldInfo.java b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketWorldInfo.java index d57a405..07674d7 100644 --- a/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketWorldInfo.java +++ b/src/main/java/com/xcompwiz/lookingglass/network/packet/PacketWorldInfo.java @@ -1,82 +1,68 @@ package com.xcompwiz.lookingglass.network.packet; +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import com.xcompwiz.lookingglass.client.proxyworld.WorldView; import io.netty.buffer.ByteBuf; - -import java.util.Collection; - import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.server.MinecraftServer; -import net.minecraft.util.ChunkCoordinates; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.WorldServer; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; -import com.xcompwiz.lookingglass.client.proxyworld.WorldView; -import com.xcompwiz.lookingglass.log.LoggerUtils; - -import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import java.util.Collection; -/** - * Based on code from Ken Butler/shadowking97 - */ public class PacketWorldInfo extends PacketHandlerBase { + public static FMLProxyPacket createPacket(int dimension) { + WorldServer world = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dimension); + if (world == null) { + LookingGlass.logger().warn("Server-side world for dimension {} is null", dimension); + return null; + } + BlockPos pos = world.getSpawnPoint(); + int posX = pos.getX(); + int posY = pos.getY(); + int posZ = pos.getZ(); + int skyLightSubtracted = world.getSkylightSubtracted(); + float thunderingStrength = world.thunderingStrength; + float rainingStrength = world.rainingStrength; + long worldTime = world.provider.getWorldTime(); - public static FMLProxyPacket createPacket(int dimension) { - WorldServer world = MinecraftServer.getServer().worldServerForDimension(dimension); - if (world == null) { - LoggerUtils.warn("Server-side world for dimension %i is null!", dimension); - return null; - } - ChunkCoordinates cc = world.provider.getSpawnPoint(); - int posX = cc.posX; - int posY = cc.posY; - int posZ = cc.posZ; - int skylightSubtracted = world.skylightSubtracted; - float thunderingStrength = world.thunderingStrength; - float rainingStrength = world.rainingStrength; - long worldTime = world.provider.getWorldTime(); - - // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. - ByteBuf data = PacketHandlerBase.createDataBuffer((Class) new Object() {}.getClass().getEnclosingClass()); + ByteBuf data = createDataBuffer(PacketWorldInfo.class); - data.writeInt(dimension); - data.writeInt(posX); - data.writeInt(posY); - data.writeInt(posZ); - data.writeInt(skylightSubtracted); - data.writeFloat(thunderingStrength); - data.writeFloat(rainingStrength); - data.writeLong(worldTime); + data.writeInt(dimension); + data.writeInt(posX).writeInt(posY).writeInt(posZ); + data.writeInt(skyLightSubtracted); + data.writeFloat(thunderingStrength); + data.writeFloat(rainingStrength); + data.writeLong(worldTime); - return buildPacket(data); - } + return buildPacket(data); + } - @Override - public void handle(ByteBuf in, EntityPlayer player) { - int dimension = in.readInt(); - int posX = in.readInt(); - int posY = in.readInt(); - int posZ = in.readInt(); - int skylightSubtracted = in.readInt(); - float thunderingStrength = in.readFloat(); - float rainingStrength = in.readFloat(); - long worldTime = in.readLong(); + @Override + public void handle(ByteBuf data, EntityPlayer player) { + int dimension = data.readInt(); + BlockPos spawn = new BlockPos(data.readInt(), data.readInt(), data.readInt()); + int skyLightSubtracted = data.readInt(); + float thunderingStrength = data.readFloat(); + float rainingStrength = data.readFloat(); + long worldTime = data.readLong(); - WorldClient proxyworld = ProxyWorldManager.getProxyworld(dimension); + WorldClient proxyWorld = ProxyWorldManager.getProxyWorld(dimension); - if (proxyworld == null) return; - if (proxyworld.provider.dimensionId != dimension) return; + if (proxyWorld == null) return; + if (proxyWorld.provider.getDimension() != dimension) return; - ChunkCoordinates cc = new ChunkCoordinates(); - cc.set(posX, posY, posZ); - Collection views = ProxyWorldManager.getWorldViews(dimension); - for (WorldView view : views) { - view.updateWorldSpawn(cc); - } - proxyworld.setSpawnLocation(posX, posY, posZ); - proxyworld.skylightSubtracted = skylightSubtracted; - proxyworld.thunderingStrength = thunderingStrength; - proxyworld.setRainStrength(rainingStrength); - proxyworld.setWorldTime(worldTime); - } + Collection views = ProxyWorldManager.getWorldViews(dimension); + for (WorldView view : views) { + view.updateWorldSpawn(spawn); + } + proxyWorld.setSpawnPoint(spawn); + proxyWorld.setSkylightSubtracted(skyLightSubtracted); + proxyWorld.rainingStrength = rainingStrength; + proxyWorld.thunderingStrength = thunderingStrength; + proxyWorld.setWorldTime(worldTime); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinder.java b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinder.java index ba25a0b..726688f 100644 --- a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinder.java +++ b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinder.java @@ -1,317 +1,319 @@ package com.xcompwiz.lookingglass.proxyworld; -import java.util.LinkedList; -import java.util.List; - -import net.minecraft.block.Block; +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; +import com.xcompwiz.lookingglass.network.packet.PacketChunkInfo; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.multiplayer.ChunkProviderClient; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.util.ChunkCoordinates; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.IChunkProvider; +import net.minecraft.world.gen.ChunkProviderServer; -import com.xcompwiz.lookingglass.log.LoggerUtils; -import com.xcompwiz.lookingglass.network.ServerPacketDispatcher; -import com.xcompwiz.lookingglass.network.packet.PacketChunkInfo; +import java.util.LinkedList; +import java.util.List; -/** - * Finds 16x16x16 chunks exposed to the passed chunk location. - * @author Ken Butler/shadowking97 - */ public class ChunkFinder { + private final IChunkProvider chunkProvider; + private final int rootX; + private final int rootZ; + private final int range; + private final int dimension; + private final EntityPlayer player; + private ChunkData[][] map; + private List positions; + private final int d; + private int step; + private int stepRange; + private long startTime; - private final IChunkProvider chunkProvider; - private final int rootX; - private final int rootZ; - private final int range; - private final int dimension; - private final EntityPlayer player; - private ChunkData[][] map; - private List cc; - private final int d; - private int step; - private int stepRange; - private long startTime; - - /** - * Finds exposed chunks. Chunks must be loaded. - * @param root The chunk in chunk coordinates. - * @param dimension The target dimension - * @param chunkProvider The world server that contains the chunks - * @param player The player to send the chunks to - * @param range The radius of the chunkfinder. - * @return Sorted Chunk Data, by range. Prioritizes closest chunks. - */ - public ChunkFinder(ChunkCoordinates root, int dimension, IChunkProvider chunkProvider, EntityPlayer player, int range) { - this.chunkProvider = chunkProvider; - this.range = range; - this.dimension = dimension; - this.player = player; - this.d = (range << 1) + 1; - this.map = new ChunkData[d][d]; - this.rootX = root.posX - range; - this.rootZ = root.posZ - range; - this.stepRange = 16 - root.posY; - if (root.posY > stepRange) stepRange = root.posY; - startTime = System.nanoTime(); - LoggerUtils.debug("ChunkFinder scan started at nano: " + startTime); - for (int i = 0; i < d; i++) { - for (int j = 0; j < d; j++) { - map[i][j] = new ChunkData(i + rootX, j + rootZ); - int x1 = i - range; - int z1 = j - range; - map[i][j].distance = x1 * x1 + z1 * z1; - } - } - cc = new LinkedList(); - cc.add(new ChunkCoordinates(range, root.posY, range)); - step = 0; - List cc2 = new LinkedList(); - while (step - 1 < stepRange && !cc.isEmpty()) { - while (!cc.isEmpty()) { - cc2.addAll(scan(chunkProvider, map, cc.get(0), range)); - cc.remove(0); - } - step++; - cc.addAll(cc2); - cc2.clear(); - if (step >= stepRange) { - int range2 = step - stepRange + 1; - range2 *= range2; - int range3 = step - stepRange; - if (range3 < 0) range3 = 0; - range3 *= range3; - int minStep = range - (step - stepRange); - int maxStep = range + (step - stepRange) + 1; - if (minStep < 0) minStep = 0; - if (maxStep > d) maxStep = d; - for (int i = minStep; i < maxStep; i++) { - for (int j = minStep; j < maxStep; j++) { - int dist = map[i][j].distance; - if (map[i][j].doAdd() && dist < range2 && dist >= range3) { - ChunkData data = map[i][j]; - Chunk c2 = chunkProvider.provideChunk(data.x, data.z); - if (!c2.isChunkLoaded) c2 = chunkProvider.loadChunk(data.x, data.z); - - ServerPacketDispatcher.getInstance().addPacket(player, PacketChunkInfo.createPacket(c2, true, data.levels(), dimension)); - } - } - } - } - } - } - - public boolean findChunks() { - if (!cc.isEmpty()) { - int tick = 0; - List cc2 = new LinkedList(); - while (!cc.isEmpty() && tick < 15) { - ChunkCoordinates ch = cc.get(0); - cc2.addAll(scan(chunkProvider, map, ch, range)); - cc.remove(0); - ++tick; - } - if (!cc.isEmpty()) return false; - - step++; - - cc.addAll(cc2); - cc2.clear(); - - if (step >= stepRange) { - int range2 = step - stepRange + 1; - range2 *= range2; - int range3 = step - stepRange; - if (range3 < 0) range3 = 0; - range3 *= range3; - int minStep = range - (step - stepRange); - int maxStep = range + (step - stepRange) + 1; - if (minStep < 0) minStep = 0; - if (maxStep > d) maxStep = d; - for (int i = minStep; i < maxStep; i++) { - for (int j = minStep; j < maxStep; j++) { - int dist = map[i][j].distance; - if (map[i][j].doAdd() && dist < range2 && dist >= range3) { - ChunkData data = map[i][j]; - Chunk c2 = chunkProvider.provideChunk(data.x, data.z); - if (!c2.isChunkLoaded) c2 = chunkProvider.loadChunk(data.x, data.z); - ServerPacketDispatcher.getInstance().addPacket(player, PacketChunkInfo.createPacket(c2, true, data.levels(), dimension)); - } - } - } - } - return false; - } - - if (step >= stepRange) { - int range2 = step - stepRange; - range2 *= range2; - for (int i = 0; i < d; i++) { - for (int j = 0; j < d; j++) { - int dist = map[i][j].distance; - if (map[i][j].doAdd() && dist >= range2) { - ChunkData data = map[i][j]; - Chunk c2 = chunkProvider.provideChunk(data.x, data.z); - if (!c2.isChunkLoaded) c2 = chunkProvider.loadChunk(data.x, data.z); - ServerPacketDispatcher.getInstance().addPacket(player, PacketChunkInfo.createPacket(c2, true, data.levels(), dimension)); - } - } - } - } - LoggerUtils.debug("Scan finished. nanoseconds: " + (System.nanoTime() - startTime)); - return true; - } + /** + * Finds exposed chunks. Chunks must be loaded. + * @param root The chunk in chunk coordinates. + * @param dimension The target dimension + * @param chunkProvider The world server that contains the chunks + * @param player The player to send the chunks to + * @param range The radius of the chunkfinder. + * @return Sorted Chunk Data, by range. Prioritizes closest chunks. + */ + public ChunkFinder(BlockPos root, int dimension, IChunkProvider chunkProvider, EntityPlayer player, int range) { + this.chunkProvider = chunkProvider; + this.range = range; + this.dimension = dimension; + this.player = player; + this.d = (range << 1) + 1; + this.map = new ChunkData[this.d][this.d]; + this.rootX = root.getX() - range; + this.rootZ = root.getZ() - range; + this.stepRange = 16 - root.getY(); + if (root.getY() > this.stepRange) this.stepRange = root.getY(); + this.startTime = System.nanoTime(); + LookingGlass.logger().debug("ChunkFinder scan started at nano time {}", this.startTime); + for (int i = 0; i < this.d; i++) { + for (int j = 0; j < this.d; j++) { + this.map[i][j] = new ChunkData(i + this.rootX, j + this.rootZ); + int x1 = i - this.range; + int z1 = j - this.range; + this.map[i][j].distance = x1 * x1 + z1 * z1; + } + } + this.positions = new LinkedList<>(); + this.positions.add(new BlockPos(this.range, root.getY(), this.range)); + this.step = 0; + List positions2 = new LinkedList<>(); + while (this.step - 1 < this.stepRange && !this.positions.isEmpty()) { + while (!this.positions.isEmpty()) { + positions2.addAll(scan(this.chunkProvider, this.map, this.positions.get(0), this.range)); + this.positions.remove(0); + } + this.step++; + this.positions.addAll(positions2); + positions2.clear(); + if (this.step >= this.stepRange) { + int range2 = this.step - this.stepRange + 1; + range2 *= range2; + int range3 = this.step - this.stepRange; + if (range3 < 0) range3 = 0; + range3 *= range3; + int minStep = this.range - (this.step - this.stepRange); + int maxStep = this.range + (this.step - this.stepRange) + 1; + if (minStep < 0) minStep = 0; + if (maxStep > this.d) maxStep = this.d; + for (int i = minStep; i < maxStep; i++) { + for (int j = minStep; j < maxStep; j++) { + int dist = this.map[i][j].distance; + if (this.map[i][j].doAdd() && dist < range2 && dist >= range3) { + ChunkData data = this.map[i][j]; + Chunk c2 = this.chunkProvider.provideChunk(data.x, data.z); + if (!c2.isLoaded()) c2 = loadChunk(this.chunkProvider, data.x, data.z); - /** - * Recursive function to find all chunk segments attached to the surface. - */ - private static List scan(IChunkProvider chunkProvider, ChunkData[][] map, ChunkCoordinates coord, int range) { - int rangeSqr = range * range; - List cc3 = new LinkedList(); - int x = coord.posX; - int y = coord.posY; - int z = coord.posZ; - ChunkData data = map[x][z]; - if (data.isAdded(y) || data.distance > rangeSqr) return cc3; - data.add(y); - Chunk c = chunkProvider.provideChunk(data.x, data.z); - if (!c.isChunkLoaded) { - c = chunkProvider.loadChunk(data.x, data.z); - } - if (c.getAreLevelsEmpty(y << 4, (y << 4) + 15)) { - data.empty(y); - if (x < (range << 1) && !(map[x + 1][z].isAdded(y) || map[x + 1][z].distance > rangeSqr || map[x + 1][z].distance < map[x][z].distance)) cc3.add(new ChunkCoordinates(x + 1, y, z)); - if (x > 0 && !(map[x - 1][z].isAdded(y) || map[x - 1][z].distance > rangeSqr || map[x - 1][z].distance < map[x][z].distance)) cc3.add(new ChunkCoordinates(x - 1, y, z)); - if (y < 15 && !(map[x][z].isAdded(y + 1) || map[x][z].distance > rangeSqr)) cc3.add(new ChunkCoordinates(x, y + 1, z)); - if (y > 0 && !(map[x][z].isAdded(y - 1) || map[x][z].distance > rangeSqr)) cc3.add(new ChunkCoordinates(x, y - 1, z)); - ; - if (z < (range << 1) && !(map[x][z + 1].isAdded(y) || map[x][z + 1].distance > rangeSqr || map[x][z + 1].distance < map[x][z].distance)) cc3.add(new ChunkCoordinates(x, y, z + 1)); - if (z > 0 && !(map[x][z - 1].isAdded(y) || map[x][z - 1].distance > rangeSqr || map[x][z - 1].distance < map[x][z].distance)) cc3.add(new ChunkCoordinates(x, y, z - 1)); - } else { - boolean ok = false; - if (z > 0 && !(map[x][z - 1].isAdded(y) || map[x][z - 1].distance > rangeSqr || map[x][z - 1].distance < map[x][z].distance)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, l, (y << 4) + i, 0, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x, y, z - 1)); - } - ok = false; - } - if (z < (range << 1) && !(map[x][z + 1].isAdded(y) || map[x][z + 1].distance > rangeSqr || map[x][z + 1].distance < map[x][z].distance)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, l, (y << 4) + i, 15, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x, y, z + 1)); - } - ok = false; - } - if (y > 0 && !(map[x][z].isAdded(y - 1) || map[x][z].distance > rangeSqr)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, l, (y << 4), i, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x, y - 1, z)); - } - ok = false; - } - if (y < 15 && !(map[x][z].isAdded(y + 1) || map[x][z].distance > rangeSqr)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, l, (y << 4) + 15, i, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x, y + 1, z)); - } - ok = false; - } - if (x > 0 && !(map[x - 1][z].isAdded(y) || map[x - 1][z].distance > rangeSqr || map[x - 1][z].distance < map[x][z].distance)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, 0, (y << 4) + l, i, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x - 1, y, z)); - } - ok = false; - } - if (x < (range << 1) && !(map[x + 1][z].isAdded(y) || map[x + 1][z].distance > rangeSqr || map[x + 1][z].distance < map[x][z].distance)) { - for (int i = 0; i < 16 && !ok; i++) { - for (int l = 0; l < 16 && !ok; l++) { - if (!isBlockNormalCubeDefault(c, 15, (y << 4) + l, i, false)) ok = true; - } - } - if (ok) { - cc3.add(new ChunkCoordinates(x + 1, y, z)); - } - } - } - return cc3; - } + ServerPacketDispatcher.getInstance().addPacket(this.player, PacketChunkInfo.createPacket(c2, true, data.levels(), dimension)); + } + } + } + } + } + } - public static boolean isBlockNormalCubeDefault(Chunk chunk, int par1, int par2, int par3, boolean par4) { - if (par1 >= -30000000 && par3 >= -30000000 && par1 < 30000000 && par3 < 30000000) { - if (chunk != null && !chunk.isEmpty()) { - Block block = chunk.getBlock(par1 & 15, par2, par3 & 15); - return block.isNormalCube(); - } - } - return par4; - } + public boolean findChunks() { + if (!this.positions.isEmpty()) { + int tick = 0; + List positions2 = new LinkedList<>(); + while (!this.positions.isEmpty() && tick < 15) { + BlockPos pos = this.positions.get(0); + positions2.addAll(scan(this.chunkProvider, this.map, pos, this.range)); + this.positions.remove(0); + ++tick; + } + if (!this.positions.isEmpty()) return false; - public class ChunkData implements Comparable { - public int x; - public int z; - public int added; - public int empty; - public int distance; + this.step++; - public ChunkData(int x, int z) { - this.x = x; - this.z = z; - added = 0; - } + this.positions.addAll(positions2); + positions2.clear(); - public boolean isAdded(int level) { - return (added & (1 << level)) != 0; - } + if (this.step >= this.stepRange) { + int range2 = this.step - this.stepRange + 1; + range2 *= range2; + int range3 = this.step - this.stepRange; + if (range3 < 0) range3 = 0; + range3 *= range3; + int minStep = this.range - (this.step - this.stepRange); + int maxStep = this.range + (this.step - this.stepRange) + 1; + if (minStep < 0) minStep = 0; + if (maxStep > this.d) maxStep = this.d; + for (int i = minStep; i < maxStep; i++) { + for (int j = minStep; j < maxStep; j++) { + int dist = this.map[i][j].distance; + if (this.map[i][j].doAdd() && dist < range2 && dist >= range3) { + ChunkData data = this.map[i][j]; + Chunk c2 = this.chunkProvider.provideChunk(data.x, data.z); + if (!c2.isLoaded()) c2 = loadChunk(this.chunkProvider, data.x, data.z); + ServerPacketDispatcher.getInstance().addPacket(this.player, PacketChunkInfo.createPacket(c2, true, data.levels(), this.dimension)); + } + } + } + } + return false; + } - public boolean doAdd() { - return (added ^ empty) != 0; - } + if (this.step >= this.stepRange) { + int range2 = this.step - this.stepRange; + range2 *= range2; + for (int i = 0; i < d; i++) { + for (int j = 0; j < d; j++) { + int dist = this.map[i][j].distance; + if (this.map[i][j].doAdd() && dist >= range2) { + ChunkData data = map[i][j]; + Chunk c2 = this.chunkProvider.provideChunk(data.x, data.z); + if (!c2.isLoaded()) c2 = loadChunk(this.chunkProvider, data.x, data.z); + ServerPacketDispatcher.getInstance().addPacket(this.player, PacketChunkInfo.createPacket(c2, true, data.levels(), this.dimension)); + } + } + } + } + LookingGlass.logger().debug("Scan finished in {}ns", System.nanoTime() - this.startTime); + return true; + } - public boolean doAdd(int level) { - return isAdded(level) && !isEmpty(level); - } + private static List scan(IChunkProvider chunkProvider, ChunkData[][] map, BlockPos pos, int range) { + int rangeSqr = range * range; + List positions3 = new LinkedList<>(); + int x = pos.getX(); + int y = pos.getY(); + int z = pos.getZ(); + ChunkData data = map[x][z]; + if (data.isAdded(y) || data.distance > rangeSqr) return positions3; + data.add(y); + Chunk c = chunkProvider.provideChunk(data.x, data.z); + if (!c.isLoaded()) { + c = loadChunk(chunkProvider, data.x, data.z); + } + if (c.isEmptyBetween(y << 4, (y << 4) + 15)) { + data.empty(y); + if (x < (range << 1) && !(map[x + 1][z].isAdded(y) || map[x + 1][z].distance > rangeSqr || map[x + 1][z].distance < map[x][z].distance)) { + positions3.add(new BlockPos(x + 1, y, z)); + } + if (x > 0 && !(map[x - 1][z].isAdded(y) || map[x - 1][z].distance > rangeSqr || map[x - 1][z].distance < map[x][z].distance)) { + positions3.add(new BlockPos(x - 1, y, z)); + } + if (y < 15 && !(map[x][z].isAdded(y + 1) || map[x][z].distance > rangeSqr)) { + positions3.add(new BlockPos(x, y + 1, z)); + } + if (y > 0 && !(map[x][z].isAdded(y - 1) || map[x][z].distance > rangeSqr)) { + positions3.add(new BlockPos(x, y - 1, z)); + } + ; + if (z < (range << 1) && !(map[x][z + 1].isAdded(y) || map[x][z + 1].distance > rangeSqr || map[x][z + 1].distance < map[x][z].distance)) { + positions3.add(new BlockPos(x, y, z + 1)); + } + if (z > 0 && !(map[x][z - 1].isAdded(y) || map[x][z - 1].distance > rangeSqr || map[x][z - 1].distance < map[x][z].distance)) { + positions3.add(new BlockPos(x, y, z - 1)); + } + } else { + boolean ok = false; + if (z > 0 && !(map[x][z - 1].isAdded(y) || map[x][z - 1].distance > rangeSqr || map[x][z - 1].distance < map[x][z].distance)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, l, (y << 4) + i, 0, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x, y, z - 1)); + } + ok = false; + } + if (z < (range << 1) && !(map[x][z + 1].isAdded(y) || map[x][z + 1].distance > rangeSqr || map[x][z + 1].distance < map[x][z].distance)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, l, (y << 4) + i, 15, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x, y, z + 1)); + } + ok = false; + } + if (y > 0 && !(map[x][z].isAdded(y - 1) || map[x][z].distance > rangeSqr)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, l, (y << 4), i, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x, y - 1, z)); + } + ok = false; + } + if (y < 15 && !(map[x][z].isAdded(y + 1) || map[x][z].distance > rangeSqr)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, l, (y << 4) + 15, i, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x, y + 1, z)); + } + ok = false; + } + if (x > 0 && !(map[x - 1][z].isAdded(y) || map[x - 1][z].distance > rangeSqr || map[x - 1][z].distance < map[x][z].distance)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, 0, (y << 4) + l, i, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x - 1, y, z)); + } + ok = false; + } + if (x < (range << 1) && !(map[x + 1][z].isAdded(y) || map[x + 1][z].distance > rangeSqr || map[x + 1][z].distance < map[x][z].distance)) { + for (int i = 0; i < 16 && !ok; i++) { + for (int l = 0; l < 16 && !ok; l++) { + if (!isBlockNormalCubeDefault(c, 15, (y << 4) + l, i, false)) ok = true; + } + } + if (ok) { + positions3.add(new BlockPos(x + 1, y, z)); + } + } + } + return positions3; + } - public void add(int level) { - added |= 1 << level; - } + public static boolean isBlockNormalCubeDefault(Chunk chunk, int x, int y, int z, boolean fallback) { + if (x >= -30000000 && z >= -30000000 && x < 30000000 && z < 30000000) { + if (chunk != null && !chunk.isEmpty()) { + IBlockState state = chunk.getBlockState(x & 15, y, z & 15); + return state.isNormalCube(); + } + } + return fallback; + } - public boolean isEmpty(int level) { - return (empty & (1 << level)) == 0; - } + public static Chunk loadChunk(IChunkProvider provider, int x, int z) { + return provider instanceof ChunkProviderServer ? ((ChunkProviderServer)provider).loadChunk(x, z) : provider instanceof ChunkProviderClient ? ((ChunkProviderClient)provider).loadChunk(x, z) : provider.getLoadedChunk(x, z); + } - public void empty(int level) { - empty |= 1 << level; - } + public class ChunkData implements Comparable { + public int x; + public int z; + public int added; + public int empty; + public int distance; - public int levels() { - return added ^ empty; - } + public ChunkData(int x, int z) { + this.x = x; + this.z = z; + this.added = 0; + } - @Override - public int compareTo(ChunkData arg0) { + public boolean isAdded(int level) { + return (this.added & (1 << level)) != 0; + } + public boolean doAdd() { + return (this.added ^ this.empty) != 0; + } + public boolean doAdd(int level) { + return this.isAdded(level) && !this.isEmpty(level); + } + public void add(int level) { + this.added |= 1 << level; + } + public boolean isEmpty(int level) { + return (this.empty & (1 << level)) == 0; + } + public void empty(int level) { + this.empty |= 1 << level; + } + public int levels() { + return this.added ^ this.empty; + } - return this.distance - arg0.distance; - } - } + @Override + public int compareTo(ChunkData o) { + return this.distance - o.distance; + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinderManager.java b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinderManager.java index 6491a7d..d8ea24b 100644 --- a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinderManager.java +++ b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ChunkFinderManager.java @@ -7,24 +7,24 @@ * @author Ken Butler/shadowking97 */ public class ChunkFinderManager { - public static ChunkFinderManager instance = new ChunkFinderManager(); + public static final ChunkFinderManager instance = new ChunkFinderManager(); - private List finders; + private final List finders; - public ChunkFinderManager() { - finders = new LinkedList(); - } + public ChunkFinderManager() { + this.finders = new LinkedList<>(); + } - public void addFinder(ChunkFinder f) { - finders.add(f); - } + public void addFinder(ChunkFinder finder) { + this.finders.add(finder); + } - public void tick() { - for (int i = 0; i < finders.size(); ++i) { - if (finders.get(i).findChunks()) { - finders.remove(i); - --i; - } - } - } + public void tick() { + for (int i = 0; i < this.finders.size(); i++) { + if (this.finders.get(i).findChunks()) { + this.finders.remove(i); + --i; + } + } + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/proxyworld/LookingGlassEventHandler.java b/src/main/java/com/xcompwiz/lookingglass/proxyworld/LookingGlassEventHandler.java index 3178688..609443c 100644 --- a/src/main/java/com/xcompwiz/lookingglass/proxyworld/LookingGlassEventHandler.java +++ b/src/main/java/com/xcompwiz/lookingglass/proxyworld/LookingGlassEventHandler.java @@ -1,119 +1,96 @@ -package com.xcompwiz.lookingglass.proxyworld; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.PrintStream; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.WorldClient; - -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; -import com.xcompwiz.lookingglass.log.LoggerUtils; -import com.xcompwiz.lookingglass.render.PerspectiveRenderManager; -import com.xcompwiz.lookingglass.render.WorldViewRenderManager; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import cpw.mods.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; - -/** - * This class handles the FML events. Primarily it is used to listen for tick events. - */ -public class LookingGlassEventHandler { - - /** An output stream we can use for proxy world logging */ - private final PrintStream printstream; - - /** The client world as we last saw it. We use this to check if the client has changed worlds */ - @SideOnly(Side.CLIENT) - private WorldClient previousWorld; - - /** A simple accumulator to handle triggering freeing world views and such */ - @SideOnly(Side.CLIENT) - private int tickcounter; - - public LookingGlassEventHandler(File logfile) { - PrintStream stream = null; - try { - stream = new PrintStream(logfile); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } finally { - printstream = stream; - } - if (printstream == null) throw new RuntimeException("Could not set up debug exception logger file for Proxy World system"); - } - - @SideOnly(Side.CLIENT) - @SubscribeEvent - public void onClientTick(TickEvent.ClientTickEvent event) { - Minecraft mc = Minecraft.getMinecraft(); - // If no client world we're not playing. Abort. - if (mc.theWorld == null) return; - // We don't want to tick twice per tick loop. Just once. We chose to tick at the start of the tick loop. - if (event.phase != TickEvent.Phase.START) return; - - // Every now and then we want to check to see if there are frame buffers we could free - if (++this.tickcounter % 200 == 0) ProxyWorldManager.detectFreedWorldViews(); - - // Handle whenever the client world has changed since we last looked - if (mc.theWorld != previousWorld) { - // We need to handle the removal of the old world. Particularly, the player will still be visible in it. - // We may consider replacing the old client world with a new proxy world. - if (previousWorld != null) previousWorld.removeAllEntities(); //TODO: This is hardly an ideal solution (It also doesn't seem to work well) - previousWorld = mc.theWorld; // At this point we can safely assert that the client world has changed - - // We let our local world manager know that the client world changed. - ProxyWorldManager.handleWorldChange(mc.theWorld); - } - - // Tick loop for our own worlds. - WorldClient worldBackup = mc.theWorld; - for (WorldClient proxyworld : ProxyWorldManager.getProxyworlds()) { - if (proxyworld.lastLightningBolt > 0) --proxyworld.lastLightningBolt; - if (worldBackup == proxyworld) continue; // This prevents us from double ticking the client world. - try { - mc.theWorld = proxyworld; - //TODO: relays for views (renderGlobal and effectRenderer) (See ProxyWorld.makeFireworks ln23) - proxyworld.tick(); - } catch (Exception e) { - LoggerUtils.error("Client Proxy Dim had error while ticking: %s", e.getLocalizedMessage()); - e.printStackTrace(printstream); - } - } - mc.theWorld = worldBackup; - } - - @SideOnly(Side.CLIENT) - @SubscribeEvent - public void onRenderTick(TickEvent.RenderTickEvent event) { - // If no client world we're not playing. Abort. - if (Minecraft.getMinecraft().theWorld == null) return; - if (event.phase == TickEvent.Phase.START) { - // Anything we need to render for the current frame should happen either during or before the main world render - // Here we call the renderer for "live portal" renders. - PerspectiveRenderManager.onRenderTick(printstream); - return; - } - if (event.phase == TickEvent.Phase.END) { - // We render the world views at the end of the render tick. - WorldViewRenderManager.onRenderTick(printstream); - return; - } - } - - @SubscribeEvent - public void onServerTick(TickEvent.ServerTickEvent event) { - if (event.phase != TickEvent.Phase.END) return; - // On server ticks we try to send clients who need chunk data some of that data they need - ChunkFinderManager.instance.tick(); - } - - @SubscribeEvent - public void onClientDisconnect(ClientDisconnectionFromServerEvent event) { - // Abandon ship! - ProxyWorldManager.clearProxyworlds(); - } -} +package com.xcompwiz.lookingglass.proxyworld; + +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import com.xcompwiz.lookingglass.render.PerspectiveRenderManager; +import com.xcompwiz.lookingglass.render.WorldViewRenderManager; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.fml.common.network.FMLNetworkEvent; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintStream; + +public class LookingGlassEventHandler { + private final PrintStream logger; + + @SideOnly(Side.CLIENT) + private WorldClient previousWorld; + + @SideOnly(Side.CLIENT) + private int tickCounter; + + public LookingGlassEventHandler(File logFile) { + PrintStream stream = null; + try { + stream = new PrintStream(logFile); + } catch (FileNotFoundException e) { + e.printStackTrace(System.err); + } finally { + this.logger = stream; + } + if (this.logger == null) throw new RuntimeException("Could not set up proxyworlds debug logger"); + } + + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onClientTick(TickEvent.ClientTickEvent event) { + Minecraft mc = Minecraft.getMinecraft(); + if (mc.world == null) return; + if (event.phase != TickEvent.Phase.START) return; + + if (++this.tickCounter % 200 == 0) ProxyWorldManager.detectFreedWorldViews(); + + if (mc.world != this.previousWorld) { + if (this.previousWorld != null) this.previousWorld.removeAllEntities(); + this.previousWorld = mc.world; + + ProxyWorldManager.handleWorldChange(mc.world); + } + + WorldClient worldBackup = mc.world; + for (WorldClient proxyWorld : ProxyWorldManager.getProxyWorlds()) { + if (proxyWorld.getLastLightningBolt() > 0) proxyWorld.setLastLightningBolt(proxyWorld.getLastLightningBolt() - 1); + if (worldBackup == proxyWorld) continue; + try { + mc.world = proxyWorld; + proxyWorld.tick(); + } catch (Exception e) { + LookingGlass.logger().error("Client proxy dimension had error while ticking: {}", e.toString()); + e.printStackTrace(this.logger); + } + } + mc.world = worldBackup; + } + + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onRenderTick(TickEvent.RenderTickEvent event) { + if (Minecraft.getMinecraft().world == null) return; + if (event.phase == TickEvent.Phase.START) { + PerspectiveRenderManager.onRenderTick(this.logger); + return; + } + if (event.phase == TickEvent.Phase.END) { + WorldViewRenderManager.onRenderTick(this.logger); + return; + } + } + + @SubscribeEvent + public void onServerTick(TickEvent.ServerTickEvent event) { + if (event.phase != TickEvent.Phase.END) return; + ChunkFinderManager.instance.tick(); + } + + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onClientDisconnect(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { + ProxyWorldManager.clearProxyWorlds(); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ModConfigs.java b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ModConfigs.java index a4a78c9..6640211 100644 --- a/src/main/java/com/xcompwiz/lookingglass/proxyworld/ModConfigs.java +++ b/src/main/java/com/xcompwiz/lookingglass/proxyworld/ModConfigs.java @@ -1,29 +1,40 @@ -package com.xcompwiz.lookingglass.proxyworld; - -import net.minecraftforge.common.config.Configuration; -import net.minecraftforge.common.config.Property; - -public class ModConfigs { - private static final String CATAGORY_SERVER = "server"; - - public static boolean disabled = false; - - public static int dataRate = 2048; - - public static byte renderDistance = 7; - - public static void loadConfigs(Configuration config) { - Property off = config.get(CATAGORY_SERVER, "disabled", disabled); - off.comment = "On the client this disables other world renders entirely, preventing world requests. On the server this disables sending world info to all clients."; - disabled = off.getBoolean(disabled); - - Property d = config.get(CATAGORY_SERVER, "datarate", dataRate); - d.comment = "The number of bytes to send per tick before the server cuts off sending. Only applies to other-world chunks. Default: " + dataRate; - dataRate = d.getInt(dataRate); - - if (dataRate <= 0) disabled = true; - - if (config.hasChanged()) config.save(); - } - -} +package com.xcompwiz.lookingglass.proxyworld; + +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.common.config.Property; + +public class ModConfigs { + private static final String CATEGORY_SERVER = "server"; + public static boolean disabled = false; + public static int dataRate = 2048; + public static byte renderDistance = 7; + public static boolean alternativePortal = false; + public static boolean disableRenderInRenderPortal = false; + public static boolean forceLoadAllWorlds = false; + + public static void loadConfig(Configuration config) { + Property off = config.get(CATEGORY_SERVER, "disabled", disabled); + off.setComment("On the client this disabled world renders, entirely, preventing world requests. On the server this disables sending world info to all clients."); + disabled = off.getBoolean(disabled); + + Property data = config.get(CATEGORY_SERVER, "datarate", dataRate); + data.setComment("The number of bytes to send per tick before the server cuts off sending. Only applies to other-world chunks. Default: " + dataRate); + dataRate = data.getInt(dataRate); + + if (dataRate <= 0) disabled = true; + + Property alternative = config.get(CATEGORY_SERVER, "alternativePortal", alternativePortal); + alternative.setComment("Whether the portal should have an alternative animation (debug)"); + alternativePortal = alternative.getBoolean(); + + Property renderInRender = config.get(CATEGORY_SERVER, "disableRenderInRender", disableRenderInRenderPortal); + renderInRender.setComment("Whether to allow render portals to render render portals in them"); + disableRenderInRenderPortal = renderInRender.getBoolean(); + + Property loading = config.get(CATEGORY_SERVER, "forceLoadAllWorlds", forceLoadAllWorlds); + loading.setComment("When all else fails, enable this (default: false)"); + forceLoadAllWorlds = loading.getBoolean(); + + if (config.hasChanged()) config.save(); + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/proxyworld/SubChunkUtils.java b/src/main/java/com/xcompwiz/lookingglass/proxyworld/SubChunkUtils.java index ce28159..3483ab0 100644 --- a/src/main/java/com/xcompwiz/lookingglass/proxyworld/SubChunkUtils.java +++ b/src/main/java/com/xcompwiz/lookingglass/proxyworld/SubChunkUtils.java @@ -1,35 +1,31 @@ -package com.xcompwiz.lookingglass.proxyworld; - -import net.minecraft.util.ChunkCoordinates; - -public class SubChunkUtils { - public static final boolean withinDistance(ChunkCoordinates c1, int x, int y, int z, int distance) { - return distance * distance >= c1.getDistanceSquared(x, y, z); - } - - public static final boolean withinDistance(int x, int y, int z, int x2, int y2, int z2, int distance) { - int x3 = x - x2; - int y3 = y - y2; - int z3 = z - z2; - return distance * distance >= x3 * x3 + y3 * y3 + z3 * z3; - } - - public static final boolean withinRange(ChunkCoordinates c1, int x, int y, int z, int d1, int d2) { - float cDistance = c1.getDistanceSquared(x, y, z); - return d2 * d2 >= cDistance && d1 * d1 <= cDistance; - } - - public static final boolean withinDistance2D(int x, int z, int x2, int z2, int distance) { - int x3 = x - x2; - int z3 = z - z2; - int distance2 = x3 * x3 + z3 * z3; - return distance * distance >= distance2; - } - - public static final boolean withinRange2D(int x, int z, int x2, int z2, int d1, int d2) { - int x3 = x - x2; - int z3 = z - z2; - int distance = x3 * x3 + z3 * z3; - return d2 * d2 >= distance && d1 * d1 <= distance; - } -} +package com.xcompwiz.lookingglass.proxyworld; + +import net.minecraft.util.math.BlockPos; + +public class SubChunkUtils { + public static boolean withinDistance(BlockPos pos, int x, int y, int z, int distance) { + return distance * distance >= pos.distanceSq(x, y, z); + } + public static boolean withinDistance(int x, int y, int z, int x2, int y2, int z2, int distance) { + int x3 = x - x2; + int y3 = y - y2; + int z3 = z - z2; + return distance * distance >= x3 * x3 + y3 * y3 + z3 * z3; + } + public static boolean withinRange(BlockPos pos, int x, int y, int z, int d1, int d2) { + double cDistance = pos.distanceSq(x, y, z); + return d2 * d2 >= cDistance && d1 * d1 <= cDistance; + } + public static boolean withinDistance2D(int x, int z, int x2, int z2, int distance) { + int x3 = x - x2; + int z3 = z - z2; + int distance2 = x3 * x3 + z3 * z3; + return distance * distance >= distance2; + } + public static boolean withingRange2D(int x, int z, int x2, int z2, int d1, int d2) { + int x3 = x - x2; + int z3 = z - z2; + int distance = x3 * x3 + z3 * z3; + return d2 * d2 >= distance && d1 * d1 <= distance; + } +} diff --git a/src/main/java/com/xcompwiz/lookingglass/render/PerspectiveRenderManager.java b/src/main/java/com/xcompwiz/lookingglass/render/PerspectiveRenderManager.java index 08acee4..2432dba 100644 --- a/src/main/java/com/xcompwiz/lookingglass/render/PerspectiveRenderManager.java +++ b/src/main/java/com/xcompwiz/lookingglass/render/PerspectiveRenderManager.java @@ -3,10 +3,7 @@ import java.io.PrintStream; public class PerspectiveRenderManager { - - public static void onRenderTick(PrintStream printstream) { - // TODO: OK, I lied. It doesn't do anything yet. - // I've been testing this in another mod. - } - + public static void onRenderTick(PrintStream logger) { + //Why would you lie to us, XCompWiz ;-; + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/render/WorldViewRenderManager.java b/src/main/java/com/xcompwiz/lookingglass/render/WorldViewRenderManager.java index 9103b02..a76a8be 100644 --- a/src/main/java/com/xcompwiz/lookingglass/render/WorldViewRenderManager.java +++ b/src/main/java/com/xcompwiz/lookingglass/render/WorldViewRenderManager.java @@ -1,82 +1,88 @@ package com.xcompwiz.lookingglass.render; -import java.io.PrintStream; -import java.util.Collection; - +import com.xcompwiz.lookingglass.LookingGlass; +import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; +import com.xcompwiz.lookingglass.client.proxyworld.WorldView; +import com.xcompwiz.lookingglass.client.render.RenderUtils; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; -import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.client.particle.ParticleManager; +import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.RenderGlobal; import net.minecraft.client.renderer.entity.RenderManager; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; -import net.minecraft.util.MathHelper; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; -import com.xcompwiz.lookingglass.client.proxyworld.ProxyWorldManager; -import com.xcompwiz.lookingglass.client.proxyworld.WorldView; -import com.xcompwiz.lookingglass.client.render.RenderUtils; -import com.xcompwiz.lookingglass.log.LoggerUtils; +import java.io.PrintStream; +import java.util.Collection; public class WorldViewRenderManager { - public static void onRenderTick(PrintStream printstream) { - Minecraft mc = Minecraft.getMinecraft(); - Collection worlds = ProxyWorldManager.getProxyworlds(); - if (worlds == null || worlds.isEmpty()) return; + public static void onRenderTick(PrintStream logger) { + Minecraft mc = Minecraft.getMinecraft(); + Collection worlds = ProxyWorldManager.getProxyWorlds(); + if (worlds == null || worlds.isEmpty()) return; + + long renderT = Minecraft.getSystemTime(); + WorldClient worldBackup = mc.world; + RenderGlobal renderBackup = mc.renderGlobal; + ParticleManager effectBackup = mc.effectRenderer; + EntityPlayerSP playerBackup = mc.player; + Entity renderViewBackup = mc.getRenderViewEntity(); + RenderManager renderManager = mc.getRenderManager(); - long renderT = Minecraft.getSystemTime(); - //TODO: This and the renderWorldToTexture need to be remixed - WorldClient worldBackup = mc.theWorld; - RenderGlobal renderBackup = mc.renderGlobal; - EffectRenderer effectBackup = mc.effectRenderer; - EntityClientPlayerMP playerBackup = mc.thePlayer; - EntityLivingBase viewportBackup = mc.renderViewEntity; + float fov = playerBackup.getFovModifier(); + ItemStack currentClientItem = playerBackup.inventory.getCurrentItem(); - //TODO: This is a hack to work around some of the vanilla rendering hacks... Yay hacks. - float fovmult = playerBackup.getFOVMultiplier(); - ItemStack currentClientItem = playerBackup.inventory.getCurrentItem(); + for (WorldClient proxyWorld : worlds) { + if (proxyWorld == null) continue; + mc.world = proxyWorld; + renderManager.setWorld(mc.world); + for (WorldView activeView : ProxyWorldManager.getWorldViews(proxyWorld.provider.getDimension())) { + if (activeView.hasChunks() && activeView.markClean()) { + activeView.startRender(renderT); - for (WorldClient proxyworld : worlds) { - if (proxyworld == null) continue; - mc.theWorld = proxyworld; - RenderManager.instance.set(mc.theWorld); - for (WorldView activeview : ProxyWorldManager.getWorldViews(proxyworld.provider.dimensionId)) { - if (activeview.hasChunks() && activeview.markClean()) { - activeview.startRender(renderT); + mc.renderGlobal = activeView.getRenderGlobal(); + mc.effectRenderer = activeView.getEffectRenderer(); + mc.setRenderViewEntity(activeView.camera); + mc.player = activeView.camera; + activeView.camera.setFOVMult(fov); + activeView.camera.inventory.currentItem = playerBackup.inventory.currentItem; + activeView.camera.inventory.mainInventory.set(playerBackup.inventory.currentItem, currentClientItem); - mc.renderGlobal = activeview.getRenderGlobal(); - mc.effectRenderer = activeview.getEffectRenderer(); - mc.renderViewEntity = activeview.camera; - mc.thePlayer = activeview.camera; - //Other half of hack - activeview.camera.setFOVMult(fovmult); //Prevents the FOV from flickering - activeview.camera.inventory.currentItem = playerBackup.inventory.currentItem; - activeview.camera.inventory.mainInventory[playerBackup.inventory.currentItem] = currentClientItem; //Prevents the hand from flickering + try { + mc.renderGlobal.updateClouds(); + mc.world.doVoidFogParticles( + MathHelper.floor(activeView.camera.posX), + MathHelper.floor(activeView.camera.posY), + MathHelper.floor(activeView.camera.posZ) + ); + mc.effectRenderer.updateEffects(); + } catch (Exception e) { + LookingGlass.logger().error("Client Proxy Dimension had error while rendering: {}", e.getLocalizedMessage()); + e.printStackTrace(logger); + } - try { - mc.renderGlobal.updateClouds(); - mc.theWorld.doVoidFogParticles(MathHelper.floor_double(activeview.camera.posX), MathHelper.floor_double(activeview.camera.posY), MathHelper.floor_double(activeview.camera.posZ)); - mc.effectRenderer.updateEffects(); - } catch (Exception e) { - LoggerUtils.error("Client Proxy Dim had error while updating render elements: %s", e.getLocalizedMessage()); - e.printStackTrace(printstream); - } + try { + RenderUtils.renderWorldToTexture(0.1F, activeView.getFramebuffer(), activeView.width, activeView.height); + } catch (Exception e) { + LookingGlass.logger().error("Client Proxy Dimension had error while buffering: {}", e.getLocalizedMessage()); + e.printStackTrace(logger); + } + } + } + } - try { - RenderUtils.renderWorldToTexture(0.1f, activeview.getFramebuffer(), activeview.width, activeview.height); - } catch (Exception e) { - LoggerUtils.error("Client Proxy Dim had error while rendering: %s", e.getLocalizedMessage()); - e.printStackTrace(printstream); - } - } - } - } - mc.renderViewEntity = viewportBackup; - mc.thePlayer = playerBackup; - mc.effectRenderer = effectBackup; - mc.renderGlobal = renderBackup; - mc.theWorld = worldBackup; - RenderManager.instance.set(mc.theWorld); - } + Vec3d fog = worldBackup.getFogColor(1.0F); + GlStateManager.clearColor((float) fog.x, (float) fog.y, (float) fog.z, 0.0F); + mc.setRenderViewEntity(renderViewBackup); + mc.player = playerBackup; + mc.effectRenderer = effectBackup; + mc.renderGlobal = renderBackup; + mc.world = worldBackup; + renderManager.setWorld(mc.world); + } } diff --git a/src/main/java/com/xcompwiz/lookingglass/utils/MathUtils.java b/src/main/java/com/xcompwiz/lookingglass/utils/MathUtils.java index cb8b01b..3bfcbad 100644 --- a/src/main/java/com/xcompwiz/lookingglass/utils/MathUtils.java +++ b/src/main/java/com/xcompwiz/lookingglass/utils/MathUtils.java @@ -1,13 +1,13 @@ -package com.xcompwiz.lookingglass.utils; - -import io.netty.buffer.ByteBuf; -import net.minecraft.util.Vec3; - -public class MathUtils { - - public static Vec3 readCoordinates(ByteBuf data) { - Vec3 coords = Vec3.createVectorHelper(data.readDouble(), data.readDouble(), data.readDouble()); - return coords; - } - -} +package com.xcompwiz.lookingglass.utils; + +import io.netty.buffer.ByteBuf; +import net.minecraft.util.math.Vec3d; + +public class MathUtils { + public static Vec3d readCoordinates(ByteBuf data) { + return new Vec3d(data.readDouble(), data.readDouble(), data.readDouble()); + } + public static void writeCoordinates(ByteBuf data, Vec3d vec) { + data.writeDouble(vec.x).writeDouble(vec.y).writeDouble(vec.z); + } +} diff --git a/src/main/resources/assets/lookingglass/lang/en_US.lang b/src/main/resources/assets/lookingglass/lang/en_us.lang similarity index 50% rename from src/main/resources/assets/lookingglass/lang/en_US.lang rename to src/main/resources/assets/lookingglass/lang/en_us.lang index f82e24b..b2a9c9f 100644 --- a/src/main/resources/assets/lookingglass/lang/en_US.lang +++ b/src/main/resources/assets/lookingglass/lang/en_us.lang @@ -1 +1,2 @@ lookingglass.commands.generic.player.notfound=Could not get Player by name: %s +lookingglass.commands.generic.world.notloaded=The target world is not loaded \ No newline at end of file diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index fa3015c..02e1dea 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -1,16 +1,16 @@ [ -{ - "modid": "LookingGlass", - "name": "Looking Glass", - "description": "\"In another moment Alice was through the glass, and had jumped lightly down into the Looking-glass room.\" - Through the Looking-Glass, and What Alice Found There; Lewis Carroll", - "version": "${version}", - "mcversion": "${mcversion}", - "url": "http://xcompwiz.com", - "updateUrl": "", - "authorList": ["XCompWiz"], - "credits": "shadowking97, for following the plan in my head", - "logoFile": "/logo.png", - "screenshots": [], - "dependencies": [] -} + { + "modid": "lookingglass", + "name": "LookingGlass", + "description": "\"In another moment Alice was through the glass, and had jumped lightly down into the Looking-glass room.\" - Through the Looking-Glass, and What Alice Found There; Lewis Carroll", + "version": "${version}", + "mcversion": "${mc_version}", + "url": "http://xcompwiz.com", + "updateUrl": "", + "authorList": ["XCompWiz", "Siepert"], + "credits": "shadowking97, for following the plan in my head", + "logoFile": "/logo.png", + "screenshots": [], + "dependencies": [] + } ] diff --git a/src/main/resources/pack.mcmeta b/src/main/resources/pack.mcmeta new file mode 100644 index 0000000..25e9e31 --- /dev/null +++ b/src/main/resources/pack.mcmeta @@ -0,0 +1,7 @@ +{ + "pack": { + "description": "lookingglass resources", + "pack_format": 3, + "_comment": "A pack_format of 3 should be used starting with Minecraft 1.11. All resources, including language files, should be lowercase (eg: en_us.lang). A pack_format of 2 will load your mod resources with LegacyV2Adapter, which requires language files to have uppercase letters (eg: en_US.lang)." + } +}