mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-14 12:07:10 +00:00
Compare commits
98 Commits
v1.12.2-1.
...
v1.13.2-1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55d54fec63 | ||
|
|
220e4bd660 | ||
|
|
978c28a686 | ||
|
|
b867ada5e5 | ||
|
|
7071cc972b | ||
|
|
6898f932a0 | ||
|
|
2e0ef6385d | ||
|
|
f93da7ea51 | ||
|
|
1210bb8a4d | ||
|
|
48a71e96eb | ||
|
|
3bf47b5290 | ||
|
|
9e9f199e55 | ||
|
|
5a8a111857 | ||
|
|
48ba247ab4 | ||
|
|
362dbd97ac | ||
|
|
aa0e1883d1 | ||
|
|
9cdbcb4332 | ||
|
|
23ddd4feb5 | ||
|
|
fcaa777c95 | ||
|
|
b195cab6a7 | ||
|
|
63dc0daa09 | ||
|
|
34602ec4be | ||
|
|
f3ce44042f | ||
|
|
4205f18f0c | ||
|
|
6be330ae8d | ||
|
|
4569af2130 | ||
|
|
765c31315a | ||
|
|
0e191e42a0 | ||
|
|
ca34b2a1b8 | ||
|
|
7afc3e5260 | ||
|
|
f9e13ca67a | ||
|
|
810258e9b8 | ||
|
|
5e462adc5c | ||
|
|
1fd0b40776 | ||
|
|
2965fb666f | ||
|
|
390575ab4d | ||
|
|
e4ef92ca2d | ||
|
|
9bf586b018 | ||
|
|
173ea72001 | ||
|
|
1230cabcb0 | ||
|
|
6ed03e1fcd | ||
|
|
c4b371b124 | ||
|
|
a600213b00 | ||
|
|
7799b8d4cb | ||
|
|
245bf26480 | ||
|
|
5d05205d69 | ||
|
|
853e2622a1 | ||
|
|
d0bf9e9cd7 | ||
|
|
7a7951ae68 | ||
|
|
bd28955c8e | ||
|
|
e46f09a939 | ||
|
|
71b1f8138d | ||
|
|
1d82a1c98c | ||
|
|
b5f60f3f11 | ||
|
|
259665d9f1 | ||
|
|
ba823bae13 | ||
|
|
1290a4402c | ||
|
|
379076a5e2 | ||
|
|
d12bdf50d8 | ||
|
|
cbfd5aeeee | ||
|
|
41429bdc0b | ||
|
|
54b9966feb | ||
|
|
105c66127c | ||
|
|
765ad0bd3f | ||
|
|
dd05478483 | ||
|
|
5d028dea39 | ||
|
|
629c51d260 | ||
|
|
9ea57961af | ||
|
|
07b9b1c9c7 | ||
|
|
5b942ff9c1 | ||
|
|
7b5a918941 | ||
|
|
47721bf76b | ||
|
|
35ce0974cd | ||
|
|
52e1906d42 | ||
|
|
eaf24a3ceb | ||
|
|
62760e371e | ||
|
|
e154e11186 | ||
|
|
72d079ef61 | ||
|
|
0bfb7049b0 | ||
|
|
f7cb526793 | ||
|
|
e34e833d3d | ||
|
|
a125a19728 | ||
|
|
b3e6a53868 | ||
|
|
218f8e53bb | ||
|
|
d02575528b | ||
|
|
c78adb2cdc | ||
|
|
3e28f79ce9 | ||
|
|
67af7a698b | ||
|
|
06e76f9b15 | ||
|
|
6d383d005c | ||
|
|
c373583723 | ||
|
|
f1d10809d5 | ||
|
|
474f571798 | ||
|
|
fb9c125ab8 | ||
|
|
162fb37421 | ||
|
|
d953f031f0 | ||
|
|
7fde89ad95 | ||
|
|
bd04a93ffb |
19
README.md
19
README.md
@@ -45,3 +45,22 @@ develop CC:T, you'll need to follow these steps:
|
|||||||
- **Test your changes:** `./gradlew runClient` (or run the `GradleStart` class from your IDE).
|
- **Test your changes:** `./gradlew runClient` (or run the `GradleStart` class from your IDE).
|
||||||
|
|
||||||
If you want to run CC:T in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from `build/libs`.
|
If you want to run CC:T in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from `build/libs`.
|
||||||
|
|
||||||
|
## Using
|
||||||
|
If you want to depend on CC:Tweaked, we have a maven repo. However, you should be wary that some functionality is only
|
||||||
|
exposed by CC:T's API and not vanilla ComputerCraft. If you wish to support all variations of ComputerCraft, I recommend
|
||||||
|
using [cc.crzd.me's maven](https://cc.crzd.me/maven/) instead.
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
dependencies {
|
||||||
|
maven { url 'https://squiddev.cc/maven/' }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.squiddev:cc-tweaked-${mc_version}:${cct_version}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You should also be careful to only use classes within the `dan200.computercraft.api` package. Non-API classes are
|
||||||
|
subject to change at any point. If you depend on functionality outside the API, file an issue, and we can look into
|
||||||
|
exposing more features.
|
||||||
|
|||||||
173
build.gradle
173
build.gradle
@@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
// For those who want the bleeding edge
|
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
maven {
|
maven {
|
||||||
name = "forge"
|
name = "forge"
|
||||||
url = "http://files.minecraftforge.net/maven"
|
url = "http://files.minecraftforge.net/maven"
|
||||||
@@ -10,53 +9,76 @@ buildscript {
|
|||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.google.code.gson:gson:2.8.1'
|
classpath 'com.google.code.gson:gson:2.8.1'
|
||||||
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
|
classpath 'net.minecraftforge.gradle:ForgeGradle:3.0.117'
|
||||||
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta1'
|
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta2'
|
||||||
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
|
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id 'com.matthewprenger.cursegradle' version '1.0.10'
|
id 'com.matthewprenger.cursegradle' version '1.2.0'
|
||||||
|
id "com.github.breadmoirai.github-release" version "2.2.4"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply plugin: 'net.minecraftforge.gradle.forge'
|
apply plugin: 'net.minecraftforge.gradle'
|
||||||
apply plugin: 'org.ajoberstar.grgit'
|
apply plugin: 'org.ajoberstar.grgit'
|
||||||
apply plugin: 'maven-publish'
|
apply plugin: 'maven-publish'
|
||||||
apply plugin: 'maven'
|
apply plugin: 'maven'
|
||||||
|
|
||||||
def mc_version = "1.12.2"
|
version = mod_version
|
||||||
def main_version = "1.81.1"
|
|
||||||
version = "${mc_version}-${main_version}"
|
|
||||||
|
|
||||||
group = "org.squiddev"
|
group = "org.squiddev"
|
||||||
archivesBaseName = "cc-tweaked"
|
archivesBaseName = "cc-tweaked-${mc_version}"
|
||||||
|
|
||||||
minecraft {
|
minecraft {
|
||||||
version = "${mc_version}-14.23.4.2749"
|
runs {
|
||||||
runDir = "run"
|
client {
|
||||||
replace '${version}', main_version
|
workingDirectory project.file('run')
|
||||||
|
property 'forge.logging.markers', 'REGISTRIES'
|
||||||
|
property 'forge.logging.console.level', 'debug'
|
||||||
|
|
||||||
// the mappings can be changed at any time, and must be in the following format.
|
mods {
|
||||||
// snapshot_YYYYMMDD snapshot are built nightly.
|
computercraft {
|
||||||
// stable_# stables are built at the discretion of the MCP team.
|
source sourceSets.main
|
||||||
// Use non-default mappings at your own risk. they may not allways work.
|
}
|
||||||
// simply re-run your setup task after changing the mappings to update your workspace.
|
}
|
||||||
mappings = "snapshot_20180724"
|
}
|
||||||
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
|
|
||||||
|
server {
|
||||||
|
workingDirectory project.file('run')
|
||||||
|
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
|
||||||
|
property 'forge.logging.console.level', 'debug'
|
||||||
|
|
||||||
|
mods {
|
||||||
|
computercraft {
|
||||||
|
source sourceSets.main
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mappings channel: 'snapshot', version: "${mappings_version}".toString()
|
||||||
|
|
||||||
|
accessTransformer file('src/main/resources/META-INF/accesstransformer.cfg')
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
maven {
|
maven {
|
||||||
name = "JEI"
|
name "JEI"
|
||||||
url = "http://dvs1.progwml6.com/files/maven"
|
url "http://dvs1.progwml6.com/files/maven"
|
||||||
}
|
}
|
||||||
maven {
|
maven {
|
||||||
name = "squiddev"
|
name "SquidDev"
|
||||||
url = "https://squiddev.cc/maven"
|
url "https://squiddev.cc/maven"
|
||||||
|
}
|
||||||
|
ivy {
|
||||||
|
name "Charset"
|
||||||
|
artifactPattern "https://asie.pl/files/mods/Charset/LibOnly/[module]-[revision](-[classifier]).[ext]"
|
||||||
|
}
|
||||||
|
maven {
|
||||||
|
name "Amadornes"
|
||||||
|
url "http://maven.amadornes.com/"
|
||||||
}
|
}
|
||||||
|
|
||||||
ivy { artifactPattern "https://asie.pl/files/mods/Charset/LibOnly/[module]-[revision](-[classifier]).[ext]" }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
configurations {
|
configurations {
|
||||||
@@ -66,18 +88,31 @@ configurations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
deobfProvided "mezz.jei:jei_1.12.2:4.8.5.159:api"
|
minecraft "net.minecraftforge:forge:${mc_version}-${forge_version}"
|
||||||
deobfProvided "pl.asie:Charset-Lib:0.5.4.6"
|
|
||||||
|
|
||||||
runtime "mezz.jei:jei_1.12.2:4.8.5.159"
|
compileOnly fg.deobf("mezz.jei:jei-1.13.2:5.0.0.20:api")
|
||||||
|
// deobfProvided "pl.asie:Charset-Lib:0.5.4.6"
|
||||||
|
// deobfProvided "MCMultiPart2:MCMultiPart:2.5.3"
|
||||||
|
|
||||||
|
runtimeOnly fg.deobf("mezz.jei:jei-1.13.2:5.0.0.20")
|
||||||
|
|
||||||
shade 'org.squiddev:Cobalt:0.5.0-SNAPSHOT'
|
shade 'org.squiddev:Cobalt:0.5.0-SNAPSHOT'
|
||||||
|
|
||||||
testCompile 'junit:junit:4.11'
|
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
|
||||||
|
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
|
||||||
|
|
||||||
deployerJars "org.apache.maven.wagon:wagon-ssh:3.0.0"
|
deployerJars "org.apache.maven.wagon:wagon-ssh:3.0.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
main {
|
||||||
|
java {
|
||||||
|
exclude 'dan200/computercraft/shared/integration/mcmp'
|
||||||
|
exclude 'dan200/computercraft/shared/integration/charset'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
javadoc {
|
javadoc {
|
||||||
include "dan200/computercraft/api/**/*.java"
|
include "dan200/computercraft/api/**/*.java"
|
||||||
}
|
}
|
||||||
@@ -86,7 +121,13 @@ jar {
|
|||||||
dependsOn javadoc
|
dependsOn javadoc
|
||||||
|
|
||||||
manifest {
|
manifest {
|
||||||
attributes('FMLAT': 'computercraft_at.cfg')
|
attributes(["Specification-Title": "computercraft",
|
||||||
|
"Specification-Vendor": "SquidDev",
|
||||||
|
"Specification-Version": "25.0",
|
||||||
|
"Implementation-Title": "CC: Tweaked",
|
||||||
|
"Implementation-Version": "${mod_version}",
|
||||||
|
"Implementation-Vendor" :"SquidDev",
|
||||||
|
"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")])
|
||||||
}
|
}
|
||||||
|
|
||||||
from (sourceSets.main.allSource) {
|
from (sourceSets.main.allSource) {
|
||||||
@@ -131,6 +172,10 @@ task proguard(type: ProGuardTask, dependsOn: jar) {
|
|||||||
|
|
||||||
// Preserve the constructors in Cobalt library class, as we init them via reflection
|
// Preserve the constructors in Cobalt library class, as we init them via reflection
|
||||||
keepclassmembers 'class org.squiddev.cobalt.lib.** { <init>(...); }'
|
keepclassmembers 'class org.squiddev.cobalt.lib.** { <init>(...); }'
|
||||||
|
|
||||||
|
// LWJGL and Apache bundle Java 9 versions, which is great, but rather breaks Proguard
|
||||||
|
dontwarn 'module-info'
|
||||||
|
dontwarn 'org.apache.**,org.lwjgl.**'
|
||||||
}
|
}
|
||||||
|
|
||||||
task proguardMove(dependsOn: proguard) {
|
task proguardMove(dependsOn: proguard) {
|
||||||
@@ -146,10 +191,10 @@ task proguardMove(dependsOn: proguard) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reobfJar.dependsOn proguardMove
|
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
inputs.property "version", main_version
|
inputs.property "version", mod_version
|
||||||
inputs.property "mcversion", mc_version
|
inputs.property "mcversion", mc_version
|
||||||
|
|
||||||
def hash = 'none'
|
def hash = 'none'
|
||||||
@@ -168,21 +213,21 @@ processResources {
|
|||||||
inputs.property "commithash", hash
|
inputs.property "commithash", hash
|
||||||
|
|
||||||
from(sourceSets.main.resources.srcDirs) {
|
from(sourceSets.main.resources.srcDirs) {
|
||||||
include 'mcmod.info'
|
include 'META-INF/mods.toml'
|
||||||
include 'assets/computercraft/lua/rom/help/credits.txt'
|
include 'data/computercraft/lua/rom/help/credits.txt'
|
||||||
|
|
||||||
expand 'version': main_version,
|
expand 'version': mod_version,
|
||||||
'mcversion': mc_version,
|
'mcversion': mc_version,
|
||||||
'gitcontributors': contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
|
'gitcontributors': contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
from(sourceSets.main.resources.srcDirs) {
|
from(sourceSets.main.resources.srcDirs) {
|
||||||
exclude 'mcmod.info'
|
exclude 'META-INF/mods.toml'
|
||||||
exclude 'assets/computercraft/lua/rom/help/credits.txt'
|
exclude 'data/computercraft/lua/rom/help/credits.txt'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
task compressJson(dependsOn: extractAnnotationsJar) {
|
task compressJson(dependsOn: jar) {
|
||||||
group "compact"
|
group "compact"
|
||||||
description "Minifies all JSON files, stripping whitespace"
|
description "Minifies all JSON files, stripping whitespace"
|
||||||
|
|
||||||
@@ -228,8 +273,8 @@ curseforge {
|
|||||||
apiKey = project.hasProperty('curseForgeApiKey') ? project.curseForgeApiKey : ''
|
apiKey = project.hasProperty('curseForgeApiKey') ? project.curseForgeApiKey : ''
|
||||||
project {
|
project {
|
||||||
id = '282001'
|
id = '282001'
|
||||||
releaseType = 'release'
|
releaseType = 'beta'
|
||||||
changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${project.version})."
|
changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${mc_version}-${mod_version})."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +282,7 @@ publishing {
|
|||||||
publications {
|
publications {
|
||||||
mavenJava(MavenPublication) {
|
mavenJava(MavenPublication) {
|
||||||
from components.java
|
from components.java
|
||||||
artifact sourceJar
|
// artifact sourceJar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,22 +302,22 @@ uploadArchives {
|
|||||||
pom.project {
|
pom.project {
|
||||||
name 'CC: Tweaked'
|
name 'CC: Tweaked'
|
||||||
packaging 'jar'
|
packaging 'jar'
|
||||||
description 'A fork of ComputerCraft which aims to provide earlier access to the more experimental and in-development features of the mod.'
|
description 'CC: Tweaked is a fork of ComputerCraft, adding programmable computers, turtles and more to Minecraft.'
|
||||||
url 'https://github.com/SquidDev-CC/CC-Tweaked'
|
url 'https://github.com/SquidDev-CC/CC-Tweaked'
|
||||||
|
|
||||||
scm {
|
scm {
|
||||||
url 'https://github.com/dan200/ComputerCraft.git'
|
url 'https://github.com/SquidDev-CC/CC-Tweaked.git'
|
||||||
}
|
}
|
||||||
|
|
||||||
issueManagement {
|
issueManagement {
|
||||||
system 'github'
|
system 'github'
|
||||||
url 'https://github.com/dan200/ComputerCraft/issues'
|
url 'https://github.com/SquidDev-CC/CC-Tweaked/issues'
|
||||||
}
|
}
|
||||||
|
|
||||||
licenses {
|
licenses {
|
||||||
license {
|
license {
|
||||||
name 'ComputerCraft Public License, Version 1.0'
|
name 'ComputerCraft Public License, Version 1.0'
|
||||||
url 'https://github.com/dan200/ComputerCraft/blob/master/LICENSE'
|
url 'https://github.com/SquidDev-CC/CC-Tweaked/blob/master/LICENSE'
|
||||||
distribution 'repo'
|
distribution 'repo'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -286,11 +331,37 @@ uploadArchives {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gradle.projectsEvaluated {
|
githubRelease {
|
||||||
tasks.withType(JavaCompile) {
|
token project.hasProperty('githubApiKey') ? project.githubApiKey : ''
|
||||||
options.compilerArgs << "-Xlint"
|
owner 'SquidDev-CC'
|
||||||
|
repo 'CC-Tweaked'
|
||||||
|
targetCommitish (mc_version == "1.12.2" ? "master" : mc_version)
|
||||||
|
|
||||||
|
tagName "v${mc_version}-${mod_version}"
|
||||||
|
releaseName "[${mc_version}] ${mod_version}"
|
||||||
|
body ''
|
||||||
|
prerelease true
|
||||||
|
|
||||||
|
releaseAssets.from(jar.archivePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
task uploadAll(dependsOn: [uploadArchives, "curseforge", "githubRelease"]) {
|
||||||
|
group "upload"
|
||||||
|
description "Uploads to all repositories (Maven, Curse, GitHub release)"
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
testLogging {
|
||||||
|
events "passed", "skipped", "failed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gradle.projectsEvaluated {
|
||||||
|
reobfJar.dependsOn proguardMove
|
||||||
|
|
||||||
|
tasks.withType(JavaCompile) {
|
||||||
|
options.compilerArgs << "-Xlint" << "-Xlint:-processing" // Causes Forge build to fail << "-Werror"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runClient.outputs.upToDateWhen { false }
|
|
||||||
runServer.outputs.upToDateWhen { false }
|
|
||||||
|
|||||||
2491
codeInspectionSettings.xml
Normal file
2491
codeInspectionSettings.xml
Normal file
File diff suppressed because it is too large
Load Diff
7
gradle.properties
Normal file
7
gradle.properties
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Mod properties
|
||||||
|
mod_version=1.82.3
|
||||||
|
|
||||||
|
# Minecraft properties
|
||||||
|
mc_version=1.13.2
|
||||||
|
forge_version=25.0.175
|
||||||
|
mappings_version=20190424-1.13.2
|
||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-bin.zip
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
rootProject.name = 'cc-tweaked'
|
rootProject.name = "cc-tweaked-${mc_version}"
|
||||||
|
|||||||
@@ -7,115 +7,50 @@
|
|||||||
package dan200.computercraft;
|
package dan200.computercraft;
|
||||||
|
|
||||||
import dan200.computercraft.api.filesystem.IMount;
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
|
||||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
|
||||||
import dan200.computercraft.api.media.IMedia;
|
|
||||||
import dan200.computercraft.api.media.IMediaProvider;
|
|
||||||
import dan200.computercraft.api.network.IPacketNetwork;
|
|
||||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
|
||||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
|
||||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
|
||||||
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
|
|
||||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
|
||||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
|
||||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
|
||||||
import dan200.computercraft.api.turtle.event.TurtleAction;
|
import dan200.computercraft.api.turtle.event.TurtleAction;
|
||||||
import dan200.computercraft.core.apis.AddressPredicate;
|
import dan200.computercraft.core.apis.AddressPredicate;
|
||||||
import dan200.computercraft.core.apis.ApiFactories;
|
|
||||||
import dan200.computercraft.core.apis.http.websocket.Websocket;
|
import dan200.computercraft.core.apis.http.websocket.Websocket;
|
||||||
import dan200.computercraft.core.filesystem.ComboMount;
|
import dan200.computercraft.core.filesystem.ResourceMount;
|
||||||
import dan200.computercraft.core.filesystem.FileMount;
|
import dan200.computercraft.shared.Config;
|
||||||
import dan200.computercraft.core.filesystem.JarMount;
|
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
|
||||||
import dan200.computercraft.core.tracking.Tracking;
|
|
||||||
import dan200.computercraft.shared.*;
|
|
||||||
import dan200.computercraft.shared.computer.blocks.BlockCommandComputer;
|
|
||||||
import dan200.computercraft.shared.computer.blocks.BlockComputer;
|
import dan200.computercraft.shared.computer.blocks.BlockComputer;
|
||||||
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputerRegistry;
|
import dan200.computercraft.shared.computer.core.ClientComputerRegistry;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
|
||||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
|
||||||
import dan200.computercraft.shared.computer.core.ServerComputerRegistry;
|
import dan200.computercraft.shared.computer.core.ServerComputerRegistry;
|
||||||
import dan200.computercraft.shared.computer.items.ItemCommandComputer;
|
|
||||||
import dan200.computercraft.shared.computer.items.ItemComputer;
|
import dan200.computercraft.shared.computer.items.ItemComputer;
|
||||||
import dan200.computercraft.shared.media.items.ItemDiskExpanded;
|
import dan200.computercraft.shared.media.items.ItemDisk;
|
||||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
|
||||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||||
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
|
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
|
||||||
import dan200.computercraft.shared.network.NetworkHandler;
|
import dan200.computercraft.shared.peripheral.diskdrive.BlockDiskDrive;
|
||||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
|
|
||||||
import dan200.computercraft.shared.peripheral.common.ItemPeripheral;
|
|
||||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
|
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull;
|
import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.ItemCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.ItemBlockCable;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wireless.BlockAdvancedModem;
|
import dan200.computercraft.shared.peripheral.modem.wireless.BlockWirelessModem;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wireless.ItemAdvancedModem;
|
import dan200.computercraft.shared.peripheral.monitor.BlockMonitor;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
|
import dan200.computercraft.shared.peripheral.printer.BlockPrinter;
|
||||||
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
|
import dan200.computercraft.shared.peripheral.speaker.BlockSpeaker;
|
||||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||||
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
|
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
|
||||||
import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker;
|
import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker;
|
||||||
import dan200.computercraft.shared.proxy.ICCTurtleProxy;
|
|
||||||
import dan200.computercraft.shared.proxy.IComputerCraftProxy;
|
|
||||||
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
|
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
|
||||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
import dan200.computercraft.shared.turtle.items.ItemTurtle;
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleAdvanced;
|
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleLegacy;
|
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleNormal;
|
|
||||||
import dan200.computercraft.shared.turtle.upgrades.*;
|
import dan200.computercraft.shared.turtle.upgrades.*;
|
||||||
import dan200.computercraft.shared.util.CreativeTabMain;
|
import net.minecraft.resources.IReloadableResourceManager;
|
||||||
import dan200.computercraft.shared.util.IDAssigner;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import dan200.computercraft.shared.util.IoUtil;
|
|
||||||
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
|
||||||
import dan200.computercraft.shared.wired.WiredNode;
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
|
||||||
import net.minecraft.item.ItemBlock;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraft.util.EnumFacing;
|
|
||||||
import net.minecraft.util.EnumHand;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.world.IBlockAccess;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
import net.minecraftforge.common.DimensionManager;
|
|
||||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.SidedProxy;
|
import net.minecraftforge.fml.server.ServerLifecycleHooks;
|
||||||
import net.minecraftforge.fml.common.event.*;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.io.InputStream;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipFile;
|
|
||||||
|
|
||||||
@Mod(
|
@Mod( ComputerCraft.MOD_ID )
|
||||||
modid = ComputerCraft.MOD_ID, name = "CC: Tweaked", version = "${version}",
|
public final class ComputerCraft
|
||||||
guiFactory = "dan200.computercraft.client.gui.GuiConfigCC$Factory",
|
|
||||||
dependencies = "required:forge@[14.23.4.2746,)"
|
|
||||||
)
|
|
||||||
public class ComputerCraft
|
|
||||||
{
|
{
|
||||||
public static final String MOD_ID = "computercraft";
|
public static final String MOD_ID = "computercraft";
|
||||||
|
|
||||||
// GUI IDs
|
public static final int DATAFIXER_VERSION = 0;
|
||||||
public static final int diskDriveGUIID = 100;
|
|
||||||
public static final int computerGUIID = 101;
|
|
||||||
public static final int printerGUIID = 102;
|
|
||||||
public static final int turtleGUIID = 103;
|
|
||||||
// ComputerCraftEdu uses ID 104
|
|
||||||
public static final int printoutGUIID = 105;
|
|
||||||
public static final int pocketComputerGUIID = 106;
|
|
||||||
public static final int viewComputerGUIID = 110;
|
|
||||||
|
|
||||||
// Configuration options
|
// Configuration options
|
||||||
public static final String[] DEFAULT_HTTP_WHITELIST = new String[] { "*" };
|
public static final String[] DEFAULT_HTTP_WHITELIST = new String[] { "*" };
|
||||||
@@ -133,9 +68,12 @@ public class ComputerCraft
|
|||||||
public static boolean disable_lua51_features = false;
|
public static boolean disable_lua51_features = false;
|
||||||
public static String default_computer_settings = "";
|
public static String default_computer_settings = "";
|
||||||
public static boolean debug_enable = true;
|
public static boolean debug_enable = true;
|
||||||
public static int computer_threads = 1;
|
|
||||||
public static boolean logPeripheralErrors = false;
|
public static boolean logPeripheralErrors = false;
|
||||||
|
|
||||||
|
public static int computer_threads = 1;
|
||||||
|
public static long maxMainGlobalTime = TimeUnit.MILLISECONDS.toNanos( 10 );
|
||||||
|
public static long maxMainComputerTime = TimeUnit.MILLISECONDS.toNanos( 5 );
|
||||||
|
|
||||||
public static boolean http_enable = true;
|
public static boolean http_enable = true;
|
||||||
public static boolean http_websocket_enable = true;
|
public static boolean http_websocket_enable = true;
|
||||||
public static AddressPredicate http_whitelist = new AddressPredicate( DEFAULT_HTTP_WHITELIST );
|
public static AddressPredicate http_whitelist = new AddressPredicate( DEFAULT_HTTP_WHITELIST );
|
||||||
@@ -172,48 +110,56 @@ public class ComputerCraft
|
|||||||
public static final int terminalHeight_pocketComputer = 20;
|
public static final int terminalHeight_pocketComputer = 20;
|
||||||
|
|
||||||
// Blocks and Items
|
// Blocks and Items
|
||||||
public static class Blocks
|
public static final class Blocks
|
||||||
{
|
{
|
||||||
public static BlockComputer computer;
|
public static BlockComputer computerNormal;
|
||||||
public static BlockCommandComputer commandComputer;
|
public static BlockComputer computerAdvanced;
|
||||||
|
public static BlockComputer computerCommand;
|
||||||
|
|
||||||
public static BlockTurtle turtle;
|
public static BlockTurtle turtleNormal;
|
||||||
public static BlockTurtle turtleExpanded;
|
|
||||||
public static BlockTurtle turtleAdvanced;
|
public static BlockTurtle turtleAdvanced;
|
||||||
|
|
||||||
public static BlockPeripheral peripheral;
|
public static BlockSpeaker speaker;
|
||||||
public static BlockCable cable;
|
public static BlockDiskDrive diskDrive;
|
||||||
public static BlockAdvancedModem advancedModem;
|
public static BlockPrinter printer;
|
||||||
|
|
||||||
|
public static BlockMonitor monitorNormal;
|
||||||
|
public static BlockMonitor monitorAdvanced;
|
||||||
|
|
||||||
|
public static BlockWirelessModem wirelessModemNormal;
|
||||||
|
public static BlockWirelessModem wirelessModemAdvanced;
|
||||||
|
|
||||||
public static BlockWiredModemFull wiredModemFull;
|
public static BlockWiredModemFull wiredModemFull;
|
||||||
|
public static BlockCable cable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Items
|
public static final class Items
|
||||||
{
|
{
|
||||||
public static ItemComputer computer;
|
public static ItemComputer computerNormal;
|
||||||
public static ItemCommandComputer commandComputer;
|
public static ItemComputer computerAdvanced;
|
||||||
|
public static ItemComputer computerCommand;
|
||||||
|
|
||||||
public static ItemTurtleLegacy turtle;
|
public static ItemPocketComputer pocketComputerNormal;
|
||||||
public static ItemTurtleNormal turtleExpanded;
|
public static ItemPocketComputer pocketComputerAdvanced;
|
||||||
public static ItemTurtleAdvanced turtleAdvanced;
|
|
||||||
|
|
||||||
public static ItemPocketComputer pocketComputer;
|
public static ItemTurtle turtleNormal;
|
||||||
|
public static ItemTurtle turtleAdvanced;
|
||||||
|
|
||||||
public static ItemDiskLegacy disk;
|
public static ItemDisk disk;
|
||||||
public static ItemDiskExpanded diskExpanded;
|
|
||||||
public static ItemTreasureDisk treasureDisk;
|
public static ItemTreasureDisk treasureDisk;
|
||||||
|
|
||||||
public static ItemPrintout printout;
|
public static ItemPrintout printedPage;
|
||||||
|
public static ItemPrintout printedPages;
|
||||||
|
public static ItemPrintout printedBook;
|
||||||
|
|
||||||
public static ItemPeripheral peripheral;
|
public static ItemBlockCable.Cable cable;
|
||||||
public static ItemAdvancedModem advancedModem;
|
public static ItemBlockCable.WiredModem wiredModem;
|
||||||
public static ItemCable cable;
|
|
||||||
public static ItemBlock wiredModemFull;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TurtleUpgrades
|
public static final class TurtleUpgrades
|
||||||
{
|
{
|
||||||
public static TurtleModem wirelessModem;
|
public static TurtleModem wirelessModemNormal;
|
||||||
public static TurtleModem advancedModem;
|
public static TurtleModem wirelessModemAdvanced;
|
||||||
public static TurtleSpeaker speaker;
|
public static TurtleSpeaker speaker;
|
||||||
|
|
||||||
public static TurtleCraftingTable craftingTable;
|
public static TurtleCraftingTable craftingTable;
|
||||||
@@ -224,99 +170,23 @@ public class ComputerCraft
|
|||||||
public static TurtleHoe diamondHoe;
|
public static TurtleHoe diamondHoe;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class PocketUpgrades
|
public static final class PocketUpgrades
|
||||||
{
|
{
|
||||||
public static PocketModem wirelessModem;
|
public static PocketModem wirelessModemNormal;
|
||||||
public static PocketModem advancedModem;
|
public static PocketModem wirelessModemAdvanced;
|
||||||
public static PocketSpeaker speaker;
|
public static PocketSpeaker speaker;
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static PocketSpeaker pocketSpeaker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static class Upgrades
|
|
||||||
{
|
|
||||||
public static TurtleModem advancedModem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registries
|
// Registries
|
||||||
public static final ClientComputerRegistry clientComputerRegistry = new ClientComputerRegistry();
|
public static final ClientComputerRegistry clientComputerRegistry = new ClientComputerRegistry();
|
||||||
public static final ServerComputerRegistry serverComputerRegistry = new ServerComputerRegistry();
|
public static final ServerComputerRegistry serverComputerRegistry = new ServerComputerRegistry();
|
||||||
|
|
||||||
// Creative
|
|
||||||
public static CreativeTabMain mainCreativeTab;
|
|
||||||
|
|
||||||
// Logging
|
// Logging
|
||||||
public static Logger log;
|
public static final Logger log = LogManager.getLogger( MOD_ID );
|
||||||
|
|
||||||
// Peripheral providers. This is still here to ensure compatibility with Plethora and Computronics
|
public ComputerCraft()
|
||||||
public static List<IPeripheralProvider> peripheralProviders = new ArrayList<>();
|
|
||||||
|
|
||||||
// Implementation
|
|
||||||
@Mod.Instance( value = ComputerCraft.MOD_ID )
|
|
||||||
public static ComputerCraft instance;
|
|
||||||
|
|
||||||
@SidedProxy(
|
|
||||||
clientSide = "dan200.computercraft.client.proxy.ComputerCraftProxyClient",
|
|
||||||
serverSide = "dan200.computercraft.shared.proxy.ComputerCraftProxyCommon"
|
|
||||||
)
|
|
||||||
private static IComputerCraftProxy proxy;
|
|
||||||
|
|
||||||
@SidedProxy(
|
|
||||||
clientSide = "dan200.computercraft.client.proxy.CCTurtleProxyClient",
|
|
||||||
serverSide = "dan200.computercraft.shared.proxy.CCTurtleProxyCommon"
|
|
||||||
)
|
|
||||||
private static ICCTurtleProxy turtleProxy;
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
|
||||||
public void preInit( FMLPreInitializationEvent event )
|
|
||||||
{
|
{
|
||||||
log = event.getModLog();
|
Config.load();
|
||||||
|
|
||||||
// Load config
|
|
||||||
Config.load( event.getSuggestedConfigurationFile() );
|
|
||||||
|
|
||||||
// Setup network
|
|
||||||
NetworkHandler.setup();
|
|
||||||
|
|
||||||
proxy.preInit();
|
|
||||||
turtleProxy.preInit();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
|
||||||
public void init( FMLInitializationEvent event )
|
|
||||||
{
|
|
||||||
proxy.init();
|
|
||||||
turtleProxy.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
|
||||||
public void onServerStarting( FMLServerStartingEvent event )
|
|
||||||
{
|
|
||||||
proxy.initServer( event.getServer() );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
|
||||||
public void onServerStart( FMLServerStartedEvent event )
|
|
||||||
{
|
|
||||||
if( FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER )
|
|
||||||
{
|
|
||||||
ComputerCraft.serverComputerRegistry.reset();
|
|
||||||
WirelessNetwork.resetNetworks();
|
|
||||||
Tracking.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
|
||||||
public void onServerStopped( FMLServerStoppedEvent event )
|
|
||||||
{
|
|
||||||
if( FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER )
|
|
||||||
{
|
|
||||||
ComputerCraft.serverComputerRegistry.reset();
|
|
||||||
WirelessNetwork.resetNetworks();
|
|
||||||
Tracking.reset();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getVersion()
|
public static String getVersion()
|
||||||
@@ -324,411 +194,23 @@ public class ComputerCraft
|
|||||||
return "${version}";
|
return "${version}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openDiskDriveGUI( EntityPlayer player, TileDiskDrive drive )
|
static IMount createResourceMount( String domain, String subPath )
|
||||||
{
|
{
|
||||||
BlockPos pos = drive.getPos();
|
IReloadableResourceManager manager = ServerLifecycleHooks.getCurrentServer().getResourceManager();
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.diskDriveGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
|
ResourceMount mount = new ResourceMount( domain, subPath, manager );
|
||||||
|
return mount.exists( "" ) ? mount : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openComputerGUI( EntityPlayer player, TileComputer computer )
|
public static InputStream getResourceFile( String domain, String subPath )
|
||||||
{
|
|
||||||
BlockPos pos = computer.getPos();
|
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.computerGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openPrinterGUI( EntityPlayer player, TilePrinter printer )
|
|
||||||
{
|
|
||||||
BlockPos pos = printer.getPos();
|
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.printerGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openTurtleGUI( EntityPlayer player, TileTurtle turtle )
|
|
||||||
{
|
|
||||||
BlockPos pos = turtle.getPos();
|
|
||||||
player.openGui( instance, ComputerCraft.turtleGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openPrintoutGUI( EntityPlayer player, EnumHand hand )
|
|
||||||
{
|
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.printoutGUIID, player.getEntityWorld(), hand.ordinal(), 0, 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openPocketComputerGUI( EntityPlayer player, EnumHand hand )
|
|
||||||
{
|
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.pocketComputerGUIID, player.getEntityWorld(), hand.ordinal(), 0, 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openComputerGUI( EntityPlayer player, ServerComputer computer )
|
|
||||||
{
|
|
||||||
ComputerFamily family = computer.getFamily();
|
|
||||||
int width = 0, height = 0;
|
|
||||||
Terminal terminal = computer.getTerminal();
|
|
||||||
if( terminal != null )
|
|
||||||
{
|
|
||||||
width = terminal.getWidth();
|
|
||||||
height = terminal.getHeight();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pack useful terminal information into the various coordinate bits.
|
|
||||||
// These are extracted in ComputerCraftProxyCommon.getClientGuiElement
|
|
||||||
player.openGui( ComputerCraft.instance, ComputerCraft.viewComputerGUIID, player.getEntityWorld(),
|
|
||||||
computer.getInstanceID(), family.ordinal(), (width & 0xFFFF) << 16 | (height & 0xFFFF)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static File getBaseDir()
|
|
||||||
{
|
|
||||||
return FMLCommonHandler.instance().getMinecraftServerInstance().getDataDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static File getResourcePackDir()
|
|
||||||
{
|
|
||||||
return new File( getBaseDir(), "resourcepacks" );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean canPlayerUseCommands( EntityPlayer player )
|
|
||||||
{
|
|
||||||
MinecraftServer server = player.getServer();
|
|
||||||
if( server != null )
|
|
||||||
{
|
|
||||||
return server.getPlayerList().canSendCommands( player.getGameProfile() );
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerPermissionProvider( ITurtlePermissionProvider provider )
|
|
||||||
{
|
|
||||||
TurtlePermissions.register( provider );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerPocketUpgrade( IPocketUpgrade upgrade )
|
|
||||||
{
|
|
||||||
dan200.computercraft.shared.PocketUpgrades.register( upgrade );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerPeripheralProvider( IPeripheralProvider provider )
|
|
||||||
{
|
|
||||||
Peripherals.register( provider );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerBundledRedstoneProvider( IBundledRedstoneProvider provider )
|
|
||||||
{
|
|
||||||
BundledRedstone.register( provider );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerMediaProvider( IMediaProvider provider )
|
|
||||||
{
|
|
||||||
MediaProviders.register( provider );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerAPIFactory( ILuaAPIFactory factory )
|
|
||||||
{
|
|
||||||
ApiFactories.register( factory );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IWiredNode createWiredNodeForElement( IWiredElement element )
|
|
||||||
{
|
|
||||||
return new WiredNode( element );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IWiredElement getWiredElementAt( IBlockAccess world, BlockPos pos, EnumFacing side )
|
|
||||||
{
|
|
||||||
TileEntity tile = world.getTileEntity( pos );
|
|
||||||
return tile != null && tile.hasCapability( CapabilityWiredElement.CAPABILITY, side )
|
|
||||||
? tile.getCapability( CapabilityWiredElement.CAPABILITY, side )
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static int getDefaultBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
|
|
||||||
{
|
|
||||||
return BundledRedstone.getDefaultOutput( world, pos, side );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IPacketNetwork getWirelessNetwork()
|
|
||||||
{
|
|
||||||
return WirelessNetwork.getUniversal();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static int createUniqueNumberedSaveDir( World world, String parentSubPath )
|
|
||||||
{
|
|
||||||
return IDAssigner.getNextIDFromDirectory( parentSubPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IWritableMount createSaveDirMount( World world, String subPath, long capacity )
|
|
||||||
{
|
{
|
||||||
|
IReloadableResourceManager manager = ServerLifecycleHooks.getCurrentServer().getResourceManager();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return new FileMount( new File( getWorldDir(), subPath ), capacity );
|
return manager.getResource( new ResourceLocation( domain, subPath ) ).getInputStream();
|
||||||
}
|
}
|
||||||
catch( Exception e )
|
catch( IOException ignored )
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IMount createResourceMount( Class<?> modClass, String domain, String subPath )
|
|
||||||
{
|
|
||||||
// Start building list of mounts
|
|
||||||
List<IMount> mounts = new ArrayList<>();
|
|
||||||
subPath = "assets/" + domain + "/" + subPath;
|
|
||||||
|
|
||||||
// Mount from debug dir
|
|
||||||
File codeDir = getDebugCodeDir( modClass );
|
|
||||||
if( codeDir != null )
|
|
||||||
{
|
|
||||||
File subResource = new File( codeDir, subPath );
|
|
||||||
if( subResource.exists() )
|
|
||||||
{
|
|
||||||
IMount resourcePackMount = new FileMount( subResource, 0 );
|
|
||||||
mounts.add( resourcePackMount );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mount from mod jar
|
|
||||||
File modJar = getContainingJar( modClass );
|
|
||||||
if( modJar != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
mounts.add( new JarMount( modJar, subPath ) );
|
|
||||||
}
|
|
||||||
catch( IOException | RuntimeException e )
|
|
||||||
{
|
|
||||||
ComputerCraft.log.error( "Could not load mount from mod jar", e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mount from resource packs
|
|
||||||
File resourcePackDir = getResourcePackDir();
|
|
||||||
if( resourcePackDir.exists() && resourcePackDir.isDirectory() )
|
|
||||||
{
|
|
||||||
String[] resourcePacks = resourcePackDir.list();
|
|
||||||
for( String resourcePackName : resourcePacks )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File resourcePack = new File( resourcePackDir, resourcePackName );
|
|
||||||
if( !resourcePack.isDirectory() )
|
|
||||||
{
|
|
||||||
// Mount a resource pack from a jar
|
|
||||||
mounts.add( new JarMount( resourcePack, subPath ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Mount a resource pack from a folder
|
|
||||||
File subResource = new File( resourcePack, subPath );
|
|
||||||
if( subResource.exists() ) mounts.add( new FileMount( subResource, 0 ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch( IOException | RuntimeException e )
|
|
||||||
{
|
|
||||||
ComputerCraft.log.error( "Could not load resource pack '" + resourcePackName + "'", e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the combination of all the mounts found
|
|
||||||
if( mounts.size() >= 2 )
|
|
||||||
{
|
|
||||||
IMount[] mountArray = new IMount[mounts.size()];
|
|
||||||
mounts.toArray( mountArray );
|
|
||||||
return new ComboMount( mountArray );
|
|
||||||
}
|
|
||||||
else if( mounts.size() == 1 )
|
|
||||||
{
|
|
||||||
return mounts.get( 0 );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static InputStream getResourceFile( Class<?> modClass, String domain, String subPath )
|
|
||||||
{
|
|
||||||
// Start searching in possible locations
|
|
||||||
subPath = "assets/" + domain + "/" + subPath;
|
|
||||||
|
|
||||||
// Look in resource packs
|
|
||||||
File resourcePackDir = getResourcePackDir();
|
|
||||||
if( resourcePackDir.exists() && resourcePackDir.isDirectory() )
|
|
||||||
{
|
|
||||||
String[] resourcePacks = resourcePackDir.list();
|
|
||||||
for( String resourcePackPath : resourcePacks )
|
|
||||||
{
|
|
||||||
File resourcePack = new File( resourcePackDir, resourcePackPath );
|
|
||||||
if( resourcePack.isDirectory() )
|
|
||||||
{
|
|
||||||
// Mount a resource pack from a folder
|
|
||||||
File subResource = new File( resourcePack, subPath );
|
|
||||||
if( subResource.exists() && subResource.isFile() )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return new FileInputStream( subResource );
|
|
||||||
}
|
|
||||||
catch( FileNotFoundException ignored )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ZipFile zipFile = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
final ZipFile zip = zipFile = new ZipFile( resourcePack );
|
|
||||||
ZipEntry entry = zipFile.getEntry( subPath );
|
|
||||||
if( entry != null )
|
|
||||||
{
|
|
||||||
// Return a custom InputStream which will close the original zip when finished.
|
|
||||||
return new FilterInputStream( zipFile.getInputStream( entry ) )
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException
|
|
||||||
{
|
|
||||||
super.close();
|
|
||||||
zip.close();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
IoUtil.closeQuietly( zipFile );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch( IOException e )
|
|
||||||
{
|
|
||||||
if( zipFile != null ) IoUtil.closeQuietly( zipFile );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look in debug dir
|
|
||||||
File codeDir = getDebugCodeDir( modClass );
|
|
||||||
if( codeDir != null )
|
|
||||||
{
|
|
||||||
File subResource = new File( codeDir, subPath );
|
|
||||||
if( subResource.exists() && subResource.isFile() )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return new FileInputStream( subResource );
|
|
||||||
}
|
|
||||||
catch( FileNotFoundException ignored )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look in class loader
|
|
||||||
return modClass.getClassLoader().getResourceAsStream( subPath );
|
|
||||||
}
|
|
||||||
|
|
||||||
private static File getContainingJar( Class<?> modClass )
|
|
||||||
{
|
|
||||||
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
|
|
||||||
int bangIndex = path.indexOf( "!" );
|
|
||||||
if( bangIndex >= 0 )
|
|
||||||
{
|
|
||||||
path = path.substring( 0, bangIndex );
|
|
||||||
}
|
|
||||||
|
|
||||||
URL url;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
url = new URL( path );
|
|
||||||
}
|
|
||||||
catch( MalformedURLException e1 )
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
file = new File( url.toURI() );
|
|
||||||
}
|
|
||||||
catch( URISyntaxException e )
|
|
||||||
{
|
|
||||||
file = new File( url.getPath() );
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static File getDebugCodeDir( Class<?> modClass )
|
|
||||||
{
|
|
||||||
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
|
|
||||||
int bangIndex = path.indexOf( "!" );
|
|
||||||
return bangIndex >= 0 ? null : new File( new File( path ).getParentFile(), "../.." );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static void registerTurtleUpgrade( ITurtleUpgrade upgrade )
|
|
||||||
{
|
|
||||||
dan200.computercraft.shared.TurtleUpgrades.register( upgrade );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File getWorldDir()
|
|
||||||
{
|
|
||||||
return DimensionManager.getCurrentSaveRootDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
//region Compatibility
|
|
||||||
@Deprecated
|
|
||||||
public static File getWorldDir( World world )
|
|
||||||
{
|
|
||||||
return DimensionManager.getCurrentSaveRootDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IMedia getMedia( ItemStack stack )
|
|
||||||
{
|
|
||||||
return MediaProviders.get( stack );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IPocketUpgrade getPocketUpgrade( ItemStack stack )
|
|
||||||
{
|
|
||||||
return dan200.computercraft.shared.PocketUpgrades.get( stack );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static ITurtleUpgrade getTurtleUpgrade( ItemStack stack )
|
|
||||||
{
|
|
||||||
return dan200.computercraft.shared.TurtleUpgrades.get( stack );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IPocketUpgrade getPocketUpgrade( String id )
|
|
||||||
{
|
|
||||||
return dan200.computercraft.shared.PocketUpgrades.get( id );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static ITurtleUpgrade getTurtleUpgrade( String id )
|
|
||||||
{
|
|
||||||
return dan200.computercraft.shared.TurtleUpgrades.get( id );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
|
|
||||||
{
|
|
||||||
return Peripherals.getPeripheral( world, pos, side );
|
|
||||||
}
|
|
||||||
//endregion
|
|
||||||
}
|
}
|
||||||
|
|||||||
141
src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java
Normal file
141
src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.ComputerCraftAPI.IComputerCraftAPI;
|
||||||
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
|
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||||
|
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||||
|
import dan200.computercraft.api.media.IMediaProvider;
|
||||||
|
import dan200.computercraft.api.network.IPacketNetwork;
|
||||||
|
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||||
|
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||||
|
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||||
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||||
|
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||||
|
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||||
|
import dan200.computercraft.core.apis.ApiFactories;
|
||||||
|
import dan200.computercraft.core.filesystem.FileMount;
|
||||||
|
import dan200.computercraft.shared.*;
|
||||||
|
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
|
||||||
|
import dan200.computercraft.shared.util.IDAssigner;
|
||||||
|
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
||||||
|
import dan200.computercraft.shared.wired.WiredNode;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.EnumFacing;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.IBlockReader;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.common.util.LazyOptional;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public final class ComputerCraftAPIImpl implements IComputerCraftAPI
|
||||||
|
{
|
||||||
|
public static final ComputerCraftAPIImpl INSTANCE = new ComputerCraftAPIImpl();
|
||||||
|
|
||||||
|
private ComputerCraftAPIImpl()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public String getInstalledVersion()
|
||||||
|
{
|
||||||
|
return "${version}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int createUniqueNumberedSaveDir( @Nonnull World world, @Nonnull String parentSubPath )
|
||||||
|
{
|
||||||
|
return IDAssigner.getNextId( parentSubPath );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IWritableMount createSaveDirMount( @Nonnull World world, @Nonnull String subPath, long capacity )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new FileMount( new File( IDAssigner.getDir(), subPath ), capacity );
|
||||||
|
}
|
||||||
|
catch( Exception e )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IMount createResourceMount( @Nonnull String domain, @Nonnull String subPath )
|
||||||
|
{
|
||||||
|
return ComputerCraft.createResourceMount( domain, subPath );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerPeripheralProvider( @Nonnull IPeripheralProvider provider )
|
||||||
|
{
|
||||||
|
Peripherals.register( provider );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerTurtleUpgrade( @Nonnull ITurtleUpgrade upgrade )
|
||||||
|
{
|
||||||
|
TurtleUpgrades.register( upgrade );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerBundledRedstoneProvider( @Nonnull IBundledRedstoneProvider provider )
|
||||||
|
{
|
||||||
|
BundledRedstone.register( provider );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||||
|
{
|
||||||
|
return BundledRedstone.getDefaultOutput( world, pos, side );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerMediaProvider( @Nonnull IMediaProvider provider )
|
||||||
|
{
|
||||||
|
MediaProviders.register( provider );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade )
|
||||||
|
{
|
||||||
|
PocketUpgrades.register( upgrade );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IPacketNetwork getWirelessNetwork()
|
||||||
|
{
|
||||||
|
return WirelessNetwork.getUniversal();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerAPIFactory( @Nonnull ILuaAPIFactory factory )
|
||||||
|
{
|
||||||
|
ApiFactories.register( factory );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element )
|
||||||
|
{
|
||||||
|
return new WiredNode( element );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||||
|
{
|
||||||
|
TileEntity tile = world.getTileEntity( pos );
|
||||||
|
return tile == null ? LazyOptional.empty() : tile.getCapability( CapabilityWiredElement.CAPABILITY, side );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,57 +4,50 @@
|
|||||||
* Send enquiries to dratcliffe@gmail.com
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package dan200.computercraft.shared.turtle.upgrades;
|
package dan200.computercraft.api;
|
||||||
|
|
||||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||||
import dan200.computercraft.api.turtle.TurtleUpgradeType;
|
import dan200.computercraft.api.turtle.TurtleUpgradeType;
|
||||||
import net.minecraft.block.Block;
|
|
||||||
import net.minecraft.item.Item;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.IItemProvider;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraft.util.Util;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base class for {@link ITurtleUpgrade}s.
|
||||||
|
*
|
||||||
|
* One does not have to use this, but it does provide a convenient template.
|
||||||
|
*/
|
||||||
public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
|
public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
|
||||||
{
|
{
|
||||||
private final ResourceLocation id;
|
private final ResourceLocation id;
|
||||||
private final int legacyId;
|
|
||||||
private final TurtleUpgradeType type;
|
private final TurtleUpgradeType type;
|
||||||
private final String adjective;
|
private final String adjective;
|
||||||
private final ItemStack stack;
|
private final ItemStack stack;
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, ItemStack stack )
|
protected AbstractTurtleUpgrade( ResourceLocation id, TurtleUpgradeType type, String adjective, ItemStack stack )
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.legacyId = legacyId;
|
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.adjective = adjective;
|
this.adjective = adjective;
|
||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Item item )
|
protected AbstractTurtleUpgrade( ResourceLocation id, TurtleUpgradeType type, String adjective, IItemProvider item )
|
||||||
{
|
{
|
||||||
this( id, legacyId, type, adjective, new ItemStack( item ) );
|
this( id, type, adjective, new ItemStack( item ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Block block )
|
protected AbstractTurtleUpgrade( ResourceLocation id, TurtleUpgradeType type, ItemStack stack )
|
||||||
{
|
{
|
||||||
this( id, legacyId, type, adjective, new ItemStack( block ) );
|
this( id, type, Util.makeTranslationKey( "upgrade", id ) + ".adjective", stack );
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, ItemStack stack )
|
protected AbstractTurtleUpgrade( ResourceLocation id, TurtleUpgradeType type, IItemProvider item )
|
||||||
{
|
{
|
||||||
this( id, legacyId, type, "upgrade." + id + ".adjective", stack );
|
this( id, type, new ItemStack( item ) );
|
||||||
}
|
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Item item )
|
|
||||||
{
|
|
||||||
this( id, legacyId, type, new ItemStack( item ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Block block )
|
|
||||||
{
|
|
||||||
this( id, legacyId, type, new ItemStack( block ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -64,12 +57,6 @@ public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final int getLegacyUpgradeID()
|
|
||||||
{
|
|
||||||
return legacyId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public final String getUnlocalisedAdjective()
|
public final String getUnlocalisedAdjective()
|
||||||
@@ -17,18 +17,17 @@ import dan200.computercraft.api.network.wired.IWiredNode;
|
|||||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||||
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
|
|
||||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.IBlockAccess;
|
import net.minecraft.world.IBlockReader;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.common.util.LazyOptional;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The static entry point to the ComputerCraft API.
|
* The static entry point to the ComputerCraft API.
|
||||||
@@ -37,28 +36,10 @@ import java.lang.reflect.Method;
|
|||||||
*/
|
*/
|
||||||
public final class ComputerCraftAPI
|
public final class ComputerCraftAPI
|
||||||
{
|
{
|
||||||
public static boolean isInstalled()
|
|
||||||
{
|
|
||||||
findCC();
|
|
||||||
return computerCraft != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static String getInstalledVersion()
|
public static String getInstalledVersion()
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().getInstalledVersion();
|
||||||
if( computerCraft_getVersion != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (String) computerCraft_getVersion.invoke( null );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -82,19 +63,7 @@ public final class ComputerCraftAPI
|
|||||||
*/
|
*/
|
||||||
public static int createUniqueNumberedSaveDir( @Nonnull World world, @Nonnull String parentSubPath )
|
public static int createUniqueNumberedSaveDir( @Nonnull World world, @Nonnull String parentSubPath )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().createUniqueNumberedSaveDir( world, parentSubPath );
|
||||||
if( computerCraft_createUniqueNumberedSaveDir != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (Integer) computerCraft_createUniqueNumberedSaveDir.invoke( null, world, parentSubPath );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -118,78 +87,66 @@ public final class ComputerCraftAPI
|
|||||||
@Nullable
|
@Nullable
|
||||||
public static IWritableMount createSaveDirMount( @Nonnull World world, @Nonnull String subPath, long capacity )
|
public static IWritableMount createSaveDirMount( @Nonnull World world, @Nonnull String subPath, long capacity )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().createSaveDirMount( world, subPath, capacity );
|
||||||
if( computerCraft_createSaveDirMount != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (IWritableMount) computerCraft_createSaveDirMount.invoke( null, world, subPath, capacity );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a file system mount to a resource folder, and returns it.
|
* Creates a file system mount to a resource folder, and returns it.
|
||||||
*
|
*
|
||||||
* Use in conjunction with IComputerAccess.mount() or IComputerAccess.mountWritable() to mount a resource folder
|
* Use in conjunction with {@link IComputerAccess#mount} or {@link IComputerAccess#mountWritable} to mount a
|
||||||
* onto a computer's file system.
|
* resource folder onto a computer's file system.
|
||||||
*
|
*
|
||||||
* The files in this mount will be a combination of files in the specified mod jar, and resource packs that contain
|
* The files in this mount will be a combination of files in all mod jar, and data packs that contain
|
||||||
* resources with the same domain and path.
|
* resources with the same domain and path.
|
||||||
*
|
*
|
||||||
* @param modClass A class in whose jar to look first for the resources to mount. Using your main mod class is recommended. eg: MyMod.class
|
* @param domain The domain under which to look for resources. eg: "mymod".
|
||||||
* @param domain The domain under which to look for resources. eg: "mymod".
|
* @param subPath The subPath under which to look for resources. eg: "lua/myfiles".
|
||||||
* @param subPath The domain under which to look for resources. eg: "mymod/lua/myfiles".
|
* @return The mount, or {@code null} if it could be created for some reason.
|
||||||
* @return The mount, or {@code null} if it could be created for some reason. Use IComputerAccess.mount() or
|
|
||||||
* IComputerAccess.mountWritable() to mount this on a Computers' file system.
|
|
||||||
* @see IComputerAccess#mount(String, IMount)
|
* @see IComputerAccess#mount(String, IMount)
|
||||||
* @see IComputerAccess#mountWritable(String, IWritableMount)
|
* @see IComputerAccess#mountWritable(String, IWritableMount)
|
||||||
* @see IMount
|
* @see IMount
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static IMount createResourceMount( @Nonnull Class<?> modClass, @Nonnull String domain, @Nonnull String subPath )
|
public static IMount createResourceMount( @Nonnull String domain, @Nonnull String subPath )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().createResourceMount( domain, subPath );
|
||||||
if( computerCraft_createResourceMount != null )
|
}
|
||||||
{
|
|
||||||
try
|
/**
|
||||||
{
|
* Creates a file system mount to a resource folder, and returns it.
|
||||||
return (IMount) computerCraft_createResourceMount.invoke( null, modClass, domain, subPath );
|
*
|
||||||
}
|
* Use in conjunction with {@link IComputerAccess#mount} or {@link IComputerAccess#mountWritable} to mount a
|
||||||
catch( Exception e )
|
* resource folder onto a computer's file system.
|
||||||
{
|
*
|
||||||
// It failed
|
* The files in this mount will be a combination of files in all mod jar, and data packs that contain
|
||||||
}
|
* resources with the same domain and path.
|
||||||
}
|
*
|
||||||
return null;
|
* @param klass The mod class to which the files belong.
|
||||||
|
* @param domain The domain under which to look for resources. eg: "mymod".
|
||||||
|
* @param subPath The subPath under which to look for resources. eg: "lua/myfiles".
|
||||||
|
* @return The mount, or {@code null} if it could be created for some reason.
|
||||||
|
* @see IComputerAccess#mount(String, IMount)
|
||||||
|
* @see IComputerAccess#mountWritable(String, IWritableMount)
|
||||||
|
* @see IMount
|
||||||
|
* @deprecated Use {@link #createResourceMount(String, String)} instead.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
@Deprecated
|
||||||
|
public static IMount createResourceMount( Class<?> klass, @Nonnull String domain, @Nonnull String subPath )
|
||||||
|
{
|
||||||
|
return getInstance().createResourceMount( domain, subPath );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a peripheral provider to convert blocks into {@link IPeripheral} implementations.
|
* Registers a peripheral provider to convert blocks into {@link IPeripheral} implementations.
|
||||||
*
|
*
|
||||||
* @param provider The peripheral provider to register.
|
* @param provider The peripheral provider to register.
|
||||||
* @see dan200.computercraft.api.peripheral.IPeripheral
|
* @see IPeripheral
|
||||||
* @see dan200.computercraft.api.peripheral.IPeripheralProvider
|
* @see IPeripheralProvider
|
||||||
*/
|
*/
|
||||||
public static void registerPeripheralProvider( @Nonnull IPeripheralProvider provider )
|
public static void registerPeripheralProvider( @Nonnull IPeripheralProvider provider )
|
||||||
{
|
{
|
||||||
findCC();
|
getInstance().registerPeripheralProvider( provider );
|
||||||
if( computerCraft_registerPeripheralProvider != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerPeripheralProvider.invoke( null, provider );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,47 +155,22 @@ public final class ComputerCraftAPI
|
|||||||
* this during the load() method of your mod.
|
* this during the load() method of your mod.
|
||||||
*
|
*
|
||||||
* @param upgrade The turtle upgrade to register.
|
* @param upgrade The turtle upgrade to register.
|
||||||
* @see dan200.computercraft.api.turtle.ITurtleUpgrade
|
* @see ITurtleUpgrade
|
||||||
*/
|
*/
|
||||||
public static void registerTurtleUpgrade( @Nonnull ITurtleUpgrade upgrade )
|
public static void registerTurtleUpgrade( @Nonnull ITurtleUpgrade upgrade )
|
||||||
{
|
{
|
||||||
if( upgrade != null )
|
getInstance().registerTurtleUpgrade( upgrade );
|
||||||
{
|
|
||||||
findCC();
|
|
||||||
if( computerCraft_registerTurtleUpgrade != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerTurtleUpgrade.invoke( null, upgrade );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a bundled redstone provider to provide bundled redstone output for blocks.
|
* Registers a bundled redstone provider to provide bundled redstone output for blocks.
|
||||||
*
|
*
|
||||||
* @param provider The bundled redstone provider to register.
|
* @param provider The bundled redstone provider to register.
|
||||||
* @see dan200.computercraft.api.redstone.IBundledRedstoneProvider
|
* @see IBundledRedstoneProvider
|
||||||
*/
|
*/
|
||||||
public static void registerBundledRedstoneProvider( @Nonnull IBundledRedstoneProvider provider )
|
public static void registerBundledRedstoneProvider( @Nonnull IBundledRedstoneProvider provider )
|
||||||
{
|
{
|
||||||
findCC();
|
getInstance().registerBundledRedstoneProvider( provider );
|
||||||
if( computerCraft_registerBundledRedstoneProvider != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerBundledRedstoneProvider.invoke( null, provider );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,85 +181,27 @@ public final class ComputerCraftAPI
|
|||||||
* @param side The side to extract the bundled redstone output from.
|
* @param side The side to extract the bundled redstone output from.
|
||||||
* @return If there is a block capable of emitting bundled redstone at the location, it's signal (0-65535) will be returned.
|
* @return If there is a block capable of emitting bundled redstone at the location, it's signal (0-65535) will be returned.
|
||||||
* If there is no block capable of emitting bundled redstone at the location, -1 will be returned.
|
* If there is no block capable of emitting bundled redstone at the location, -1 will be returned.
|
||||||
* @see dan200.computercraft.api.redstone.IBundledRedstoneProvider
|
* @see IBundledRedstoneProvider
|
||||||
*/
|
*/
|
||||||
public static int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
public static int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().getBundledRedstoneOutput( world, pos, side );
|
||||||
if( computerCraft_getDefaultBundledRedstoneOutput != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (Integer) computerCraft_getDefaultBundledRedstoneOutput.invoke( null, world, pos, side );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a media provider to provide {@link IMedia} implementations for Items
|
* Registers a media provider to provide {@link IMedia} implementations for Items
|
||||||
*
|
*
|
||||||
* @param provider The media provider to register.
|
* @param provider The media provider to register.
|
||||||
* @see dan200.computercraft.api.media.IMediaProvider
|
* @see IMediaProvider
|
||||||
*/
|
*/
|
||||||
public static void registerMediaProvider( @Nonnull IMediaProvider provider )
|
public static void registerMediaProvider( @Nonnull IMediaProvider provider )
|
||||||
{
|
{
|
||||||
findCC();
|
getInstance().registerMediaProvider( provider );
|
||||||
if( computerCraft_registerMediaProvider != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerMediaProvider.invoke( null, provider );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers a permission provider to restrict where turtles can move or build.
|
|
||||||
*
|
|
||||||
* @param provider The turtle permission provider to register.
|
|
||||||
* @see dan200.computercraft.api.permissions.ITurtlePermissionProvider
|
|
||||||
* @deprecated Prefer using {@link dan200.computercraft.api.turtle.event.TurtleBlockEvent} or the standard Forge events.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static void registerPermissionProvider( @Nonnull ITurtlePermissionProvider provider )
|
|
||||||
{
|
|
||||||
findCC();
|
|
||||||
if( computerCraft_registerPermissionProvider != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerPermissionProvider.invoke( null, provider );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade )
|
public static void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade )
|
||||||
{
|
{
|
||||||
findCC();
|
getInstance().registerPocketUpgrade( upgrade );
|
||||||
if( computerCraft_registerPocketUpgrade != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerPocketUpgrade.invoke( null, upgrade );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -337,36 +211,12 @@ public final class ComputerCraftAPI
|
|||||||
*/
|
*/
|
||||||
public static IPacketNetwork getWirelessNetwork()
|
public static IPacketNetwork getWirelessNetwork()
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().getWirelessNetwork();
|
||||||
if( computerCraft_getWirelessNetwork != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (IPacketNetwork) computerCraft_getWirelessNetwork.invoke( null );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void registerAPIFactory( @Nonnull ILuaAPIFactory upgrade )
|
public static void registerAPIFactory( @Nonnull ILuaAPIFactory factory )
|
||||||
{
|
{
|
||||||
findCC();
|
getInstance().registerAPIFactory( factory );
|
||||||
if( computerCraft_registerAPIFactory != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft_registerAPIFactory.invoke( null, upgrade );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
// It failed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -379,22 +229,7 @@ public final class ComputerCraftAPI
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
public static IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element )
|
public static IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().createWiredNodeForElement( element );
|
||||||
if( computerCraft_createWiredNodeForElement != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (IWiredNode) computerCraft_createWiredNodeForElement.invoke( null, element );
|
|
||||||
}
|
|
||||||
catch( ReflectiveOperationException e )
|
|
||||||
{
|
|
||||||
throw new IllegalStateException( "Error creating wired node", e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new IllegalStateException( "ComputerCraft cannot be found" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -406,118 +241,64 @@ public final class ComputerCraftAPI
|
|||||||
* @return The element's node
|
* @return The element's node
|
||||||
* @see IWiredElement#getNode()
|
* @see IWiredElement#getNode()
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nonnull
|
||||||
public static IWiredElement getWiredElementAt( @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
public static LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||||
{
|
{
|
||||||
findCC();
|
return getInstance().getWiredElementAt( world, pos, side );
|
||||||
if( computerCraft_getWiredElementAt != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (IWiredElement) computerCraft_getWiredElementAt.invoke( null, world, pos, side );
|
|
||||||
}
|
|
||||||
catch( ReflectiveOperationException ignored )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The functions below here are private, and are used to interface with the non-API ComputerCraft classes.
|
private static IComputerCraftAPI instance;
|
||||||
// Reflection is used here so you can develop your mod without decompiling ComputerCraft and including
|
|
||||||
// it in your solution, and so your mod won't crash if ComputerCraft is installed.
|
|
||||||
|
|
||||||
private static void findCC()
|
@Nonnull
|
||||||
|
private static IComputerCraftAPI getInstance()
|
||||||
{
|
{
|
||||||
if( !ccSearched )
|
if( instance != null ) return instance;
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
computerCraft = Class.forName( "dan200.computercraft.ComputerCraft" );
|
|
||||||
computerCraft_getVersion = findCCMethod( "getVersion", new Class<?>[] {
|
|
||||||
} );
|
|
||||||
computerCraft_createUniqueNumberedSaveDir = findCCMethod( "createUniqueNumberedSaveDir", new Class<?>[] {
|
|
||||||
World.class, String.class
|
|
||||||
} );
|
|
||||||
computerCraft_createSaveDirMount = findCCMethod( "createSaveDirMount", new Class<?>[] {
|
|
||||||
World.class, String.class, Long.TYPE
|
|
||||||
} );
|
|
||||||
computerCraft_createResourceMount = findCCMethod( "createResourceMount", new Class<?>[] {
|
|
||||||
Class.class, String.class, String.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerPeripheralProvider = findCCMethod( "registerPeripheralProvider", new Class<?>[] {
|
|
||||||
IPeripheralProvider.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerTurtleUpgrade = findCCMethod( "registerTurtleUpgrade", new Class<?>[] {
|
|
||||||
ITurtleUpgrade.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerBundledRedstoneProvider = findCCMethod( "registerBundledRedstoneProvider", new Class<?>[] {
|
|
||||||
IBundledRedstoneProvider.class
|
|
||||||
} );
|
|
||||||
computerCraft_getDefaultBundledRedstoneOutput = findCCMethod( "getDefaultBundledRedstoneOutput", new Class<?>[] {
|
|
||||||
World.class, BlockPos.class, EnumFacing.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerMediaProvider = findCCMethod( "registerMediaProvider", new Class<?>[] {
|
|
||||||
IMediaProvider.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerPermissionProvider = findCCMethod( "registerPermissionProvider", new Class<?>[] {
|
|
||||||
ITurtlePermissionProvider.class
|
|
||||||
} );
|
|
||||||
computerCraft_registerPocketUpgrade = findCCMethod( "registerPocketUpgrade", new Class<?>[] {
|
|
||||||
IPocketUpgrade.class
|
|
||||||
} );
|
|
||||||
computerCraft_getWirelessNetwork = findCCMethod( "getWirelessNetwork", new Class<?>[] {
|
|
||||||
} );
|
|
||||||
computerCraft_registerAPIFactory = findCCMethod( "registerAPIFactory", new Class<?>[] {
|
|
||||||
ILuaAPIFactory.class
|
|
||||||
} );
|
|
||||||
computerCraft_createWiredNodeForElement = findCCMethod( "createWiredNodeForElement", new Class<?>[] {
|
|
||||||
IWiredElement.class
|
|
||||||
} );
|
|
||||||
computerCraft_getWiredElementAt = findCCMethod( "getWiredElementAt", new Class<?>[] {
|
|
||||||
IBlockAccess.class, BlockPos.class, EnumFacing.class
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
System.out.println( "ComputerCraftAPI: ComputerCraft not found." );
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ccSearched = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Method findCCMethod( String name, Class<?>[] args )
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return computerCraft != null ? computerCraft.getMethod( name, args ) : null;
|
return instance = (IComputerCraftAPI) Class.forName( "dan200.computercraft.ComputerCraftAPIImpl" )
|
||||||
|
.getField( "INSTANCE" ).get( null );
|
||||||
}
|
}
|
||||||
catch( NoSuchMethodException e )
|
catch( ReflectiveOperationException e )
|
||||||
{
|
{
|
||||||
System.out.println( "ComputerCraftAPI: ComputerCraft method " + name + " not found." );
|
throw new IllegalStateException( "Cannot find ComputerCraft API", e );
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean ccSearched = false;
|
public interface IComputerCraftAPI
|
||||||
private static Class<?> computerCraft = null;
|
{
|
||||||
private static Method computerCraft_getVersion = null;
|
@Nonnull
|
||||||
private static Method computerCraft_createUniqueNumberedSaveDir = null;
|
String getInstalledVersion();
|
||||||
private static Method computerCraft_createSaveDirMount = null;
|
|
||||||
private static Method computerCraft_createResourceMount = null;
|
int createUniqueNumberedSaveDir( @Nonnull World world, @Nonnull String parentSubPath );
|
||||||
private static Method computerCraft_registerPeripheralProvider = null;
|
|
||||||
private static Method computerCraft_registerTurtleUpgrade = null;
|
@Nullable
|
||||||
private static Method computerCraft_registerBundledRedstoneProvider = null;
|
IWritableMount createSaveDirMount( @Nonnull World world, @Nonnull String subPath, long capacity );
|
||||||
private static Method computerCraft_getDefaultBundledRedstoneOutput = null;
|
|
||||||
private static Method computerCraft_registerMediaProvider = null;
|
@Nullable
|
||||||
private static Method computerCraft_registerPermissionProvider = null;
|
IMount createResourceMount( @Nonnull String domain, @Nonnull String subPath );
|
||||||
private static Method computerCraft_registerPocketUpgrade = null;
|
|
||||||
private static Method computerCraft_getWirelessNetwork = null;
|
void registerPeripheralProvider( @Nonnull IPeripheralProvider provider );
|
||||||
private static Method computerCraft_registerAPIFactory = null;
|
|
||||||
private static Method computerCraft_createWiredNodeForElement = null;
|
void registerTurtleUpgrade( @Nonnull ITurtleUpgrade upgrade );
|
||||||
private static Method computerCraft_getWiredElementAt = null;
|
|
||||||
|
void registerBundledRedstoneProvider( @Nonnull IBundledRedstoneProvider provider );
|
||||||
|
|
||||||
|
int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side );
|
||||||
|
|
||||||
|
void registerMediaProvider( @Nonnull IMediaProvider provider );
|
||||||
|
|
||||||
|
void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade );
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
IPacketNetwork getWirelessNetwork();
|
||||||
|
|
||||||
|
void registerAPIFactory( @Nonnull ILuaAPIFactory factory );
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element );
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|FileSystem", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.filesystem;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -17,6 +17,7 @@ import javax.annotation.Nullable;
|
|||||||
* @see ILuaAPI
|
* @see ILuaAPI
|
||||||
* @see ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
|
* @see ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface ILuaAPIFactory
|
public interface ILuaAPIFactory
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -32,7 +32,12 @@ public interface ILuaContext
|
|||||||
* intercepted, or the computer will leak memory and end up in a broken state.
|
* intercepted, or the computer will leak memory and end up in a broken state.
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException;
|
default Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException
|
||||||
|
{
|
||||||
|
Object[] results = pullEventRaw( filter );
|
||||||
|
if( results.length >= 1 && results[0].equals( "terminate" ) ) throw new LuaException( "Terminated", 0 );
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The same as {@link #pullEvent(String)}, except "terminated" events are ignored. Only use this if you want to
|
* The same as {@link #pullEvent(String)}, except "terminated" events are ignored. Only use this if you want to
|
||||||
@@ -47,7 +52,10 @@ public interface ILuaContext
|
|||||||
* @see #pullEvent(String)
|
* @see #pullEvent(String)
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException;
|
default Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException
|
||||||
|
{
|
||||||
|
return yield( new Object[] { filter } );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Yield the current coroutine with some arguments until it is resumed. This method is exactly equivalent to
|
* Yield the current coroutine with some arguments until it is resumed. This method is exactly equivalent to
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Lua", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.lua;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
package dan200.computercraft.api.media;
|
package dan200.computercraft.api.media;
|
||||||
|
|
||||||
import dan200.computercraft.api.filesystem.IMount;
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.SoundEvent;
|
import net.minecraft.util.SoundEvent;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
@@ -16,7 +17,9 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an item that can be placed in a disk drive and used by a Computer.
|
* Represents an item that can be placed in a disk drive and used by a Computer.
|
||||||
* Implement this interface on your Item class to allow it to be used in the drive.
|
*
|
||||||
|
* Implement this interface on your {@link Item} class to allow it to be used in the drive. Alternatively, register
|
||||||
|
* a {@link IMediaProvider}.
|
||||||
*/
|
*/
|
||||||
public interface IMedia
|
public interface IMedia
|
||||||
{
|
{
|
||||||
@@ -43,7 +46,7 @@ public interface IMedia
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* If this disk represents an item with audio (like a record), get the readable name of the audio track. ie:
|
* If this disk represents an item with audio (like a record), get the readable name of the audio track. ie:
|
||||||
* "Jonathon Coulton - Still Alive"
|
* "Jonathan Coulton - Still Alive"
|
||||||
*
|
*
|
||||||
* @param stack The {@link ItemStack} to modify.
|
* @param stack The {@link ItemStack} to modify.
|
||||||
* @return The name, or null if this item does not represent an item with audio.
|
* @return The name, or null if this item does not represent an item with audio.
|
||||||
@@ -74,7 +77,7 @@ public interface IMedia
|
|||||||
* @param world The world in which the item and disk drive reside.
|
* @param world The world in which the item and disk drive reside.
|
||||||
* @return The mount, or null if this item does not represent an item with data. If the mount returned also
|
* @return The mount, or null if this item does not represent an item with data. If the mount returned also
|
||||||
* implements {@link dan200.computercraft.api.filesystem.IWritableMount}, it will mounted using mountWritable()
|
* implements {@link dan200.computercraft.api.filesystem.IWritableMount}, it will mounted using mountWritable()
|
||||||
* @see dan200.computercraft.api.filesystem.IMount
|
* @see IMount
|
||||||
* @see dan200.computercraft.api.filesystem.IWritableMount
|
* @see dan200.computercraft.api.filesystem.IWritableMount
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#createSaveDirMount(World, String, long)
|
* @see dan200.computercraft.api.ComputerCraftAPI#createSaveDirMount(World, String, long)
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#createResourceMount(Class, String, String)
|
* @see dan200.computercraft.api.ComputerCraftAPI#createResourceMount(Class, String, String)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public interface IMediaProvider
|
|||||||
* Produce an IMedia implementation from an ItemStack.
|
* Produce an IMedia implementation from an ItemStack.
|
||||||
*
|
*
|
||||||
* @param stack The stack from which to extract the media information.
|
* @param stack The stack from which to extract the media information.
|
||||||
* @return An IMedia implementation, or null if the item is not something you wish to handle
|
* @return An {@link IMedia} implementation, or {@code null} if the item is not something you wish to handle
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#registerMediaProvider(IMediaProvider)
|
* @see dan200.computercraft.api.ComputerCraftAPI#registerMediaProvider(IMediaProvider)
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Media", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.media;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Network", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.network;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Network|Wired", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.network.wired;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -9,6 +9,8 @@ package dan200.computercraft.api.peripheral;
|
|||||||
import dan200.computercraft.api.ComputerCraftAPI;
|
import dan200.computercraft.api.ComputerCraftAPI;
|
||||||
import dan200.computercraft.api.filesystem.IMount;
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||||
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
|
import dan200.computercraft.api.lua.ILuaTask;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@@ -146,7 +148,7 @@ public interface IComputerAccess
|
|||||||
*
|
*
|
||||||
* You may supply {@code null} to indicate that no arguments are to be supplied.
|
* You may supply {@code null} to indicate that no arguments are to be supplied.
|
||||||
* @throws RuntimeException If the peripheral has been detached.
|
* @throws RuntimeException If the peripheral has been detached.
|
||||||
* @see dan200.computercraft.api.peripheral.IPeripheral#callMethod
|
* @see IPeripheral#callMethod
|
||||||
*/
|
*/
|
||||||
void queueEvent( @Nonnull String event, @Nullable Object[] arguments );
|
void queueEvent( @Nonnull String event, @Nullable Object[] arguments );
|
||||||
|
|
||||||
@@ -179,8 +181,8 @@ public interface IComputerAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a reachable peripheral with the given attachement name. This is a equivalent to
|
* Get a reachable peripheral with the given attachment name. This is a equivalent to
|
||||||
* {@link #getAvailablePeripherals()}{@code .get(name)}, though may be more performant.
|
* {@link #getAvailablePeripherals()}{@code .get(name)}, though may be more efficient.
|
||||||
*
|
*
|
||||||
* @param name The peripheral's attached name
|
* @param name The peripheral's attached name
|
||||||
* @return The reachable peripheral, or {@code null} if none can be found.
|
* @return The reachable peripheral, or {@code null} if none can be found.
|
||||||
@@ -191,4 +193,23 @@ public interface IComputerAccess
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a {@link IWorkMonitor} for tasks your peripheral might execute on the main (server) thread.
|
||||||
|
*
|
||||||
|
* This should be used to ensure your peripheral integrates with ComputerCraft's monitoring and limiting of how much
|
||||||
|
* server time each computer consumes. You should not need to use this if you use
|
||||||
|
* {@link ILuaContext#issueMainThreadTask(ILuaTask)} - this is intended for mods with their own system for running
|
||||||
|
* work on the main thread.
|
||||||
|
*
|
||||||
|
* Please note that the returned implementation is <em>not</em> thread-safe, and should only be used from the main
|
||||||
|
* thread.
|
||||||
|
*
|
||||||
|
* @return The work monitor for the main thread, or {@code null} if this computer does not have one.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default IWorkMonitor getMainThreadMonitor()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ public interface IPeripheral
|
|||||||
* This is called when a lua program on an attached computer calls {@code peripheral.call()} with
|
* This is called when a lua program on an attached computer calls {@code peripheral.call()} with
|
||||||
* one of the methods exposed by {@link #getMethodNames()}.
|
* one of the methods exposed by {@link #getMethodNames()}.
|
||||||
*
|
*
|
||||||
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
|
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting
|
||||||
* when interacting with Minecraft objects.
|
* with Minecraft objects.
|
||||||
*
|
*
|
||||||
* @param computer The interface to the computer that is making the call. Remember that multiple
|
* @param computer The interface to the computer that is making the call. Remember that multiple
|
||||||
* computers can be attached to a peripheral at once.
|
* computers can be attached to a peripheral at once.
|
||||||
@@ -75,20 +75,21 @@ public interface IPeripheral
|
|||||||
Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
|
Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral.
|
* Is called when when a computer is attaching to the peripheral.
|
||||||
*
|
*
|
||||||
* This will occur when a peripheral is placed next to an active computer, when a computer is turned on next to a
|
* This will occur when a peripheral is placed next to an active computer, when a computer is turned on next to a
|
||||||
* peripheral, or when a turtle travels into a square next to a peripheral.
|
* peripheral, when a turtle travels into a square next to a peripheral, or when a wired modem adjacent to this
|
||||||
|
* peripheral is does any of the above.
|
||||||
*
|
*
|
||||||
* Between calls to attach() and detach(), the attached computer can make method calls on the peripheral using
|
* Between calls to attach and {@link #detach}, the attached computer can make method calls on the peripheral using
|
||||||
* {@code peripheral.call()}. This method can be used to keep track of which computers are attached to the
|
* {@code peripheral.call()}. This method can be used to keep track of which computers are attached to the
|
||||||
* peripheral, or to take action when attachment occurs.
|
* peripheral, or to take action when attachment occurs.
|
||||||
*
|
*
|
||||||
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
|
* Be aware that will be called from both the server thread and ComputerCraft Lua thread, and so must be thread-safe
|
||||||
* when interacting with Minecraft objects.
|
* and reentrant.
|
||||||
*
|
*
|
||||||
* @param computer The interface to the computer that is being attached. Remember that multiple
|
* @param computer The interface to the computer that is being attached. Remember that multiple computers can be
|
||||||
* computers can be attached to a peripheral at once.
|
* attached to a peripheral at once.
|
||||||
* @see #detach
|
* @see #detach
|
||||||
*/
|
*/
|
||||||
default void attach( @Nonnull IComputerAccess computer )
|
default void attach( @Nonnull IComputerAccess computer )
|
||||||
@@ -96,19 +97,21 @@ public interface IPeripheral
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is called when a computer is detaching from the peripheral.
|
* Called when a computer is detaching from the peripheral.
|
||||||
*
|
*
|
||||||
* This will occur when a computer shuts down, when the peripheral is removed while attached to computers,
|
* This will occur when a computer shuts down, when the peripheral is removed while attached to computers, when a
|
||||||
* or when a turtle moves away from a square attached to a peripheral. This method can be used to keep track of
|
* turtle moves away from a block attached to a peripheral, or when a wired modem adjacent to this peripheral is
|
||||||
* which computers are attached to the peripheral, or to take action when detachment
|
* detached.
|
||||||
* occurs.
|
|
||||||
*
|
*
|
||||||
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
|
* This method can be used to keep track of which computers are attached to the peripheral, or to take action when
|
||||||
* when interacting with Minecraft objects.
|
* detachment occurs.
|
||||||
*
|
*
|
||||||
* @param computer The interface to the computer that is being detached. Remember that multiple
|
* Be aware that this will be called from both the server and ComputerCraft Lua thread, and must be thread-safe
|
||||||
* computers can be attached to a peripheral at once.
|
* and reentrant.
|
||||||
* @see #detach
|
*
|
||||||
|
* @param computer The interface to the computer that is being detached. Remember that multiple computers can be
|
||||||
|
* attached to a peripheral at once.
|
||||||
|
* @see #attach
|
||||||
*/
|
*/
|
||||||
default void detach( @Nonnull IComputerAccess computer )
|
default void detach( @Nonnull IComputerAccess computer )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
package dan200.computercraft.api.peripheral;
|
package dan200.computercraft.api.peripheral;
|
||||||
|
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
@@ -16,6 +17,8 @@ import javax.annotation.Nullable;
|
|||||||
/**
|
/**
|
||||||
* This interface is used to create peripheral implementations for blocks.
|
* This interface is used to create peripheral implementations for blocks.
|
||||||
*
|
*
|
||||||
|
* If you have a {@link TileEntity} which acts as a peripheral, you may alternatively implement {@link IPeripheralTile}.
|
||||||
|
*
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)
|
* @see dan200.computercraft.api.ComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
||||||
|
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||||
|
*/
|
||||||
|
package dan200.computercraft.api.peripheral;
|
||||||
|
|
||||||
|
import net.minecraft.util.EnumFacing;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link net.minecraft.tileentity.TileEntity} which may act as a peripheral.
|
||||||
|
*
|
||||||
|
* If you need more complex capabilities (such as handling TEs not belonging to your mod), you should use
|
||||||
|
* {@link IPeripheralProvider}.
|
||||||
|
*/
|
||||||
|
public interface IPeripheralTile
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the peripheral on the given {@code side}.
|
||||||
|
*
|
||||||
|
* @param side The side to get the peripheral from.
|
||||||
|
* @return A peripheral, or {@code null} if there is not a peripheral here.
|
||||||
|
* @see IPeripheralProvider#getPeripheral(World, BlockPos, EnumFacing)
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
IPeripheral getPeripheral( @Nonnull EnumFacing side );
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.api.peripheral;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Monitors "work" associated with a computer, keeping track of how much a computer has done, and ensuring every
|
||||||
|
* computer receives a fair share of any processing time.
|
||||||
|
*
|
||||||
|
* This is primarily intended for work done by peripherals on the main thread (such as on a tile entity's tick), but
|
||||||
|
* could be used for other purposes (such as complex computations done on another thread).
|
||||||
|
*
|
||||||
|
* Before running a task, one should call {@link #canWork()} to determine if the computer is currently allowed to
|
||||||
|
* execute work. If that returns true, you should execute the task and use {@link #trackWork(long, TimeUnit)} to inform
|
||||||
|
* the monitor how long that task took.
|
||||||
|
*
|
||||||
|
* Alternatively, use {@link #runWork(Runnable)} to run and keep track of work.
|
||||||
|
*
|
||||||
|
* @see IComputerAccess#getMainThreadMonitor()
|
||||||
|
*/
|
||||||
|
public interface IWorkMonitor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* If the owning computer is currently allowed to execute work.
|
||||||
|
*
|
||||||
|
* @return If we can execute work right now.
|
||||||
|
*/
|
||||||
|
boolean canWork();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the owning computer is currently allowed to execute work, and has ample time to do so.
|
||||||
|
*
|
||||||
|
* This is effectively a more restrictive form of {@link #canWork()}. One should use that in order to determine if
|
||||||
|
* you may do an initial piece of work, and shouldWork to determine if any additional task may be performed.
|
||||||
|
*
|
||||||
|
* @return If we should execute work right now.
|
||||||
|
*/
|
||||||
|
boolean shouldWork();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inform the monitor how long some piece of work took to execute.
|
||||||
|
*
|
||||||
|
* @param time The time some task took to run
|
||||||
|
* @param unit The unit that {@code time} was measured in.
|
||||||
|
*/
|
||||||
|
void trackWork( long time, @Nonnull TimeUnit unit );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a task if possible, and inform the monitor of how long it took.
|
||||||
|
*
|
||||||
|
* @param runnable The task to run.
|
||||||
|
* @return If the task was actually run (namely, {@link #canWork()} returned {@code true}).
|
||||||
|
*/
|
||||||
|
default boolean runWork( @Nonnull Runnable runnable )
|
||||||
|
{
|
||||||
|
Objects.requireNonNull( runnable, "runnable should not be null" );
|
||||||
|
if( !canWork() ) return false;
|
||||||
|
|
||||||
|
long start = System.nanoTime();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
trackWork( System.nanoTime() - start, TimeUnit.NANOSECONDS );
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Peripheral", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.peripheral;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package dan200.computercraft.api.permissions;
|
|
||||||
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This interface is used to restrict where turtles can move or build.
|
|
||||||
*
|
|
||||||
* Turtles will call these methods before attempting to perform an action, allowing them to be cancelled.
|
|
||||||
*
|
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#registerPermissionProvider(ITurtlePermissionProvider)
|
|
||||||
*/
|
|
||||||
public interface ITurtlePermissionProvider
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Determine whether a block can be entered by a turtle.
|
|
||||||
*
|
|
||||||
* @param world The world the block exists in
|
|
||||||
* @param pos The location of the block.
|
|
||||||
* @return Whether the turtle can move into this block.
|
|
||||||
*/
|
|
||||||
boolean isBlockEnterable( @Nonnull World world, @Nonnull BlockPos pos );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine whether a block can be modified by a turtle.
|
|
||||||
*
|
|
||||||
* This includes breaking and placing blocks.
|
|
||||||
*
|
|
||||||
* @param world The world the block exists in
|
|
||||||
* @param pos The location of the block.
|
|
||||||
* @return Whether the turtle can modify this block.
|
|
||||||
*/
|
|
||||||
boolean isBlockEditable( @Nonnull World world, @Nonnull BlockPos pos );
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Permissions", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.permissions;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -4,14 +4,20 @@
|
|||||||
* Send enquiries to dratcliffe@gmail.com
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package dan200.computercraft.shared.pocket.peripherals;
|
package dan200.computercraft.api.pocket;
|
||||||
|
|
||||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.IItemProvider;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraft.util.Util;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base class for {@link IPocketUpgrade}s.
|
||||||
|
*
|
||||||
|
* One does not have to use this, but it does provide a convenient template.
|
||||||
|
*/
|
||||||
public abstract class AbstractPocketUpgrade implements IPocketUpgrade
|
public abstract class AbstractPocketUpgrade implements IPocketUpgrade
|
||||||
{
|
{
|
||||||
private final ResourceLocation id;
|
private final ResourceLocation id;
|
||||||
@@ -25,9 +31,14 @@ public abstract class AbstractPocketUpgrade implements IPocketUpgrade
|
|||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AbstractPocketUpgrade( ResourceLocation id, ItemStack stack )
|
protected AbstractPocketUpgrade( ResourceLocation id, String adjective, IItemProvider item )
|
||||||
{
|
{
|
||||||
this( id, "upgrade." + id + ".adjective", stack );
|
this( id, adjective, new ItemStack( item ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AbstractPocketUpgrade( ResourceLocation id, IItemProvider item )
|
||||||
|
{
|
||||||
|
this( id, Util.makeTranslationKey( "upgrade", id ) + ".adjective", new ItemStack( item ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -23,7 +23,9 @@ public interface IPocketAccess
|
|||||||
/**
|
/**
|
||||||
* Gets the entity holding this item.
|
* Gets the entity holding this item.
|
||||||
*
|
*
|
||||||
* @return The holding entity. This may be {@code null}.
|
* This must be called on the server thread.
|
||||||
|
*
|
||||||
|
* @return The holding entity, or {@code null} if none exists.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
Entity getEntity();
|
Entity getEntity();
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import javax.annotation.Nullable;
|
|||||||
/**
|
/**
|
||||||
* Additional peripherals for pocket computers.
|
* Additional peripherals for pocket computers.
|
||||||
*
|
*
|
||||||
* This is similar to {@link dan200.computercraft.api.turtle.ITurtleUpgrade}.
|
* This is similar to {@link ITurtleUpgrade}.
|
||||||
*/
|
*/
|
||||||
public interface IPocketUpgrade
|
public interface IPocketUpgrade
|
||||||
{
|
{
|
||||||
@@ -54,6 +54,9 @@ public interface IPocketUpgrade
|
|||||||
* pocket computer which holds this upgrade. This item stack is also used to determine the upgrade given by
|
* pocket computer which holds this upgrade. This item stack is also used to determine the upgrade given by
|
||||||
* {@code pocket.equip()}/{@code pocket.unequip()}.
|
* {@code pocket.equip()}/{@code pocket.unequip()}.
|
||||||
*
|
*
|
||||||
|
* Ideally this should be constant over a session. It is recommended that you cache
|
||||||
|
* the item too, in order to prevent constructing it every time the method is called.
|
||||||
|
*
|
||||||
* @return The item stack used for crafting. This can be {@link ItemStack#EMPTY} if crafting is disabled.
|
* @return The item stack used for crafting. This can be {@link ItemStack#EMPTY} if crafting is disabled.
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Redstone", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.redstone;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -54,8 +54,7 @@ public interface ITurtleAccess
|
|||||||
* @param world The new world to move it to
|
* @param world The new world to move it to
|
||||||
* @param pos The new position to move it to.
|
* @param pos The new position to move it to.
|
||||||
* @return Whether the movement was successful. It may fail if the block was not loaded or the block placement
|
* @return Whether the movement was successful. It may fail if the block was not loaded or the block placement
|
||||||
* was cancelled. Note this will not check
|
* was cancelled.
|
||||||
* {@link dan200.computercraft.api.permissions.ITurtlePermissionProvider#isBlockEnterable(World, BlockPos)}.
|
|
||||||
* @throws UnsupportedOperationException When attempting to teleport on the client side.
|
* @throws UnsupportedOperationException When attempting to teleport on the client side.
|
||||||
*/
|
*/
|
||||||
boolean teleportTo( @Nonnull World world, @Nonnull BlockPos pos );
|
boolean teleportTo( @Nonnull World world, @Nonnull BlockPos pos );
|
||||||
|
|||||||
@@ -10,15 +10,15 @@ import dan200.computercraft.api.ComputerCraftAPI;
|
|||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.api.turtle.event.TurtleAttackEvent;
|
import dan200.computercraft.api.turtle.event.TurtleAttackEvent;
|
||||||
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
|
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
import net.minecraft.client.renderer.model.ModelResourceLocation;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.event.entity.player.AttackEntityEvent;
|
import net.minecraftforge.event.entity.player.AttackEntityEvent;
|
||||||
import net.minecraftforge.event.world.BlockEvent;
|
import net.minecraftforge.event.world.BlockEvent;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@@ -44,17 +44,6 @@ public interface ITurtleUpgrade
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
ResourceLocation getUpgradeID();
|
ResourceLocation getUpgradeID();
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a numerical identifier representing this type of turtle upgrade,
|
|
||||||
* for backwards compatibility with pre-1.76 worlds. If your upgrade was
|
|
||||||
* not released for older ComputerCraft versions, you can return -1 here.
|
|
||||||
* The turtle will fail registration if an already used positive ID is specified.
|
|
||||||
*
|
|
||||||
* @return The legacy ID, or -1 if is needed.
|
|
||||||
* @see ComputerCraftAPI#registerTurtleUpgrade(ITurtleUpgrade)
|
|
||||||
*/
|
|
||||||
int getLegacyUpgradeID();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an unlocalised string to describe this type of turtle in turtle item names.
|
* Return an unlocalised string to describe this type of turtle in turtle item names.
|
||||||
*
|
*
|
||||||
@@ -79,6 +68,9 @@ public interface ITurtleUpgrade
|
|||||||
* with to create a turtle which holds this upgrade. This item stack is also used
|
* with to create a turtle which holds this upgrade. This item stack is also used
|
||||||
* to determine the upgrade given by {@code turtle.equip()}
|
* to determine the upgrade given by {@code turtle.equip()}
|
||||||
*
|
*
|
||||||
|
* Ideally this should be constant over a session. It is recommended that you cache
|
||||||
|
* the item too, in order to prevent constructing it every time the method is called.
|
||||||
|
*
|
||||||
* @return The item stack to craft with, or {@link ItemStack#EMPTY} if it cannot be crafted.
|
* @return The item stack to craft with, or {@link ItemStack#EMPTY} if it cannot be crafted.
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -130,7 +122,7 @@ public interface ITurtleUpgrade
|
|||||||
* Called to obtain the model to be used when rendering a turtle peripheral.
|
* Called to obtain the model to be used when rendering a turtle peripheral.
|
||||||
*
|
*
|
||||||
* This can be obtained from {@link net.minecraft.client.renderer.ItemModelMesher#getItemModel(ItemStack)},
|
* This can be obtained from {@link net.minecraft.client.renderer.ItemModelMesher#getItemModel(ItemStack)},
|
||||||
* {@link net.minecraft.client.renderer.block.model.ModelManager#getModel(ModelResourceLocation)} or any other
|
* {@link net.minecraft.client.renderer.model.ModelManager#getModel(ModelResourceLocation)} or any other
|
||||||
* source.
|
* source.
|
||||||
*
|
*
|
||||||
* @param turtle Access to the turtle that the upgrade resides on. This will be null when getting item models!
|
* @param turtle Access to the turtle that the upgrade resides on. This will be null when getting item models!
|
||||||
@@ -139,7 +131,7 @@ public interface ITurtleUpgrade
|
|||||||
* a transformation of {@code null} has the same effect as the identify matrix.
|
* a transformation of {@code null} has the same effect as the identify matrix.
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@SideOnly( Side.CLIENT )
|
@OnlyIn( Dist.CLIENT )
|
||||||
Pair<IBakedModel, Matrix4f> getModel( @Nullable ITurtleAccess turtle, @Nonnull TurtleSide side );
|
Pair<IBakedModel, Matrix4f> getModel( @Nullable ITurtleAccess turtle, @Nonnull TurtleSide side );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ package dan200.computercraft.api.turtle.event;
|
|||||||
|
|
||||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||||
import dan200.computercraft.api.turtle.TurtleCommandResult;
|
import dan200.computercraft.api.turtle.TurtleCommandResult;
|
||||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
import net.minecraftforge.eventbus.api.Cancelable;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import net.minecraft.util.math.BlockPos;
|
|||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.FakePlayer;
|
import net.minecraftforge.common.util.FakePlayer;
|
||||||
import net.minecraftforge.event.world.BlockEvent;
|
import net.minecraftforge.event.world.BlockEvent;
|
||||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -37,7 +36,6 @@ import java.util.Objects;
|
|||||||
* Be aware that some events (such as {@link TurtleInventoryEvent}) do not necessarily interact
|
* Be aware that some events (such as {@link TurtleInventoryEvent}) do not necessarily interact
|
||||||
* with a block, simply objects within that block space.
|
* with a block, simply objects within that block space.
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
||||||
{
|
{
|
||||||
private final World world;
|
private final World world;
|
||||||
@@ -84,7 +82,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#DIG
|
* @see TurtleAction#DIG
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Dig extends TurtleBlockEvent
|
public static class Dig extends TurtleBlockEvent
|
||||||
{
|
{
|
||||||
private final IBlockState block;
|
private final IBlockState block;
|
||||||
@@ -142,7 +139,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#MOVE
|
* @see TurtleAction#MOVE
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Move extends TurtleBlockEvent
|
public static class Move extends TurtleBlockEvent
|
||||||
{
|
{
|
||||||
public Move( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos )
|
public Move( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos )
|
||||||
@@ -156,7 +152,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#PLACE
|
* @see TurtleAction#PLACE
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Place extends TurtleBlockEvent
|
public static class Place extends TurtleBlockEvent
|
||||||
{
|
{
|
||||||
private final ItemStack stack;
|
private final ItemStack stack;
|
||||||
@@ -188,7 +183,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#INSPECT
|
* @see TurtleAction#INSPECT
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Inspect extends TurtleBlockEvent
|
public static class Inspect extends TurtleBlockEvent
|
||||||
{
|
{
|
||||||
private final IBlockState state;
|
private final IBlockState state;
|
||||||
@@ -229,7 +223,7 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
|||||||
/**
|
/**
|
||||||
* Add new information to the inspection result. Note this will override fields with the same name.
|
* Add new information to the inspection result. Note this will override fields with the same name.
|
||||||
*
|
*
|
||||||
* @param newData The data to add. Note all values should be convertable to Lua (see
|
* @param newData The data to add. Note all values should be convertible to Lua (see
|
||||||
* {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
|
* {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
|
||||||
*/
|
*/
|
||||||
public void addData( @Nonnull Map<String, ?> newData )
|
public void addData( @Nonnull Map<String, ?> newData )
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
package dan200.computercraft.api.turtle.event;
|
package dan200.computercraft.api.turtle.event;
|
||||||
|
|
||||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
import net.minecraftforge.eventbus.api.Event;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.api.turtle.event;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
|
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired when a turtle gathers data on an item in its inventory.
|
||||||
|
*
|
||||||
|
* You may prevent items being inspected, or add additional information to the result. Be aware that this is fired on
|
||||||
|
* the computer thread, and so any operations on it must be thread safe.
|
||||||
|
*
|
||||||
|
* @see TurtleAction#INSPECT_ITEM
|
||||||
|
*/
|
||||||
|
public class TurtleInspectItemEvent extends TurtleActionEvent
|
||||||
|
{
|
||||||
|
private final ItemStack stack;
|
||||||
|
private final Map<String, Object> data;
|
||||||
|
|
||||||
|
public TurtleInspectItemEvent( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, @Nonnull Map<String, Object> data )
|
||||||
|
{
|
||||||
|
super( turtle, TurtleAction.INSPECT_ITEM );
|
||||||
|
|
||||||
|
Objects.requireNonNull( stack, "stack cannot be null" );
|
||||||
|
Objects.requireNonNull( data, "data cannot be null" );
|
||||||
|
this.stack = stack;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The item which is currently being inspected.
|
||||||
|
*
|
||||||
|
* @return The item stack which is being inspected. This should <b>not</b> be modified.
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public ItemStack getStack()
|
||||||
|
{
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the "inspection data" from this item, which will be returned to the user.
|
||||||
|
*
|
||||||
|
* @return This items's inspection data.
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public Map<String, Object> getData()
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add new information to the inspection result. Note this will override fields with the same name.
|
||||||
|
*
|
||||||
|
* @param newData The data to add. Note all values should be convertible to Lua (see
|
||||||
|
* {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
|
||||||
|
*/
|
||||||
|
public void addData( @Nonnull Map<String, ?> newData )
|
||||||
|
{
|
||||||
|
Objects.requireNonNull( newData, "newData cannot be null" );
|
||||||
|
data.putAll( newData );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@ import net.minecraft.item.ItemStack;
|
|||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.FakePlayer;
|
import net.minecraftforge.common.util.FakePlayer;
|
||||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
|
||||||
import net.minecraftforge.items.IItemHandler;
|
import net.minecraftforge.items.IItemHandler;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@@ -21,7 +20,6 @@ import java.util.Objects;
|
|||||||
/**
|
/**
|
||||||
* Fired when a turtle attempts to interact with an inventory.
|
* Fired when a turtle attempts to interact with an inventory.
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
||||||
{
|
{
|
||||||
private final IItemHandler handler;
|
private final IItemHandler handler;
|
||||||
@@ -48,7 +46,6 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#SUCK
|
* @see TurtleAction#SUCK
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Suck extends TurtleInventoryEvent
|
public static class Suck extends TurtleInventoryEvent
|
||||||
{
|
{
|
||||||
public Suck( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler )
|
public Suck( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler )
|
||||||
@@ -62,7 +59,6 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
|||||||
*
|
*
|
||||||
* @see TurtleAction#DROP
|
* @see TurtleAction#DROP
|
||||||
*/
|
*/
|
||||||
@Cancelable
|
|
||||||
public static class Drop extends TurtleInventoryEvent
|
public static class Drop extends TurtleInventoryEvent
|
||||||
{
|
{
|
||||||
private final ItemStack stack;
|
private final ItemStack stack;
|
||||||
@@ -78,13 +74,12 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
|||||||
/**
|
/**
|
||||||
* The item which will be inserted into the inventory/dropped on the ground.
|
* The item which will be inserted into the inventory/dropped on the ground.
|
||||||
*
|
*
|
||||||
* Note that this is a copy of the original stack, and so should not be modified, as that will have no effect.
|
* @return The item stack which will be dropped. This should <b>not</b> be modified.
|
||||||
*
|
|
||||||
* @return The item stack which will be dropped.
|
|
||||||
*/
|
*/
|
||||||
|
@Nonnull
|
||||||
public ItemStack getStack()
|
public ItemStack getStack()
|
||||||
{
|
{
|
||||||
return stack.copy();
|
return stack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.api.turtle.event;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired when a turtle attempts to refuel from an item.
|
||||||
|
*
|
||||||
|
* One may use {@link #setCanceled(boolean, String)} to prevent refueling from this specific item. Additionally, you
|
||||||
|
* may use {@link #setHandler(Handler)} to register a custom fuel provider.
|
||||||
|
*/
|
||||||
|
public class TurtleRefuelEvent extends TurtleActionEvent
|
||||||
|
{
|
||||||
|
private final ItemStack stack;
|
||||||
|
private Handler handler;
|
||||||
|
|
||||||
|
public TurtleRefuelEvent( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack )
|
||||||
|
{
|
||||||
|
super( turtle, TurtleAction.REFUEL );
|
||||||
|
|
||||||
|
Objects.requireNonNull( turtle, "turtle cannot be null" );
|
||||||
|
this.stack = stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the stack we are attempting to refuel from.
|
||||||
|
*
|
||||||
|
* Do not modify the returned stack - all modifications should be done within the {@link Handler}.
|
||||||
|
*
|
||||||
|
* @return The stack to refuel from.
|
||||||
|
*/
|
||||||
|
public ItemStack getStack()
|
||||||
|
{
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the refuel handler for this stack.
|
||||||
|
*
|
||||||
|
* @return The refuel handler, or {@code null} if none has currently been set.
|
||||||
|
* @see #setHandler(Handler)
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Handler getHandler()
|
||||||
|
{
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the refuel handler for this stack.
|
||||||
|
*
|
||||||
|
* You should call this if you can actually refuel from this item, and ideally only if there are no existing
|
||||||
|
* handlers.
|
||||||
|
*
|
||||||
|
* @param handler The new refuel handler.
|
||||||
|
* @see #getHandler()
|
||||||
|
*/
|
||||||
|
public void setHandler( @Nullable Handler handler )
|
||||||
|
{
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles refuelling a turtle from a specific item.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Handler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Refuel a turtle using an item.
|
||||||
|
*
|
||||||
|
* @param turtle The turtle to refuel.
|
||||||
|
* @param stack The stack to refuel with.
|
||||||
|
* @param slot The slot the stack resides within. This may be used to modify the inventory afterwards.
|
||||||
|
* @param limit The maximum number of refuel operations to perform. This will often correspond to the number of
|
||||||
|
* items to consume.
|
||||||
|
* @return The amount of fuel gained.
|
||||||
|
*/
|
||||||
|
int refuel( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, int slot, int limit );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Turtle|Event", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.turtle.event;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
|
|
||||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@API( owner = "ComputerCraft", provides = "ComputerCraft|API|Turtle", apiVersion = "${version}" )
|
|
||||||
package dan200.computercraft.api.turtle;
|
|
||||||
|
|
||||||
import net.minecraftforge.fml.common.API;
|
|
||||||
@@ -8,101 +8,81 @@ package dan200.computercraft.client;
|
|||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.client.render.TurtleModelLoader;
|
import dan200.computercraft.client.render.TurtleModelLoader;
|
||||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
import dan200.computercraft.shared.common.IColouredItem;
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
import dan200.computercraft.shared.media.items.ItemDisk;
|
||||||
|
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||||
import dan200.computercraft.shared.util.Colour;
|
import dan200.computercraft.shared.util.Colour;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.ItemMeshDefinition;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IUnbakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ModelBakery;
|
import net.minecraft.client.renderer.model.ModelResourceLocation;
|
||||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
import net.minecraft.client.renderer.model.ModelRotation;
|
||||||
import net.minecraft.client.renderer.texture.TextureMap;
|
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.resources.IResourceManager;
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.client.event.ColorHandlerEvent;
|
import net.minecraftforge.client.event.ColorHandlerEvent;
|
||||||
import net.minecraftforge.client.event.ModelBakeEvent;
|
import net.minecraftforge.client.event.ModelBakeEvent;
|
||||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||||
import net.minecraftforge.client.event.TextureStitchEvent;
|
import net.minecraftforge.client.event.TextureStitchEvent;
|
||||||
import net.minecraftforge.client.model.IModel;
|
|
||||||
import net.minecraftforge.client.model.ModelLoader;
|
import net.minecraftforge.client.model.ModelLoader;
|
||||||
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers textures and models for items.
|
* Registers textures and models for items.
|
||||||
*/
|
*/
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
public class ClientRegistry
|
public final class ClientRegistry
|
||||||
{
|
{
|
||||||
private static final String[] EXTRA_MODELS = {
|
private static final String[] EXTRA_MODELS = new String[] {
|
||||||
"turtle_modem_off_left",
|
"turtle_modem_normal_off_left",
|
||||||
"turtle_modem_on_left",
|
"turtle_modem_normal_on_left",
|
||||||
"turtle_modem_off_right",
|
"turtle_modem_normal_off_right",
|
||||||
"turtle_modem_on_right",
|
"turtle_modem_normal_on_right",
|
||||||
|
|
||||||
|
"turtle_modem_advanced_off_left",
|
||||||
|
"turtle_modem_advanced_on_left",
|
||||||
|
"turtle_modem_advanced_off_right",
|
||||||
|
"turtle_modem_advanced_on_right",
|
||||||
"turtle_crafting_table_left",
|
"turtle_crafting_table_left",
|
||||||
"turtle_crafting_table_right",
|
"turtle_crafting_table_right",
|
||||||
"advanced_turtle_modem_off_left",
|
|
||||||
"advanced_turtle_modem_on_left",
|
|
||||||
"advanced_turtle_modem_off_right",
|
|
||||||
"advanced_turtle_modem_on_right",
|
|
||||||
"turtle_speaker_upgrade_left",
|
"turtle_speaker_upgrade_left",
|
||||||
"turtle_speaker_upgrade_right",
|
"turtle_speaker_upgrade_right",
|
||||||
|
|
||||||
"turtle_white",
|
"turtle_colour",
|
||||||
"turtle_elf_overlay",
|
"turtle_elf_overlay",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final String[] EXTRA_TEXTURES = new String[] {
|
||||||
|
// TODO: Gather these automatically from the model. I'm unable to get this working with Forge's current
|
||||||
|
// model loading code.
|
||||||
|
"block/turtle_colour",
|
||||||
|
"block/turtle_elf_overlay",
|
||||||
|
"block/turtle_crafty_face",
|
||||||
|
"block/turtle_speaker_face",
|
||||||
|
};
|
||||||
|
|
||||||
|
private ClientRegistry() {}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void registerModels( ModelRegistryEvent event )
|
public static void registerModels( ModelRegistryEvent event )
|
||||||
{
|
{
|
||||||
ModelLoaderRegistry.registerLoader( TurtleModelLoader.INSTANCE );
|
ModelLoaderRegistry.registerLoader( TurtleModelLoader.INSTANCE );
|
||||||
|
|
||||||
// Register item models
|
|
||||||
registerUniversalItemModel( ComputerCraft.Items.computer, "computer" );
|
|
||||||
registerItemModel( ComputerCraft.Items.commandComputer, 0, "command_computer" );
|
|
||||||
|
|
||||||
registerItemModel( ComputerCraft.Items.pocketComputer, 0, "pocket_computer" );
|
|
||||||
registerItemModel( ComputerCraft.Items.pocketComputer, 1, "advanced_pocket_computer" );
|
|
||||||
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 0, "peripheral" );
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 1, "wireless_modem" );
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 2, "monitor" );
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 3, "printer" );
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 4, "advanced_monitor" );
|
|
||||||
registerItemModel( ComputerCraft.Items.cable, 0, "cable" );
|
|
||||||
registerItemModel( ComputerCraft.Items.cable, 1, "wired_modem" );
|
|
||||||
registerItemModel( ComputerCraft.Items.advancedModem, 0, "advanced_modem" );
|
|
||||||
registerItemModel( ComputerCraft.Items.peripheral, 5, "speaker" );
|
|
||||||
registerItemModel( ComputerCraft.Items.wiredModemFull, 0, "wired_modem_full" );
|
|
||||||
|
|
||||||
registerUniversalItemModel( ComputerCraft.Items.disk, "disk" );
|
|
||||||
registerItemModel( ComputerCraft.Items.diskExpanded, 0, "disk_expanded" );
|
|
||||||
registerItemModel( ComputerCraft.Items.treasureDisk, 0, "treasure_disk" );
|
|
||||||
|
|
||||||
registerItemModel( ComputerCraft.Items.printout, 0, "printout" );
|
|
||||||
registerItemModel( ComputerCraft.Items.printout, 1, "pages" );
|
|
||||||
registerItemModel( ComputerCraft.Items.printout, 2, "book" );
|
|
||||||
|
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle" );
|
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle" );
|
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_advanced" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
||||||
{
|
{
|
||||||
// Load all textures for the extra models
|
IResourceManager manager = Minecraft.getInstance().getResourceManager();
|
||||||
TextureMap map = event.getMap();
|
for( String extra : EXTRA_TEXTURES )
|
||||||
for( String upgrade : EXTRA_MODELS )
|
|
||||||
{
|
{
|
||||||
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
|
event.getMap().registerSprite( manager, new ResourceLocation( ComputerCraft.MOD_ID, extra ) );
|
||||||
for( ResourceLocation texture : model.getTextures() ) map.registerSprite( texture );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,84 +90,79 @@ public class ClientRegistry
|
|||||||
public static void onModelBakeEvent( ModelBakeEvent event )
|
public static void onModelBakeEvent( ModelBakeEvent event )
|
||||||
{
|
{
|
||||||
// Load all extra models
|
// Load all extra models
|
||||||
for( String model : EXTRA_MODELS ) loadBlockModel( event, model );
|
ModelLoader loader = event.getModelLoader();
|
||||||
|
Map<ModelResourceLocation, IBakedModel> registry = event.getModelRegistry();
|
||||||
|
|
||||||
|
for( String model : EXTRA_MODELS )
|
||||||
|
{
|
||||||
|
IBakedModel bakedModel = bake( loader, loader.getUnbakedModel( new ResourceLocation( ComputerCraft.MOD_ID, "item/" + model ) ) );
|
||||||
|
|
||||||
|
if( bakedModel != null )
|
||||||
|
{
|
||||||
|
registry.put(
|
||||||
|
new ModelResourceLocation( new ResourceLocation( ComputerCraft.MOD_ID, model ), "inventory" ),
|
||||||
|
bakedModel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// And load the custom turtle models in too.
|
||||||
|
registry.put(
|
||||||
|
new ModelResourceLocation( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_normal" ), "inventory" ),
|
||||||
|
bake( loader, TurtleModelLoader.INSTANCE.loadModel( new ResourceLocation( ComputerCraft.MOD_ID, "item/turtle_normal" ) ) )
|
||||||
|
);
|
||||||
|
|
||||||
|
registry.put(
|
||||||
|
new ModelResourceLocation( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_advanced" ), "inventory" ),
|
||||||
|
bake( loader, TurtleModelLoader.INSTANCE.loadModel( new ResourceLocation( ComputerCraft.MOD_ID, "item/turtle_advanced" ) ) )
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onItemColours( ColorHandlerEvent.Item event )
|
public static void onItemColours( ColorHandlerEvent.Item event )
|
||||||
{
|
{
|
||||||
event.getItemColors().registerItemColorHandler(
|
if( ComputerCraft.Items.disk == null || ComputerCraft.Blocks.turtleNormal == null )
|
||||||
( stack, layer ) -> layer == 0 ? 0xFFFFFF : ((ItemDiskLegacy) stack.getItem()).getColour( stack ),
|
{
|
||||||
ComputerCraft.Items.disk, ComputerCraft.Items.diskExpanded
|
ComputerCraft.log.warn( "Block/item registration has failed. Skipping registration of item colours." );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.getItemColors().register(
|
||||||
|
( stack, layer ) -> layer == 1 ? ((ItemDisk) stack.getItem()).getColour( stack ) : 0xFFFFFF,
|
||||||
|
ComputerCraft.Items.disk
|
||||||
);
|
);
|
||||||
|
|
||||||
event.getItemColors().registerItemColorHandler( ( stack, layer ) -> {
|
event.getItemColors().register( ( stack, layer ) -> {
|
||||||
switch( layer )
|
switch( layer )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
return 0xFFFFFF;
|
return 0xFFFFFF;
|
||||||
case 1:
|
case 1: // Frame colour
|
||||||
|
return IColouredItem.getColourBasic( stack );
|
||||||
|
case 2: // Light colour
|
||||||
{
|
{
|
||||||
// Frame colour
|
int light = ItemPocketComputer.getLightState( stack );
|
||||||
int colour = ComputerCraft.Items.pocketComputer.getColour( stack );
|
return light == -1 ? Colour.Black.getHex() : light;
|
||||||
return colour == -1 ? 0xFFFFFF : colour;
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
// Light colour
|
|
||||||
int colour = ComputerCraft.Items.pocketComputer.getLightState( stack );
|
|
||||||
return colour == -1 ? Colour.Black.getHex() : colour;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, ComputerCraft.Items.pocketComputer );
|
}, ComputerCraft.Items.pocketComputerNormal, ComputerCraft.Items.pocketComputerAdvanced );
|
||||||
|
|
||||||
// Setup turtle colours
|
// Setup turtle colours
|
||||||
event.getItemColors().registerItemColorHandler( ( stack, tintIndex ) -> {
|
event.getItemColors().register(
|
||||||
if( tintIndex == 0 )
|
( stack, tintIndex ) -> tintIndex == 0 ? ((IColouredItem) stack.getItem()).getColour( stack ) : 0xFFFFFF,
|
||||||
{
|
ComputerCraft.Blocks.turtleNormal, ComputerCraft.Blocks.turtleAdvanced
|
||||||
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
|
|
||||||
int colour = turtle.getColour( stack );
|
|
||||||
if( colour != -1 ) return colour;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0xFFFFFF;
|
|
||||||
}, ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced );
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void registerItemModel( Item item, int damage, String name )
|
|
||||||
{
|
|
||||||
ResourceLocation location = new ResourceLocation( ComputerCraft.MOD_ID, name );
|
|
||||||
final ModelResourceLocation res = new ModelResourceLocation( location, "inventory" );
|
|
||||||
ModelBakery.registerItemVariants( item, location );
|
|
||||||
ModelLoader.setCustomModelResourceLocation( item, damage, res );
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void registerUniversalItemModel( Item item, String mainModel )
|
|
||||||
{
|
|
||||||
ResourceLocation mainLocation = new ResourceLocation( ComputerCraft.MOD_ID, mainModel );
|
|
||||||
ModelBakery.registerItemVariants( item, mainLocation );
|
|
||||||
|
|
||||||
final ModelResourceLocation mainModelLocation = new ModelResourceLocation( mainLocation, "inventory" );
|
|
||||||
ModelLoader.setCustomMeshDefinition( item, new ItemMeshDefinition()
|
|
||||||
{
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public ModelResourceLocation getModelLocation( @Nonnull ItemStack stack )
|
|
||||||
{
|
|
||||||
return mainModelLocation;
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void loadBlockModel( ModelBakeEvent event, String name )
|
|
||||||
{
|
|
||||||
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( ComputerCraft.MOD_ID, "block/" + name ) );
|
|
||||||
IBakedModel bakedModel = model.bake(
|
|
||||||
model.getDefaultState(), DefaultVertexFormats.ITEM,
|
|
||||||
location -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( location.toString() )
|
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
event.getModelRegistry().putObject( new ModelResourceLocation( ComputerCraft.MOD_ID + ":" + name, "inventory" ), bakedModel );
|
private static IBakedModel bake( ModelLoader loader, IUnbakedModel model )
|
||||||
|
{
|
||||||
|
model.getTextures( loader::getUnbakedModel, new HashSet<>() );
|
||||||
|
|
||||||
|
return model.bake(
|
||||||
|
loader::getUnbakedModel,
|
||||||
|
ModelLoader.defaultTextureGetter(),
|
||||||
|
ModelRotation.X0_Y0, false, DefaultVertexFormats.BLOCK
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ public class ClientTableFormatter implements TableFormatter
|
|||||||
|
|
||||||
private static Int2IntOpenHashMap lastHeights = new Int2IntOpenHashMap();
|
private static Int2IntOpenHashMap lastHeights = new Int2IntOpenHashMap();
|
||||||
|
|
||||||
private FontRenderer renderer()
|
private static FontRenderer renderer()
|
||||||
{
|
{
|
||||||
return Minecraft.getMinecraft().fontRenderer;
|
return Minecraft.getInstance().fontRenderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -42,7 +42,7 @@ public class ClientTableFormatter implements TableFormatter
|
|||||||
|
|
||||||
FontRenderer renderer = renderer();
|
FontRenderer renderer = renderer();
|
||||||
|
|
||||||
float spaceWidth = renderer.getCharWidth( ' ' );
|
float spaceWidth = renderer.getStringWidth( " " );
|
||||||
int spaces = MathHelper.floor( extraWidth / spaceWidth );
|
int spaces = MathHelper.floor( extraWidth / spaceWidth );
|
||||||
int extra = extraWidth - (int) (spaces * spaceWidth);
|
int extra = extraWidth - (int) (spaces * spaceWidth);
|
||||||
|
|
||||||
@@ -64,11 +64,11 @@ public class ClientTableFormatter implements TableFormatter
|
|||||||
@Override
|
@Override
|
||||||
public void writeLine( int id, ITextComponent component )
|
public void writeLine( int id, ITextComponent component )
|
||||||
{
|
{
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
GuiNewChat chat = mc.ingameGUI.getChatGUI();
|
GuiNewChat chat = mc.ingameGUI.getChatGUI();
|
||||||
|
|
||||||
// Trim the text if it goes over the allowed length
|
// Trim the text if it goes over the allowed length
|
||||||
int maxWidth = MathHelper.floor( chat.getChatWidth() / chat.getChatScale() );
|
int maxWidth = MathHelper.floor( chat.getChatWidth() / chat.getScale() );
|
||||||
List<ITextComponent> list = GuiUtilRenderComponents.splitText( component, maxWidth, mc.fontRenderer, false, false );
|
List<ITextComponent> list = GuiUtilRenderComponents.splitText( component, maxWidth, mc.fontRenderer, false, false );
|
||||||
if( !list.isEmpty() ) chat.printChatMessageWithOptionalDeletion( list.get( 0 ), id );
|
if( !list.isEmpty() ) chat.printChatMessageWithOptionalDeletion( list.get( 0 ), id );
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ public class ClientTableFormatter implements TableFormatter
|
|||||||
@Override
|
@Override
|
||||||
public int display( TableBuilder table )
|
public int display( TableBuilder table )
|
||||||
{
|
{
|
||||||
GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
|
GuiNewChat chat = Minecraft.getInstance().ingameGUI.getChatGUI();
|
||||||
|
|
||||||
int lastHeight = lastHeights.get( table.getId() );
|
int lastHeight = lastHeights.get( table.getId() );
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,12 @@
|
|||||||
package dan200.computercraft.client;
|
package dan200.computercraft.client;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
|
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
public final class FrameInfo
|
public final class FrameInfo
|
||||||
{
|
{
|
||||||
private static int tick;
|
private static int tick;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import org.lwjgl.opengl.GL11;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class FixedWidthFontRenderer
|
public final class FixedWidthFontRenderer
|
||||||
{
|
{
|
||||||
private static final ResourceLocation FONT = new ResourceLocation( "computercraft", "textures/gui/term_font.png" );
|
private static final ResourceLocation FONT = new ResourceLocation( "computercraft", "textures/gui/term_font.png" );
|
||||||
public static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/term_background.png" );
|
public static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/term_background.png" );
|
||||||
@@ -39,7 +39,7 @@ public class FixedWidthFontRenderer
|
|||||||
|
|
||||||
private FixedWidthFontRenderer()
|
private FixedWidthFontRenderer()
|
||||||
{
|
{
|
||||||
m_textureManager = Minecraft.getMinecraft().getTextureManager();
|
m_textureManager = Minecraft.getInstance().getTextureManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void greyscaleify( double[] rgb )
|
private static void greyscaleify( double[] rgb )
|
||||||
@@ -93,7 +93,7 @@ public class FixedWidthFontRenderer
|
|||||||
|
|
||||||
private boolean isGreyScale( int colour )
|
private boolean isGreyScale( int colour )
|
||||||
{
|
{
|
||||||
return (colour == 0 || colour == 15 || colour == 7 || colour == 8);
|
return colour == 0 || colour == 15 || colour == 7 || colour == 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
|
public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
|
||||||
@@ -195,6 +195,6 @@ public class FixedWidthFontRenderer
|
|||||||
public void bindFont()
|
public void bindFont()
|
||||||
{
|
{
|
||||||
m_textureManager.bindTexture( FONT );
|
m_textureManager.bindTexture( FONT );
|
||||||
GlStateManager.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
|
GlStateManager.texParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,23 +8,19 @@ package dan200.computercraft.client.gui;
|
|||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||||
|
import dan200.computercraft.client.gui.widgets.WidgetWrapper;
|
||||||
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.computer.core.IComputer;
|
|
||||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.inventory.Container;
|
import net.minecraft.inventory.Container;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import org.lwjgl.input.Keyboard;
|
|
||||||
import org.lwjgl.input.Mouse;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class GuiComputer extends GuiContainer
|
public class GuiComputer extends GuiContainer
|
||||||
{
|
{
|
||||||
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/corners.png" );
|
private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( "computercraft", "textures/gui/corners_normal.png" );
|
||||||
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( "computercraft", "textures/gui/corners_advanced.png" );
|
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( "computercraft", "textures/gui/corners_advanced.png" );
|
||||||
private static final ResourceLocation BACKGROUND_COMMAND = new ResourceLocation( "computercraft", "textures/gui/corners_command.png" );
|
private static final ResourceLocation BACKGROUND_COMMAND = new ResourceLocation( "computercraft", "textures/gui/corners_command.png" );
|
||||||
|
|
||||||
@@ -32,7 +28,9 @@ public class GuiComputer extends GuiContainer
|
|||||||
private final ClientComputer m_computer;
|
private final ClientComputer m_computer;
|
||||||
private final int m_termWidth;
|
private final int m_termWidth;
|
||||||
private final int m_termHeight;
|
private final int m_termHeight;
|
||||||
private WidgetTerminal m_terminal;
|
|
||||||
|
private WidgetTerminal terminal;
|
||||||
|
private WidgetWrapper terminalWrapper;
|
||||||
|
|
||||||
public GuiComputer( Container container, ComputerFamily family, ClientComputer computer, int termWidth, int termHeight )
|
public GuiComputer( Container container, ComputerFamily family, ClientComputer computer, int termWidth, int termHeight )
|
||||||
{
|
{
|
||||||
@@ -41,13 +39,7 @@ public class GuiComputer extends GuiContainer
|
|||||||
m_computer = computer;
|
m_computer = computer;
|
||||||
m_termWidth = termWidth;
|
m_termWidth = termWidth;
|
||||||
m_termHeight = termHeight;
|
m_termHeight = termHeight;
|
||||||
m_terminal = null;
|
terminal = null;
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public GuiComputer( Container container, ComputerFamily family, IComputer computer, int termWidth, int termHeight )
|
|
||||||
{
|
|
||||||
this( container, family, (ClientComputer) computer, termWidth, termHeight );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public GuiComputer( TileComputer computer )
|
public GuiComputer( TileComputer computer )
|
||||||
@@ -62,122 +54,67 @@ public class GuiComputer extends GuiContainer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initGui()
|
protected void initGui()
|
||||||
{
|
{
|
||||||
super.initGui();
|
mc.keyboardListener.enableRepeatEvents( true );
|
||||||
Keyboard.enableRepeatEvents( true );
|
|
||||||
|
|
||||||
m_terminal = new WidgetTerminal( 0, 0, m_termWidth, m_termHeight, () -> m_computer, 2, 2, 2, 2 );
|
int termPxWidth = m_termWidth * FixedWidthFontRenderer.FONT_WIDTH;
|
||||||
m_terminal.setAllowFocusLoss( false );
|
int termPxHeight = m_termHeight * FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
xSize = m_terminal.getWidth() + 24;
|
|
||||||
ySize = m_terminal.getHeight() + 24;
|
xSize = termPxWidth + 4 + 24;
|
||||||
|
ySize = termPxHeight + 4 + 24;
|
||||||
|
|
||||||
|
super.initGui();
|
||||||
|
|
||||||
|
terminal = new WidgetTerminal( mc, () -> m_computer, m_termWidth, m_termHeight, 2, 2, 2, 2 );
|
||||||
|
terminalWrapper = new WidgetWrapper( terminal, 2 + 12 + guiLeft, 2 + 12 + guiTop, termPxWidth, termPxHeight );
|
||||||
|
|
||||||
|
children.add( terminalWrapper );
|
||||||
|
setFocused( terminalWrapper );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGuiClosed()
|
public void onGuiClosed()
|
||||||
{
|
{
|
||||||
super.onGuiClosed();
|
super.onGuiClosed();
|
||||||
Keyboard.enableRepeatEvents( false );
|
children.remove( terminal );
|
||||||
|
terminal = null;
|
||||||
|
mc.keyboardListener.enableRepeatEvents( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean doesGuiPauseGame()
|
public void tick()
|
||||||
{
|
{
|
||||||
return false;
|
super.tick();
|
||||||
|
terminal.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateScreen()
|
public void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
|
||||||
{
|
|
||||||
super.updateScreen();
|
|
||||||
m_terminal.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void keyTyped( char c, int k ) throws IOException
|
|
||||||
{
|
|
||||||
if( k == 1 )
|
|
||||||
{
|
|
||||||
super.keyTyped( c, k );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( m_terminal.onKeyTyped( c, k ) ) keyHandled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void mouseClicked( int x, int y, int button )
|
|
||||||
{
|
|
||||||
int startX = (width - m_terminal.getWidth()) / 2;
|
|
||||||
int startY = (height - m_terminal.getHeight()) / 2;
|
|
||||||
m_terminal.mouseClicked( x - startX, y - startY, button );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleMouseInput() throws IOException
|
|
||||||
{
|
|
||||||
super.handleMouseInput();
|
|
||||||
|
|
||||||
int x = Mouse.getEventX() * width / mc.displayWidth;
|
|
||||||
int y = height - Mouse.getEventY() * height / mc.displayHeight - 1;
|
|
||||||
int startX = (width - m_terminal.getWidth()) / 2;
|
|
||||||
int startY = (height - m_terminal.getHeight()) / 2;
|
|
||||||
m_terminal.handleMouseInput( x - startX, y - startY );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleKeyboardInput() throws IOException
|
|
||||||
{
|
|
||||||
super.handleKeyboardInput();
|
|
||||||
if( m_terminal.onKeyboardInput() ) keyHandled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void drawGuiContainerForegroundLayer( int par1, int par2 )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void drawGuiContainerBackgroundLayer( float var1, int var2, int var3 )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawScreen( int mouseX, int mouseY, float f )
|
|
||||||
{
|
{
|
||||||
// Work out where to draw
|
// Work out where to draw
|
||||||
int startX = (width - m_terminal.getWidth()) / 2;
|
int startX = terminalWrapper.getX() - 2;
|
||||||
int startY = (height - m_terminal.getHeight()) / 2;
|
int startY = terminalWrapper.getY() - 2;
|
||||||
int endX = startX + m_terminal.getWidth();
|
int endX = startX + terminalWrapper.getWidth() + 4;
|
||||||
int endY = startY + m_terminal.getHeight();
|
int endY = startY + terminalWrapper.getHeight() + 4;
|
||||||
|
|
||||||
// Draw background
|
|
||||||
drawDefaultBackground();
|
|
||||||
|
|
||||||
// Draw terminal
|
// Draw terminal
|
||||||
m_terminal.draw( this.mc, startX, startY, mouseX, mouseY );
|
terminal.draw( terminalWrapper.getX(), terminalWrapper.getY() );
|
||||||
|
|
||||||
// Draw a border around the terminal
|
// Draw a border around the terminal
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
switch( m_family )
|
switch( m_family )
|
||||||
{
|
{
|
||||||
case Normal:
|
case Normal:
|
||||||
default:
|
default:
|
||||||
{
|
mc.getTextureManager().bindTexture( BACKGROUND_NORMAL );
|
||||||
this.mc.getTextureManager().bindTexture( BACKGROUND );
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case Advanced:
|
case Advanced:
|
||||||
{
|
mc.getTextureManager().bindTexture( BACKGROUND_ADVANCED );
|
||||||
this.mc.getTextureManager().bindTexture( BACKGROUND_ADVANCED );
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case Command:
|
case Command:
|
||||||
{
|
mc.getTextureManager().bindTexture( BACKGROUND_COMMAND );
|
||||||
this.mc.getTextureManager().bindTexture( BACKGROUND_COMMAND );
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
drawTexturedModalRect( startX - 12, startY - 12, 12, 28, 12, 12 );
|
drawTexturedModalRect( startX - 12, startY - 12, 12, 28, 12, 12 );
|
||||||
@@ -191,4 +128,26 @@ public class GuiComputer extends GuiContainer
|
|||||||
drawTexturedModalRect( startX - 12, startY, 0, 28, 12, endY - startY );
|
drawTexturedModalRect( startX - 12, startY, 0, 28, 12, endY - startY );
|
||||||
drawTexturedModalRect( endX, startY, 36, 28, 12, endY - startY );
|
drawTexturedModalRect( endX, startY, 36, 28, 12, endY - startY );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render( int mouseX, int mouseY, float partialTicks )
|
||||||
|
{
|
||||||
|
drawDefaultBackground();
|
||||||
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
|
renderHoveredToolTip( mouseX, mouseY );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseDragged( double x, double y, int button, double deltaX, double deltaY )
|
||||||
|
{
|
||||||
|
return (getFocused() != null && getFocused().mouseDragged( x, y, button, deltaX, deltaY ))
|
||||||
|
|| super.mouseDragged( x, y, button, deltaX, deltaY );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseReleased( double x, double y, int button )
|
||||||
|
{
|
||||||
|
return (getFocused() != null && getFocused().mouseReleased( x, y, button ))
|
||||||
|
|| super.mouseReleased( x, y, button );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
||||||
* Send enquiries to dratcliffe@gmail.com
|
|
||||||
*/
|
|
||||||
|
|
||||||
package dan200.computercraft.client.gui;
|
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
|
||||||
import dan200.computercraft.shared.Config;
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.gui.GuiScreen;
|
|
||||||
import net.minecraftforge.fml.client.IModGuiFactory;
|
|
||||||
import net.minecraftforge.fml.client.config.GuiConfig;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class GuiConfigCC extends GuiConfig
|
|
||||||
{
|
|
||||||
public GuiConfigCC( GuiScreen parentScreen )
|
|
||||||
{
|
|
||||||
super( parentScreen, Config.getConfigElements(), ComputerCraft.MOD_ID, false, false, "CC: Tweaked" );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Factory
|
|
||||||
implements IModGuiFactory
|
|
||||||
{
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initialize( Minecraft minecraft )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasConfigGui()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public GuiScreen createConfigGui( GuiScreen parentScreen )
|
|
||||||
{
|
|
||||||
return new GuiConfigCC( parentScreen );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories()
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,7 +14,7 @@ import net.minecraft.util.ResourceLocation;
|
|||||||
|
|
||||||
public class GuiDiskDrive extends GuiContainer
|
public class GuiDiskDrive extends GuiContainer
|
||||||
{
|
{
|
||||||
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/diskdrive.png" );
|
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/disk_drive.png" );
|
||||||
|
|
||||||
private final ContainerDiskDrive m_container;
|
private final ContainerDiskDrive m_container;
|
||||||
|
|
||||||
@@ -25,28 +25,26 @@ public class GuiDiskDrive extends GuiContainer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawGuiContainerForegroundLayer( int par1, int par2 )
|
protected void drawGuiContainerForegroundLayer( int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
String title = m_container.getDiskDrive().getDisplayName().getUnformattedText();
|
String title = m_container.getDiskDrive().getDisplayName().getString();
|
||||||
fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2, 6, 0x404040 );
|
fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2.0f, 6, 0x404040 );
|
||||||
fontRenderer.drawString( I18n.format( "container.inventory" ), 8, (ySize - 96) + 2, 0x404040 );
|
fontRenderer.drawString( I18n.format( "container.inventory" ), 8, ySize - 96 + 2, 0x404040 );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawGuiContainerBackgroundLayer( float f, int i, int j )
|
protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
this.mc.getTextureManager().bindTexture( BACKGROUND );
|
mc.getTextureManager().bindTexture( BACKGROUND );
|
||||||
int l = (width - xSize) / 2;
|
drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
|
||||||
int i1 = (height - ySize) / 2;
|
|
||||||
drawTexturedModalRect( l, i1, 0, 0, xSize, ySize );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawScreen( int mouseX, int mouseY, float partialTicks )
|
public void render( int mouseX, int mouseY, float partialTicks )
|
||||||
{
|
{
|
||||||
drawDefaultBackground();
|
drawDefaultBackground();
|
||||||
super.drawScreen( mouseX, mouseY, partialTicks );
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
renderHoveredToolTip( mouseX, mouseY );
|
renderHoveredToolTip( mouseX, mouseY );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,18 +7,28 @@
|
|||||||
package dan200.computercraft.client.gui;
|
package dan200.computercraft.client.gui;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
|
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||||
|
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
public class GuiPocketComputer extends GuiComputer
|
public class GuiPocketComputer extends GuiComputer
|
||||||
{
|
{
|
||||||
public GuiPocketComputer( ContainerHeldItem container )
|
public GuiPocketComputer( ContainerPocketComputer container )
|
||||||
{
|
{
|
||||||
super(
|
super(
|
||||||
container,
|
container,
|
||||||
ComputerCraft.Items.pocketComputer.getFamily( container.getStack() ),
|
getFamily( container.getStack() ),
|
||||||
ComputerCraft.Items.pocketComputer.createClientComputer( container.getStack() ),
|
ItemPocketComputer.createClientComputer( container.getStack() ),
|
||||||
ComputerCraft.terminalWidth_pocketComputer,
|
ComputerCraft.terminalWidth_pocketComputer,
|
||||||
ComputerCraft.terminalHeight_pocketComputer
|
ComputerCraft.terminalHeight_pocketComputer
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ComputerFamily getFamily( ItemStack stack )
|
||||||
|
{
|
||||||
|
Item item = stack.getItem();
|
||||||
|
return item instanceof ItemPocketComputer ? ((ItemPocketComputer) item).getFamily() : ComputerFamily.Normal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,43 +16,37 @@ public class GuiPrinter extends GuiContainer
|
|||||||
{
|
{
|
||||||
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/printer.png" );
|
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/printer.png" );
|
||||||
|
|
||||||
private final ContainerPrinter m_container;
|
private final ContainerPrinter container;
|
||||||
|
|
||||||
public GuiPrinter( ContainerPrinter container )
|
public GuiPrinter( ContainerPrinter container )
|
||||||
{
|
{
|
||||||
super( container );
|
super( container );
|
||||||
m_container = container;
|
this.container = container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawGuiContainerForegroundLayer( int par1, int par2 )
|
protected void drawGuiContainerForegroundLayer( int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
String title = m_container.getPrinter().getDisplayName().getUnformattedText();
|
String title = container.getPrinter().getDisplayName().getString();
|
||||||
fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2, 6, 0x404040 );
|
fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2.0f, 6, 0x404040 );
|
||||||
fontRenderer.drawString( I18n.format( "container.inventory" ), 8, (ySize - 96) + 2, 0x404040 );
|
fontRenderer.drawString( I18n.format( "container.inventory" ), 8, ySize - 96 + 2, 0x404040 );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawGuiContainerBackgroundLayer( float f, int i, int j )
|
protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
this.mc.getTextureManager().bindTexture( BACKGROUND );
|
mc.getTextureManager().bindTexture( BACKGROUND );
|
||||||
int startX = (width - xSize) / 2;
|
drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
|
||||||
int startY = (height - ySize) / 2;
|
|
||||||
drawTexturedModalRect( startX, startY, 0, 0, xSize, ySize );
|
|
||||||
|
|
||||||
boolean printing = m_container.isPrinting();
|
if( container.isPrinting() ) drawTexturedModalRect( guiLeft + 34, guiTop + 21, 176, 0, 25, 45 );
|
||||||
if( printing )
|
|
||||||
{
|
|
||||||
drawTexturedModalRect( startX + 34, startY + 21, 176, 0, 25, 45 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawScreen( int mouseX, int mouseY, float partialTicks )
|
public void render( int mouseX, int mouseY, float partialTicks )
|
||||||
{
|
{
|
||||||
drawDefaultBackground();
|
drawDefaultBackground();
|
||||||
super.drawScreen( mouseX, mouseY, partialTicks );
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
renderHoveredToolTip( mouseX, mouseY );
|
renderHoveredToolTip( mouseX, mouseY );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,11 @@
|
|||||||
package dan200.computercraft.client.gui;
|
package dan200.computercraft.client.gui;
|
||||||
|
|
||||||
import dan200.computercraft.core.terminal.TextBuffer;
|
import dan200.computercraft.core.terminal.TextBuffer;
|
||||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
import dan200.computercraft.shared.common.ContainerHeldItem;
|
||||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import org.lwjgl.input.Mouse;
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static dan200.computercraft.client.render.PrintoutRenderer.*;
|
import static dan200.computercraft.client.render.PrintoutRenderer.*;
|
||||||
|
|
||||||
@@ -29,6 +27,8 @@ public class GuiPrintout extends GuiContainer
|
|||||||
{
|
{
|
||||||
super( container );
|
super( container );
|
||||||
|
|
||||||
|
ySize = Y_SIZE;
|
||||||
|
|
||||||
String[] text = ItemPrintout.getText( container.getStack() );
|
String[] text = ItemPrintout.getText( container.getStack() );
|
||||||
m_text = new TextBuffer[text.length];
|
m_text = new TextBuffer[text.length];
|
||||||
for( int i = 0; i < m_text.length; i++ ) m_text[i] = new TextBuffer( text[i] );
|
for( int i = 0; i < m_text.length; i++ ) m_text[i] = new TextBuffer( text[i] );
|
||||||
@@ -39,75 +39,70 @@ public class GuiPrintout extends GuiContainer
|
|||||||
|
|
||||||
m_page = 0;
|
m_page = 0;
|
||||||
m_pages = Math.max( m_text.length / ItemPrintout.LINES_PER_PAGE, 1 );
|
m_pages = Math.max( m_text.length / ItemPrintout.LINES_PER_PAGE, 1 );
|
||||||
m_book = ItemPrintout.getType( container.getStack() ) == ItemPrintout.Type.Book;
|
m_book = ((ItemPrintout) container.getStack().getItem()).getType() == ItemPrintout.Type.BOOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean doesGuiPauseGame()
|
public boolean keyPressed( int key, int scancode, int modifiers )
|
||||||
{
|
{
|
||||||
|
if( super.keyPressed( key, scancode, modifiers ) ) return true;
|
||||||
|
|
||||||
|
if( key == GLFW.GLFW_KEY_RIGHT )
|
||||||
|
{
|
||||||
|
if( m_page < m_pages - 1 ) m_page++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( key == GLFW.GLFW_KEY_LEFT )
|
||||||
|
{
|
||||||
|
if( m_page > 0 ) m_page--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void keyTyped( char c, int k ) throws IOException
|
public boolean mouseScrolled( double delta )
|
||||||
{
|
{
|
||||||
super.keyTyped( c, k );
|
if( super.mouseScrolled( delta ) ) return true;
|
||||||
|
if( delta < 0 )
|
||||||
if( k == 205 )
|
|
||||||
{
|
{
|
||||||
// Right
|
// Scroll up goes to the next page
|
||||||
if( m_page < m_pages - 1 ) m_page++;
|
if( m_page < m_pages - 1 ) m_page++;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else if( k == 203 )
|
|
||||||
|
if( delta > 0 )
|
||||||
{
|
{
|
||||||
// Left
|
// Scroll down goes to the previous page
|
||||||
if( m_page > 0 ) m_page--;
|
if( m_page > 0 ) m_page--;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMouseInput() throws IOException
|
public void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
super.handleMouseInput();
|
|
||||||
|
|
||||||
int mouseWheelChange = Mouse.getEventDWheel();
|
|
||||||
if( mouseWheelChange < 0 )
|
|
||||||
{
|
|
||||||
// Up
|
|
||||||
if( m_page < m_pages - 1 ) m_page++;
|
|
||||||
}
|
|
||||||
else if( mouseWheelChange > 0 )
|
|
||||||
{
|
|
||||||
// Down
|
|
||||||
if( m_page > 0 ) m_page--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void drawGuiContainerForegroundLayer( int par1, int par2 )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void drawGuiContainerBackgroundLayer( float var1, int var2, int var3 )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawScreen( int mouseX, int mouseY, float f )
|
|
||||||
{
|
|
||||||
// Draw background
|
|
||||||
zLevel = zLevel - 1;
|
|
||||||
drawDefaultBackground();
|
|
||||||
zLevel = zLevel + 1;
|
|
||||||
|
|
||||||
// Draw the printout
|
// Draw the printout
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
|
GlStateManager.enableDepthTest();
|
||||||
|
|
||||||
int startY = (height - Y_SIZE) / 2;
|
drawBorder( guiLeft, guiTop, zLevel, m_page, m_pages, m_book );
|
||||||
int startX = (width - X_SIZE) / 2;
|
drawText( guiLeft + X_TEXT_MARGIN, guiTop + Y_TEXT_MARGIN, ItemPrintout.LINES_PER_PAGE * m_page, m_text, m_colours );
|
||||||
|
}
|
||||||
|
|
||||||
drawBorder( startX, startY, zLevel, m_page, m_pages, m_book );
|
@Override
|
||||||
drawText( startX + X_TEXT_MARGIN, startY + Y_TEXT_MARGIN, ItemPrintout.LINES_PER_PAGE * m_page, m_text, m_colours );
|
public void render( int mouseX, int mouseY, float partialTicks )
|
||||||
|
{
|
||||||
|
// We must take the background further back in order to not overlap with our printed pages.
|
||||||
|
zLevel--;
|
||||||
|
drawDefaultBackground();
|
||||||
|
zLevel++;
|
||||||
|
|
||||||
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
|
renderHoveredToolTip( mouseX, mouseY );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,29 +8,27 @@ package dan200.computercraft.client.gui;
|
|||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||||
|
import dan200.computercraft.client.gui.widgets.WidgetWrapper;
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||||
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import org.lwjgl.input.Keyboard;
|
|
||||||
import org.lwjgl.input.Mouse;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class GuiTurtle extends GuiContainer
|
public class GuiTurtle extends GuiContainer
|
||||||
{
|
{
|
||||||
private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/turtle.png" );
|
private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( "computercraft", "textures/gui/turtle_normal.png" );
|
||||||
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( "computercraft", "textures/gui/turtle_advanced.png" );
|
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( "computercraft", "textures/gui/turtle_advanced.png" );
|
||||||
|
|
||||||
private ContainerTurtle m_container;
|
private ContainerTurtle m_container;
|
||||||
|
|
||||||
private final ComputerFamily m_family;
|
private final ComputerFamily m_family;
|
||||||
private final ClientComputer m_computer;
|
private final ClientComputer m_computer;
|
||||||
private WidgetTerminal m_terminalGui;
|
|
||||||
|
private WidgetTerminal terminal;
|
||||||
|
private WidgetWrapper terminalWrapper;
|
||||||
|
|
||||||
public GuiTurtle( TileTurtle turtle, ContainerTurtle container )
|
public GuiTurtle( TileTurtle turtle, ContainerTurtle container )
|
||||||
{
|
{
|
||||||
@@ -45,110 +43,75 @@ public class GuiTurtle extends GuiContainer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initGui()
|
protected void initGui()
|
||||||
{
|
{
|
||||||
super.initGui();
|
super.initGui();
|
||||||
Keyboard.enableRepeatEvents( true );
|
mc.keyboardListener.enableRepeatEvents( true );
|
||||||
m_terminalGui = new WidgetTerminal(
|
|
||||||
(width - xSize) / 2 + 8,
|
int termPxWidth = ComputerCraft.terminalWidth_turtle * FixedWidthFontRenderer.FONT_WIDTH;
|
||||||
(height - ySize) / 2 + 8,
|
int termPxHeight = ComputerCraft.terminalHeight_turtle * FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
|
|
||||||
|
terminal = new WidgetTerminal(
|
||||||
|
mc, () -> m_computer,
|
||||||
ComputerCraft.terminalWidth_turtle,
|
ComputerCraft.terminalWidth_turtle,
|
||||||
ComputerCraft.terminalHeight_turtle,
|
ComputerCraft.terminalHeight_turtle,
|
||||||
() -> m_computer,
|
|
||||||
2, 2, 2, 2
|
2, 2, 2, 2
|
||||||
);
|
);
|
||||||
m_terminalGui.setAllowFocusLoss( false );
|
terminalWrapper = new WidgetWrapper( terminal, 2 + 8 + guiLeft, 2 + 8 + guiTop, termPxWidth, termPxHeight );
|
||||||
|
|
||||||
|
children.add( terminalWrapper );
|
||||||
|
setFocused( terminalWrapper );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGuiClosed()
|
public void onGuiClosed()
|
||||||
{
|
{
|
||||||
super.onGuiClosed();
|
children.remove( terminal );
|
||||||
Keyboard.enableRepeatEvents( false );
|
terminal = null;
|
||||||
|
mc.keyboardListener.enableRepeatEvents( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateScreen()
|
public void tick()
|
||||||
{
|
{
|
||||||
super.updateScreen();
|
super.tick();
|
||||||
m_terminalGui.update();
|
terminal.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void drawSelectionSlot( boolean advanced )
|
||||||
protected void keyTyped( char c, int k ) throws IOException
|
|
||||||
{
|
{
|
||||||
if( k == 1 )
|
|
||||||
{
|
|
||||||
super.keyTyped( c, k );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( m_terminalGui.onKeyTyped( c, k ) ) keyHandled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void mouseClicked( int x, int y, int button ) throws IOException
|
|
||||||
{
|
|
||||||
super.mouseClicked( x, y, button );
|
|
||||||
m_terminalGui.mouseClicked( x, y, button );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleMouseInput() throws IOException
|
|
||||||
{
|
|
||||||
super.handleMouseInput();
|
|
||||||
int x = Mouse.getEventX() * this.width / mc.displayWidth;
|
|
||||||
int y = this.height - Mouse.getEventY() * this.height / mc.displayHeight - 1;
|
|
||||||
m_terminalGui.handleMouseInput( x, y );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleKeyboardInput() throws IOException
|
|
||||||
{
|
|
||||||
super.handleKeyboardInput();
|
|
||||||
if( m_terminalGui.onKeyboardInput() ) keyHandled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void drawSelectionSlot( boolean advanced )
|
|
||||||
{
|
|
||||||
int x = (width - xSize) / 2;
|
|
||||||
int y = (height - ySize) / 2;
|
|
||||||
|
|
||||||
// Draw selection slot
|
// Draw selection slot
|
||||||
int slot = m_container.getSelectedSlot();
|
int slot = m_container.getSelectedSlot();
|
||||||
if( slot >= 0 )
|
if( slot >= 0 )
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
int slotX = (slot % 4);
|
int slotX = slot % 4;
|
||||||
int slotY = (slot / 4);
|
int slotY = slot / 4;
|
||||||
this.mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND );
|
mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
|
||||||
drawTexturedModalRect( x + m_container.m_turtleInvStartX - 2 + slotX * 18, y + m_container.m_playerInvStartY - 2 + slotY * 18, 0, 217, 24, 24 );
|
drawTexturedModalRect( guiLeft + m_container.m_turtleInvStartX - 2 + slotX * 18, guiTop + m_container.m_playerInvStartY - 2 + slotY * 18, 0, 217, 24, 24 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawGuiContainerBackgroundLayer( float f, int mouseX, int mouseY )
|
protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
|
||||||
{
|
{
|
||||||
// Draw term
|
// Draw term
|
||||||
boolean advanced = (m_family == ComputerFamily.Advanced);
|
boolean advanced = m_family == ComputerFamily.Advanced;
|
||||||
m_terminalGui.draw( Minecraft.getMinecraft(), 0, 0, mouseX, mouseY );
|
terminal.draw( terminalWrapper.getX(), terminalWrapper.getY() );
|
||||||
|
|
||||||
// Draw border/inventory
|
// Draw border/inventory
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
this.mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND );
|
mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
|
||||||
int x = (width - xSize) / 2;
|
drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
|
||||||
int y = (height - ySize) / 2;
|
|
||||||
drawTexturedModalRect( x, y, 0, 0, xSize, ySize );
|
|
||||||
|
|
||||||
drawSelectionSlot( advanced );
|
drawSelectionSlot( advanced );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawScreen( int mouseX, int mouseY, float partialTicks )
|
public void render( int mouseX, int mouseY, float partialTicks )
|
||||||
{
|
{
|
||||||
drawDefaultBackground();
|
drawDefaultBackground();
|
||||||
super.drawScreen( mouseX, mouseY, partialTicks );
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
renderHoveredToolTip( mouseX, mouseY );
|
renderHoveredToolTip( mouseX, mouseY );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
||||||
* Send enquiries to dratcliffe@gmail.com
|
|
||||||
*/
|
|
||||||
|
|
||||||
package dan200.computercraft.client.gui.widgets;
|
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.gui.Gui;
|
|
||||||
|
|
||||||
public abstract class Widget extends Gui
|
|
||||||
{
|
|
||||||
private int m_xPosition;
|
|
||||||
private int m_yPosition;
|
|
||||||
private int m_width;
|
|
||||||
private int m_height;
|
|
||||||
|
|
||||||
protected Widget( int x, int y, int width, int height )
|
|
||||||
{
|
|
||||||
m_xPosition = x;
|
|
||||||
m_yPosition = y;
|
|
||||||
m_width = width;
|
|
||||||
m_height = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getXPosition()
|
|
||||||
{
|
|
||||||
return m_xPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getYPosition()
|
|
||||||
{
|
|
||||||
return m_yPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getWidth()
|
|
||||||
{
|
|
||||||
return m_width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight()
|
|
||||||
{
|
|
||||||
return m_height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleMouseInput( int mouseX, int mouseY )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean onKeyboardInput()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void handleKeyboardInput()
|
|
||||||
{
|
|
||||||
onKeyboardInput();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void mouseClicked( int mouseX, int mouseY, int mouseButton )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean onKeyTyped( char c, int k )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void keyTyped( char c, int k )
|
|
||||||
{
|
|
||||||
onKeyTyped( c, k );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,411 +10,349 @@ import dan200.computercraft.client.FrameInfo;
|
|||||||
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
|
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
import dan200.computercraft.core.terminal.TextBuffer;
|
import dan200.computercraft.core.terminal.TextBuffer;
|
||||||
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
import dan200.computercraft.shared.computer.core.IComputer;
|
import dan200.computercraft.shared.computer.core.IComputer;
|
||||||
import dan200.computercraft.shared.computer.core.IComputerContainer;
|
|
||||||
import dan200.computercraft.shared.util.Colour;
|
import dan200.computercraft.shared.util.Colour;
|
||||||
import dan200.computercraft.shared.util.Palette;
|
import dan200.computercraft.shared.util.Palette;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.GuiScreen;
|
import net.minecraft.client.gui.IGuiEventListener;
|
||||||
|
import net.minecraft.client.renderer.BufferBuilder;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.util.ChatAllowedCharacters;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import org.lwjgl.input.Keyboard;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import org.lwjgl.input.Mouse;
|
import net.minecraft.util.SharedConstants;
|
||||||
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.BitSet;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.BACKGROUND;
|
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.BACKGROUND;
|
||||||
|
|
||||||
public class WidgetTerminal extends Widget
|
public class WidgetTerminal implements IGuiEventListener
|
||||||
{
|
{
|
||||||
private static final float TERMINATE_TIME = 0.5f;
|
private static final float TERMINATE_TIME = 0.5f;
|
||||||
|
|
||||||
private final IComputerContainer m_computer;
|
private final Minecraft client;
|
||||||
|
|
||||||
private float m_terminateTimer;
|
private final Supplier<ClientComputer> computer;
|
||||||
private float m_rebootTimer;
|
private final int termWidth;
|
||||||
private float m_shutdownTimer;
|
private final int termHeight;
|
||||||
|
|
||||||
private int m_lastClickButton;
|
private float terminateTimer = -1;
|
||||||
private int m_lastClickX;
|
private float rebootTimer = -1;
|
||||||
private int m_lastClickY;
|
private float shutdownTimer = -1;
|
||||||
|
|
||||||
private boolean m_focus;
|
private int lastMouseButton = -1;
|
||||||
private boolean m_allowFocusLoss;
|
private int lastMouseX = -1;
|
||||||
|
private int lastMouseY = -1;
|
||||||
|
|
||||||
private int m_leftMargin;
|
private final int leftMargin;
|
||||||
private int m_rightMargin;
|
private final int rightMargin;
|
||||||
private int m_topMargin;
|
private final int topMargin;
|
||||||
private int m_bottomMargin;
|
private final int bottomMargin;
|
||||||
|
|
||||||
private ArrayList<Integer> m_keysDown;
|
private final BitSet keysDown = new BitSet( 256 );
|
||||||
|
|
||||||
public WidgetTerminal( int x, int y, int termWidth, int termHeight, IComputerContainer computer, int leftMargin, int rightMargin, int topMargin, int bottomMargin )
|
public WidgetTerminal( Minecraft client, Supplier<ClientComputer> computer, int termWidth, int termHeight, int leftMargin, int rightMargin, int topMargin, int bottomMargin )
|
||||||
{
|
{
|
||||||
super(
|
this.client = client;
|
||||||
x, y,
|
this.computer = computer;
|
||||||
leftMargin + rightMargin + termWidth * FixedWidthFontRenderer.FONT_WIDTH,
|
this.termWidth = termWidth;
|
||||||
topMargin + bottomMargin + termHeight * FixedWidthFontRenderer.FONT_HEIGHT
|
this.termHeight = termHeight;
|
||||||
);
|
this.leftMargin = leftMargin;
|
||||||
|
this.rightMargin = rightMargin;
|
||||||
m_computer = computer;
|
this.topMargin = topMargin;
|
||||||
m_terminateTimer = 0.0f;
|
this.bottomMargin = bottomMargin;
|
||||||
m_rebootTimer = 0.0f;
|
|
||||||
m_shutdownTimer = 0.0f;
|
|
||||||
|
|
||||||
m_lastClickButton = -1;
|
|
||||||
m_lastClickX = -1;
|
|
||||||
m_lastClickY = -1;
|
|
||||||
|
|
||||||
m_focus = false;
|
|
||||||
m_allowFocusLoss = true;
|
|
||||||
|
|
||||||
m_leftMargin = leftMargin;
|
|
||||||
m_rightMargin = rightMargin;
|
|
||||||
m_topMargin = topMargin;
|
|
||||||
m_bottomMargin = bottomMargin;
|
|
||||||
|
|
||||||
m_keysDown = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAllowFocusLoss( boolean allowFocusLoss )
|
|
||||||
{
|
|
||||||
m_allowFocusLoss = allowFocusLoss;
|
|
||||||
m_focus = m_focus || !allowFocusLoss;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onKeyTyped( char ch, int key )
|
public boolean charTyped( char ch, int modifiers )
|
||||||
{
|
{
|
||||||
if( m_focus )
|
if( ch >= 32 && ch <= 126 || ch >= 160 && ch <= 255 ) // printable chars in byte range
|
||||||
{
|
{
|
||||||
// Ctrl+V for paste
|
// Queue the "char" event
|
||||||
if( ch == 22 )
|
queueEvent( "char", Character.toString( ch ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyPressed( int key, int scancode, int modifiers )
|
||||||
|
{
|
||||||
|
if( key == GLFW.GLFW_KEY_ESCAPE ) return false;
|
||||||
|
if( (modifiers & GLFW.GLFW_MOD_CONTROL) != 0 )
|
||||||
|
{
|
||||||
|
switch( key )
|
||||||
{
|
{
|
||||||
String clipboard = GuiScreen.getClipboardString();
|
case GLFW.GLFW_KEY_T:
|
||||||
if( clipboard != null )
|
if( terminateTimer < 0 ) terminateTimer = 0;
|
||||||
{
|
return true;
|
||||||
// Clip to the first occurance of \r or \n
|
case GLFW.GLFW_KEY_S:
|
||||||
int newLineIndex1 = clipboard.indexOf( "\r" );
|
if( shutdownTimer < 0 ) shutdownTimer = 0;
|
||||||
int newLineIndex2 = clipboard.indexOf( "\n" );
|
return true;
|
||||||
if( newLineIndex1 >= 0 && newLineIndex2 >= 0 )
|
case GLFW.GLFW_KEY_R:
|
||||||
{
|
if( rebootTimer < 0 ) rebootTimer = 0;
|
||||||
clipboard = clipboard.substring( 0, Math.min( newLineIndex1, newLineIndex2 ) );
|
return true;
|
||||||
}
|
|
||||||
else if( newLineIndex1 >= 0 )
|
|
||||||
{
|
|
||||||
clipboard = clipboard.substring( 0, newLineIndex1 );
|
|
||||||
}
|
|
||||||
else if( newLineIndex2 >= 0 )
|
|
||||||
{
|
|
||||||
clipboard = clipboard.substring( 0, newLineIndex2 );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter the string
|
case GLFW.GLFW_KEY_V:
|
||||||
clipboard = ChatAllowedCharacters.filterAllowedCharacters( clipboard );
|
// Ctrl+V for paste
|
||||||
|
String clipboard = client.keyboardListener.getClipboardString();
|
||||||
if( !clipboard.isEmpty() )
|
if( clipboard != null )
|
||||||
{
|
{
|
||||||
// Clip to 512 characters
|
// Clip to the first occurrence of \r or \n
|
||||||
if( clipboard.length() > 512 )
|
int newLineIndex1 = clipboard.indexOf( "\r" );
|
||||||
|
int newLineIndex2 = clipboard.indexOf( "\n" );
|
||||||
|
if( newLineIndex1 >= 0 && newLineIndex2 >= 0 )
|
||||||
{
|
{
|
||||||
clipboard = clipboard.substring( 0, 512 );
|
clipboard = clipboard.substring( 0, Math.min( newLineIndex1, newLineIndex2 ) );
|
||||||
|
}
|
||||||
|
else if( newLineIndex1 >= 0 )
|
||||||
|
{
|
||||||
|
clipboard = clipboard.substring( 0, newLineIndex1 );
|
||||||
|
}
|
||||||
|
else if( newLineIndex2 >= 0 )
|
||||||
|
{
|
||||||
|
clipboard = clipboard.substring( 0, newLineIndex2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue the "paste" event
|
// Filter the string
|
||||||
queueEvent( "paste", new Object[] {
|
clipboard = SharedConstants.filterAllowedCharacters( clipboard );
|
||||||
clipboard
|
if( !clipboard.isEmpty() )
|
||||||
} );
|
{
|
||||||
}
|
// Clip to 512 characters and queue the event
|
||||||
}
|
if( clipboard.length() > 512 ) clipboard = clipboard.substring( 0, 512 );
|
||||||
return true;
|
queueEvent( "paste", clipboard );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular keys normally
|
return true;
|
||||||
if( m_terminateTimer <= 0.0f && m_rebootTimer <= 0.0f && m_shutdownTimer <= 0.0f )
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( key >= 0 && terminateTimer < 0 && rebootTimer < 0 && shutdownTimer < 0 )
|
||||||
|
{
|
||||||
|
// Queue the "key" event and add to the down set
|
||||||
|
boolean repeat = keysDown.get( key );
|
||||||
|
keysDown.set( key );
|
||||||
|
IComputer computer = this.computer.get();
|
||||||
|
if( computer != null ) computer.keyDown( key, repeat );
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyReleased( int key, int scancode, int modifiers )
|
||||||
|
{
|
||||||
|
// Queue the "key_up" event and remove from the down set
|
||||||
|
if( key >= 0 && keysDown.get( key ) )
|
||||||
|
{
|
||||||
|
keysDown.set( key, false );
|
||||||
|
IComputer computer = this.computer.get();
|
||||||
|
if( computer != null ) computer.keyUp( key );
|
||||||
|
}
|
||||||
|
|
||||||
|
switch( key )
|
||||||
|
{
|
||||||
|
case GLFW.GLFW_KEY_T:
|
||||||
|
terminateTimer = -1;
|
||||||
|
break;
|
||||||
|
case GLFW.GLFW_KEY_R:
|
||||||
|
rebootTimer = -1;
|
||||||
|
break;
|
||||||
|
case GLFW.GLFW_KEY_S:
|
||||||
|
shutdownTimer = -1;
|
||||||
|
break;
|
||||||
|
case GLFW.GLFW_KEY_LEFT_CONTROL:
|
||||||
|
case GLFW.GLFW_KEY_RIGHT_CONTROL:
|
||||||
|
terminateTimer = rebootTimer = shutdownTimer = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseClicked( double mouseX, double mouseY, int button )
|
||||||
|
{
|
||||||
|
ClientComputer computer = this.computer.get();
|
||||||
|
if( computer == null || !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||||
|
|
||||||
|
Terminal term = computer.getTerminal();
|
||||||
|
if( term != null )
|
||||||
|
{
|
||||||
|
int charX = (int) (mouseX / FixedWidthFontRenderer.FONT_WIDTH);
|
||||||
|
int charY = (int) (mouseY / FixedWidthFontRenderer.FONT_HEIGHT);
|
||||||
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
|
computer.mouseClick( button + 1, charX + 1, charY + 1 );
|
||||||
|
|
||||||
|
lastMouseButton = button;
|
||||||
|
lastMouseX = charX;
|
||||||
|
lastMouseY = charY;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseReleased( double mouseX, double mouseY, int button )
|
||||||
|
{
|
||||||
|
ClientComputer computer = this.computer.get();
|
||||||
|
if( computer == null || !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||||
|
|
||||||
|
Terminal term = computer.getTerminal();
|
||||||
|
if( term != null )
|
||||||
|
{
|
||||||
|
int charX = (int) (mouseX / FixedWidthFontRenderer.FONT_WIDTH);
|
||||||
|
int charY = (int) (mouseY / FixedWidthFontRenderer.FONT_HEIGHT);
|
||||||
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
|
if( lastMouseButton == button )
|
||||||
{
|
{
|
||||||
boolean repeat = Keyboard.isRepeatEvent();
|
computer.mouseUp( lastMouseButton + 1, charX + 1, charY + 1 );
|
||||||
boolean handled = false;
|
lastMouseButton = -1;
|
||||||
if( key > 0 )
|
|
||||||
{
|
|
||||||
if( !repeat )
|
|
||||||
{
|
|
||||||
m_keysDown.add( key );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Queue the "key" event
|
|
||||||
queueEvent( "key", new Object[] {
|
|
||||||
key, repeat
|
|
||||||
} );
|
|
||||||
handled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( (ch >= 32 && ch <= 126) || (ch >= 160 && ch <= 255) ) // printable chars in byte range
|
|
||||||
{
|
|
||||||
// Queue the "char" event
|
|
||||||
queueEvent( "char", new Object[] {
|
|
||||||
Character.toString( ch )
|
|
||||||
} );
|
|
||||||
handled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return handled;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastMouseX = charX;
|
||||||
|
lastMouseY = charY;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked( int mouseX, int mouseY, int button )
|
public boolean mouseDragged( double mouseX, double mouseY, int button, double v2, double v3 )
|
||||||
{
|
{
|
||||||
if( mouseX >= getXPosition() && mouseX < getXPosition() + getWidth() &&
|
ClientComputer computer = this.computer.get();
|
||||||
mouseY >= getYPosition() && mouseY < getYPosition() + getHeight() )
|
if( computer == null || !computer.isColour() || button < 0 || button > 2 ) return false;
|
||||||
|
|
||||||
|
Terminal term = computer.getTerminal();
|
||||||
|
if( term != null )
|
||||||
{
|
{
|
||||||
if( !m_focus && button == 0 )
|
int charX = (int) (mouseX / FixedWidthFontRenderer.FONT_WIDTH);
|
||||||
{
|
int charY = (int) (mouseY / FixedWidthFontRenderer.FONT_HEIGHT);
|
||||||
m_focus = true;
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
}
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
if( m_focus )
|
computer.mouseDrag( button + 1, charX + 1, charY + 1 );
|
||||||
{
|
|
||||||
IComputer computer = m_computer.getComputer();
|
|
||||||
if( computer != null && computer.isColour() && button >= 0 && button <= 2 )
|
|
||||||
{
|
|
||||||
Terminal term = computer.getTerminal();
|
|
||||||
if( term != null )
|
|
||||||
{
|
|
||||||
int charX = (mouseX - (getXPosition() + m_leftMargin)) / FixedWidthFontRenderer.FONT_WIDTH;
|
|
||||||
int charY = (mouseY - (getYPosition() + m_topMargin)) / FixedWidthFontRenderer.FONT_HEIGHT;
|
|
||||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
|
||||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
|
||||||
|
|
||||||
computer.queueEvent( "mouse_click", new Object[] {
|
lastMouseX = charX;
|
||||||
button + 1, charX + 1, charY + 1
|
lastMouseY = charY;
|
||||||
} );
|
lastMouseButton = button;
|
||||||
|
|
||||||
m_lastClickButton = button;
|
|
||||||
m_lastClickX = charX;
|
|
||||||
m_lastClickY = charY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( m_focus && button == 0 && m_allowFocusLoss )
|
|
||||||
{
|
|
||||||
m_focus = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onKeyboardInput()
|
public boolean mouseScrolled( double delta )
|
||||||
{
|
{
|
||||||
boolean handled = false;
|
ClientComputer computer = this.computer.get();
|
||||||
for( int i = m_keysDown.size() - 1; i >= 0; --i )
|
if( computer == null || !computer.isColour() ) return false;
|
||||||
|
|
||||||
|
if( lastMouseX >= 0 && lastMouseY >= 0 && delta != 0 )
|
||||||
{
|
{
|
||||||
int key = m_keysDown.get( i );
|
queueEvent( "mouse_scroll", delta < 0 ? 1 : -1, lastMouseX + 1, lastMouseY + 1 );
|
||||||
if( !Keyboard.isKeyDown( key ) )
|
|
||||||
{
|
|
||||||
m_keysDown.remove( i );
|
|
||||||
if( m_focus )
|
|
||||||
{
|
|
||||||
// Queue the "key_up" event
|
|
||||||
queueEvent( "key_up", new Object[] {
|
|
||||||
key
|
|
||||||
} );
|
|
||||||
handled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return handled;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleMouseInput( int mouseX, int mouseY )
|
|
||||||
{
|
|
||||||
IComputer computer = m_computer.getComputer();
|
|
||||||
if( mouseX >= getXPosition() && mouseX < getXPosition() + getWidth() &&
|
|
||||||
mouseY >= getYPosition() && mouseY < getYPosition() + getHeight() &&
|
|
||||||
computer != null && computer.isColour() )
|
|
||||||
{
|
|
||||||
Terminal term = computer.getTerminal();
|
|
||||||
if( term != null )
|
|
||||||
{
|
|
||||||
int charX = (mouseX - (getXPosition() + m_leftMargin)) / FixedWidthFontRenderer.FONT_WIDTH;
|
|
||||||
int charY = (mouseY - (getYPosition() + m_topMargin)) / FixedWidthFontRenderer.FONT_HEIGHT;
|
|
||||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
|
||||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
|
||||||
|
|
||||||
if( m_lastClickButton >= 0 && !Mouse.isButtonDown( m_lastClickButton ) )
|
|
||||||
{
|
|
||||||
if( m_focus )
|
|
||||||
{
|
|
||||||
computer.queueEvent( "mouse_up", new Object[] {
|
|
||||||
m_lastClickButton + 1, charX + 1, charY + 1
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
m_lastClickButton = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int wheelChange = Mouse.getEventDWheel();
|
|
||||||
if( wheelChange == 0 && m_lastClickButton == -1 )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( m_focus )
|
|
||||||
{
|
|
||||||
if( wheelChange < 0 )
|
|
||||||
{
|
|
||||||
computer.queueEvent( "mouse_scroll", new Object[] {
|
|
||||||
1, charX + 1, charY + 1
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
else if( wheelChange > 0 )
|
|
||||||
{
|
|
||||||
computer.queueEvent( "mouse_scroll", new Object[] {
|
|
||||||
-1, charX + 1, charY + 1
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( m_lastClickButton >= 0 && (charX != m_lastClickX || charY != m_lastClickY) )
|
|
||||||
{
|
|
||||||
computer.queueEvent( "mouse_drag", new Object[] {
|
|
||||||
m_lastClickButton + 1, charX + 1, charY + 1
|
|
||||||
} );
|
|
||||||
m_lastClickX = charX;
|
|
||||||
m_lastClickY = charY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update()
|
public void update()
|
||||||
{
|
{
|
||||||
// Handle special keys
|
if( terminateTimer >= 0 && terminateTimer < TERMINATE_TIME && (terminateTimer += 0.05f) > TERMINATE_TIME )
|
||||||
if( m_focus && (Keyboard.isKeyDown( 29 ) || Keyboard.isKeyDown( 157 )) )
|
|
||||||
{
|
{
|
||||||
// Ctrl+T for terminate
|
queueEvent( "terminate" );
|
||||||
if( Keyboard.isKeyDown( 20 ) )
|
|
||||||
{
|
|
||||||
if( m_terminateTimer < TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
m_terminateTimer = m_terminateTimer + 0.05f;
|
|
||||||
if( m_terminateTimer >= TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
queueEvent( "terminate" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_terminateTimer = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ctrl+R for reboot
|
|
||||||
if( Keyboard.isKeyDown( 19 ) )
|
|
||||||
{
|
|
||||||
if( m_rebootTimer < TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
m_rebootTimer = m_rebootTimer + 0.05f;
|
|
||||||
if( m_rebootTimer >= TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
IComputer computer = m_computer.getComputer();
|
|
||||||
if( computer != null )
|
|
||||||
{
|
|
||||||
computer.reboot();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_rebootTimer = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ctrl+S for shutdown
|
|
||||||
if( Keyboard.isKeyDown( 31 ) )
|
|
||||||
{
|
|
||||||
if( m_shutdownTimer < TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
m_shutdownTimer = m_shutdownTimer + 0.05f;
|
|
||||||
if( m_shutdownTimer >= TERMINATE_TIME )
|
|
||||||
{
|
|
||||||
IComputer computer = m_computer.getComputer();
|
|
||||||
if( computer != null )
|
|
||||||
{
|
|
||||||
computer.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_shutdownTimer = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if( shutdownTimer >= 0 && shutdownTimer < TERMINATE_TIME && (shutdownTimer += 0.05f) > TERMINATE_TIME )
|
||||||
{
|
{
|
||||||
m_terminateTimer = 0.0f;
|
ClientComputer computer = this.computer.get();
|
||||||
m_rebootTimer = 0.0f;
|
if( computer != null ) computer.shutdown();
|
||||||
m_shutdownTimer = 0.0f;
|
}
|
||||||
|
|
||||||
|
if( rebootTimer >= 0 && rebootTimer < TERMINATE_TIME && (rebootTimer += 0.05f) > TERMINATE_TIME )
|
||||||
|
{
|
||||||
|
ClientComputer computer = this.computer.get();
|
||||||
|
if( computer != null ) computer.reboot();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY )
|
public void focusChanged( boolean focused )
|
||||||
{
|
{
|
||||||
int startX = xOrigin + getXPosition();
|
if( !focused )
|
||||||
int startY = yOrigin + getYPosition();
|
{
|
||||||
|
// When blurring, we should make all keys go up
|
||||||
|
for( int key = 0; key < keysDown.size(); key++ )
|
||||||
|
{
|
||||||
|
if( keysDown.get( key ) ) queueEvent( "key_up", key );
|
||||||
|
}
|
||||||
|
keysDown.clear();
|
||||||
|
|
||||||
synchronized( m_computer )
|
// When blurring, we should make the last mouse button go up
|
||||||
|
if( lastMouseButton > 0 )
|
||||||
|
{
|
||||||
|
IComputer computer = this.computer.get();
|
||||||
|
if( computer != null ) computer.mouseUp( lastMouseButton + 1, lastMouseX + 1, lastMouseY + 1 );
|
||||||
|
lastMouseButton = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
shutdownTimer = terminateTimer = rebootTimer = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw( int originX, int originY )
|
||||||
|
{
|
||||||
|
synchronized( computer )
|
||||||
{
|
{
|
||||||
// Draw the screen contents
|
// Draw the screen contents
|
||||||
IComputer computer = m_computer.getComputer();
|
ClientComputer computer = this.computer.get();
|
||||||
Terminal terminal = (computer != null) ? computer.getTerminal() : null;
|
Terminal terminal = computer != null ? computer.getTerminal() : null;
|
||||||
if( terminal != null )
|
if( terminal != null )
|
||||||
{
|
{
|
||||||
// Draw the terminal
|
// Draw the terminal
|
||||||
boolean greyscale = !computer.isColour();
|
boolean greyscale = !computer.isColour();
|
||||||
|
|
||||||
Palette palette = terminal.getPalette();
|
Palette palette = terminal.getPalette();
|
||||||
|
|
||||||
// Get the data from the terminal first
|
// Get the data from the terminal first
|
||||||
// Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us.
|
// Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us.
|
||||||
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
||||||
boolean tblink = m_focus && terminal.getCursorBlink() && FrameInfo.getGlobalCursorBlink();
|
boolean tblink = terminal.getCursorBlink() && FrameInfo.getGlobalCursorBlink();
|
||||||
int tw = terminal.getWidth();
|
int tw = terminal.getWidth();
|
||||||
int th = terminal.getHeight();
|
int th = terminal.getHeight();
|
||||||
int tx = terminal.getCursorX();
|
int tx = terminal.getCursorX();
|
||||||
int ty = terminal.getCursorY();
|
int ty = terminal.getCursorY();
|
||||||
|
|
||||||
int x = startX + m_leftMargin;
|
|
||||||
int y = startY + m_topMargin;
|
|
||||||
|
|
||||||
// Draw margins
|
// Draw margins
|
||||||
TextBuffer emptyLine = new TextBuffer( ' ', tw );
|
TextBuffer emptyLine = new TextBuffer( ' ', tw );
|
||||||
if( m_topMargin > 0 )
|
if( topMargin > 0 )
|
||||||
{
|
{
|
||||||
fontRenderer.drawString( emptyLine, x, startY, terminal.getTextColourLine( 0 ), terminal.getBackgroundColourLine( 0 ), m_leftMargin, m_rightMargin, greyscale, palette );
|
fontRenderer.drawString( emptyLine, originX, originY - topMargin,
|
||||||
|
terminal.getTextColourLine( 0 ), terminal.getBackgroundColourLine( 0 ),
|
||||||
|
leftMargin, rightMargin, greyscale, palette );
|
||||||
}
|
}
|
||||||
if( m_bottomMargin > 0 )
|
|
||||||
|
if( bottomMargin > 0 )
|
||||||
{
|
{
|
||||||
fontRenderer.drawString( emptyLine, x, startY + 2 * m_bottomMargin + (th - 1) * FixedWidthFontRenderer.FONT_HEIGHT, terminal.getTextColourLine( th - 1 ), terminal.getBackgroundColourLine( th - 1 ), m_leftMargin, m_rightMargin, greyscale, palette );
|
fontRenderer.drawString( emptyLine, originX, originY + bottomMargin + (th - 1) * FixedWidthFontRenderer.FONT_HEIGHT,
|
||||||
|
terminal.getTextColourLine( th - 1 ), terminal.getBackgroundColourLine( th - 1 ),
|
||||||
|
leftMargin, rightMargin, greyscale, palette );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw lines
|
// Draw lines
|
||||||
|
int y = originY;
|
||||||
for( int line = 0; line < th; line++ )
|
for( int line = 0; line < th; line++ )
|
||||||
{
|
{
|
||||||
TextBuffer text = terminal.getLine( line );
|
TextBuffer text = terminal.getLine( line );
|
||||||
TextBuffer colour = terminal.getTextColourLine( line );
|
TextBuffer colour = terminal.getTextColourLine( line );
|
||||||
TextBuffer backgroundColour = terminal.getBackgroundColourLine( line );
|
TextBuffer backgroundColour = terminal.getBackgroundColourLine( line );
|
||||||
fontRenderer.drawString( text, x, y, colour, backgroundColour, m_leftMargin, m_rightMargin, greyscale, palette );
|
fontRenderer.drawString( text, originX, y, colour, backgroundColour, leftMargin, rightMargin, greyscale, palette );
|
||||||
y += FixedWidthFontRenderer.FONT_HEIGHT;
|
y += FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -425,8 +363,8 @@ public class WidgetTerminal extends Widget
|
|||||||
|
|
||||||
fontRenderer.drawString(
|
fontRenderer.drawString(
|
||||||
cursor,
|
cursor,
|
||||||
x + FixedWidthFontRenderer.FONT_WIDTH * tx,
|
originX + FixedWidthFontRenderer.FONT_WIDTH * tx,
|
||||||
startY + m_topMargin + FixedWidthFontRenderer.FONT_HEIGHT * ty,
|
originY + FixedWidthFontRenderer.FONT_HEIGHT * ty,
|
||||||
cursorColour, null,
|
cursorColour, null,
|
||||||
0, 0,
|
0, 0,
|
||||||
greyscale,
|
greyscale,
|
||||||
@@ -437,16 +375,29 @@ public class WidgetTerminal extends Widget
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Draw a black background
|
// Draw a black background
|
||||||
mc.getTextureManager().bindTexture( BACKGROUND );
|
|
||||||
Colour black = Colour.Black;
|
Colour black = Colour.Black;
|
||||||
GlStateManager.color( black.getR(), black.getG(), black.getB(), 1.0f );
|
GlStateManager.color4f( black.getR(), black.getG(), black.getB(), 1.0f );
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
drawTexturedModalRect( startX, startY, 0, 0, getWidth(), getHeight() );
|
int x = originX - leftMargin;
|
||||||
|
int y = originY - rightMargin;
|
||||||
|
int width = termWidth * FixedWidthFontRenderer.FONT_WIDTH + leftMargin + rightMargin;
|
||||||
|
int height = termHeight * FixedWidthFontRenderer.FONT_HEIGHT + topMargin + bottomMargin;
|
||||||
|
|
||||||
|
client.getTextureManager().bindTexture( BACKGROUND );
|
||||||
|
|
||||||
|
Tessellator tesslector = Tessellator.getInstance();
|
||||||
|
BufferBuilder buffer = tesslector.getBuffer();
|
||||||
|
buffer.begin( GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX );
|
||||||
|
buffer.pos( x, y + height, 0 ).tex( 0 / 256.0, height / 256.0 ).endVertex();
|
||||||
|
buffer.pos( x + width, y + height, 0 ).tex( width / 256.0, height / 256.0 ).endVertex();
|
||||||
|
buffer.pos( x + width, y, 0 ).tex( width / 256.0, 0 / 256.0 ).endVertex();
|
||||||
|
buffer.pos( x, y, 0 ).tex( 0 / 256.0, 0 / 256.0 ).endVertex();
|
||||||
|
tesslector.draw();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -454,19 +405,13 @@ public class WidgetTerminal extends Widget
|
|||||||
|
|
||||||
private void queueEvent( String event )
|
private void queueEvent( String event )
|
||||||
{
|
{
|
||||||
IComputer computer = m_computer.getComputer();
|
ClientComputer computer = this.computer.get();
|
||||||
if( computer != null )
|
if( computer != null ) computer.queueEvent( event );
|
||||||
{
|
|
||||||
computer.queueEvent( event );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queueEvent( String event, Object[] args )
|
private void queueEvent( String event, Object... args )
|
||||||
{
|
{
|
||||||
IComputer computer = m_computer.getComputer();
|
ClientComputer computer = this.computer.get();
|
||||||
if( computer != null )
|
if( computer != null ) computer.queueEvent( event, args );
|
||||||
{
|
|
||||||
computer.queueEvent( event, args );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.client.gui.widgets;
|
||||||
|
|
||||||
|
import net.minecraft.client.gui.IGuiEventListener;
|
||||||
|
|
||||||
|
public class WidgetWrapper implements IGuiEventListener
|
||||||
|
{
|
||||||
|
private final IGuiEventListener listener;
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
public WidgetWrapper( IGuiEventListener listener, int x, int y, int width, int height )
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void focusChanged( boolean b )
|
||||||
|
{
|
||||||
|
listener.focusChanged( b );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canFocus()
|
||||||
|
{
|
||||||
|
return listener.canFocus();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseClicked( double x, double y, int button )
|
||||||
|
{
|
||||||
|
double dx = x - this.x, dy = y - this.y;
|
||||||
|
return dx >= 0 && dx < width && dy >= 0 && dy < height && listener.mouseClicked( dx, dy, button );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseReleased( double x, double y, int button )
|
||||||
|
{
|
||||||
|
double dx = x - this.x, dy = y - this.y;
|
||||||
|
return dx >= 0 && dx < width && dy >= 0 && dy < height && listener.mouseReleased( dx, dy, button );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseDragged( double x, double y, int button, double deltaX, double deltaY )
|
||||||
|
{
|
||||||
|
double dx = x - this.x, dy = y - this.y;
|
||||||
|
return dx >= 0 && dx < width && dy >= 0 && dy < height && listener.mouseDragged( dx, dy, button, deltaX, deltaY );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean mouseScrolled( double delta )
|
||||||
|
{
|
||||||
|
return listener.mouseScrolled( delta );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyPressed( int key, int scancode, int modifiers )
|
||||||
|
{
|
||||||
|
return listener.keyPressed( key, scancode, modifiers );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyReleased( int key, int scancode, int modifiers )
|
||||||
|
{
|
||||||
|
return listener.keyReleased( key, scancode, modifiers );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean charTyped( char character, int modifiers )
|
||||||
|
{
|
||||||
|
return listener.charTyped( character, modifiers );
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getX()
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY()
|
||||||
|
{
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth()
|
||||||
|
{
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight()
|
||||||
|
{
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
||||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
||||||
* Send enquiries to dratcliffe@gmail.com
|
|
||||||
*/
|
|
||||||
|
|
||||||
package dan200.computercraft.client.proxy;
|
|
||||||
|
|
||||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
|
||||||
import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
|
|
||||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
|
||||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
|
||||||
|
|
||||||
public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void init()
|
|
||||||
{
|
|
||||||
super.init();
|
|
||||||
|
|
||||||
// Setup renderers
|
|
||||||
ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,53 +7,90 @@
|
|||||||
package dan200.computercraft.client.proxy;
|
package dan200.computercraft.client.proxy;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import dan200.computercraft.client.gui.*;
|
||||||
import dan200.computercraft.client.render.TileEntityCableRenderer;
|
import dan200.computercraft.client.render.TileEntityCableRenderer;
|
||||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
||||||
import dan200.computercraft.shared.command.CommandCopy;
|
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
||||||
|
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
||||||
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
|
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||||
|
import dan200.computercraft.shared.network.container.*;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
|
||||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||||
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
|
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
|
||||||
import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
|
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||||
import net.minecraftforge.client.ClientCommandHandler;
|
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
import net.minecraftforge.event.world.WorldEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.ExtensionPoint;
|
||||||
|
import net.minecraftforge.fml.ModLoadingContext;
|
||||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
|
|
||||||
public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD )
|
||||||
|
public final class ComputerCraftProxyClient
|
||||||
{
|
{
|
||||||
@Override
|
@SubscribeEvent
|
||||||
public void preInit()
|
public static void setupClient( FMLClientSetupEvent event )
|
||||||
{
|
{
|
||||||
super.preInit();
|
registerContainers();
|
||||||
|
|
||||||
// Register any client-specific commands
|
// Setup TESRs
|
||||||
ClientCommandHandler.instance.registerCommand( CommandCopy.INSTANCE );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init()
|
|
||||||
{
|
|
||||||
super.init();
|
|
||||||
|
|
||||||
// Setup renderers
|
|
||||||
ClientRegistry.bindTileEntitySpecialRenderer( TileMonitor.class, new TileEntityMonitorRenderer() );
|
ClientRegistry.bindTileEntitySpecialRenderer( TileMonitor.class, new TileEntityMonitorRenderer() );
|
||||||
ClientRegistry.bindTileEntitySpecialRenderer( TileCable.class, new TileEntityCableRenderer() );
|
ClientRegistry.bindTileEntitySpecialRenderer( TileCable.class, new TileEntityCableRenderer() );
|
||||||
|
ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
private static void registerContainers()
|
||||||
public static class ForgeHandlers
|
{
|
||||||
|
ContainerType.registerGui( TileEntityContainerType::computer, ( packet, player ) ->
|
||||||
|
new GuiComputer( (TileComputer) packet.getTileEntity( player ) ) );
|
||||||
|
ContainerType.registerGui( TileEntityContainerType::diskDrive, GuiDiskDrive::new );
|
||||||
|
ContainerType.registerGui( TileEntityContainerType::printer, GuiPrinter::new );
|
||||||
|
ContainerType.registerGui( TileEntityContainerType::turtle, ( packet, player ) -> {
|
||||||
|
TileTurtle turtle = (TileTurtle) packet.getTileEntity( player );
|
||||||
|
return new GuiTurtle( turtle, new ContainerTurtle( player.inventory, turtle.getAccess(), turtle.getClientComputer() ) );
|
||||||
|
} );
|
||||||
|
|
||||||
|
ContainerType.registerGui( PocketComputerContainerType::new, GuiPocketComputer::new );
|
||||||
|
ContainerType.registerGui( PrintoutContainerType::new, GuiPrintout::new );
|
||||||
|
ContainerType.registerGui( ViewComputerContainerType::new, ( packet, player ) -> {
|
||||||
|
ClientComputer computer = ComputerCraft.clientComputerRegistry.get( packet.instanceId );
|
||||||
|
if( computer == null )
|
||||||
|
{
|
||||||
|
ComputerCraft.clientComputerRegistry.add( packet.instanceId, computer = new ClientComputer( packet.instanceId ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
ContainerViewComputer container = new ContainerViewComputer( computer );
|
||||||
|
return new GuiComputer( container, packet.family, computer, packet.width, packet.height );
|
||||||
|
} );
|
||||||
|
|
||||||
|
ModLoadingContext.get().registerExtensionPoint( ExtensionPoint.GUIFACTORY, () -> packet -> {
|
||||||
|
ContainerType<?> type = ContainerType.factories.get( packet.getId() ).get();
|
||||||
|
if( packet.getAdditionalData() != null ) type.fromBytes( packet.getAdditionalData() );
|
||||||
|
@SuppressWarnings( "unchecked" )
|
||||||
|
BiFunction<ContainerType<?>, EntityPlayer, GuiContainer> factory = (BiFunction<ContainerType<?>, EntityPlayer, GuiContainer>) ContainerType.guiFactories.get( packet.getId() );
|
||||||
|
return factory.apply( type, Minecraft.getInstance().player );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
|
public static final class ForgeHandlers
|
||||||
{
|
{
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onWorldUnload( WorldEvent.Unload event )
|
public static void onWorldUnload( WorldEvent.Unload event )
|
||||||
{
|
{
|
||||||
if( event.getWorld().isRemote )
|
if( event.getWorld().isRemote() )
|
||||||
{
|
{
|
||||||
ClientMonitor.destroyAll();
|
ClientMonitor.destroyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.renderer.FirstPersonRenderer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.ItemRenderer;
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.EnumHand;
|
import net.minecraft.util.EnumHand;
|
||||||
@@ -21,13 +21,13 @@ public abstract class ItemMapLikeRenderer
|
|||||||
* The main rendering method for the item
|
* The main rendering method for the item
|
||||||
*
|
*
|
||||||
* @param stack The stack to render
|
* @param stack The stack to render
|
||||||
* @see ItemRenderer#renderMapFirstPerson(ItemStack)
|
* @see FirstPersonRenderer#renderMapFirstPerson(ItemStack)
|
||||||
*/
|
*/
|
||||||
protected abstract void renderItem( ItemStack stack );
|
protected abstract void renderItem( ItemStack stack );
|
||||||
|
|
||||||
protected void renderItemFirstPerson( EnumHand hand, float pitch, float equipProgress, float swingProgress, ItemStack stack )
|
protected void renderItemFirstPerson( EnumHand hand, float pitch, float equipProgress, float swingProgress, ItemStack stack )
|
||||||
{
|
{
|
||||||
EntityPlayer player = Minecraft.getMinecraft().player;
|
EntityPlayer player = Minecraft.getInstance().player;
|
||||||
|
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
if( hand == EnumHand.MAIN_HAND && player.getHeldItemOffhand().isEmpty() )
|
if( hand == EnumHand.MAIN_HAND && player.getHeldItemOffhand().isEmpty() )
|
||||||
@@ -51,35 +51,35 @@ public abstract class ItemMapLikeRenderer
|
|||||||
* @param equipProgress The equip progress of this item
|
* @param equipProgress The equip progress of this item
|
||||||
* @param swingProgress The swing progress of this item
|
* @param swingProgress The swing progress of this item
|
||||||
* @param stack The stack to render
|
* @param stack The stack to render
|
||||||
* @see ItemRenderer#renderMapFirstPersonSide(float, EnumHandSide, float, ItemStack)
|
* @see FirstPersonRenderer#renderMapFirstPersonSide(float, EnumHandSide, float, ItemStack)
|
||||||
*/
|
*/
|
||||||
private void renderItemFirstPersonSide( EnumHandSide side, float equipProgress, float swingProgress, ItemStack stack )
|
private void renderItemFirstPersonSide( EnumHandSide side, float equipProgress, float swingProgress, ItemStack stack )
|
||||||
{
|
{
|
||||||
Minecraft minecraft = Minecraft.getMinecraft();
|
Minecraft minecraft = Minecraft.getInstance();
|
||||||
float offset = side == EnumHandSide.RIGHT ? 1f : -1f;
|
float offset = side == EnumHandSide.RIGHT ? 1f : -1f;
|
||||||
GlStateManager.translate( offset * 0.125f, -0.125f, 0f );
|
GlStateManager.translatef( offset * 0.125f, -0.125f, 0f );
|
||||||
|
|
||||||
// If the player is not invisible then render a single arm
|
// If the player is not invisible then render a single arm
|
||||||
if( !minecraft.player.isInvisible() )
|
if( !minecraft.player.isInvisible() )
|
||||||
{
|
{
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
GlStateManager.rotate( offset * 10f, 0f, 0f, 1f );
|
GlStateManager.rotatef( offset * 10f, 0f, 0f, 1f );
|
||||||
minecraft.getItemRenderer().renderArmFirstPerson( equipProgress, swingProgress, side );
|
minecraft.getFirstPersonRenderer().renderArmFirstPerson( equipProgress, swingProgress, side );
|
||||||
GlStateManager.popMatrix();
|
GlStateManager.popMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the appropriate transformations. This is just copied from the
|
// Setup the appropriate transformations. This is just copied from the
|
||||||
// corresponding method in ItemRenderer.
|
// corresponding method in ItemRenderer.
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
GlStateManager.translate( offset * 0.51f, -0.08f + equipProgress * -1.2f, -0.75f );
|
GlStateManager.translatef( offset * 0.51f, -0.08f + equipProgress * -1.2f, -0.75f );
|
||||||
float f1 = MathHelper.sqrt( swingProgress );
|
float f1 = MathHelper.sqrt( swingProgress );
|
||||||
float f2 = MathHelper.sin( f1 * (float) Math.PI );
|
float f2 = MathHelper.sin( f1 * (float) Math.PI );
|
||||||
float f3 = -0.5f * f2;
|
float f3 = -0.5f * f2;
|
||||||
float f4 = 0.4f * MathHelper.sin( f1 * ((float) Math.PI * 2f) );
|
float f4 = 0.4f * MathHelper.sin( f1 * ((float) Math.PI * 2f) );
|
||||||
float f5 = -0.3f * MathHelper.sin( swingProgress * (float) Math.PI );
|
float f5 = -0.3f * MathHelper.sin( swingProgress * (float) Math.PI );
|
||||||
GlStateManager.translate( offset * f3, f4 - 0.3f * f2, f5 );
|
GlStateManager.translatef( offset * f3, f4 - 0.3f * f2, f5 );
|
||||||
GlStateManager.rotate( f2 * -45f, 1f, 0f, 0f );
|
GlStateManager.rotatef( f2 * -45f, 1f, 0f, 0f );
|
||||||
GlStateManager.rotate( offset * f2 * -30f, 0f, 1f, 0f );
|
GlStateManager.rotatef( offset * f2 * -30f, 0f, 1f, 0f );
|
||||||
|
|
||||||
renderItem( stack );
|
renderItem( stack );
|
||||||
|
|
||||||
@@ -93,25 +93,25 @@ public abstract class ItemMapLikeRenderer
|
|||||||
* @param equipProgress The equip progress of this item
|
* @param equipProgress The equip progress of this item
|
||||||
* @param swingProgress The swing progress of this item
|
* @param swingProgress The swing progress of this item
|
||||||
* @param stack The stack to render
|
* @param stack The stack to render
|
||||||
* @see ItemRenderer#renderMapFirstPerson(float, float, float)
|
* @see FirstPersonRenderer#renderMapFirstPerson(float, float, float)
|
||||||
*/
|
*/
|
||||||
private void renderItemFirstPersonCenter( float pitch, float equipProgress, float swingProgress, ItemStack stack )
|
private void renderItemFirstPersonCenter( float pitch, float equipProgress, float swingProgress, ItemStack stack )
|
||||||
{
|
{
|
||||||
ItemRenderer itemRenderer = Minecraft.getMinecraft().getItemRenderer();
|
FirstPersonRenderer renderer = Minecraft.getInstance().getFirstPersonRenderer();
|
||||||
|
|
||||||
// Setup the appropriate transformations. This is just copied from the
|
// Setup the appropriate transformations. This is just copied from the
|
||||||
// corresponding method in ItemRenderer.
|
// corresponding method in ItemRenderer.
|
||||||
float swingRt = MathHelper.sqrt( swingProgress );
|
float swingRt = MathHelper.sqrt( swingProgress );
|
||||||
float tX = -0.2f * MathHelper.sin( swingProgress * (float) Math.PI );
|
float tX = -0.2f * MathHelper.sin( swingProgress * (float) Math.PI );
|
||||||
float tZ = -0.4f * MathHelper.sin( swingRt * (float) Math.PI );
|
float tZ = -0.4f * MathHelper.sin( swingRt * (float) Math.PI );
|
||||||
GlStateManager.translate( 0f, -tX / 2f, tZ );
|
GlStateManager.translatef( 0f, -tX / 2f, tZ );
|
||||||
float pitchAngle = itemRenderer.getMapAngleFromPitch( pitch );
|
float pitchAngle = renderer.getMapAngleFromPitch( pitch );
|
||||||
GlStateManager.translate( 0f, 0.04f + equipProgress * -1.2f + pitchAngle * -0.5f, -0.72f );
|
GlStateManager.translatef( 0f, 0.04f + equipProgress * -1.2f + pitchAngle * -0.5f, -0.72f );
|
||||||
GlStateManager.rotate( pitchAngle * -85f, 1f, 0f, 0f );
|
GlStateManager.rotatef( pitchAngle * -85f, 1f, 0f, 0f );
|
||||||
itemRenderer.renderArms();
|
renderer.renderArms();
|
||||||
float rX = MathHelper.sin( swingRt * (float) Math.PI );
|
float rX = MathHelper.sin( swingRt * (float) Math.PI );
|
||||||
GlStateManager.rotate( rX * 20f, 1f, 0f, 0f );
|
GlStateManager.rotatef( rX * 20f, 1f, 0f, 0f );
|
||||||
GlStateManager.scale( 2f, 2f, 2f );
|
GlStateManager.scalef( 2f, 2f, 2f );
|
||||||
|
|
||||||
renderItem( stack );
|
renderItem( stack );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,17 +16,16 @@ import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
|||||||
import dan200.computercraft.shared.util.Palette;
|
import dan200.computercraft.shared.util.Palette;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.RenderItem;
|
import net.minecraft.client.renderer.ItemRenderer;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
|
|
||||||
import net.minecraft.client.renderer.texture.TextureManager;
|
import net.minecraft.client.renderer.texture.TextureManager;
|
||||||
import net.minecraft.client.renderer.texture.TextureMap;
|
import net.minecraft.client.renderer.texture.TextureMap;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.client.ForgeHooksClient;
|
import net.minecraftforge.client.ForgeHooksClient;
|
||||||
import net.minecraftforge.client.event.RenderSpecificHandEvent;
|
import net.minecraftforge.client.event.RenderSpecificHandEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
@@ -35,7 +34,7 @@ import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
|
|||||||
/**
|
/**
|
||||||
* Emulates map rendering for pocket computers
|
* Emulates map rendering for pocket computers
|
||||||
*/
|
*/
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
||||||
{
|
{
|
||||||
private static final ItemPocketRenderer INSTANCE = new ItemPocketRenderer();
|
private static final ItemPocketRenderer INSTANCE = new ItemPocketRenderer();
|
||||||
@@ -57,44 +56,41 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
|||||||
@Override
|
@Override
|
||||||
protected void renderItem( ItemStack stack )
|
protected void renderItem( ItemStack stack )
|
||||||
{
|
{
|
||||||
// Setup various transformations. Note that these are partially adapated from the corresponding method
|
// Setup various transformations. Note that these are partially adapted from the corresponding method
|
||||||
// in ItemRenderer
|
// in ItemRenderer
|
||||||
GlStateManager.disableLighting();
|
GlStateManager.disableLighting();
|
||||||
|
|
||||||
GlStateManager.rotate( 180f, 0f, 1f, 0f );
|
GlStateManager.rotatef( 180f, 0f, 1f, 0f );
|
||||||
GlStateManager.rotate( 180f, 0f, 0f, 1f );
|
GlStateManager.rotatef( 180f, 0f, 0f, 1f );
|
||||||
GlStateManager.scale( 0.5, 0.5, 0.5 );
|
GlStateManager.scalef( 0.5f, 0.5f, 0.5f );
|
||||||
|
|
||||||
ItemPocketComputer pocketComputer = ComputerCraft.Items.pocketComputer;
|
ClientComputer computer = ItemPocketComputer.createClientComputer( stack );
|
||||||
ClientComputer computer = pocketComputer.createClientComputer( stack );
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// First render the background item. We use the item's model rather than a direct texture as this ensures
|
// First render the background item. We use the item's model rather than a direct texture as this ensures
|
||||||
// we display the pocket light and other such decorations.
|
// we display the pocket light and other such decorations.
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
|
|
||||||
GlStateManager.scale( 1.0f, -1.0f, 1.0f );
|
GlStateManager.scalef( 1.0f, -1.0f, 1.0f );
|
||||||
|
|
||||||
Minecraft minecraft = Minecraft.getMinecraft();
|
Minecraft minecraft = Minecraft.getInstance();
|
||||||
TextureManager textureManager = minecraft.getTextureManager();
|
TextureManager textureManager = minecraft.getTextureManager();
|
||||||
RenderItem renderItem = minecraft.getRenderItem();
|
ItemRenderer renderItem = minecraft.getItemRenderer();
|
||||||
|
|
||||||
// Copy of RenderItem#renderItemModelIntoGUI but without the translation or scaling
|
// Copy of RenderItem#renderItemModelIntoGUI but without the translation or scaling
|
||||||
textureManager.bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
|
textureManager.bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
|
||||||
textureManager.getTexture( TextureMap.LOCATION_BLOCKS_TEXTURE ).setBlurMipmap( false, false );
|
textureManager.getTexture( TextureMap.LOCATION_BLOCKS_TEXTURE ).setBlurMipmap( false, false );
|
||||||
|
|
||||||
GlStateManager.enableRescaleNormal();
|
GlStateManager.enableRescaleNormal();
|
||||||
GlStateManager.enableAlpha();
|
GlStateManager.enableAlphaTest();
|
||||||
GlStateManager.alphaFunc( GL11.GL_GREATER, 0.1F );
|
GlStateManager.alphaFunc( GL11.GL_GREATER, 0.1F );
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.blendFunc( GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA );
|
GlStateManager.blendFunc( GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA );
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
|
|
||||||
IBakedModel bakedmodel = renderItem.getItemModelWithOverrides( stack, null, null );
|
renderItem.renderItem( stack, transform( renderItem.getItemModelWithOverrides( stack, null, null ) ) );
|
||||||
bakedmodel = ForgeHooksClient.handleCameraTransforms( bakedmodel, ItemCameraTransforms.TransformType.GUI, false );
|
|
||||||
renderItem.renderItem( stack, bakedmodel );
|
|
||||||
|
|
||||||
GlStateManager.disableAlpha();
|
GlStateManager.disableAlphaTest();
|
||||||
GlStateManager.disableRescaleNormal();
|
GlStateManager.disableRescaleNormal();
|
||||||
|
|
||||||
GlStateManager.popMatrix();
|
GlStateManager.popMatrix();
|
||||||
@@ -109,13 +105,13 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
|||||||
synchronized( terminal )
|
synchronized( terminal )
|
||||||
{
|
{
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
GlStateManager.disableDepth();
|
GlStateManager.disableDepthTest();
|
||||||
|
|
||||||
// Reset the position to be at the top left corner of the pocket computer
|
// Reset the position to be at the top left corner of the pocket computer
|
||||||
// Note we translate towards the screen slightly too.
|
// Note we translate towards the screen slightly too.
|
||||||
GlStateManager.translate( -8 / 16.0, -8 / 16.0, 0.5 / 16.0 );
|
GlStateManager.translated( -8 / 16.0, -8 / 16.0, 0.5 / 16.0 );
|
||||||
// Translate to the top left of the screen.
|
// Translate to the top left of the screen.
|
||||||
GlStateManager.translate( 4 / 16.0, 3 / 16.0, 0 );
|
GlStateManager.translated( 4 / 16.0, 3 / 16.0, 0 );
|
||||||
|
|
||||||
// Work out the scaling required to resize the terminal in order to fit on the computer
|
// Work out the scaling required to resize the terminal in order to fit on the computer
|
||||||
final int margin = 2;
|
final int margin = 2;
|
||||||
@@ -127,7 +123,7 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
|||||||
|
|
||||||
// The grid is 8 * 8 wide, so we start with a base of 1/2 (8 / 16).
|
// The grid is 8 * 8 wide, so we start with a base of 1/2 (8 / 16).
|
||||||
double scale = 1.0 / 2.0 / max;
|
double scale = 1.0 / 2.0 / max;
|
||||||
GlStateManager.scale( scale, scale, scale );
|
GlStateManager.scaled( scale, scale, scale );
|
||||||
|
|
||||||
// The margin/start positions are determined in order for the terminal to be centred.
|
// The margin/start positions are determined in order for the terminal to be centred.
|
||||||
int startX = (max - width) / 2 + margin;
|
int startX = (max - width) / 2 + margin;
|
||||||
@@ -161,7 +157,7 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
GlStateManager.enableDepth();
|
GlStateManager.enableDepthTest();
|
||||||
GlStateManager.popMatrix();
|
GlStateManager.popMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,4 +165,10 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
|
|||||||
|
|
||||||
GlStateManager.enableLighting();
|
GlStateManager.enableLighting();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings( { "deprecation" } )
|
||||||
|
private static IBakedModel transform( IBakedModel model )
|
||||||
|
{
|
||||||
|
return ForgeHooksClient.handleCameraTransforms( model, net.minecraft.client.renderer.model.ItemCameraTransforms.TransformType.GUI, false );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import dan200.computercraft.ComputerCraft;
|
|||||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.client.event.RenderItemInFrameEvent;
|
import net.minecraftforge.client.event.RenderItemInFrameEvent;
|
||||||
import net.minecraftforge.client.event.RenderSpecificHandEvent;
|
import net.minecraftforge.client.event.RenderSpecificHandEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
|
|
||||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
|
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
|
||||||
@@ -23,9 +23,9 @@ import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAG
|
|||||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH;
|
import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emulates map and item-frame rendering for prinouts
|
* Emulates map and item-frame rendering for printouts
|
||||||
*/
|
*/
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
||||||
{
|
{
|
||||||
private static final ItemPrintoutRenderer INSTANCE = new ItemPrintoutRenderer();
|
private static final ItemPrintoutRenderer INSTANCE = new ItemPrintoutRenderer();
|
||||||
@@ -38,10 +38,9 @@ public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
|||||||
public static void onRenderInHand( RenderSpecificHandEvent event )
|
public static void onRenderInHand( RenderSpecificHandEvent event )
|
||||||
{
|
{
|
||||||
ItemStack stack = event.getItemStack();
|
ItemStack stack = event.getItemStack();
|
||||||
if( stack.getItem() != ComputerCraft.Items.printout ) return;
|
if( !(stack.getItem() instanceof ItemPrintout) ) return;
|
||||||
|
|
||||||
event.setCanceled( true );
|
event.setCanceled( true );
|
||||||
|
|
||||||
INSTANCE.renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() );
|
INSTANCE.renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,13 +48,13 @@ public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
|||||||
protected void renderItem( ItemStack stack )
|
protected void renderItem( ItemStack stack )
|
||||||
{
|
{
|
||||||
// Setup various transformations. Note that these are partially adapated from the corresponding method
|
// Setup various transformations. Note that these are partially adapated from the corresponding method
|
||||||
// in ItemRenderer.renderMapFirstPerson
|
// in FirstPersonRenderer.renderFirstPersonMap
|
||||||
GlStateManager.disableLighting();
|
GlStateManager.disableLighting();
|
||||||
|
|
||||||
GlStateManager.rotate( 180f, 0f, 1f, 0f );
|
GlStateManager.rotatef( 180f, 0f, 1f, 0f );
|
||||||
GlStateManager.rotate( 180f, 0f, 0f, 1f );
|
GlStateManager.rotatef( 180f, 0f, 0f, 1f );
|
||||||
GlStateManager.scale( 0.42f, 0.42f, -0.42f );
|
GlStateManager.scalef( 0.42f, 0.42f, -0.42f );
|
||||||
GlStateManager.translate( -0.5f, -0.48f, 0.0f );
|
GlStateManager.translatef( -0.5f, -0.48f, 0.0f );
|
||||||
|
|
||||||
drawPrintout( stack );
|
drawPrintout( stack );
|
||||||
|
|
||||||
@@ -66,17 +65,17 @@ public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
|||||||
public static void onRenderInFrame( RenderItemInFrameEvent event )
|
public static void onRenderInFrame( RenderItemInFrameEvent event )
|
||||||
{
|
{
|
||||||
ItemStack stack = event.getItem();
|
ItemStack stack = event.getItem();
|
||||||
if( stack.getItem() != ComputerCraft.Items.printout ) return;
|
if( !(stack.getItem() instanceof ItemPrintout) ) return;
|
||||||
|
|
||||||
event.setCanceled( true );
|
event.setCanceled( true );
|
||||||
|
|
||||||
GlStateManager.disableLighting();
|
GlStateManager.disableLighting();
|
||||||
|
|
||||||
// Move a little bit forward to ensure we're not clipping with the frame
|
// Move a little bit forward to ensure we're not clipping with the frame
|
||||||
GlStateManager.translate( 0.0f, 0.0f, -0.001f );
|
GlStateManager.translatef( 0.0f, 0.0f, -0.001f );
|
||||||
GlStateManager.rotate( 180f, 0f, 0f, 1f );
|
GlStateManager.rotatef( 180f, 0f, 0f, 1f );
|
||||||
GlStateManager.scale( 0.95f, 0.95f, -0.95f );
|
GlStateManager.scalef( 0.95f, 0.95f, -0.95f );
|
||||||
GlStateManager.translate( -0.5f, -0.5f, 0.0f );
|
GlStateManager.translatef( -0.5f, -0.5f, 0.0f );
|
||||||
|
|
||||||
drawPrintout( stack );
|
drawPrintout( stack );
|
||||||
|
|
||||||
@@ -87,7 +86,7 @@ public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
|||||||
private static void drawPrintout( ItemStack stack )
|
private static void drawPrintout( ItemStack stack )
|
||||||
{
|
{
|
||||||
int pages = ItemPrintout.getPageCount( stack );
|
int pages = ItemPrintout.getPageCount( stack );
|
||||||
boolean book = ItemPrintout.getType( stack ) == ItemPrintout.Type.Book;
|
boolean book = ((ItemPrintout) stack.getItem()).getType() == ItemPrintout.Type.BOOK;
|
||||||
|
|
||||||
double width = LINE_MAX_LENGTH * FONT_WIDTH + X_TEXT_MARGIN * 2;
|
double width = LINE_MAX_LENGTH * FONT_WIDTH + X_TEXT_MARGIN * 2;
|
||||||
double height = LINES_PER_PAGE * FONT_HEIGHT + Y_TEXT_MARGIN * 2;
|
double height = LINES_PER_PAGE * FONT_HEIGHT + Y_TEXT_MARGIN * 2;
|
||||||
@@ -108,8 +107,8 @@ public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
|
|||||||
|
|
||||||
// Scale the printout to fit correctly.
|
// Scale the printout to fit correctly.
|
||||||
double scale = 1.0 / max;
|
double scale = 1.0 / max;
|
||||||
GlStateManager.scale( scale, scale, scale );
|
GlStateManager.scaled( scale, scale, scale );
|
||||||
GlStateManager.translate( (max - width) / 2.0f, (max - height) / 2.0f, 0.0f );
|
GlStateManager.translated( (max - width) / 2.0, (max - height) / 2.0, 0.0 );
|
||||||
|
|
||||||
drawBorder( 0, 0, -0.01, 0, pages, book );
|
drawBorder( 0, 0, -0.01, 0, pages, book );
|
||||||
drawText( X_TEXT_MARGIN, Y_TEXT_MARGIN, 0, ItemPrintout.getText( stack ), ItemPrintout.getColours( stack ) );
|
drawText( X_TEXT_MARGIN, Y_TEXT_MARGIN, 0, ItemPrintout.getText( stack ), ItemPrintout.getColours( stack ) );
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import net.minecraft.client.renderer.block.model.BakedQuad;
|
import net.minecraft.client.renderer.model.BakedQuad;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
@@ -180,7 +180,7 @@ public final class ModelTransformer
|
|||||||
*
|
*
|
||||||
* This also provides the ability to swap vertices through {@link #swap(int, int)} to allow reordering.
|
* This also provides the ability to swap vertices through {@link #swap(int, int)} to allow reordering.
|
||||||
*/
|
*/
|
||||||
private static class BakedQuadBuilder implements IVertexConsumer
|
private static final class BakedQuadBuilder implements IVertexConsumer
|
||||||
{
|
{
|
||||||
private final VertexFormat format;
|
private final VertexFormat format;
|
||||||
|
|
||||||
@@ -195,7 +195,7 @@ public final class ModelTransformer
|
|||||||
private BakedQuadBuilder( VertexFormat format )
|
private BakedQuadBuilder( VertexFormat format )
|
||||||
{
|
{
|
||||||
this.format = format;
|
this.format = format;
|
||||||
this.vertexData = new int[format.getSize()];
|
vertexData = new int[format.getSize()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -208,7 +208,7 @@ public final class ModelTransformer
|
|||||||
@Override
|
@Override
|
||||||
public void setQuadTint( int tint )
|
public void setQuadTint( int tint )
|
||||||
{
|
{
|
||||||
this.quadTint = tint;
|
quadTint = tint;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import dan200.computercraft.shared.util.Palette;
|
|||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.BufferBuilder;
|
import net.minecraft.client.renderer.BufferBuilder;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
|
import net.minecraft.client.renderer.GlStateManager.DestFactor;
|
||||||
|
import net.minecraft.client.renderer.GlStateManager.SourceFactor;
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
@@ -20,7 +22,7 @@ import org.lwjgl.opengl.GL11;
|
|||||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
|
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
|
||||||
|
|
||||||
public class PrintoutRenderer
|
public final class PrintoutRenderer
|
||||||
{
|
{
|
||||||
private static final ResourceLocation BG = new ResourceLocation( "computercraft", "textures/gui/printout.png" );
|
private static final ResourceLocation BG = new ResourceLocation( "computercraft", "textures/gui/printout.png" );
|
||||||
private static final double BG_SIZE = 256.0;
|
private static final double BG_SIZE = 256.0;
|
||||||
@@ -58,6 +60,8 @@ public class PrintoutRenderer
|
|||||||
private static final int COVER_Y = Y_SIZE;
|
private static final int COVER_Y = Y_SIZE;
|
||||||
private static final int COVER_X = X_SIZE + 4 * X_FOLD_SIZE;
|
private static final int COVER_X = X_SIZE + 4 * X_FOLD_SIZE;
|
||||||
|
|
||||||
|
private PrintoutRenderer() {}
|
||||||
|
|
||||||
public static void drawText( int x, int y, int start, TextBuffer[] text, TextBuffer[] colours )
|
public static void drawText( int x, int y, int start, TextBuffer[] text, TextBuffer[] colours )
|
||||||
{
|
{
|
||||||
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
||||||
@@ -70,9 +74,10 @@ public class PrintoutRenderer
|
|||||||
|
|
||||||
public static void drawText( int x, int y, int start, String[] text, String[] colours )
|
public static void drawText( int x, int y, int start, String[] text, String[] colours )
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.enableTexture2D();
|
GlStateManager.enableTexture2D();
|
||||||
|
GlStateManager.blendFuncSeparate( SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO );
|
||||||
|
|
||||||
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
|
||||||
|
|
||||||
@@ -84,11 +89,12 @@ public class PrintoutRenderer
|
|||||||
|
|
||||||
public static void drawBorder( double x, double y, double z, int page, int pages, boolean isBook )
|
public static void drawBorder( double x, double y, double z, int page, int pages, boolean isBook )
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.enableTexture2D();
|
GlStateManager.enableTexture2D();
|
||||||
|
GlStateManager.blendFuncSeparate( SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO );
|
||||||
|
|
||||||
Minecraft.getMinecraft().getTextureManager().bindTexture( BG );
|
Minecraft.getInstance().getTextureManager().bindTexture( BG );
|
||||||
|
|
||||||
Tessellator tessellator = Tessellator.getInstance();
|
Tessellator tessellator = Tessellator.getInstance();
|
||||||
BufferBuilder buffer = tessellator.getBuffer();
|
BufferBuilder buffer = tessellator.getBuffer();
|
||||||
|
|||||||
@@ -7,216 +7,82 @@
|
|||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.CableBounds;
|
import dan200.computercraft.shared.peripheral.modem.wired.CableShapes;
|
||||||
import dan200.computercraft.shared.util.WorldUtil;
|
import dan200.computercraft.shared.util.WorldUtil;
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.client.renderer.BufferBuilder;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.RenderGlobal;
|
import net.minecraft.client.renderer.WorldRenderer;
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.util.EnumFacing;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.RayTraceResult;
|
import net.minecraft.util.math.RayTraceResult;
|
||||||
|
import net.minecraft.util.math.shapes.VoxelShape;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Dist.CLIENT )
|
||||||
public final class RenderOverlayCable
|
public final class RenderOverlayCable
|
||||||
{
|
{
|
||||||
private static final float EXPAND = 0.002f;
|
|
||||||
private static final double MIN = CableBounds.MIN - EXPAND;
|
|
||||||
private static final double MAX = CableBounds.MAX + EXPAND;
|
|
||||||
|
|
||||||
private RenderOverlayCable()
|
private RenderOverlayCable()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw an outline for a specific part of a cable "Multipart".
|
||||||
|
*
|
||||||
|
* @param event The event to observe
|
||||||
|
* @see WorldRenderer#drawSelectionBox(EntityPlayer, RayTraceResult, int, float)
|
||||||
|
*/
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void drawHighlight( DrawBlockHighlightEvent event )
|
public static void drawHighlight( DrawBlockHighlightEvent event )
|
||||||
{
|
{
|
||||||
if( event.getTarget().typeOfHit != RayTraceResult.Type.BLOCK ) return;
|
if( event.getTarget().type != RayTraceResult.Type.BLOCK ) return;
|
||||||
|
|
||||||
BlockPos pos = event.getTarget().getBlockPos();
|
BlockPos pos = event.getTarget().getBlockPos();
|
||||||
World world = event.getPlayer().getEntityWorld();
|
World world = event.getPlayer().getEntityWorld();
|
||||||
|
|
||||||
IBlockState state = world.getBlockState( pos );
|
IBlockState state = world.getBlockState( pos );
|
||||||
if( state.getBlock() != ComputerCraft.Blocks.cable ) return;
|
|
||||||
|
|
||||||
state = state.getActualState( world, pos );
|
// We only care about instances with both cable and modem.
|
||||||
|
if( state.getBlock() != ComputerCraft.Blocks.cable || state.get( BlockCable.MODEM ).getFacing() == null || !state.get( BlockCable.CABLE ) )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.setCanceled( true );
|
event.setCanceled( true );
|
||||||
PeripheralType type = ComputerCraft.Blocks.cable.getPeripheralType( state );
|
|
||||||
|
EntityPlayer player = event.getPlayer();
|
||||||
|
Minecraft mc = Minecraft.getInstance();
|
||||||
|
float partialTicks = event.getPartialTicks();
|
||||||
|
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.tryBlendFuncSeparate( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0 );
|
GlStateManager.blendFuncSeparate( GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO );
|
||||||
GlStateManager.color( 0.0f, 0.0f, 0.0f, 0.4f );
|
GlStateManager.lineWidth( Math.max( 2.5F, mc.mainWindow.getFramebufferWidth() / 1920.0F * 2.5F ) );
|
||||||
GL11.glLineWidth( 2.0F );
|
|
||||||
GlStateManager.disableTexture2D();
|
GlStateManager.disableTexture2D();
|
||||||
GlStateManager.depthMask( false );
|
GlStateManager.depthMask( false );
|
||||||
|
GlStateManager.matrixMode( GL11.GL_PROJECTION );
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
|
GlStateManager.scalef( 1.0F, 1.0F, 0.999F );
|
||||||
|
|
||||||
{
|
double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTicks;
|
||||||
EntityPlayer player = event.getPlayer();
|
double y = player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTicks;
|
||||||
double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * event.getPartialTicks();
|
double z = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks;
|
||||||
double y = player.lastTickPosY + (player.posY - player.lastTickPosY) * event.getPartialTicks();
|
|
||||||
double z = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * event.getPartialTicks();
|
|
||||||
|
|
||||||
GlStateManager.translate( -x + pos.getX(), -y + pos.getY(), -z + pos.getZ() );
|
VoxelShape shape = WorldUtil.isVecInside( CableShapes.getModemShape( state ), event.getTarget().hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
|
||||||
}
|
? CableShapes.getModemShape( state )
|
||||||
|
: CableShapes.getCableShape( state );
|
||||||
|
|
||||||
if( type != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), event.getTarget().hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
|
WorldRenderer.drawShape( shape, pos.getX() - x, pos.getY() - y, pos.getZ() - z, 0.0F, 0.0F, 0.0F, 0.4F );
|
||||||
{
|
|
||||||
RenderGlobal.drawSelectionBoundingBox( CableBounds.getModemBounds( state ), 0, 0, 0, 0.4f );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int flags = 0;
|
|
||||||
|
|
||||||
Tessellator tessellator = Tessellator.getInstance();
|
|
||||||
BufferBuilder buffer = tessellator.getBuffer();
|
|
||||||
|
|
||||||
for( EnumFacing facing : EnumFacing.VALUES )
|
|
||||||
{
|
|
||||||
if( BlockCable.doesConnectVisually( state, world, pos, facing ) )
|
|
||||||
{
|
|
||||||
flags |= 1 << facing.ordinal();
|
|
||||||
|
|
||||||
|
|
||||||
switch( facing.getAxis() )
|
|
||||||
{
|
|
||||||
case X:
|
|
||||||
{
|
|
||||||
double offset = facing == EnumFacing.WEST ? -EXPAND : 1 + EXPAND;
|
|
||||||
double centre = facing == EnumFacing.WEST ? MIN : MAX;
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( offset, MIN, MIN ).endVertex();
|
|
||||||
buffer.pos( offset, MAX, MIN ).endVertex();
|
|
||||||
buffer.pos( offset, MAX, MAX ).endVertex();
|
|
||||||
buffer.pos( offset, MIN, MAX ).endVertex();
|
|
||||||
buffer.pos( offset, MIN, MIN ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINES, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( offset, MIN, MIN ).endVertex();
|
|
||||||
buffer.pos( centre, MIN, MIN ).endVertex();
|
|
||||||
buffer.pos( offset, MAX, MIN ).endVertex();
|
|
||||||
buffer.pos( centre, MAX, MIN ).endVertex();
|
|
||||||
buffer.pos( offset, MAX, MAX ).endVertex();
|
|
||||||
buffer.pos( centre, MAX, MAX ).endVertex();
|
|
||||||
buffer.pos( offset, MIN, MAX ).endVertex();
|
|
||||||
buffer.pos( centre, MIN, MAX ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Y:
|
|
||||||
{
|
|
||||||
double offset = facing == EnumFacing.DOWN ? -EXPAND : 1 + EXPAND;
|
|
||||||
double centre = facing == EnumFacing.DOWN ? MIN : MAX;
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( MIN, offset, MIN ).endVertex();
|
|
||||||
buffer.pos( MAX, offset, MIN ).endVertex();
|
|
||||||
buffer.pos( MAX, offset, MAX ).endVertex();
|
|
||||||
buffer.pos( MIN, offset, MAX ).endVertex();
|
|
||||||
buffer.pos( MIN, offset, MIN ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINES, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( MIN, offset, MIN ).endVertex();
|
|
||||||
buffer.pos( MIN, centre, MIN ).endVertex();
|
|
||||||
buffer.pos( MAX, offset, MIN ).endVertex();
|
|
||||||
buffer.pos( MAX, centre, MIN ).endVertex();
|
|
||||||
buffer.pos( MAX, offset, MAX ).endVertex();
|
|
||||||
buffer.pos( MAX, centre, MAX ).endVertex();
|
|
||||||
buffer.pos( MIN, offset, MAX ).endVertex();
|
|
||||||
buffer.pos( MIN, centre, MAX ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Z:
|
|
||||||
{
|
|
||||||
double offset = facing == EnumFacing.NORTH ? -EXPAND : 1 + EXPAND;
|
|
||||||
double centre = facing == EnumFacing.NORTH ? MIN : MAX;
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( MIN, MIN, offset ).endVertex();
|
|
||||||
buffer.pos( MAX, MIN, offset ).endVertex();
|
|
||||||
buffer.pos( MAX, MAX, offset ).endVertex();
|
|
||||||
buffer.pos( MIN, MAX, offset ).endVertex();
|
|
||||||
buffer.pos( MIN, MIN, offset ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINES, DefaultVertexFormats.POSITION );
|
|
||||||
buffer.pos( MIN, MIN, offset ).endVertex();
|
|
||||||
buffer.pos( MIN, MIN, centre ).endVertex();
|
|
||||||
buffer.pos( MAX, MIN, offset ).endVertex();
|
|
||||||
buffer.pos( MAX, MIN, centre ).endVertex();
|
|
||||||
buffer.pos( MAX, MAX, offset ).endVertex();
|
|
||||||
buffer.pos( MAX, MAX, centre ).endVertex();
|
|
||||||
buffer.pos( MIN, MAX, offset ).endVertex();
|
|
||||||
buffer.pos( MIN, MAX, centre ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.begin( GL11.GL_LINES, DefaultVertexFormats.POSITION );
|
|
||||||
|
|
||||||
draw( buffer, flags, EnumFacing.WEST, EnumFacing.DOWN, EnumFacing.Axis.Z );
|
|
||||||
draw( buffer, flags, EnumFacing.WEST, EnumFacing.UP, EnumFacing.Axis.Z );
|
|
||||||
draw( buffer, flags, EnumFacing.EAST, EnumFacing.DOWN, EnumFacing.Axis.Z );
|
|
||||||
draw( buffer, flags, EnumFacing.EAST, EnumFacing.UP, EnumFacing.Axis.Z );
|
|
||||||
|
|
||||||
draw( buffer, flags, EnumFacing.WEST, EnumFacing.NORTH, EnumFacing.Axis.Y );
|
|
||||||
draw( buffer, flags, EnumFacing.WEST, EnumFacing.SOUTH, EnumFacing.Axis.Y );
|
|
||||||
draw( buffer, flags, EnumFacing.EAST, EnumFacing.NORTH, EnumFacing.Axis.Y );
|
|
||||||
draw( buffer, flags, EnumFacing.EAST, EnumFacing.SOUTH, EnumFacing.Axis.Y );
|
|
||||||
|
|
||||||
draw( buffer, flags, EnumFacing.DOWN, EnumFacing.NORTH, EnumFacing.Axis.X );
|
|
||||||
draw( buffer, flags, EnumFacing.DOWN, EnumFacing.SOUTH, EnumFacing.Axis.X );
|
|
||||||
draw( buffer, flags, EnumFacing.UP, EnumFacing.NORTH, EnumFacing.Axis.X );
|
|
||||||
draw( buffer, flags, EnumFacing.UP, EnumFacing.SOUTH, EnumFacing.Axis.X );
|
|
||||||
|
|
||||||
tessellator.draw();
|
|
||||||
}
|
|
||||||
|
|
||||||
GlStateManager.popMatrix();
|
GlStateManager.popMatrix();
|
||||||
|
GlStateManager.matrixMode( GL11.GL_MODELVIEW );
|
||||||
GlStateManager.depthMask( true );
|
GlStateManager.depthMask( true );
|
||||||
GlStateManager.enableTexture2D();
|
GlStateManager.enableTexture2D();
|
||||||
GlStateManager.disableBlend();
|
GlStateManager.disableBlend();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void draw( BufferBuilder buffer, int flags, EnumFacing a, EnumFacing b, EnumFacing.Axis other )
|
|
||||||
{
|
|
||||||
if( ((flags >> a.ordinal()) & 1) != ((flags >> b.ordinal()) & 1) ) return;
|
|
||||||
|
|
||||||
double offA = a.getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE ? MIN : MAX;
|
|
||||||
double offB = b.getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE ? MIN : MAX;
|
|
||||||
switch( other )
|
|
||||||
{
|
|
||||||
case X:
|
|
||||||
buffer.pos( MIN, offA, offB ).endVertex();
|
|
||||||
buffer.pos( MAX, offA, offB ).endVertex();
|
|
||||||
break;
|
|
||||||
case Y:
|
|
||||||
buffer.pos( offA, MIN, offB ).endVertex();
|
|
||||||
buffer.pos( offA, MAX, offB ).endVertex();
|
|
||||||
break;
|
|
||||||
case Z:
|
|
||||||
buffer.pos( offA, offB, MIN ).endVertex();
|
|
||||||
buffer.pos( offA, offB, MAX ).endVertex();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,9 @@
|
|||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockCableModemVariant;
|
import dan200.computercraft.shared.peripheral.modem.wired.CableModemVariant;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.CableBounds;
|
import dan200.computercraft.shared.peripheral.modem.wired.CableShapes;
|
||||||
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
|
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
|
||||||
import dan200.computercraft.shared.util.WorldUtil;
|
import dan200.computercraft.shared.util.WorldUtil;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
@@ -18,11 +17,11 @@ import net.minecraft.block.state.IBlockState;
|
|||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.BufferBuilder;
|
import net.minecraft.client.renderer.BufferBuilder;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.RenderGlobal;
|
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.WorldRenderer;
|
||||||
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.util.BlockRenderLayer;
|
import net.minecraft.util.BlockRenderLayer;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
@@ -33,20 +32,23 @@ import net.minecraftforge.client.MinecraftForgeClient;
|
|||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render breaking animation only over part of a {@link TileCable}.
|
* Render breaking animation only over part of a {@link TileCable}.
|
||||||
*/
|
*/
|
||||||
public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable>
|
public class TileEntityCableRenderer extends TileEntityRenderer<TileCable>
|
||||||
{
|
{
|
||||||
|
private static final Random random = new Random();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render( @Nonnull TileCable te, double x, double y, double z, float partialTicks, int destroyStage, float alpha )
|
public void render( @Nonnull TileCable te, double x, double y, double z, float partialTicks, int destroyStage )
|
||||||
{
|
{
|
||||||
if( destroyStage < 0 ) return;
|
if( destroyStage < 0 ) return;
|
||||||
|
|
||||||
BlockPos pos = te.getPos();
|
BlockPos pos = te.getPos();
|
||||||
|
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
|
|
||||||
RayTraceResult hit = mc.objectMouseOver;
|
RayTraceResult hit = mc.objectMouseOver;
|
||||||
if( hit == null || !hit.getBlockPos().equals( pos ) ) return;
|
if( hit == null || !hit.getBlockPos().equals( pos ) ) return;
|
||||||
@@ -58,18 +60,11 @@ public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable
|
|||||||
Block block = state.getBlock();
|
Block block = state.getBlock();
|
||||||
if( block != ComputerCraft.Blocks.cable ) return;
|
if( block != ComputerCraft.Blocks.cable ) return;
|
||||||
|
|
||||||
state = state.getActualState( world, pos );
|
state = WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
|
||||||
if( te.getPeripheralType() != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
|
? block.getDefaultState().with( BlockCable.MODEM, state.get( BlockCable.MODEM ) )
|
||||||
{
|
: state.with( BlockCable.MODEM, CableModemVariant.None );
|
||||||
state = block.getDefaultState().withProperty( BlockCable.Properties.MODEM, state.getValue( BlockCable.Properties.MODEM ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
state = state.withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None );
|
|
||||||
}
|
|
||||||
|
|
||||||
IBakedModel model = mc.getBlockRendererDispatcher().getModelForState( state );
|
IBakedModel model = mc.getBlockRendererDispatcher().getModelForState( state );
|
||||||
if( model == null ) return;
|
|
||||||
|
|
||||||
preRenderDamagedBlocks();
|
preRenderDamagedBlocks();
|
||||||
|
|
||||||
@@ -81,11 +76,11 @@ public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable
|
|||||||
ForgeHooksClient.setRenderLayer( block.getRenderLayer() );
|
ForgeHooksClient.setRenderLayer( block.getRenderLayer() );
|
||||||
|
|
||||||
// See BlockRendererDispatcher#renderBlockDamage
|
// See BlockRendererDispatcher#renderBlockDamage
|
||||||
TextureAtlasSprite breakingTexture = mc.getTextureMapBlocks().getAtlasSprite( "minecraft:blocks/destroy_stage_" + destroyStage );
|
TextureAtlasSprite breakingTexture = mc.getTextureMap().getSprite( DESTROY_STAGES[destroyStage] );
|
||||||
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModel(
|
mc.getBlockRendererDispatcher().getBlockModelRenderer().renderModel(
|
||||||
world,
|
world,
|
||||||
ForgeHooksClient.getDamageModel( model, breakingTexture, state, world, pos ),
|
ForgeHooksClient.getDamageModel( model, breakingTexture, state, world, pos ),
|
||||||
state, pos, buffer, true
|
state, pos, buffer, true, random, state.getPositionRandom( pos )
|
||||||
);
|
);
|
||||||
|
|
||||||
ForgeHooksClient.setRenderLayer( BlockRenderLayer.SOLID );
|
ForgeHooksClient.setRenderLayer( BlockRenderLayer.SOLID );
|
||||||
@@ -97,30 +92,30 @@ public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see RenderGlobal#preRenderDamagedBlocks()
|
* @see WorldRenderer#preRenderDamagedBlocks()
|
||||||
*/
|
*/
|
||||||
private void preRenderDamagedBlocks()
|
private void preRenderDamagedBlocks()
|
||||||
{
|
{
|
||||||
GlStateManager.disableLighting();
|
GlStateManager.disableLighting();
|
||||||
|
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.tryBlendFuncSeparate( GlStateManager.SourceFactor.DST_COLOR, GlStateManager.DestFactor.SRC_COLOR, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO );
|
GlStateManager.blendFuncSeparate( GlStateManager.SourceFactor.DST_COLOR, GlStateManager.DestFactor.SRC_COLOR, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO );
|
||||||
GlStateManager.enableBlend();
|
GlStateManager.enableBlend();
|
||||||
GlStateManager.color( 1.0F, 1.0F, 1.0F, 0.5F );
|
GlStateManager.color4f( 1.0F, 1.0F, 1.0F, 0.5F );
|
||||||
GlStateManager.doPolygonOffset( -3.0F, -3.0F );
|
GlStateManager.polygonOffset( -3.0F, -3.0F );
|
||||||
GlStateManager.enablePolygonOffset();
|
GlStateManager.enablePolygonOffset();
|
||||||
GlStateManager.alphaFunc( 516, 0.1F );
|
GlStateManager.alphaFunc( 516, 0.1F );
|
||||||
GlStateManager.enableAlpha();
|
GlStateManager.enableAlphaTest();
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see RenderGlobal#postRenderDamagedBlocks()
|
* @see WorldRenderer#postRenderDamagedBlocks()
|
||||||
*/
|
*/
|
||||||
private void postRenderDamagedBlocks()
|
private void postRenderDamagedBlocks()
|
||||||
{
|
{
|
||||||
GlStateManager.disableAlpha();
|
GlStateManager.disableAlphaTest();
|
||||||
GlStateManager.doPolygonOffset( 0.0F, 0.0F );
|
GlStateManager.polygonOffset( 0.0F, 0.0F );
|
||||||
GlStateManager.disablePolygonOffset();
|
GlStateManager.disablePolygonOffset();
|
||||||
GlStateManager.disablePolygonOffset();
|
GlStateManager.disablePolygonOffset();
|
||||||
GlStateManager.depthMask( true );
|
GlStateManager.depthMask( true );
|
||||||
|
|||||||
@@ -20,16 +20,16 @@ import net.minecraft.client.renderer.BufferBuilder;
|
|||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.OpenGlHelper;
|
import net.minecraft.client.renderer.OpenGlHelper;
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMonitor>
|
public class TileEntityMonitorRenderer extends TileEntityRenderer<TileMonitor>
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void render( TileMonitor tileEntity, double posX, double posY, double posZ, float f, int i, float f2 )
|
public void render( TileMonitor tileEntity, double posX, double posY, double posZ, float f, int i )
|
||||||
{
|
{
|
||||||
if( tileEntity != null )
|
if( tileEntity != null )
|
||||||
{
|
{
|
||||||
@@ -37,7 +37,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderMonitorAt( TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
|
private static void renderMonitorAt( TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
|
||||||
{
|
{
|
||||||
// Render from the origin monitor
|
// Render from the origin monitor
|
||||||
ClientMonitor originTerminal = monitor.getClientMonitor();
|
ClientMonitor originTerminal = monitor.getClientMonitor();
|
||||||
@@ -73,19 +73,19 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Setup initial transform
|
// Setup initial transform
|
||||||
GlStateManager.translate( posX + 0.5, posY + 0.5, posZ + 0.5 );
|
GlStateManager.translated( posX + 0.5, posY + 0.5, posZ + 0.5 );
|
||||||
GlStateManager.rotate( -yaw, 0.0f, 1.0f, 0.0f );
|
GlStateManager.rotatef( -yaw, 0.0f, 1.0f, 0.0f );
|
||||||
GlStateManager.rotate( pitch, 1.0f, 0.0f, 0.0f );
|
GlStateManager.rotatef( pitch, 1.0f, 0.0f, 0.0f );
|
||||||
GlStateManager.translate(
|
GlStateManager.translated(
|
||||||
-0.5 + TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN,
|
-0.5 + TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN,
|
||||||
(origin.getHeight() - 0.5) - (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN),
|
origin.getHeight() - 0.5 - (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN) + 0,
|
||||||
0.5
|
0.5
|
||||||
);
|
);
|
||||||
double xSize = origin.getWidth() - 2.0 * (TileMonitor.RENDER_MARGIN + TileMonitor.RENDER_BORDER);
|
double xSize = origin.getWidth() - 2.0 * (TileMonitor.RENDER_MARGIN + TileMonitor.RENDER_BORDER);
|
||||||
double ySize = origin.getHeight() - 2.0 * (TileMonitor.RENDER_MARGIN + TileMonitor.RENDER_BORDER);
|
double ySize = origin.getHeight() - 2.0 * (TileMonitor.RENDER_MARGIN + TileMonitor.RENDER_BORDER);
|
||||||
|
|
||||||
// Get renderers
|
// Get renderers
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
Tessellator tessellator = Tessellator.getInstance();
|
Tessellator tessellator = Tessellator.getInstance();
|
||||||
BufferBuilder renderer = tessellator.getBuffer();
|
BufferBuilder renderer = tessellator.getBuffer();
|
||||||
|
|
||||||
@@ -94,9 +94,9 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
|
|
||||||
// Draw the contents
|
// Draw the contents
|
||||||
GlStateManager.depthMask( false );
|
GlStateManager.depthMask( false );
|
||||||
OpenGlHelper.setLightmapTextureCoords( OpenGlHelper.lightmapTexUnit, 0xFF, 0xFF );
|
OpenGlHelper.glMultiTexCoord2f( OpenGlHelper.GL_TEXTURE1, 0xFFFF, 0xFFFF );
|
||||||
GlStateManager.disableLighting();
|
GlStateManager.disableLighting();
|
||||||
mc.entityRenderer.disableLightmap();
|
mc.gameRenderer.disableLightmap();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Terminal terminal = originTerminal.getTerminal();
|
Terminal terminal = originTerminal.getTerminal();
|
||||||
@@ -124,14 +124,14 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
{
|
{
|
||||||
double xScale = xSize / (width * FixedWidthFontRenderer.FONT_WIDTH);
|
double xScale = xSize / (width * FixedWidthFontRenderer.FONT_WIDTH);
|
||||||
double yScale = ySize / (height * FixedWidthFontRenderer.FONT_HEIGHT);
|
double yScale = ySize / (height * FixedWidthFontRenderer.FONT_HEIGHT);
|
||||||
GlStateManager.scale( xScale, -yScale, 1.0 );
|
GlStateManager.scaled( xScale, -yScale, 1.0 );
|
||||||
|
|
||||||
// Draw background
|
// Draw background
|
||||||
mc.getTextureManager().bindTexture( FixedWidthFontRenderer.BACKGROUND );
|
mc.getTextureManager().bindTexture( FixedWidthFontRenderer.BACKGROUND );
|
||||||
if( redraw )
|
if( redraw )
|
||||||
{
|
{
|
||||||
// Build background display list
|
// Build background display list
|
||||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[0], GL11.GL_COMPILE );
|
GlStateManager.newList( originTerminal.renderDisplayLists[0], GL11.GL_COMPILE );
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
double marginXSize = TileMonitor.RENDER_MARGIN / xScale;
|
double marginXSize = TileMonitor.RENDER_MARGIN / xScale;
|
||||||
@@ -142,10 +142,10 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
GlStateManager.scale( 1.0, marginSquash, 1.0 );
|
GlStateManager.scaled( 1.0, marginSquash, 1.0 );
|
||||||
GlStateManager.translate( 0.0, -marginYSize / marginSquash, 0.0 );
|
GlStateManager.translated( 0.0, -marginYSize / marginSquash, 0.0 );
|
||||||
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( 0 ), marginXSize, marginXSize, greyscale, palette );
|
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( 0 ), marginXSize, marginXSize, greyscale, palette );
|
||||||
GlStateManager.translate( 0.0, (marginYSize + height * FixedWidthFontRenderer.FONT_HEIGHT) / marginSquash, 0.0 );
|
GlStateManager.translated( 0.0, (marginYSize + height * FixedWidthFontRenderer.FONT_HEIGHT) / marginSquash, 0.0 );
|
||||||
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( height - 1 ), marginXSize, marginXSize, greyscale, palette );
|
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( height - 1 ), marginXSize, marginXSize, greyscale, palette );
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -167,7 +167,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.glEndList();
|
GlStateManager.endList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GlStateManager.callList( originTerminal.renderDisplayLists[0] );
|
GlStateManager.callList( originTerminal.renderDisplayLists[0] );
|
||||||
@@ -178,7 +178,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
if( redraw )
|
if( redraw )
|
||||||
{
|
{
|
||||||
// Build text display list
|
// Build text display list
|
||||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[1], GL11.GL_COMPILE );
|
GlStateManager.newList( originTerminal.renderDisplayLists[1], GL11.GL_COMPILE );
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Lines
|
// Lines
|
||||||
@@ -195,7 +195,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.glEndList();
|
GlStateManager.endList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GlStateManager.callList( originTerminal.renderDisplayLists[1] );
|
GlStateManager.callList( originTerminal.renderDisplayLists[1] );
|
||||||
@@ -206,7 +206,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
if( redraw )
|
if( redraw )
|
||||||
{
|
{
|
||||||
// Build cursor display list
|
// Build cursor display list
|
||||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[2], GL11.GL_COMPILE );
|
GlStateManager.newList( originTerminal.renderDisplayLists[2], GL11.GL_COMPILE );
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Cursor
|
// Cursor
|
||||||
@@ -227,7 +227,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.glEndList();
|
GlStateManager.endList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( FrameInfo.getGlobalCursorBlink() )
|
if( FrameInfo.getGlobalCursorBlink() )
|
||||||
@@ -262,7 +262,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.depthMask( true );
|
GlStateManager.depthMask( true );
|
||||||
mc.entityRenderer.enableLightmap();
|
mc.gameRenderer.enableLightmap();
|
||||||
GlStateManager.enableLighting();
|
GlStateManager.enableLighting();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
GlStateManager.color4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||||
GlStateManager.popMatrix();
|
GlStateManager.popMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,20 +10,21 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
|||||||
import dan200.computercraft.api.turtle.TurtleSide;
|
import dan200.computercraft.api.turtle.TurtleSide;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||||
|
import dan200.computercraft.shared.util.DirectionUtil;
|
||||||
import dan200.computercraft.shared.util.Holiday;
|
import dan200.computercraft.shared.util.Holiday;
|
||||||
import dan200.computercraft.shared.util.HolidayUtil;
|
import dan200.computercraft.shared.util.HolidayUtil;
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.BufferBuilder;
|
import net.minecraft.client.renderer.BufferBuilder;
|
||||||
import net.minecraft.client.renderer.EntityRenderer;
|
import net.minecraft.client.renderer.GameRenderer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import net.minecraft.client.renderer.block.model.BakedQuad;
|
import net.minecraft.client.renderer.model.BakedQuad;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ModelManager;
|
import net.minecraft.client.renderer.model.ModelManager;
|
||||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
import net.minecraft.client.renderer.model.ModelResourceLocation;
|
||||||
import net.minecraft.client.renderer.texture.TextureMap;
|
import net.minecraft.client.renderer.texture.TextureMap;
|
||||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
@@ -36,18 +37,19 @@ import org.lwjgl.opengl.GL11;
|
|||||||
|
|
||||||
import javax.vecmath.Matrix4f;
|
import javax.vecmath.Matrix4f;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurtle>
|
public class TileEntityTurtleRenderer extends TileEntityRenderer<TileTurtle>
|
||||||
{
|
{
|
||||||
private static final ModelResourceLocation NORMAL_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle", "inventory" );
|
private static final ModelResourceLocation NORMAL_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_normal", "inventory" );
|
||||||
private static final ModelResourceLocation ADVANCED_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_advanced", "inventory" );
|
private static final ModelResourceLocation ADVANCED_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_advanced", "inventory" );
|
||||||
private static final ModelResourceLocation COLOUR_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_white", "inventory" );
|
private static final ModelResourceLocation COLOUR_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_colour", "inventory" );
|
||||||
private static final ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
|
private static final ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render( TileTurtle tileEntity, double posX, double posY, double posZ, float f, int i, float f2 )
|
public void render( TileTurtle tileEntity, double posX, double posY, double posZ, float partialTicks, int breaking )
|
||||||
{
|
{
|
||||||
if( tileEntity != null ) renderTurtleAt( tileEntity, posX, posY, posZ, f, i );
|
if( tileEntity != null ) renderTurtleAt( tileEntity, posX, posY, posZ, partialTicks );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ModelResourceLocation getTurtleModel( ComputerFamily family, boolean coloured )
|
public static ModelResourceLocation getTurtleModel( ComputerFamily family, boolean coloured )
|
||||||
@@ -78,14 +80,14 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float f, int i )
|
private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float partialTicks )
|
||||||
{
|
{
|
||||||
// Render the label
|
// Render the label
|
||||||
String label = turtle.createProxy().getLabel();
|
String label = turtle.createProxy().getLabel();
|
||||||
if( label != null && rendererDispatcher.cameraHitResult != null && turtle.getPos().equals( rendererDispatcher.cameraHitResult.getBlockPos() ) )
|
if( label != null && rendererDispatcher.cameraHitResult != null && turtle.getPos().equals( rendererDispatcher.cameraHitResult.getBlockPos() ) )
|
||||||
{
|
{
|
||||||
setLightmapDisabled( true );
|
setLightmapDisabled( true );
|
||||||
EntityRenderer.drawNameplate(
|
GameRenderer.drawNameplate(
|
||||||
getFontRenderer(), label,
|
getFontRenderer(), label,
|
||||||
(float) posX + 0.5F, (float) posY + 1.2F, (float) posZ + 0.5F, 0,
|
(float) posX + 0.5F, (float) posY + 1.2F, (float) posZ + 0.5F, 0,
|
||||||
rendererDispatcher.entityYaw, rendererDispatcher.entityPitch, false, false
|
rendererDispatcher.entityYaw, rendererDispatcher.entityPitch, false, false
|
||||||
@@ -93,34 +95,28 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
setLightmapDisabled( false );
|
setLightmapDisabled( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
IBlockState state = turtle.getWorld().getBlockState( turtle.getPos() );
|
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
IBlockState state = turtle.getBlockState();
|
||||||
// Setup the transform
|
// Setup the transform
|
||||||
Vec3d offset;
|
Vec3d offset = turtle.getRenderOffset( partialTicks );
|
||||||
float yaw;
|
float yaw = turtle.getRenderYaw( partialTicks );
|
||||||
offset = turtle.getRenderOffset( f );
|
GlStateManager.translated( posX + offset.x, posY + offset.y, posZ + offset.z );
|
||||||
yaw = turtle.getRenderYaw( f );
|
|
||||||
GlStateManager.translate( posX + offset.x, posY + offset.y, posZ + offset.z );
|
|
||||||
|
|
||||||
// Render the turtle
|
// Render the turtle
|
||||||
GlStateManager.translate( 0.5f, 0.5f, 0.5f );
|
GlStateManager.translatef( 0.5f, 0.5f, 0.5f );
|
||||||
GlStateManager.rotate( 180.0f - yaw, 0.0f, 1.0f, 0.0f );
|
GlStateManager.rotatef( 180.0f - yaw, 0.0f, 1.0f, 0.0f );
|
||||||
if( label != null && (label.equals( "Dinnerbone" ) || label.equals( "Grumm" )) )
|
if( label != null && (label.equals( "Dinnerbone" ) || label.equals( "Grumm" )) )
|
||||||
{
|
{
|
||||||
// Flip the model and swap the cull face as winding order will have changed.
|
// Flip the model and swap the cull face as winding order will have changed.
|
||||||
GlStateManager.scale( 1.0f, -1.0f, 1.0f );
|
GlStateManager.scalef( 1.0f, -1.0f, 1.0f );
|
||||||
GlStateManager.cullFace( GlStateManager.CullFace.FRONT );
|
GlStateManager.cullFace( GlStateManager.CullFace.FRONT );
|
||||||
}
|
}
|
||||||
GlStateManager.translate( -0.5f, -0.5f, -0.5f );
|
GlStateManager.translatef( -0.5f, -0.5f, -0.5f );
|
||||||
// Render the turtle
|
// Render the turtle
|
||||||
int colour;
|
int colour = turtle.getColour();
|
||||||
ComputerFamily family;
|
ComputerFamily family = turtle.getFamily();
|
||||||
ResourceLocation overlay;
|
ResourceLocation overlay = turtle.getOverlay();
|
||||||
colour = turtle.getColour();
|
|
||||||
family = turtle.getFamily();
|
|
||||||
overlay = turtle.getOverlay();
|
|
||||||
|
|
||||||
renderModel( state, getTurtleModel( family, colour != -1 ), colour == -1 ? null : new int[] { colour } );
|
renderModel( state, getTurtleModel( family, colour != -1 ), colour == -1 ? null : new int[] { colour } );
|
||||||
|
|
||||||
@@ -146,8 +142,8 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render the upgrades
|
// Render the upgrades
|
||||||
renderUpgrade( state, turtle, TurtleSide.Left, f );
|
renderUpgrade( state, turtle, TurtleSide.Left, partialTicks );
|
||||||
renderUpgrade( state, turtle, TurtleSide.Right, f );
|
renderUpgrade( state, turtle, TurtleSide.Right, partialTicks );
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -165,9 +161,9 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
float toolAngle = turtle.getToolRenderAngle( side, f );
|
float toolAngle = turtle.getToolRenderAngle( side, f );
|
||||||
GlStateManager.translate( 0.0f, 0.5f, 0.5f );
|
GlStateManager.translatef( 0.0f, 0.5f, 0.5f );
|
||||||
GlStateManager.rotate( -toolAngle, 1.0f, 0.0f, 0.0f );
|
GlStateManager.rotatef( -toolAngle, 1.0f, 0.0f, 0.0f );
|
||||||
GlStateManager.translate( 0.0f, -0.5f, -0.5f );
|
GlStateManager.translatef( 0.0f, -0.5f, -0.5f );
|
||||||
|
|
||||||
Pair<IBakedModel, Matrix4f> pair = upgrade.getModel( turtle.getAccess(), side );
|
Pair<IBakedModel, Matrix4f> pair = upgrade.getModel( turtle.getAccess(), side );
|
||||||
if( pair != null )
|
if( pair != null )
|
||||||
@@ -191,24 +187,24 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
|
|
||||||
private void renderModel( IBlockState state, ModelResourceLocation modelLocation, int[] tints )
|
private void renderModel( IBlockState state, ModelResourceLocation modelLocation, int[] tints )
|
||||||
{
|
{
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
|
ModelManager modelManager = mc.getItemRenderer().getItemModelMesher().getModelManager();
|
||||||
renderModel( state, modelManager.getModel( modelLocation ), tints );
|
renderModel( state, modelManager.getModel( modelLocation ), tints );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderModel( IBlockState state, IBakedModel model, int[] tints )
|
private void renderModel( IBlockState state, IBakedModel model, int[] tints )
|
||||||
{
|
{
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Random random = new Random( 0 );
|
||||||
Tessellator tessellator = Tessellator.getInstance();
|
Tessellator tessellator = Tessellator.getInstance();
|
||||||
mc.getTextureManager().bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
|
rendererDispatcher.textureManager.bindTexture( TextureMap.LOCATION_BLOCKS_TEXTURE );
|
||||||
renderQuads( tessellator, model.getQuads( state, null, 0 ), tints );
|
renderQuads( tessellator, model.getQuads( state, null, random ), tints );
|
||||||
for( EnumFacing facing : EnumFacing.VALUES )
|
for( EnumFacing facing : DirectionUtil.FACINGS )
|
||||||
{
|
{
|
||||||
renderQuads( tessellator, model.getQuads( state, facing, 0 ), tints );
|
renderQuads( tessellator, model.getQuads( state, facing, random ), tints );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderQuads( Tessellator tessellator, List<BakedQuad> quads, int[] tints )
|
private static void renderQuads( Tessellator tessellator, List<BakedQuad> quads, int[] tints )
|
||||||
{
|
{
|
||||||
BufferBuilder buffer = tessellator.getBuffer();
|
BufferBuilder buffer = tessellator.getBuffer();
|
||||||
VertexFormat format = DefaultVertexFormats.ITEM;
|
VertexFormat format = DefaultVertexFormats.ITEM;
|
||||||
|
|||||||
@@ -6,26 +6,29 @@
|
|||||||
|
|
||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
|
import net.minecraft.client.renderer.model.IUnbakedModel;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||||
import net.minecraft.client.resources.IResourceManager;
|
import net.minecraft.resources.IResourceManager;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraftforge.client.model.ICustomModelLoader;
|
import net.minecraftforge.client.model.ICustomModelLoader;
|
||||||
import net.minecraftforge.client.model.IModel;
|
|
||||||
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
|
||||||
import net.minecraftforge.common.model.IModelState;
|
import net.minecraftforge.common.model.IModelState;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TurtleModelLoader implements ICustomModelLoader
|
public final class TurtleModelLoader implements ICustomModelLoader
|
||||||
{
|
{
|
||||||
private static final ResourceLocation NORMAL_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle" );
|
private static final ResourceLocation NORMAL_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_normal" );
|
||||||
private static final ResourceLocation ADVANCED_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/advanced_turtle" );
|
private static final ResourceLocation ADVANCED_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_advanced" );
|
||||||
private static final ResourceLocation COLOUR_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_white" );
|
private static final ResourceLocation COLOUR_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_colour" );
|
||||||
|
|
||||||
public static final TurtleModelLoader INSTANCE = new TurtleModelLoader();
|
public static final TurtleModelLoader INSTANCE = new TurtleModelLoader();
|
||||||
|
|
||||||
@@ -42,80 +45,57 @@ public class TurtleModelLoader implements ICustomModelLoader
|
|||||||
public boolean accepts( @Nonnull ResourceLocation name )
|
public boolean accepts( @Nonnull ResourceLocation name )
|
||||||
{
|
{
|
||||||
return name.getNamespace().equals( ComputerCraft.MOD_ID )
|
return name.getNamespace().equals( ComputerCraft.MOD_ID )
|
||||||
&& (name.getPath().equals( "turtle" ) || name.getPath().equals( "turtle_advanced" ));
|
&& (name.getPath().equals( "item/turtle_normal" ) || name.getPath().equals( "item/turtle_advanced" ));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public IModel loadModel( @Nonnull ResourceLocation name ) throws Exception
|
public IUnbakedModel loadModel( @Nonnull ResourceLocation name )
|
||||||
{
|
{
|
||||||
if( name.getNamespace().equals( ComputerCraft.MOD_ID ) )
|
if( name.getNamespace().equals( ComputerCraft.MOD_ID ) )
|
||||||
{
|
{
|
||||||
IModel colourModel = ModelLoaderRegistry.getModel( COLOUR_TURTLE_MODEL );
|
|
||||||
switch( name.getPath() )
|
switch( name.getPath() )
|
||||||
{
|
{
|
||||||
case "turtle":
|
case "item/turtle_normal":
|
||||||
return new TurtleModel( ModelLoaderRegistry.getModel( NORMAL_TURTLE_MODEL ), colourModel );
|
return new TurtleModel( NORMAL_TURTLE_MODEL );
|
||||||
case "turtle_advanced":
|
case "item/turtle_advanced":
|
||||||
return new TurtleModel( ModelLoaderRegistry.getModel( ADVANCED_TURTLE_MODEL ), colourModel );
|
return new TurtleModel( ADVANCED_TURTLE_MODEL );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalStateException( "Loader does not accept " + name );
|
throw new IllegalStateException( "Loader does not accept " + name );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TurtleModel implements IModel
|
private static final class TurtleModel implements IUnbakedModel
|
||||||
{
|
{
|
||||||
private final IModel family;
|
private final ResourceLocation family;
|
||||||
private final IModel colour;
|
|
||||||
|
|
||||||
private TurtleModel( IModel family, IModel colour )
|
private TurtleModel( ResourceLocation family ) {this.family = family;}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public Collection<ResourceLocation> getDependencies()
|
||||||
{
|
{
|
||||||
this.family = family;
|
return Arrays.asList( family, COLOUR_TURTLE_MODEL );
|
||||||
this.colour = colour;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public IBakedModel bake( @Nonnull IModelState state, @Nonnull VertexFormat format, @Nonnull Function<ResourceLocation, TextureAtlasSprite> function )
|
public Collection<ResourceLocation> getTextures( @Nonnull Function<ResourceLocation, IUnbakedModel> modelGetter, @Nonnull Set<String> missingTextureErrors )
|
||||||
|
{
|
||||||
|
return getDependencies().stream()
|
||||||
|
.flatMap( x -> modelGetter.apply( x ).getTextures( modelGetter, missingTextureErrors ).stream() )
|
||||||
|
.collect( Collectors.toSet() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public IBakedModel bake( @Nonnull Function<ResourceLocation, IUnbakedModel> modelGetter, @Nonnull Function<ResourceLocation, TextureAtlasSprite> spriteGetter, @Nonnull IModelState state, boolean uvlock, @Nonnull VertexFormat format )
|
||||||
{
|
{
|
||||||
return new TurtleSmartItemModel(
|
return new TurtleSmartItemModel(
|
||||||
family.bake( state, format, function ),
|
modelGetter.apply( family ).bake( modelGetter, spriteGetter, state, uvlock, format ),
|
||||||
colour.bake( state, format, function )
|
modelGetter.apply( COLOUR_TURTLE_MODEL ).bake( modelGetter, spriteGetter, state, uvlock, format )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TurtleModel copy( IModel family, IModel colour )
|
|
||||||
{
|
|
||||||
return this.family == family && this.colour == colour ? this : new TurtleModel( family, colour );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public IModel smoothLighting( boolean value )
|
|
||||||
{
|
|
||||||
return copy( family.smoothLighting( value ), colour.smoothLighting( value ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public IModel gui3d( boolean value )
|
|
||||||
{
|
|
||||||
return copy( family.gui3d( value ), colour.gui3d( value ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public IModel uvlock( boolean value )
|
|
||||||
{
|
|
||||||
return copy( family.uvlock( value ), colour.uvlock( value ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public IModel retexture( ImmutableMap<String, String> textures )
|
|
||||||
{
|
|
||||||
return copy( family.retexture( textures ), colour.retexture( textures ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,19 +7,15 @@
|
|||||||
package dan200.computercraft.client.render;
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.client.renderer.block.model.BakedQuad;
|
import net.minecraft.client.renderer.model.BakedQuad;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
|
import net.minecraft.client.renderer.model.ItemOverrideList;
|
||||||
import net.minecraft.client.renderer.block.model.ItemOverrideList;
|
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.vecmath.Matrix4f;
|
import javax.vecmath.Matrix4f;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class TurtleMultiModel implements IBakedModel
|
public class TurtleMultiModel implements IBakedModel
|
||||||
{
|
{
|
||||||
@@ -30,8 +26,8 @@ public class TurtleMultiModel implements IBakedModel
|
|||||||
private final Matrix4f m_leftUpgradeTransform;
|
private final Matrix4f m_leftUpgradeTransform;
|
||||||
private final IBakedModel m_rightUpgradeModel;
|
private final IBakedModel m_rightUpgradeModel;
|
||||||
private final Matrix4f m_rightUpgradeTransform;
|
private final Matrix4f m_rightUpgradeTransform;
|
||||||
private List<BakedQuad> m_generalQuads;
|
private List<BakedQuad> m_generalQuads = null;
|
||||||
private Map<EnumFacing, List<BakedQuad>> m_faceQuads;
|
private Map<EnumFacing, List<BakedQuad>> m_faceQuads = new EnumMap<>( EnumFacing.class );
|
||||||
|
|
||||||
public TurtleMultiModel( IBakedModel baseModel, IBakedModel overlayModel, Matrix4f generalTransform, IBakedModel leftUpgradeModel, Matrix4f leftUpgradeTransform, IBakedModel rightUpgradeModel, Matrix4f rightUpgradeTransform )
|
public TurtleMultiModel( IBakedModel baseModel, IBakedModel overlayModel, Matrix4f generalTransform, IBakedModel leftUpgradeModel, Matrix4f leftUpgradeTransform, IBakedModel rightUpgradeModel, Matrix4f rightUpgradeTransform )
|
||||||
{
|
{
|
||||||
@@ -43,13 +39,11 @@ public class TurtleMultiModel implements IBakedModel
|
|||||||
m_rightUpgradeModel = rightUpgradeModel;
|
m_rightUpgradeModel = rightUpgradeModel;
|
||||||
m_rightUpgradeTransform = rightUpgradeTransform;
|
m_rightUpgradeTransform = rightUpgradeTransform;
|
||||||
m_generalTransform = generalTransform;
|
m_generalTransform = generalTransform;
|
||||||
m_generalQuads = null;
|
|
||||||
m_faceQuads = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public List<BakedQuad> getQuads( IBlockState state, EnumFacing side, long rand )
|
public List<BakedQuad> getQuads( IBlockState state, EnumFacing side, @Nonnull Random rand )
|
||||||
{
|
{
|
||||||
if( side != null )
|
if( side != null )
|
||||||
{
|
{
|
||||||
@@ -63,7 +57,7 @@ public class TurtleMultiModel implements IBakedModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<BakedQuad> buildQuads( IBlockState state, EnumFacing side, long rand )
|
private List<BakedQuad> buildQuads( IBlockState state, EnumFacing side, Random rand )
|
||||||
{
|
{
|
||||||
ArrayList<BakedQuad> quads = new ArrayList<>();
|
ArrayList<BakedQuad> quads = new ArrayList<>();
|
||||||
ModelTransformer.transformQuadsTo( quads, m_baseModel.getQuads( state, side, rand ), m_generalTransform );
|
ModelTransformer.transformQuadsTo( quads, m_baseModel.getQuads( state, side, rand ), m_generalTransform );
|
||||||
@@ -127,7 +121,7 @@ public class TurtleMultiModel implements IBakedModel
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public ItemCameraTransforms getItemCameraTransforms()
|
public net.minecraft.client.renderer.model.ItemCameraTransforms getItemCameraTransforms()
|
||||||
{
|
{
|
||||||
return m_baseModel.getItemCameraTransforms();
|
return m_baseModel.getItemCameraTransforms();
|
||||||
}
|
}
|
||||||
@@ -136,6 +130,6 @@ public class TurtleMultiModel implements IBakedModel
|
|||||||
@Override
|
@Override
|
||||||
public ItemOverrideList getOverrides()
|
public ItemOverrideList getOverrides()
|
||||||
{
|
{
|
||||||
return ItemOverrideList.NONE;
|
return ItemOverrideList.EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ package dan200.computercraft.client.render;
|
|||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||||
import dan200.computercraft.api.turtle.TurtleSide;
|
import dan200.computercraft.api.turtle.TurtleSide;
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
import dan200.computercraft.shared.turtle.items.ItemTurtle;
|
||||||
import dan200.computercraft.shared.util.Holiday;
|
import dan200.computercraft.shared.util.Holiday;
|
||||||
import dan200.computercraft.shared.util.HolidayUtil;
|
import dan200.computercraft.shared.util.HolidayUtil;
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.block.model.*;
|
import net.minecraft.client.renderer.model.*;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
import net.minecraft.entity.EntityLivingBase;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
@@ -26,9 +26,9 @@ import org.apache.commons.lang3.tuple.Pair;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.vecmath.Matrix4f;
|
import javax.vecmath.Matrix4f;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class TurtleSmartItemModel implements IBakedModel
|
public class TurtleSmartItemModel implements IBakedModel
|
||||||
{
|
{
|
||||||
@@ -106,13 +106,13 @@ public class TurtleSmartItemModel implements IBakedModel
|
|||||||
this.colourModel = colourModel;
|
this.colourModel = colourModel;
|
||||||
|
|
||||||
m_cachedModels = new HashMap<>();
|
m_cachedModels = new HashMap<>();
|
||||||
m_overrides = new ItemOverrideList( new ArrayList<>() )
|
m_overrides = new ItemOverrideList()
|
||||||
{
|
{
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public IBakedModel handleItemState( @Nonnull IBakedModel originalModel, @Nonnull ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity )
|
public IBakedModel getModelWithOverrides( @Nonnull IBakedModel originalModel, @Nonnull ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity )
|
||||||
{
|
{
|
||||||
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
|
ItemTurtle turtle = (ItemTurtle) stack.getItem();
|
||||||
int colour = turtle.getColour( stack );
|
int colour = turtle.getColour( stack );
|
||||||
ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
|
ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
|
||||||
ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
|
ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
|
||||||
@@ -138,15 +138,15 @@ public class TurtleSmartItemModel implements IBakedModel
|
|||||||
|
|
||||||
private IBakedModel buildModel( TurtleModelCombination combo )
|
private IBakedModel buildModel( TurtleModelCombination combo )
|
||||||
{
|
{
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
|
ModelManager modelManager = mc.getItemRenderer().getItemModelMesher().getModelManager();
|
||||||
ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_overlay, combo.m_christmas );
|
ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_overlay, combo.m_christmas );
|
||||||
|
|
||||||
IBakedModel baseModel = combo.m_colour ? colourModel : familyModel;
|
IBakedModel baseModel = combo.m_colour ? colourModel : familyModel;
|
||||||
IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( overlayModelLocation ) : null;
|
IBakedModel overlayModel = overlayModelLocation != null ? modelManager.getModel( overlayModelLocation ) : null;
|
||||||
Matrix4f transform = combo.m_flip ? s_flip : s_identity;
|
Matrix4f transform = combo.m_flip ? s_flip : s_identity;
|
||||||
Pair<IBakedModel, Matrix4f> leftModel = (combo.m_leftUpgrade != null) ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
|
Pair<IBakedModel, Matrix4f> leftModel = combo.m_leftUpgrade != null ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
|
||||||
Pair<IBakedModel, Matrix4f> rightModel = (combo.m_rightUpgrade != null) ? combo.m_rightUpgrade.getModel( null, TurtleSide.Right ) : null;
|
Pair<IBakedModel, Matrix4f> rightModel = combo.m_rightUpgrade != null ? combo.m_rightUpgrade.getModel( null, TurtleSide.Right ) : null;
|
||||||
if( leftModel != null && rightModel != null )
|
if( leftModel != null && rightModel != null )
|
||||||
{
|
{
|
||||||
return new TurtleMultiModel( baseModel, overlayModel, transform, leftModel.getLeft(), leftModel.getRight(), rightModel.getLeft(), rightModel.getRight() );
|
return new TurtleMultiModel( baseModel, overlayModel, transform, leftModel.getLeft(), leftModel.getRight(), rightModel.getLeft(), rightModel.getRight() );
|
||||||
@@ -167,7 +167,7 @@ public class TurtleSmartItemModel implements IBakedModel
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public List<BakedQuad> getQuads( IBlockState state, EnumFacing facing, long rand )
|
public List<BakedQuad> getQuads( IBlockState state, EnumFacing facing, @Nonnull Random rand )
|
||||||
{
|
{
|
||||||
return familyModel.getQuads( state, facing, rand );
|
return familyModel.getQuads( state, facing, rand );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import dan200.computercraft.ComputerCraft;
|
|||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ import java.util.regex.Pattern;
|
|||||||
*/
|
*/
|
||||||
public class AddressPredicate
|
public class AddressPredicate
|
||||||
{
|
{
|
||||||
private static class HostRange
|
private static final class HostRange
|
||||||
{
|
{
|
||||||
private final byte[] min;
|
private final byte[] min;
|
||||||
private final byte[] max;
|
private final byte[] max;
|
||||||
@@ -50,6 +51,11 @@ public class AddressPredicate
|
|||||||
private final List<HostRange> ranges;
|
private final List<HostRange> ranges;
|
||||||
|
|
||||||
public AddressPredicate( String... filters )
|
public AddressPredicate( String... filters )
|
||||||
|
{
|
||||||
|
this( Arrays.asList( filters ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public AddressPredicate( Iterable<? extends String> filters )
|
||||||
{
|
{
|
||||||
List<Pattern> wildcards = this.wildcards = new ArrayList<>();
|
List<Pattern> wildcards = this.wildcards = new ArrayList<>();
|
||||||
List<HostRange> ranges = this.ranges = new ArrayList<>();
|
List<HostRange> ranges = this.ranges = new ArrayList<>();
|
||||||
@@ -69,7 +75,10 @@ public class AddressPredicate
|
|||||||
}
|
}
|
||||||
catch( NumberFormatException e )
|
catch( NumberFormatException e )
|
||||||
{
|
{
|
||||||
ComputerCraft.log.warn( "Cannot parse CIDR size from {} ({})", filter, prefixSizeStr );
|
ComputerCraft.log.error(
|
||||||
|
"Malformed http whitelist/blacklist entry '{}': Cannot extract size of CIDR mask from '{}'.",
|
||||||
|
filter, prefixSizeStr
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +89,10 @@ public class AddressPredicate
|
|||||||
}
|
}
|
||||||
catch( IllegalArgumentException e )
|
catch( IllegalArgumentException e )
|
||||||
{
|
{
|
||||||
ComputerCraft.log.warn( "Cannot parse IP address from {} ({})", filter, addressStr );
|
ComputerCraft.log.error(
|
||||||
|
"Malformed http whitelist/blacklist entry '{}': Cannot extract IP address from '{}'.",
|
||||||
|
filter, prefixSizeStr
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,13 @@
|
|||||||
|
|
||||||
package dan200.computercraft.core.apis;
|
package dan200.computercraft.core.apis;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public final class ApiFactories
|
public final class ApiFactories
|
||||||
{
|
{
|
||||||
@@ -23,9 +23,9 @@ public final class ApiFactories
|
|||||||
private static final Collection<ILuaAPIFactory> factories = new LinkedHashSet<>();
|
private static final Collection<ILuaAPIFactory> factories = new LinkedHashSet<>();
|
||||||
private static final Collection<ILuaAPIFactory> factoriesView = Collections.unmodifiableCollection( factories );
|
private static final Collection<ILuaAPIFactory> factoriesView = Collections.unmodifiableCollection( factories );
|
||||||
|
|
||||||
public static void register( @Nonnull ILuaAPIFactory factory )
|
public static synchronized void register( @Nonnull ILuaAPIFactory factory )
|
||||||
{
|
{
|
||||||
Preconditions.checkNotNull( factory, "provider cannot be null" );
|
Objects.requireNonNull( factory, "provider cannot be null" );
|
||||||
factories.add( factory );
|
factories.add( factory );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,10 @@
|
|||||||
|
|
||||||
package dan200.computercraft.core.apis;
|
package dan200.computercraft.core.apis;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import dan200.computercraft.api.filesystem.IMount;
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
import dan200.computercraft.core.computer.Computer;
|
import dan200.computercraft.api.peripheral.IWorkMonitor;
|
||||||
import dan200.computercraft.core.computer.IComputerOwned;
|
|
||||||
import dan200.computercraft.core.filesystem.FileSystem;
|
import dan200.computercraft.core.filesystem.FileSystem;
|
||||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||||
|
|
||||||
@@ -21,22 +19,22 @@ import java.util.HashSet;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
|
public abstract class ComputerAccess implements IComputerAccess
|
||||||
{
|
{
|
||||||
private final IAPIEnvironment m_environment;
|
private final IAPIEnvironment m_environment;
|
||||||
private final Set<String> m_mounts = new HashSet<>();
|
private final Set<String> m_mounts = new HashSet<>();
|
||||||
|
|
||||||
protected ComputerAccess( IAPIEnvironment m_environment )
|
protected ComputerAccess( IAPIEnvironment environment )
|
||||||
{
|
{
|
||||||
this.m_environment = m_environment;
|
this.m_environment = environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unmountAll()
|
public void unmountAll()
|
||||||
{
|
{
|
||||||
FileSystem fileSystem = m_environment.getFileSystem();
|
FileSystem fileSystem = m_environment.getFileSystem();
|
||||||
for( String m_mount : m_mounts )
|
for( String mount : m_mounts )
|
||||||
{
|
{
|
||||||
fileSystem.unmount( m_mount );
|
fileSystem.unmount( mount );
|
||||||
}
|
}
|
||||||
m_mounts.clear();
|
m_mounts.clear();
|
||||||
}
|
}
|
||||||
@@ -122,15 +120,15 @@ public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
|
|||||||
@Override
|
@Override
|
||||||
public void queueEvent( @Nonnull final String event, final Object[] arguments )
|
public void queueEvent( @Nonnull final String event, final Object[] arguments )
|
||||||
{
|
{
|
||||||
Preconditions.checkNotNull( event, "event cannot be null" );
|
Objects.requireNonNull( event, "event cannot be null" );
|
||||||
m_environment.queueEvent( event, arguments );
|
m_environment.queueEvent( event, arguments );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public Computer getComputer()
|
public IWorkMonitor getMainThreadMonitor()
|
||||||
{
|
{
|
||||||
return m_environment.getComputer();
|
return m_environment.getMainThreadMonitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String findFreeLocation( String desiredLoc )
|
private String findFreeLocation( String desiredLoc )
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ public class FSAPI implements ILuaAPI
|
|||||||
private IAPIEnvironment m_env;
|
private IAPIEnvironment m_env;
|
||||||
private FileSystem m_fileSystem;
|
private FileSystem m_fileSystem;
|
||||||
|
|
||||||
public FSAPI( IAPIEnvironment _env )
|
public FSAPI( IAPIEnvironment env )
|
||||||
{
|
{
|
||||||
m_env = _env;
|
m_env = env;
|
||||||
m_fileSystem = null;
|
m_fileSystem = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,10 +353,8 @@ public class FSAPI implements ILuaAPI
|
|||||||
return new Object[] { FileSystem.getDirectory( path ) };
|
return new Object[] { FileSystem.getDirectory( path ) };
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
assert false;
|
||||||
assert (false);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,9 +199,7 @@ public class HTTPAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,14 +207,14 @@ public class HTTPAPI implements ILuaAPI
|
|||||||
private static HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws LuaException
|
private static HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws LuaException
|
||||||
{
|
{
|
||||||
HttpHeaders headers = new DefaultHttpHeaders();
|
HttpHeaders headers = new DefaultHttpHeaders();
|
||||||
for( Object key : headerTable.keySet() )
|
for( Map.Entry<?, ?> entry : headerTable.entrySet() )
|
||||||
{
|
{
|
||||||
Object value = headerTable.get( key );
|
Object value = entry.getValue();
|
||||||
if( key instanceof String && value instanceof String )
|
if( entry.getKey() instanceof String && value instanceof String )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
headers.add( (String) key, value );
|
headers.add( (String) entry.getKey(), value );
|
||||||
}
|
}
|
||||||
catch( IllegalArgumentException e )
|
catch( IllegalArgumentException e )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,9 +7,9 @@
|
|||||||
package dan200.computercraft.core.apis;
|
package dan200.computercraft.core.apis;
|
||||||
|
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.core.computer.Computer;
|
import dan200.computercraft.api.peripheral.IWorkMonitor;
|
||||||
|
import dan200.computercraft.core.computer.ComputerSide;
|
||||||
import dan200.computercraft.core.computer.IComputerEnvironment;
|
import dan200.computercraft.core.computer.IComputerEnvironment;
|
||||||
import dan200.computercraft.core.computer.IComputerOwned;
|
|
||||||
import dan200.computercraft.core.filesystem.FileSystem;
|
import dan200.computercraft.core.filesystem.FileSystem;
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
import dan200.computercraft.core.tracking.TrackingField;
|
import dan200.computercraft.core.tracking.TrackingField;
|
||||||
@@ -17,28 +17,22 @@ import dan200.computercraft.core.tracking.TrackingField;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public interface IAPIEnvironment extends IComputerOwned
|
public interface IAPIEnvironment
|
||||||
{
|
{
|
||||||
String[] SIDE_NAMES = new String[] {
|
@FunctionalInterface
|
||||||
"bottom", "top", "back", "front", "right", "left",
|
|
||||||
};
|
|
||||||
|
|
||||||
int SIDE_COUNT = 6;
|
|
||||||
|
|
||||||
interface IPeripheralChangeListener
|
interface IPeripheralChangeListener
|
||||||
{
|
{
|
||||||
void onPeripheralChanged( int side, @Nullable IPeripheral newPeripheral );
|
void onPeripheralChanged( ComputerSide side, @Nullable IPeripheral newPeripheral );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
Computer getComputer();
|
|
||||||
|
|
||||||
int getComputerID();
|
int getComputerID();
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
IComputerEnvironment getComputerEnvironment();
|
IComputerEnvironment getComputerEnvironment();
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
IWorkMonitor getMainThreadMonitor();
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
Terminal getTerminal();
|
Terminal getTerminal();
|
||||||
|
|
||||||
@@ -50,22 +44,22 @@ public interface IAPIEnvironment extends IComputerOwned
|
|||||||
|
|
||||||
void queueEvent( String event, Object[] args );
|
void queueEvent( String event, Object[] args );
|
||||||
|
|
||||||
void setOutput( int side, int output );
|
void setOutput( ComputerSide side, int output );
|
||||||
|
|
||||||
int getOutput( int side );
|
int getOutput( ComputerSide side );
|
||||||
|
|
||||||
int getInput( int side );
|
int getInput( ComputerSide side );
|
||||||
|
|
||||||
void setBundledOutput( int side, int output );
|
void setBundledOutput( ComputerSide side, int output );
|
||||||
|
|
||||||
int getBundledOutput( int side );
|
int getBundledOutput( ComputerSide side );
|
||||||
|
|
||||||
int getBundledInput( int side );
|
int getBundledInput( ComputerSide side );
|
||||||
|
|
||||||
void setPeripheralChangeListener( @Nullable IPeripheralChangeListener listener );
|
void setPeripheralChangeListener( @Nullable IPeripheralChangeListener listener );
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
IPeripheral getPeripheral( int side );
|
IPeripheral getPeripheral( ComputerSide side );
|
||||||
|
|
||||||
String getLabel();
|
String getLabel();
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Alarm implements Comparable<Alarm>
|
private static class Alarm implements Comparable<Alarm>
|
||||||
{
|
{
|
||||||
public final double m_time;
|
public final double m_time;
|
||||||
public final int m_day;
|
public final int m_day;
|
||||||
@@ -110,7 +110,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
{
|
{
|
||||||
Map.Entry<Integer, Timer> entry = it.next();
|
Map.Entry<Integer, Timer> entry = it.next();
|
||||||
Timer timer = entry.getValue();
|
Timer timer = entry.getValue();
|
||||||
timer.m_ticksLeft = timer.m_ticksLeft - 1;
|
timer.m_ticksLeft--;
|
||||||
if( timer.m_ticksLeft <= 0 )
|
if( timer.m_ticksLeft <= 0 )
|
||||||
{
|
{
|
||||||
// Queue the "timer" event
|
// Queue the "timer" event
|
||||||
@@ -198,7 +198,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
|
|
||||||
private int getDayForCalendar( Calendar c )
|
private int getDayForCalendar( Calendar c )
|
||||||
{
|
{
|
||||||
GregorianCalendar g = (c instanceof GregorianCalendar) ? (GregorianCalendar) c : new GregorianCalendar();
|
GregorianCalendar g = c instanceof GregorianCalendar ? (GregorianCalendar) c : new GregorianCalendar();
|
||||||
int year = c.get( Calendar.YEAR );
|
int year = c.get( Calendar.YEAR );
|
||||||
int day = 0;
|
int day = 0;
|
||||||
for( int y = 1970; y < year; y++ )
|
for( int y = 1970; y < year; y++ )
|
||||||
@@ -219,12 +219,9 @@ public class OSAPI implements ILuaAPI
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // queueEvent
|
||||||
{
|
|
||||||
// queueEvent
|
|
||||||
queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) );
|
queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) );
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
// startTimer
|
// startTimer
|
||||||
@@ -245,29 +242,20 @@ public class OSAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
synchronized( m_alarms )
|
synchronized( m_alarms )
|
||||||
{
|
{
|
||||||
int day = (time > m_time) ? m_day : (m_day + 1);
|
int day = time > m_time ? m_day : m_day + 1;
|
||||||
m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) );
|
m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) );
|
||||||
return new Object[] { m_nextAlarmToken++ };
|
return new Object[] { m_nextAlarmToken++ };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 3:
|
case 3: // shutdown
|
||||||
{
|
|
||||||
// shutdown
|
|
||||||
m_apiEnvironment.shutdown();
|
m_apiEnvironment.shutdown();
|
||||||
return null;
|
return null;
|
||||||
}
|
case 4: // reboot
|
||||||
case 4:
|
|
||||||
{
|
|
||||||
// reboot
|
|
||||||
m_apiEnvironment.reboot();
|
m_apiEnvironment.reboot();
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
case 5:
|
case 5:
|
||||||
case 6:
|
case 6: // computerID/getComputerID
|
||||||
{
|
|
||||||
// computerID/getComputerID
|
|
||||||
return new Object[] { getComputerID() };
|
return new Object[] { getComputerID() };
|
||||||
}
|
|
||||||
case 7:
|
case 7:
|
||||||
{
|
{
|
||||||
// setComputerLabel
|
// setComputerLabel
|
||||||
@@ -286,19 +274,16 @@ public class OSAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 10:
|
case 10: // clock
|
||||||
{
|
|
||||||
// clock
|
|
||||||
synchronized( m_timers )
|
synchronized( m_timers )
|
||||||
{
|
{
|
||||||
return new Object[] { m_clock * 0.05 };
|
return new Object[] { m_clock * 0.05 };
|
||||||
}
|
}
|
||||||
}
|
|
||||||
case 11:
|
case 11:
|
||||||
{
|
{
|
||||||
// time
|
// time
|
||||||
String param = optString( args, 0, "ingame" );
|
String param = optString( args, 0, "ingame" );
|
||||||
switch( param )
|
switch( param.toLowerCase( Locale.ROOT ) )
|
||||||
{
|
{
|
||||||
case "utc":
|
case "utc":
|
||||||
{
|
{
|
||||||
@@ -326,7 +311,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
{
|
{
|
||||||
// day
|
// day
|
||||||
String param = optString( args, 0, "ingame" );
|
String param = optString( args, 0, "ingame" );
|
||||||
switch( param )
|
switch( param.toLowerCase( Locale.ROOT ) )
|
||||||
{
|
{
|
||||||
case "utc":
|
case "utc":
|
||||||
{
|
{
|
||||||
@@ -356,10 +341,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
int token = getInt( args, 0 );
|
int token = getInt( args, 0 );
|
||||||
synchronized( m_timers )
|
synchronized( m_timers )
|
||||||
{
|
{
|
||||||
if( m_timers.containsKey( token ) )
|
m_timers.remove( token );
|
||||||
{
|
|
||||||
m_timers.remove( token );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -369,10 +351,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
int token = getInt( args, 0 );
|
int token = getInt( args, 0 );
|
||||||
synchronized( m_alarms )
|
synchronized( m_alarms )
|
||||||
{
|
{
|
||||||
if( m_alarms.containsKey( token ) )
|
m_alarms.remove( token );
|
||||||
{
|
|
||||||
m_alarms.remove( token );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -380,7 +359,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
{
|
{
|
||||||
// epoch
|
// epoch
|
||||||
String param = optString( args, 0, "ingame" );
|
String param = optString( args, 0, "ingame" );
|
||||||
switch( param )
|
switch( param.toLowerCase( Locale.ROOT ) )
|
||||||
{
|
{
|
||||||
case "utc":
|
case "utc":
|
||||||
{
|
{
|
||||||
@@ -407,9 +386,7 @@ public class OSAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ import dan200.computercraft.api.lua.ILuaAPI;
|
|||||||
import dan200.computercraft.api.lua.ILuaContext;
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
import dan200.computercraft.api.lua.LuaException;
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.core.computer.Computer;
|
import dan200.computercraft.core.computer.ComputerSide;
|
||||||
import dan200.computercraft.core.computer.ComputerThread;
|
|
||||||
import dan200.computercraft.core.computer.ITask;
|
|
||||||
import dan200.computercraft.core.tracking.TrackingField;
|
import dan200.computercraft.core.tracking.TrackingField;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@@ -47,8 +45,8 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
|
|
||||||
m_type = peripheral.getType();
|
m_type = peripheral.getType();
|
||||||
m_methods = peripheral.getMethodNames();
|
m_methods = peripheral.getMethodNames();
|
||||||
assert (m_type != null);
|
assert m_type != null;
|
||||||
assert (m_methods != null);
|
assert m_methods != null;
|
||||||
|
|
||||||
m_methodMap = new HashMap<>();
|
m_methodMap = new HashMap<>();
|
||||||
for( int i = 0; i < m_methods.length; i++ )
|
for( int i = 0; i < m_methods.length; i++ )
|
||||||
@@ -231,9 +229,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
private final PeripheralWrapper[] m_peripherals;
|
private final PeripheralWrapper[] m_peripherals;
|
||||||
private boolean m_running;
|
private boolean m_running;
|
||||||
|
|
||||||
public PeripheralAPI( IAPIEnvironment _environment )
|
public PeripheralAPI( IAPIEnvironment environment )
|
||||||
{
|
{
|
||||||
m_environment = _environment;
|
m_environment = environment;
|
||||||
m_environment.setPeripheralChangeListener( this );
|
m_environment.setPeripheralChangeListener( this );
|
||||||
|
|
||||||
m_peripherals = new PeripheralWrapper[6];
|
m_peripherals = new PeripheralWrapper[6];
|
||||||
@@ -248,76 +246,33 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
// IPeripheralChangeListener
|
// IPeripheralChangeListener
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPeripheralChanged( int side, IPeripheral newPeripheral )
|
public void onPeripheralChanged( ComputerSide side, IPeripheral newPeripheral )
|
||||||
{
|
{
|
||||||
synchronized( m_peripherals )
|
synchronized( m_peripherals )
|
||||||
{
|
{
|
||||||
if( m_peripherals[side] != null )
|
int index = side.ordinal();
|
||||||
|
if( m_peripherals[index] != null )
|
||||||
{
|
{
|
||||||
// Queue a detachment
|
// Queue a detachment
|
||||||
final PeripheralWrapper wrapper = m_peripherals[side];
|
final PeripheralWrapper wrapper = m_peripherals[index];
|
||||||
ComputerThread.queueTask( new ITask()
|
if( wrapper.isAttached() ) wrapper.detach();
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public Computer getOwner()
|
|
||||||
{
|
|
||||||
return m_environment.getComputer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute()
|
|
||||||
{
|
|
||||||
synchronized( m_peripherals )
|
|
||||||
{
|
|
||||||
if( wrapper.isAttached() )
|
|
||||||
{
|
|
||||||
wrapper.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, null );
|
|
||||||
|
|
||||||
// Queue a detachment event
|
// Queue a detachment event
|
||||||
m_environment.queueEvent( "peripheral_detach", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } );
|
m_environment.queueEvent( "peripheral_detach", new Object[] { side.getName() } );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign the new peripheral
|
// Assign the new peripheral
|
||||||
if( newPeripheral != null )
|
m_peripherals[index] = newPeripheral == null ? null
|
||||||
{
|
: new PeripheralWrapper( newPeripheral, side.getName() );
|
||||||
m_peripherals[side] = new PeripheralWrapper( newPeripheral, IAPIEnvironment.SIDE_NAMES[side] );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_peripherals[side] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( m_peripherals[side] != null )
|
if( m_peripherals[index] != null )
|
||||||
{
|
{
|
||||||
// Queue an attachment
|
// Queue an attachment
|
||||||
final PeripheralWrapper wrapper = m_peripherals[side];
|
final PeripheralWrapper wrapper = m_peripherals[index];
|
||||||
ComputerThread.queueTask( new ITask()
|
if( m_running && !wrapper.isAttached() ) wrapper.attach();
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public Computer getOwner()
|
|
||||||
{
|
|
||||||
return m_environment.getComputer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute()
|
|
||||||
{
|
|
||||||
synchronized( m_peripherals )
|
|
||||||
{
|
|
||||||
if( m_running && !wrapper.isAttached() )
|
|
||||||
{
|
|
||||||
wrapper.attach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, null );
|
|
||||||
|
|
||||||
// Queue an attachment event
|
// Queue an attachment event
|
||||||
m_environment.queueEvent( "peripheral", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } );
|
m_environment.queueEvent( "peripheral", new Object[] { side.getName() } );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,10 +296,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
for( int i = 0; i < 6; i++ )
|
for( int i = 0; i < 6; i++ )
|
||||||
{
|
{
|
||||||
PeripheralWrapper wrapper = m_peripherals[i];
|
PeripheralWrapper wrapper = m_peripherals[i];
|
||||||
if( wrapper != null && !wrapper.isAttached() )
|
if( wrapper != null && !wrapper.isAttached() ) wrapper.attach();
|
||||||
{
|
|
||||||
wrapper.attach();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -387,16 +339,13 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
{
|
{
|
||||||
// isPresent
|
// isPresent
|
||||||
boolean present = false;
|
boolean present = false;
|
||||||
int side = parseSide( args );
|
ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) );
|
||||||
if( side >= 0 )
|
if( side != null )
|
||||||
{
|
{
|
||||||
synchronized( m_peripherals )
|
synchronized( m_peripherals )
|
||||||
{
|
{
|
||||||
PeripheralWrapper p = m_peripherals[side];
|
PeripheralWrapper p = m_peripherals[side.ordinal()];
|
||||||
if( p != null )
|
if( p != null ) present = true;
|
||||||
{
|
|
||||||
present = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Object[] { present };
|
return new Object[] { present };
|
||||||
@@ -404,21 +353,14 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
// getType
|
// getType
|
||||||
String type = null;
|
ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) );
|
||||||
int side = parseSide( args );
|
if( side != null )
|
||||||
if( side >= 0 )
|
|
||||||
{
|
{
|
||||||
|
String type = null;
|
||||||
synchronized( m_peripherals )
|
synchronized( m_peripherals )
|
||||||
{
|
{
|
||||||
PeripheralWrapper p = m_peripherals[side];
|
PeripheralWrapper p = m_peripherals[side.ordinal()];
|
||||||
if( p != null )
|
if( p != null ) return new Object[] { p.getType() };
|
||||||
{
|
|
||||||
type = p.getType();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( type != null )
|
|
||||||
{
|
|
||||||
return new Object[] { type };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -427,12 +369,12 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
{
|
{
|
||||||
// getMethods
|
// getMethods
|
||||||
String[] methods = null;
|
String[] methods = null;
|
||||||
int side = parseSide( args );
|
ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) );
|
||||||
if( side >= 0 )
|
if( side != null )
|
||||||
{
|
{
|
||||||
synchronized( m_peripherals )
|
synchronized( m_peripherals )
|
||||||
{
|
{
|
||||||
PeripheralWrapper p = m_peripherals[side];
|
PeripheralWrapper p = m_peripherals[side.ordinal()];
|
||||||
if( p != null )
|
if( p != null )
|
||||||
{
|
{
|
||||||
methods = p.getMethods();
|
methods = p.getMethods();
|
||||||
@@ -453,16 +395,16 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
case 3:
|
case 3:
|
||||||
{
|
{
|
||||||
// call
|
// call
|
||||||
int side = parseSide( args );
|
ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) );
|
||||||
String methodName = getString( args, 1 );
|
String methodName = getString( args, 1 );
|
||||||
Object[] methodArgs = Arrays.copyOfRange( args, 2, args.length );
|
Object[] methodArgs = Arrays.copyOfRange( args, 2, args.length );
|
||||||
|
|
||||||
if( side >= 0 )
|
if( side != null )
|
||||||
{
|
{
|
||||||
PeripheralWrapper p;
|
PeripheralWrapper p;
|
||||||
synchronized( m_peripherals )
|
synchronized( m_peripherals )
|
||||||
{
|
{
|
||||||
p = m_peripherals[side];
|
p = m_peripherals[side.ordinal()];
|
||||||
}
|
}
|
||||||
if( p != null )
|
if( p != null )
|
||||||
{
|
{
|
||||||
@@ -472,24 +414,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
|||||||
throw new LuaException( "No peripheral attached" );
|
throw new LuaException( "No peripheral attached" );
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Privates
|
|
||||||
|
|
||||||
private int parseSide( Object[] args ) throws LuaException
|
|
||||||
{
|
|
||||||
String side = getString( args, 0 );
|
|
||||||
for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ )
|
|
||||||
{
|
|
||||||
if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) )
|
|
||||||
{
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ package dan200.computercraft.core.apis;
|
|||||||
import dan200.computercraft.api.lua.ILuaAPI;
|
import dan200.computercraft.api.lua.ILuaAPI;
|
||||||
import dan200.computercraft.api.lua.ILuaContext;
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
import dan200.computercraft.api.lua.LuaException;
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
|
import dan200.computercraft.core.computer.ComputerSide;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -64,65 +65,49 @@ public class RedstoneAPI implements ILuaAPI
|
|||||||
{
|
{
|
||||||
// getSides
|
// getSides
|
||||||
Map<Object, Object> table = new HashMap<>();
|
Map<Object, Object> table = new HashMap<>();
|
||||||
for( int i = 0; i < IAPIEnvironment.SIDE_NAMES.length; i++ )
|
for( int i = 0; i < ComputerSide.NAMES.length; i++ )
|
||||||
{
|
{
|
||||||
table.put( i + 1, IAPIEnvironment.SIDE_NAMES[i] );
|
table.put( i + 1, ComputerSide.NAMES[i] );
|
||||||
}
|
}
|
||||||
return new Object[] { table };
|
return new Object[] { table };
|
||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
// setOutput
|
// setOutput
|
||||||
int side = parseSide( args );
|
ComputerSide side = parseSide( args );
|
||||||
boolean output = getBoolean( args, 1 );
|
boolean output = getBoolean( args, 1 );
|
||||||
m_environment.setOutput( side, output ? 15 : 0 );
|
m_environment.setOutput( side, output ? 15 : 0 );
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: // getOutput
|
||||||
{
|
return new Object[] { m_environment.getOutput( parseSide( args ) ) > 0 };
|
||||||
// getOutput
|
case 3: // getInput
|
||||||
int side = parseSide( args );
|
return new Object[] { m_environment.getInput( parseSide( args ) ) > 0 };
|
||||||
return new Object[] { m_environment.getOutput( side ) > 0 };
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
// getInput
|
|
||||||
int side = parseSide( args );
|
|
||||||
return new Object[] { m_environment.getInput( side ) > 0 };
|
|
||||||
}
|
|
||||||
case 4:
|
case 4:
|
||||||
{
|
{
|
||||||
// setBundledOutput
|
// setBundledOutput
|
||||||
int side = parseSide( args );
|
ComputerSide side = parseSide( args );
|
||||||
int output = getInt( args, 1 );
|
int output = getInt( args, 1 );
|
||||||
m_environment.setBundledOutput( side, output );
|
m_environment.setBundledOutput( side, output );
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 5:
|
case 5: // getBundledOutput
|
||||||
{
|
return new Object[] { m_environment.getBundledOutput( parseSide( args ) ) };
|
||||||
// getBundledOutput
|
case 6: // getBundledInput
|
||||||
int side = parseSide( args );
|
return new Object[] { m_environment.getBundledInput( parseSide( args ) ) };
|
||||||
return new Object[] { m_environment.getBundledOutput( side ) };
|
|
||||||
}
|
|
||||||
case 6:
|
|
||||||
{
|
|
||||||
// getBundledInput
|
|
||||||
int side = parseSide( args );
|
|
||||||
return new Object[] { m_environment.getBundledInput( side ) };
|
|
||||||
}
|
|
||||||
case 7:
|
case 7:
|
||||||
{
|
{
|
||||||
// testBundledInput
|
// testBundledInput
|
||||||
int side = parseSide( args );
|
ComputerSide side = parseSide( args );
|
||||||
int mask = getInt( args, 1 );
|
int mask = getInt( args, 1 );
|
||||||
int input = m_environment.getBundledInput( side );
|
int input = m_environment.getBundledInput( side );
|
||||||
return new Object[] { ((input & mask) == mask) };
|
return new Object[] { (input & mask) == mask };
|
||||||
}
|
}
|
||||||
case 8:
|
case 8:
|
||||||
case 9:
|
case 9:
|
||||||
{
|
{
|
||||||
// setAnalogOutput/setAnalogueOutput
|
// setAnalogOutput/setAnalogueOutput
|
||||||
int side = parseSide( args );
|
ComputerSide side = parseSide( args );
|
||||||
int output = getInt( args, 1 );
|
int output = getInt( args, 1 );
|
||||||
if( output < 0 || output > 15 )
|
if( output < 0 || output > 15 )
|
||||||
{
|
{
|
||||||
@@ -132,36 +117,20 @@ public class RedstoneAPI implements ILuaAPI
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 10:
|
case 10:
|
||||||
case 11:
|
case 11: // getAnalogOutput/getAnalogueOutput
|
||||||
{
|
return new Object[] { m_environment.getOutput( parseSide( args ) ) };
|
||||||
// getAnalogOutput/getAnalogueOutput
|
|
||||||
int side = parseSide( args );
|
|
||||||
return new Object[] { m_environment.getOutput( side ) };
|
|
||||||
}
|
|
||||||
case 12:
|
case 12:
|
||||||
case 13:
|
case 13: // getAnalogInput/getAnalogueInput
|
||||||
{
|
return new Object[] { m_environment.getInput( parseSide( args ) ) };
|
||||||
// getAnalogInput/getAnalogueInput
|
|
||||||
int side = parseSide( args );
|
|
||||||
return new Object[] { m_environment.getInput( side ) };
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int parseSide( Object[] args ) throws LuaException
|
private static ComputerSide parseSide( Object[] args ) throws LuaException
|
||||||
{
|
{
|
||||||
String side = getString( args, 0 );
|
ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) );
|
||||||
for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ )
|
if( side == null ) throw new LuaException( "Invalid side." );
|
||||||
{
|
return side;
|
||||||
if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) )
|
|
||||||
{
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new LuaException( "Invalid side." );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,10 @@ public class TermAPI implements ILuaAPI
|
|||||||
private final Terminal m_terminal;
|
private final Terminal m_terminal;
|
||||||
private final IComputerEnvironment m_environment;
|
private final IComputerEnvironment m_environment;
|
||||||
|
|
||||||
public TermAPI( IAPIEnvironment _environment )
|
public TermAPI( IAPIEnvironment environment )
|
||||||
{
|
{
|
||||||
m_terminal = _environment.getTerminal();
|
m_terminal = environment.getTerminal();
|
||||||
m_environment = _environment.getComputerEnvironment();
|
m_environment = environment.getComputerEnvironment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -111,16 +111,7 @@ public class TermAPI implements ILuaAPI
|
|||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
// write
|
// write
|
||||||
String text;
|
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
||||||
if( args.length > 0 && args[0] != null )
|
|
||||||
{
|
|
||||||
text = args[0].toString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized( m_terminal )
|
synchronized( m_terminal )
|
||||||
{
|
{
|
||||||
m_terminal.write( text );
|
m_terminal.write( text );
|
||||||
@@ -181,24 +172,18 @@ public class TermAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
return new Object[] { width, height };
|
return new Object[] { width, height };
|
||||||
}
|
}
|
||||||
case 6:
|
case 6: // clear
|
||||||
{
|
|
||||||
// clear
|
|
||||||
synchronized( m_terminal )
|
synchronized( m_terminal )
|
||||||
{
|
{
|
||||||
m_terminal.clear();
|
m_terminal.clear();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
case 7: // clearLine
|
||||||
case 7:
|
|
||||||
{
|
|
||||||
// clearLine
|
|
||||||
synchronized( m_terminal )
|
synchronized( m_terminal )
|
||||||
{
|
{
|
||||||
m_terminal.clearLine();
|
m_terminal.clearLine();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
case 8:
|
case 8:
|
||||||
case 9:
|
case 9:
|
||||||
{
|
{
|
||||||
@@ -222,23 +207,14 @@ public class TermAPI implements ILuaAPI
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 12:
|
case 12:
|
||||||
case 13:
|
case 13: // isColour/isColor
|
||||||
{
|
|
||||||
// isColour/isColor
|
|
||||||
return new Object[] { m_environment.isColour() };
|
return new Object[] { m_environment.isColour() };
|
||||||
}
|
|
||||||
case 14:
|
case 14:
|
||||||
case 15:
|
case 15: // getTextColour/getTextColor
|
||||||
{
|
|
||||||
// getTextColour/getTextColor
|
|
||||||
return encodeColour( m_terminal.getTextColour() );
|
return encodeColour( m_terminal.getTextColour() );
|
||||||
}
|
|
||||||
case 16:
|
case 16:
|
||||||
case 17:
|
case 17: // getBackgroundColour/getBackgroundColor
|
||||||
{
|
|
||||||
// getBackgroundColour/getBackgroundColor
|
|
||||||
return encodeColour( m_terminal.getBackgroundColour() );
|
return encodeColour( m_terminal.getBackgroundColour() );
|
||||||
}
|
|
||||||
case 18:
|
case 18:
|
||||||
{
|
{
|
||||||
// blit
|
// blit
|
||||||
@@ -308,9 +284,7 @@ public class TermAPI implements ILuaAPI
|
|||||||
// getCursorBlink
|
// getCursorBlink
|
||||||
return new Object[] { m_terminal.getCursorBlink() };
|
return new Object[] { m_terminal.getCursorBlink() };
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,11 @@
|
|||||||
|
|
||||||
package dan200.computercraft.core.apis.handles;
|
package dan200.computercraft.core.apis.handles;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.nio.channels.NonWritableChannelException;
|
import java.nio.channels.NonWritableChannelException;
|
||||||
import java.nio.channels.SeekableByteChannel;
|
import java.nio.channels.SeekableByteChannel;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A seekable, readable byte channel which is backed by a simple byte array.
|
* A seekable, readable byte channel which is backed by a simple byte array.
|
||||||
@@ -30,10 +28,10 @@ public class ArrayByteChannel implements SeekableByteChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read( ByteBuffer destination ) throws IOException
|
public int read( ByteBuffer destination ) throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
Preconditions.checkNotNull( destination, "destination" );
|
Objects.requireNonNull( destination, "destination" );
|
||||||
|
|
||||||
if( position >= backing.length ) return -1;
|
if( position >= backing.length ) return -1;
|
||||||
|
|
||||||
@@ -44,21 +42,21 @@ public class ArrayByteChannel implements SeekableByteChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int write( ByteBuffer src ) throws IOException
|
public int write( ByteBuffer src ) throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
throw new NonWritableChannelException();
|
throw new NonWritableChannelException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long position() throws IOException
|
public long position() throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SeekableByteChannel position( long newPosition ) throws IOException
|
public SeekableByteChannel position( long newPosition ) throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
if( newPosition < 0 || newPosition > Integer.MAX_VALUE )
|
if( newPosition < 0 || newPosition > Integer.MAX_VALUE )
|
||||||
@@ -70,14 +68,14 @@ public class ArrayByteChannel implements SeekableByteChannel
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long size() throws IOException
|
public long size() throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
return backing.length;
|
return backing.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SeekableByteChannel truncate( long size ) throws IOException
|
public SeekableByteChannel truncate( long size ) throws ClosedChannelException
|
||||||
{
|
{
|
||||||
if( closed ) throw new ClosedChannelException();
|
if( closed ) throw new ClosedChannelException();
|
||||||
throw new NonWritableChannelException();
|
throw new NonWritableChannelException();
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
public BinaryReadableHandle( ReadableByteChannel channel, Closeable closeable )
|
public BinaryReadableHandle( ReadableByteChannel channel, Closeable closeable )
|
||||||
{
|
{
|
||||||
super( closeable );
|
super( closeable );
|
||||||
this.m_reader = channel;
|
m_reader = channel;
|
||||||
this.m_seekable = asSeekable( channel );
|
m_seekable = asSeekable( channel );
|
||||||
}
|
}
|
||||||
|
|
||||||
public BinaryReadableHandle( ReadableByteChannel channel )
|
public BinaryReadableHandle( ReadableByteChannel channel )
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ public class BinaryWritableHandle extends HandleGeneric
|
|||||||
public BinaryWritableHandle( WritableByteChannel channel, Closeable closeable )
|
public BinaryWritableHandle( WritableByteChannel channel, Closeable closeable )
|
||||||
{
|
{
|
||||||
super( closeable );
|
super( closeable );
|
||||||
this.m_writer = channel;
|
m_writer = channel;
|
||||||
this.m_seekable = asSeekable( channel );
|
m_seekable = asSeekable( channel );
|
||||||
}
|
}
|
||||||
|
|
||||||
public BinaryWritableHandle( WritableByteChannel channel )
|
public BinaryWritableHandle( WritableByteChannel channel )
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
public EncodedReadableHandle( @Nonnull BufferedReader reader, @Nonnull Closeable closable )
|
public EncodedReadableHandle( @Nonnull BufferedReader reader, @Nonnull Closeable closable )
|
||||||
{
|
{
|
||||||
super( closable );
|
super( closable );
|
||||||
this.m_reader = reader;
|
m_reader = reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EncodedReadableHandle( @Nonnull BufferedReader reader )
|
public EncodedReadableHandle( @Nonnull BufferedReader reader )
|
||||||
@@ -84,7 +84,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StringBuilder result = new StringBuilder( "" );
|
StringBuilder result = new StringBuilder();
|
||||||
String line = m_reader.readLine();
|
String line = m_reader.readLine();
|
||||||
while( line != null )
|
while( line != null )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class EncodedWritableHandle extends HandleGeneric
|
|||||||
public EncodedWritableHandle( @Nonnull BufferedWriter writer, @Nonnull Closeable closable )
|
public EncodedWritableHandle( @Nonnull BufferedWriter writer, @Nonnull Closeable closable )
|
||||||
{
|
{
|
||||||
super( closable );
|
super( closable );
|
||||||
this.m_writer = writer;
|
m_writer = writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EncodedWritableHandle( @Nonnull BufferedWriter writer )
|
public EncodedWritableHandle( @Nonnull BufferedWriter writer )
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public abstract class HandleGeneric implements ILuaObject
|
|||||||
|
|
||||||
protected HandleGeneric( @Nonnull Closeable closable )
|
protected HandleGeneric( @Nonnull Closeable closable )
|
||||||
{
|
{
|
||||||
this.m_closable = closable;
|
m_closable = closable;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void checkOpen() throws LuaException
|
protected void checkOpen() throws LuaException
|
||||||
@@ -46,7 +46,7 @@ public abstract class HandleGeneric implements ILuaObject
|
|||||||
*
|
*
|
||||||
* @param channel The channel to seek in
|
* @param channel The channel to seek in
|
||||||
* @param args The Lua arguments to process, like Lua's {@code file:seek}.
|
* @param args The Lua arguments to process, like Lua's {@code file:seek}.
|
||||||
* @return The new position of the file, or null if some error occured.
|
* @return The new position of the file, or null if some error occurred.
|
||||||
* @throws LuaException If the arguments were invalid
|
* @throws LuaException If the arguments were invalid
|
||||||
* @see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-file:seek">{@code file:seek} in the Lua manual.</a>
|
* @see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-file:seek">{@code file:seek} in the Lua manual.</a>
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class CheckUrl extends Resource<CheckUrl>
|
|||||||
super( limiter );
|
super( limiter );
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.host = uri.getHost();
|
host = uri.getHost();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
|
|||||||
@@ -140,6 +140,6 @@ public abstract class Resource<T extends Resource<T>> implements Closeable
|
|||||||
public static void cleanup()
|
public static void cleanup()
|
||||||
{
|
{
|
||||||
Reference<?> reference;
|
Reference<?> reference;
|
||||||
while( (reference = QUEUE.poll()) != null ) ((CloseReference) reference).resource.close();
|
while( (reference = QUEUE.poll()) != null ) ((CloseReference<?>) reference).resource.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class ResourceGroup<T extends Resource<T>>
|
|||||||
|
|
||||||
public ResourceGroup()
|
public ResourceGroup()
|
||||||
{
|
{
|
||||||
this.limit = ZERO;
|
limit = ZERO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startup()
|
public void startup()
|
||||||
|
|||||||
@@ -67,12 +67,12 @@ public class HttpRequest extends Resource<HttpRequest>
|
|||||||
super( limiter );
|
super( limiter );
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.postBuffer = postText != null
|
postBuffer = postText != null
|
||||||
? Unpooled.wrappedBuffer( postText.getBytes( StandardCharsets.UTF_8 ) )
|
? Unpooled.wrappedBuffer( postText.getBytes( StandardCharsets.UTF_8 ) )
|
||||||
: Unpooled.buffer( 0 );
|
: Unpooled.buffer( 0 );
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
this.binary = binary;
|
this.binary = binary;
|
||||||
this.redirects = new AtomicInteger( followRedirects ? MAX_REDIRECTS : 0 );
|
redirects = new AtomicInteger( followRedirects ? MAX_REDIRECTS : 0 );
|
||||||
|
|
||||||
if( postText != null )
|
if( postText != null )
|
||||||
{
|
{
|
||||||
@@ -113,6 +113,7 @@ public class HttpRequest extends Resource<HttpRequest>
|
|||||||
{
|
{
|
||||||
// Validate the URL
|
// Validate the URL
|
||||||
if( url.getScheme() == null ) throw new HTTPRequestException( "Must specify http or https" );
|
if( url.getScheme() == null ) throw new HTTPRequestException( "Must specify http or https" );
|
||||||
|
if( url.getHost() == null ) throw new HTTPRequestException( "URL malformed" );
|
||||||
|
|
||||||
String scheme = url.getScheme().toLowerCase( Locale.ROOT );
|
String scheme = url.getScheme().toLowerCase( Locale.ROOT );
|
||||||
if( !scheme.equalsIgnoreCase( "http" ) && !scheme.equalsIgnoreCase( "https" ) )
|
if( !scheme.equalsIgnoreCase( "http" ) && !scheme.equalsIgnoreCase( "https" ) )
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb
|
|||||||
/**
|
/**
|
||||||
* Same as {@link io.netty.handler.codec.MessageAggregator}.
|
* Same as {@link io.netty.handler.codec.MessageAggregator}.
|
||||||
*/
|
*/
|
||||||
private static final int DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS = 1024;
|
private static final int DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS = 1024;
|
||||||
|
|
||||||
private static final byte[] EMPTY_BYTES = new byte[0];
|
private static final byte[] EMPTY_BYTES = new byte[0];
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb
|
|||||||
|
|
||||||
if( responseBody == null )
|
if( responseBody == null )
|
||||||
{
|
{
|
||||||
responseBody = ctx.alloc().compositeBuffer( DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS );
|
responseBody = ctx.alloc().compositeBuffer( DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS );
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf partial = content.content();
|
ByteBuf partial = content.content();
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ public class Websocket extends Resource<Websocket>
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
uri = new URI( "ws://" + uri.toString() );
|
uri = new URI( "ws://" + uri );
|
||||||
}
|
}
|
||||||
catch( URISyntaxException e )
|
catch( URISyntaxException e )
|
||||||
{
|
{
|
||||||
@@ -186,7 +186,7 @@ public class Websocket extends Resource<Websocket>
|
|||||||
|
|
||||||
WebsocketHandle handle = new WebsocketHandle( this, channel );
|
WebsocketHandle handle = new WebsocketHandle( this, channel );
|
||||||
environment().queueEvent( SUCCESS_EVENT, new Object[] { address, handle } );
|
environment().queueEvent( SUCCESS_EVENT, new Object[] { address, handle } );
|
||||||
this.websocketHandle = createOwnerReference( handle );
|
websocketHandle = createOwnerReference( handle );
|
||||||
|
|
||||||
checkClosed();
|
checkClosed();
|
||||||
}
|
}
|
||||||
@@ -216,7 +216,7 @@ public class Websocket extends Resource<Websocket>
|
|||||||
executorFuture = closeFuture( executorFuture );
|
executorFuture = closeFuture( executorFuture );
|
||||||
connectFuture = closeChannel( connectFuture );
|
connectFuture = closeChannel( connectFuture );
|
||||||
|
|
||||||
WeakReference<WebsocketHandle> websocketHandleRef = this.websocketHandle;
|
WeakReference<WebsocketHandle> websocketHandleRef = websocketHandle;
|
||||||
WebsocketHandle websocketHandle = websocketHandleRef == null ? null : websocketHandleRef.get();
|
WebsocketHandle websocketHandle = websocketHandleRef == null ? null : websocketHandleRef.get();
|
||||||
if( websocketHandle != null ) IoUtil.closeQuietly( websocketHandle );
|
if( websocketHandle != null ) IoUtil.closeQuietly( websocketHandle );
|
||||||
this.websocketHandle = null;
|
this.websocketHandle = null;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import javax.annotation.Nullable;
|
|||||||
/**
|
/**
|
||||||
* A wrapper for {@link ILuaAPI}s which cleans up after a {@link ComputerSystem} when the computer is shutdown.
|
* A wrapper for {@link ILuaAPI}s which cleans up after a {@link ComputerSystem} when the computer is shutdown.
|
||||||
*/
|
*/
|
||||||
public class ApiWrapper implements ILuaAPI
|
final class ApiWrapper implements ILuaAPI
|
||||||
{
|
{
|
||||||
private final ILuaAPI delegate;
|
private final ILuaAPI delegate;
|
||||||
private final ComputerSystem system;
|
private final ComputerSystem system;
|
||||||
|
|||||||
@@ -7,54 +7,48 @@
|
|||||||
package dan200.computercraft.core.computer;
|
package dan200.computercraft.core.computer;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import dan200.computercraft.ComputerCraft;
|
|
||||||
import dan200.computercraft.api.filesystem.IMount;
|
|
||||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
|
||||||
import dan200.computercraft.api.lua.ILuaAPI;
|
import dan200.computercraft.api.lua.ILuaAPI;
|
||||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
import dan200.computercraft.api.peripheral.IWorkMonitor;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||||
import dan200.computercraft.core.apis.*;
|
|
||||||
import dan200.computercraft.core.filesystem.FileSystem;
|
import dan200.computercraft.core.filesystem.FileSystem;
|
||||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
|
||||||
import dan200.computercraft.core.lua.CobaltLuaMachine;
|
|
||||||
import dan200.computercraft.core.lua.ILuaMachine;
|
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a computer which may exist in-world or elsewhere.
|
||||||
|
*
|
||||||
|
* Note, this class has several (read: far, far too many) responsibilities, so can get a little unwieldy at times.
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>Updates the {@link Environment}.</li>
|
||||||
|
* <li>Keeps track of whether the computer is on and blinking.</li>
|
||||||
|
* <li>Monitors whether the computer's visible state (redstone, on/off/blinking) has changed.</li>
|
||||||
|
* <li>Passes commands and events to the {@link ComputerExecutor}.</li>
|
||||||
|
* <li>Passes main thread tasks to the {@link MainThreadExecutor}.</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
public class Computer
|
public class Computer
|
||||||
{
|
{
|
||||||
private enum State
|
private static final int START_DELAY = 50;
|
||||||
{
|
|
||||||
Off,
|
|
||||||
Starting,
|
|
||||||
Running,
|
|
||||||
Stopping,
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IMount s_romMount = null;
|
|
||||||
|
|
||||||
|
// Various properties of the computer
|
||||||
private int m_id;
|
private int m_id;
|
||||||
private String m_label = null;
|
private String m_label = null;
|
||||||
|
|
||||||
|
// Read-only fields about the computer
|
||||||
private final IComputerEnvironment m_environment;
|
private final IComputerEnvironment m_environment;
|
||||||
|
|
||||||
private int m_ticksSinceStart = -1;
|
|
||||||
private boolean m_startRequested = false;
|
|
||||||
private State m_state = State.Off;
|
|
||||||
private boolean m_blinking = false;
|
|
||||||
|
|
||||||
private ILuaMachine m_machine = null;
|
|
||||||
private final List<ILuaAPI> m_apis = new ArrayList<>();
|
|
||||||
private final Environment m_internalEnvironment = new Environment( this );
|
|
||||||
|
|
||||||
private final Terminal m_terminal;
|
private final Terminal m_terminal;
|
||||||
private FileSystem m_fileSystem = null;
|
private final ComputerExecutor executor;
|
||||||
private IWritableMount m_rootMount = null;
|
private final MainThreadExecutor serverExecutor;
|
||||||
|
|
||||||
private boolean m_externalOutputChanged;
|
// Additional state about the computer and its environment.
|
||||||
|
private boolean m_blinking = false;
|
||||||
|
private final Environment internalEnvironment = new Environment( this );
|
||||||
|
private AtomicBoolean externalOutputChanged = new AtomicBoolean();
|
||||||
|
|
||||||
|
private boolean startRequested;
|
||||||
|
private int m_ticksSinceStart = -1;
|
||||||
|
|
||||||
public Computer( IComputerEnvironment environment, Terminal terminal, int id )
|
public Computer( IComputerEnvironment environment, Terminal terminal, int id )
|
||||||
{
|
{
|
||||||
@@ -62,24 +56,8 @@ public class Computer
|
|||||||
m_environment = environment;
|
m_environment = environment;
|
||||||
m_terminal = terminal;
|
m_terminal = terminal;
|
||||||
|
|
||||||
// Ensure the computer thread is running as required.
|
executor = new ComputerExecutor( this );
|
||||||
ComputerThread.start();
|
serverExecutor = new MainThreadExecutor( this );
|
||||||
|
|
||||||
// Add all default APIs to the loaded list.
|
|
||||||
m_apis.add( new TermAPI( m_internalEnvironment ) );
|
|
||||||
m_apis.add( new RedstoneAPI( m_internalEnvironment ) );
|
|
||||||
m_apis.add( new FSAPI( m_internalEnvironment ) );
|
|
||||||
m_apis.add( new PeripheralAPI( m_internalEnvironment ) );
|
|
||||||
m_apis.add( new OSAPI( m_internalEnvironment ) );
|
|
||||||
if( ComputerCraft.http_enable ) m_apis.add( new HTTPAPI( m_internalEnvironment ) );
|
|
||||||
|
|
||||||
// Load in the API registered APIs.
|
|
||||||
for( ILuaAPIFactory factory : ApiFactories.getAll() )
|
|
||||||
{
|
|
||||||
ComputerSystem system = new ComputerSystem( m_internalEnvironment );
|
|
||||||
ILuaAPI api = factory.create( system );
|
|
||||||
if( api != null ) m_apis.add( new ApiWrapper( api, system ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IComputerEnvironment getComputerEnvironment()
|
IComputerEnvironment getComputerEnvironment()
|
||||||
@@ -89,7 +67,7 @@ public class Computer
|
|||||||
|
|
||||||
FileSystem getFileSystem()
|
FileSystem getFileSystem()
|
||||||
{
|
{
|
||||||
return m_fileSystem;
|
return executor.getFileSystem();
|
||||||
}
|
}
|
||||||
|
|
||||||
Terminal getTerminal()
|
Terminal getTerminal()
|
||||||
@@ -99,58 +77,58 @@ public class Computer
|
|||||||
|
|
||||||
public Environment getEnvironment()
|
public Environment getEnvironment()
|
||||||
{
|
{
|
||||||
return m_internalEnvironment;
|
return internalEnvironment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAPIEnvironment getAPIEnvironment()
|
public IAPIEnvironment getAPIEnvironment()
|
||||||
{
|
{
|
||||||
return m_internalEnvironment;
|
return internalEnvironment;
|
||||||
}
|
|
||||||
|
|
||||||
public void turnOn()
|
|
||||||
{
|
|
||||||
if( m_state == State.Off ) m_startRequested = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void shutdown()
|
|
||||||
{
|
|
||||||
stopComputer( false );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reboot()
|
|
||||||
{
|
|
||||||
stopComputer( true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOn()
|
public boolean isOn()
|
||||||
{
|
{
|
||||||
synchronized( this )
|
return executor.isOn();
|
||||||
{
|
|
||||||
return m_state == State.Running;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void abort( boolean hard )
|
public void turnOn()
|
||||||
{
|
{
|
||||||
synchronized( this )
|
startRequested = true;
|
||||||
{
|
}
|
||||||
if( m_state != State.Off && m_machine != null )
|
|
||||||
{
|
public void shutdown()
|
||||||
if( hard )
|
{
|
||||||
{
|
executor.queueStop( false, false );
|
||||||
m_machine.hardAbort( "Too long without yielding" );
|
}
|
||||||
}
|
|
||||||
else
|
public void reboot()
|
||||||
{
|
{
|
||||||
m_machine.softAbort( "Too long without yielding" );
|
executor.queueStop( true, false );
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unload()
|
public void unload()
|
||||||
{
|
{
|
||||||
stopComputer( false );
|
executor.queueStop( false, true );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void queueEvent( String event, Object[] args )
|
||||||
|
{
|
||||||
|
executor.queueEvent( event, args );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queue a task to be run on the main thread, using {@link MainThread}.
|
||||||
|
*
|
||||||
|
* @param runnable The task to run
|
||||||
|
* @return If the task was successfully queued (namely, whether there is space on it).
|
||||||
|
*/
|
||||||
|
public boolean queueMainThread( Runnable runnable )
|
||||||
|
{
|
||||||
|
return serverExecutor.enqueue( runnable );
|
||||||
|
}
|
||||||
|
|
||||||
|
public IWorkMonitor getMainThreadMonitor()
|
||||||
|
{
|
||||||
|
return serverExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getID()
|
public int getID()
|
||||||
@@ -182,59 +160,52 @@ public class Computer
|
|||||||
if( !Objects.equal( label, m_label ) )
|
if( !Objects.equal( label, m_label ) )
|
||||||
{
|
{
|
||||||
m_label = label;
|
m_label = label;
|
||||||
m_externalOutputChanged = true;
|
externalOutputChanged.set( true );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void advance()
|
public void tick()
|
||||||
{
|
{
|
||||||
synchronized( this )
|
// We keep track of the number of ticks since the last start, only
|
||||||
|
if( m_ticksSinceStart >= 0 && m_ticksSinceStart <= START_DELAY ) m_ticksSinceStart++;
|
||||||
|
|
||||||
|
if( startRequested && (m_ticksSinceStart < 0 || m_ticksSinceStart > START_DELAY) )
|
||||||
{
|
{
|
||||||
// Start after a number of ticks
|
startRequested = false;
|
||||||
if( m_ticksSinceStart >= 0 )
|
if( !executor.isOn() )
|
||||||
{
|
{
|
||||||
m_ticksSinceStart++;
|
m_ticksSinceStart = 0;
|
||||||
}
|
executor.queueStart();
|
||||||
if( m_startRequested && (m_ticksSinceStart < 0 || m_ticksSinceStart > 50) )
|
|
||||||
{
|
|
||||||
startComputer();
|
|
||||||
m_startRequested = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( m_state == State.Running )
|
|
||||||
{
|
|
||||||
// Update the environment's internal state.
|
|
||||||
m_internalEnvironment.update();
|
|
||||||
|
|
||||||
// Advance our APIs
|
|
||||||
for( ILuaAPI api : m_apis ) api.update();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare to propagate the environment's output to the world.
|
executor.tick();
|
||||||
if( m_internalEnvironment.updateOutput() ) m_externalOutputChanged = true;
|
|
||||||
|
// Update the environment's internal state.
|
||||||
|
internalEnvironment.update();
|
||||||
|
|
||||||
|
// Propagate the environment's output to the world.
|
||||||
|
if( internalEnvironment.updateOutput() ) externalOutputChanged.set( true );
|
||||||
|
|
||||||
// Set output changed if the terminal has changed from blinking to not
|
// Set output changed if the terminal has changed from blinking to not
|
||||||
boolean blinking =
|
boolean blinking = m_terminal.getCursorBlink() &&
|
||||||
m_terminal.getCursorBlink() &&
|
m_terminal.getCursorX() >= 0 && m_terminal.getCursorX() < m_terminal.getWidth() &&
|
||||||
m_terminal.getCursorX() >= 0 && m_terminal.getCursorX() < m_terminal.getWidth() &&
|
m_terminal.getCursorY() >= 0 && m_terminal.getCursorY() < m_terminal.getHeight();
|
||||||
m_terminal.getCursorY() >= 0 && m_terminal.getCursorY() < m_terminal.getHeight();
|
|
||||||
|
|
||||||
if( blinking != m_blinking )
|
if( blinking != m_blinking )
|
||||||
{
|
{
|
||||||
m_blinking = blinking;
|
m_blinking = blinking;
|
||||||
m_externalOutputChanged = true;
|
externalOutputChanged.set( true );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void markChanged()
|
||||||
|
{
|
||||||
|
externalOutputChanged.set( true );
|
||||||
|
}
|
||||||
|
|
||||||
public boolean pollAndResetChanged()
|
public boolean pollAndResetChanged()
|
||||||
{
|
{
|
||||||
synchronized( this )
|
return externalOutputChanged.getAndSet( false );
|
||||||
{
|
|
||||||
boolean changed = m_externalOutputChanged;
|
|
||||||
m_externalOutputChanged = false;
|
|
||||||
return changed;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBlinking()
|
public boolean isBlinking()
|
||||||
@@ -242,334 +213,8 @@ public class Computer
|
|||||||
return isOn() && m_blinking;
|
return isOn() && m_blinking;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IWritableMount getRootMount()
|
public void addApi( ILuaAPI api )
|
||||||
{
|
{
|
||||||
if( m_rootMount == null )
|
executor.addApi( api );
|
||||||
{
|
|
||||||
m_rootMount = m_environment.createSaveDirMount( "computer/" + assignID(), m_environment.getComputerSpaceLimit() );
|
|
||||||
}
|
|
||||||
return m_rootMount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileSystem
|
|
||||||
|
|
||||||
private boolean initFileSystem()
|
|
||||||
{
|
|
||||||
// Create the file system
|
|
||||||
assignID();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_fileSystem = new FileSystem( "hdd", getRootMount() );
|
|
||||||
if( s_romMount == null ) s_romMount = m_environment.createResourceMount( "computercraft", "lua/rom" );
|
|
||||||
if( s_romMount != null )
|
|
||||||
{
|
|
||||||
m_fileSystem.mount( "rom", "rom", s_romMount );
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch( FileSystemException e )
|
|
||||||
{
|
|
||||||
ComputerCraft.log.error( "Cannot mount rom", e );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peripherals
|
|
||||||
|
|
||||||
public void addAPI( ILuaAPI api )
|
|
||||||
{
|
|
||||||
m_apis.add( api );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lua
|
|
||||||
|
|
||||||
private void initLua()
|
|
||||||
{
|
|
||||||
// Create the lua machine
|
|
||||||
ILuaMachine machine = new CobaltLuaMachine( this );
|
|
||||||
|
|
||||||
// Add the APIs
|
|
||||||
for( ILuaAPI api : m_apis )
|
|
||||||
{
|
|
||||||
machine.addAPI( api );
|
|
||||||
api.startup();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the bios resource
|
|
||||||
InputStream biosStream;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
biosStream = m_environment.createResourceFile( "computercraft", "lua/bios.lua" );
|
|
||||||
}
|
|
||||||
catch( Exception e )
|
|
||||||
{
|
|
||||||
biosStream = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the machine running the bios resource
|
|
||||||
if( biosStream != null )
|
|
||||||
{
|
|
||||||
machine.loadBios( biosStream );
|
|
||||||
try
|
|
||||||
{
|
|
||||||
biosStream.close();
|
|
||||||
}
|
|
||||||
catch( IOException e )
|
|
||||||
{
|
|
||||||
// meh
|
|
||||||
}
|
|
||||||
|
|
||||||
if( machine.isFinished() )
|
|
||||||
{
|
|
||||||
m_terminal.reset();
|
|
||||||
m_terminal.write( "Error starting bios.lua" );
|
|
||||||
m_terminal.setCursorPos( 0, 1 );
|
|
||||||
m_terminal.write( "ComputerCraft may be installed incorrectly" );
|
|
||||||
|
|
||||||
machine.unload();
|
|
||||||
m_machine = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_machine = machine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_terminal.reset();
|
|
||||||
m_terminal.write( "Error loading bios.lua" );
|
|
||||||
m_terminal.setCursorPos( 0, 1 );
|
|
||||||
m_terminal.write( "ComputerCraft may be installed incorrectly" );
|
|
||||||
|
|
||||||
machine.unload();
|
|
||||||
m_machine = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startComputer()
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Off )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_state = State.Starting;
|
|
||||||
m_externalOutputChanged = true;
|
|
||||||
m_ticksSinceStart = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn the computercraft on
|
|
||||||
final Computer computer = this;
|
|
||||||
ComputerThread.queueTask( new ITask()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public Computer getOwner()
|
|
||||||
{
|
|
||||||
return computer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute()
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Starting )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init terminal
|
|
||||||
m_terminal.reset();
|
|
||||||
|
|
||||||
// Init filesystem
|
|
||||||
if( !initFileSystem() )
|
|
||||||
{
|
|
||||||
// Init failed, so shutdown
|
|
||||||
m_terminal.reset();
|
|
||||||
m_terminal.write( "Error mounting lua/rom" );
|
|
||||||
m_terminal.setCursorPos( 0, 1 );
|
|
||||||
m_terminal.write( "ComputerCraft may be installed incorrectly" );
|
|
||||||
|
|
||||||
m_state = State.Running;
|
|
||||||
stopComputer( false );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init lua
|
|
||||||
initLua();
|
|
||||||
if( m_machine == null )
|
|
||||||
{
|
|
||||||
m_terminal.reset();
|
|
||||||
m_terminal.write( "Error loading bios.lua" );
|
|
||||||
m_terminal.setCursorPos( 0, 1 );
|
|
||||||
m_terminal.write( "ComputerCraft may be installed incorrectly" );
|
|
||||||
|
|
||||||
// Init failed, so shutdown
|
|
||||||
m_state = State.Running;
|
|
||||||
stopComputer( false );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start a new state
|
|
||||||
m_state = State.Running;
|
|
||||||
m_externalOutputChanged = true;
|
|
||||||
synchronized( m_machine )
|
|
||||||
{
|
|
||||||
m_machine.handleEvent( null, null );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, computer );
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stopComputer( final boolean reboot )
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Running )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_state = State.Stopping;
|
|
||||||
m_externalOutputChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn the computercraft off
|
|
||||||
final Computer computer = this;
|
|
||||||
ComputerThread.queueTask( new ITask()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public Computer getOwner()
|
|
||||||
{
|
|
||||||
return computer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute()
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Stopping )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shutdown our APIs
|
|
||||||
synchronized( m_apis )
|
|
||||||
{
|
|
||||||
for( ILuaAPI api : m_apis )
|
|
||||||
{
|
|
||||||
api.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shutdown terminal and filesystem
|
|
||||||
if( m_fileSystem != null )
|
|
||||||
{
|
|
||||||
m_fileSystem.unload();
|
|
||||||
m_fileSystem = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( m_machine != null )
|
|
||||||
{
|
|
||||||
m_terminal.reset();
|
|
||||||
|
|
||||||
synchronized( m_machine )
|
|
||||||
{
|
|
||||||
m_machine.unload();
|
|
||||||
m_machine = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset redstone output
|
|
||||||
m_internalEnvironment.resetOutput();
|
|
||||||
|
|
||||||
m_state = State.Off;
|
|
||||||
m_externalOutputChanged = true;
|
|
||||||
if( reboot )
|
|
||||||
{
|
|
||||||
m_startRequested = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, computer );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void queueEvent( final String event, final Object[] arguments )
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Running )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final Computer computer = this;
|
|
||||||
ITask task = new ITask()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public Computer getOwner()
|
|
||||||
{
|
|
||||||
return computer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute()
|
|
||||||
{
|
|
||||||
synchronized( this )
|
|
||||||
{
|
|
||||||
if( m_state != State.Running )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized( m_machine )
|
|
||||||
{
|
|
||||||
m_machine.handleEvent( event, arguments );
|
|
||||||
if( m_machine.isFinished() )
|
|
||||||
{
|
|
||||||
m_terminal.reset();
|
|
||||||
m_terminal.write( "Error resuming bios.lua" );
|
|
||||||
m_terminal.setCursorPos( 0, 1 );
|
|
||||||
m_terminal.write( "ComputerCraft may be installed incorrectly" );
|
|
||||||
|
|
||||||
stopComputer( false );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ComputerThread.queueTask( task, computer );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public IPeripheral getPeripheral( int side )
|
|
||||||
{
|
|
||||||
return m_internalEnvironment.getPeripheral( side );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void setPeripheral( int side, IPeripheral peripheral )
|
|
||||||
{
|
|
||||||
m_internalEnvironment.setPeripheral( side, peripheral );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void addAPI( dan200.computercraft.core.apis.ILuaAPI api )
|
|
||||||
{
|
|
||||||
addAPI( (ILuaAPI) api );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@SuppressWarnings( "unused" )
|
|
||||||
public void advance( double dt )
|
|
||||||
{
|
|
||||||
advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final String[] s_sideNames = IAPIEnvironment.SIDE_NAMES;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,668 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.core.computer;
|
||||||
|
|
||||||
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
|
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||||
|
import dan200.computercraft.api.lua.ILuaAPI;
|
||||||
|
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||||
|
import dan200.computercraft.core.apis.*;
|
||||||
|
import dan200.computercraft.core.filesystem.FileSystem;
|
||||||
|
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||||
|
import dan200.computercraft.core.lua.CobaltLuaMachine;
|
||||||
|
import dan200.computercraft.core.lua.ILuaMachine;
|
||||||
|
import dan200.computercraft.core.lua.MachineResult;
|
||||||
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
|
import dan200.computercraft.core.tracking.Tracking;
|
||||||
|
import dan200.computercraft.shared.util.Colour;
|
||||||
|
import dan200.computercraft.shared.util.IoUtil;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main task queue and executor for a single computer. This handles turning on and off a computer, as well as
|
||||||
|
* running events.
|
||||||
|
*
|
||||||
|
* When the computer is instructed to turn on or off, or handle an event, we queue a task and register this to be
|
||||||
|
* executed on the {@link ComputerThread}. Note, as we may be starting many events in a single tick, the external
|
||||||
|
* cannot lock on anything which may be held for a long time.
|
||||||
|
*
|
||||||
|
* The executor is effectively composed of two separate queues. Firstly, we have a "single element" queue
|
||||||
|
* {@link #command} which determines which state the computer should transition too. This is set by
|
||||||
|
* {@link #queueStart()} and {@link #queueStop(boolean, boolean)}.
|
||||||
|
*
|
||||||
|
* When a computer is on, we simply push any events onto to the {@link #eventQueue}.
|
||||||
|
*
|
||||||
|
* Both queues are run from the {@link #work()} method, which tries to execute a command if one exists, or resumes the
|
||||||
|
* machine with an event otherwise.
|
||||||
|
*
|
||||||
|
* One final responsibility for the executor is calling {@link ILuaAPI#update()} every tick, via the {@link #tick()}
|
||||||
|
* method. This should only be called when the computer is actually on ({@link #isOn}).
|
||||||
|
*/
|
||||||
|
final class ComputerExecutor
|
||||||
|
{
|
||||||
|
private static final int QUEUE_LIMIT = 256;
|
||||||
|
|
||||||
|
private static IMount romMount;
|
||||||
|
private static final Object romMountLock = new Object();
|
||||||
|
|
||||||
|
private final Computer computer;
|
||||||
|
private final List<ILuaAPI> apis = new ArrayList<>();
|
||||||
|
final TimeoutState timeout = new TimeoutState();
|
||||||
|
|
||||||
|
private FileSystem fileSystem;
|
||||||
|
|
||||||
|
private ILuaMachine machine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the computer is currently on. This is set to false when a shutdown starts, or when turning on completes
|
||||||
|
* (but just before the Lua machine is started).
|
||||||
|
*
|
||||||
|
* @see #isOnLock
|
||||||
|
*/
|
||||||
|
private volatile boolean isOn = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lock to acquire when you need to modify the "on state" of a computer.
|
||||||
|
*
|
||||||
|
* We hold this lock when running any command, and attempt to hold it when updating APIs. This ensures you don't
|
||||||
|
* update APIs while also starting/stopping them.
|
||||||
|
*
|
||||||
|
* @see #isOn
|
||||||
|
* @see #tick()
|
||||||
|
* @see #turnOn()
|
||||||
|
* @see #shutdown()
|
||||||
|
*/
|
||||||
|
private final ReentrantLock isOnLock = new ReentrantLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A lock used for any changes to {@link #eventQueue}, {@link #command} or {@link #onComputerQueue}. This will be
|
||||||
|
* used on the main thread, so locks should be kept as brief as possible.
|
||||||
|
*/
|
||||||
|
private final Object queueLock = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if this executor is present within {@link ComputerThread}.
|
||||||
|
*
|
||||||
|
* @see #queueLock
|
||||||
|
* @see #enqueue()
|
||||||
|
* @see #afterWork()
|
||||||
|
*/
|
||||||
|
volatile boolean onComputerQueue = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The amount of time this computer has used on a theoretical machine which shares work evenly amongst computers.
|
||||||
|
*
|
||||||
|
* @see ComputerThread
|
||||||
|
*/
|
||||||
|
long virtualRuntime = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The last time at which we updated {@link #virtualRuntime}.
|
||||||
|
*
|
||||||
|
* @see ComputerThread
|
||||||
|
*/
|
||||||
|
long vRuntimeStart;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The command that {@link #work()} should execute on the computer thread.
|
||||||
|
*
|
||||||
|
* One sets the command with {@link #queueStart()} and {@link #queueStop(boolean, boolean)}. Neither of these will
|
||||||
|
* queue a new event if there is an existing one in the queue.
|
||||||
|
*
|
||||||
|
* Note, if command is not {@code null}, then some command is scheduled to be executed. Otherwise it is not
|
||||||
|
* currently in the queue (or is currently being executed).
|
||||||
|
*/
|
||||||
|
private volatile StateCommand command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The queue of events which should be executed when this computer is on.
|
||||||
|
*
|
||||||
|
* Note, this should be empty if this computer is off - it is cleared on shutdown and when turning on again.
|
||||||
|
*/
|
||||||
|
private final Queue<Event> eventQueue = new ArrayDeque<>( 4 );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether we interrupted an event and so should resume it instead of executing another task.
|
||||||
|
*
|
||||||
|
* @see #work()
|
||||||
|
* @see #resumeMachine(String, Object[])
|
||||||
|
*/
|
||||||
|
private boolean interruptedEvent = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this executor has been closed, and will no longer accept any incoming commands or events.
|
||||||
|
*
|
||||||
|
* @see #queueStop(boolean, boolean)
|
||||||
|
*/
|
||||||
|
private boolean closed;
|
||||||
|
|
||||||
|
private IWritableMount rootMount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The thread the executor is running on. This is non-null when performing work. We use this to ensure we're only
|
||||||
|
* doing one bit of work at one time.
|
||||||
|
*
|
||||||
|
* @see ComputerThread
|
||||||
|
*/
|
||||||
|
final AtomicReference<Thread> executingThread = new AtomicReference<>();
|
||||||
|
|
||||||
|
ComputerExecutor( Computer computer )
|
||||||
|
{
|
||||||
|
// Ensure the computer thread is running as required.
|
||||||
|
ComputerThread.start();
|
||||||
|
|
||||||
|
this.computer = computer;
|
||||||
|
|
||||||
|
Environment environment = computer.getEnvironment();
|
||||||
|
|
||||||
|
// Add all default APIs to the loaded list.
|
||||||
|
apis.add( new TermAPI( environment ) );
|
||||||
|
apis.add( new RedstoneAPI( environment ) );
|
||||||
|
apis.add( new FSAPI( environment ) );
|
||||||
|
apis.add( new PeripheralAPI( environment ) );
|
||||||
|
apis.add( new OSAPI( environment ) );
|
||||||
|
if( ComputerCraft.http_enable ) apis.add( new HTTPAPI( environment ) );
|
||||||
|
|
||||||
|
// Load in the externally registered APIs.
|
||||||
|
for( ILuaAPIFactory factory : ApiFactories.getAll() )
|
||||||
|
{
|
||||||
|
ComputerSystem system = new ComputerSystem( environment );
|
||||||
|
ILuaAPI api = factory.create( system );
|
||||||
|
if( api != null ) apis.add( new ApiWrapper( api, system ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isOn()
|
||||||
|
{
|
||||||
|
return isOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSystem getFileSystem()
|
||||||
|
{
|
||||||
|
return fileSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
Computer getComputer()
|
||||||
|
{
|
||||||
|
return computer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addApi( ILuaAPI api )
|
||||||
|
{
|
||||||
|
apis.add( api );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule this computer to be started if not already on.
|
||||||
|
*/
|
||||||
|
void queueStart()
|
||||||
|
{
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
// We should only schedule a start if we're not currently on and there's turn on.
|
||||||
|
if( closed || isOn || command != null ) return;
|
||||||
|
|
||||||
|
command = StateCommand.TURN_ON;
|
||||||
|
enqueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule this computer to be stopped if not already on.
|
||||||
|
*
|
||||||
|
* @param reboot Reboot the computer after stopping
|
||||||
|
* @param close Close the computer after stopping.
|
||||||
|
* @see #closed
|
||||||
|
*/
|
||||||
|
void queueStop( boolean reboot, boolean close )
|
||||||
|
{
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
if( closed ) return;
|
||||||
|
closed = close;
|
||||||
|
|
||||||
|
StateCommand newCommand = reboot ? StateCommand.REBOOT : StateCommand.SHUTDOWN;
|
||||||
|
|
||||||
|
// We should only schedule a stop if we're currently on and there's no shutdown pending.
|
||||||
|
if( !isOn || command != null )
|
||||||
|
{
|
||||||
|
// If we're closing, set the command just in case.
|
||||||
|
if( close ) command = newCommand;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
command = newCommand;
|
||||||
|
enqueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abort this whole computer due to a timeout. This will immediately destroy the Lua machine,
|
||||||
|
* and then schedule a shutdown.
|
||||||
|
*/
|
||||||
|
void abort()
|
||||||
|
{
|
||||||
|
ILuaMachine machine = this.machine;
|
||||||
|
if( machine != null ) machine.close();
|
||||||
|
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
if( closed ) return;
|
||||||
|
command = StateCommand.ABORT;
|
||||||
|
if( isOn ) enqueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queue an event if the computer is on
|
||||||
|
*
|
||||||
|
* @param event The event's name
|
||||||
|
* @param args The event's arguments
|
||||||
|
*/
|
||||||
|
void queueEvent( @Nonnull String event, @Nullable Object[] args )
|
||||||
|
{
|
||||||
|
// Events should be skipped if we're not on.
|
||||||
|
if( !isOn ) return;
|
||||||
|
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
// And if we've got some command in the pipeline, then don't queue events - they'll
|
||||||
|
// probably be disposed of anyway.
|
||||||
|
// We also limit the number of events which can be queued.
|
||||||
|
if( closed || command != null || eventQueue.size() >= QUEUE_LIMIT ) return;
|
||||||
|
|
||||||
|
eventQueue.offer( new Event( event, args ) );
|
||||||
|
enqueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add this executor to the {@link ComputerThread} if not already there.
|
||||||
|
*/
|
||||||
|
private void enqueue()
|
||||||
|
{
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
if( !onComputerQueue ) ComputerThread.queue( this );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the internals of the executor.
|
||||||
|
*/
|
||||||
|
void tick()
|
||||||
|
{
|
||||||
|
if( isOn && isOnLock.tryLock() )
|
||||||
|
{
|
||||||
|
// This horrific structure means we don't try to update APIs while the state is being changed
|
||||||
|
// (and so they may be running startup/shutdown).
|
||||||
|
// We use tryLock here, as it has minimal delay, and it doesn't matter if we miss an advance at the
|
||||||
|
// beginning or end of a computer's lifetime.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if( isOn )
|
||||||
|
{
|
||||||
|
// Advance our APIs.
|
||||||
|
for( ILuaAPI api : apis ) api.update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isOnLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IMount getRomMount()
|
||||||
|
{
|
||||||
|
if( romMount != null ) return romMount;
|
||||||
|
|
||||||
|
synchronized( romMountLock )
|
||||||
|
{
|
||||||
|
if( romMount != null ) return romMount;
|
||||||
|
return romMount = computer.getComputerEnvironment().createResourceMount( "computercraft", "lua/rom" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IWritableMount getRootMount()
|
||||||
|
{
|
||||||
|
if( rootMount == null )
|
||||||
|
{
|
||||||
|
rootMount = computer.getComputerEnvironment().createSaveDirMount(
|
||||||
|
"computer/" + computer.assignID(),
|
||||||
|
computer.getComputerEnvironment().getComputerSpaceLimit()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return rootMount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileSystem createFileSystem()
|
||||||
|
{
|
||||||
|
FileSystem filesystem = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
filesystem = new FileSystem( "hdd", getRootMount() );
|
||||||
|
|
||||||
|
IMount romMount = getRomMount();
|
||||||
|
if( romMount == null )
|
||||||
|
{
|
||||||
|
displayFailure( "Cannot mount ROM", null );
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
filesystem.mount( "rom", "rom", romMount );
|
||||||
|
return filesystem;
|
||||||
|
}
|
||||||
|
catch( FileSystemException e )
|
||||||
|
{
|
||||||
|
if( filesystem != null ) filesystem.close();
|
||||||
|
ComputerCraft.log.error( "Cannot mount computer filesystem", e );
|
||||||
|
|
||||||
|
displayFailure( "Cannot mount computer system", null );
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ILuaMachine createLuaMachine()
|
||||||
|
{
|
||||||
|
// Load the bios resource
|
||||||
|
InputStream biosStream = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
biosStream = computer.getComputerEnvironment().createResourceFile( "computercraft", "lua/bios.lua" );
|
||||||
|
}
|
||||||
|
catch( Exception ignored )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
if( biosStream == null )
|
||||||
|
{
|
||||||
|
displayFailure( "Error loading bios.lua", null );
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the lua machine
|
||||||
|
ILuaMachine machine = new CobaltLuaMachine( computer, timeout );
|
||||||
|
|
||||||
|
// Add the APIs
|
||||||
|
for( ILuaAPI api : apis ) machine.addAPI( api );
|
||||||
|
|
||||||
|
// Start the machine running the bios resource
|
||||||
|
MachineResult result = machine.loadBios( biosStream );
|
||||||
|
IoUtil.closeQuietly( biosStream );
|
||||||
|
|
||||||
|
if( result.isError() )
|
||||||
|
{
|
||||||
|
machine.close();
|
||||||
|
displayFailure( "Error loading bios.lua", result.getMessage() );
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void turnOn() throws InterruptedException
|
||||||
|
{
|
||||||
|
isOnLock.lockInterruptibly();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Reset the terminal and event queue
|
||||||
|
computer.getTerminal().reset();
|
||||||
|
interruptedEvent = false;
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
eventQueue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init filesystem
|
||||||
|
if( (fileSystem = createFileSystem()) == null )
|
||||||
|
{
|
||||||
|
shutdown();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init APIs
|
||||||
|
for( ILuaAPI api : apis ) api.startup();
|
||||||
|
|
||||||
|
// Init lua
|
||||||
|
if( (machine = createLuaMachine()) == null )
|
||||||
|
{
|
||||||
|
shutdown();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialisation has finished, so let's mark ourselves as on.
|
||||||
|
isOn = true;
|
||||||
|
computer.markChanged();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isOnLock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now actually start the computer, now that everything is set up.
|
||||||
|
resumeMachine( null, null );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shutdown() throws InterruptedException
|
||||||
|
{
|
||||||
|
isOnLock.lockInterruptibly();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
isOn = false;
|
||||||
|
interruptedEvent = false;
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
eventQueue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown Lua machine
|
||||||
|
if( machine != null )
|
||||||
|
{
|
||||||
|
machine.close();
|
||||||
|
machine = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown our APIs
|
||||||
|
for( ILuaAPI api : apis ) api.shutdown();
|
||||||
|
|
||||||
|
// Unload filesystem
|
||||||
|
if( fileSystem != null )
|
||||||
|
{
|
||||||
|
fileSystem.close();
|
||||||
|
fileSystem = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
computer.getEnvironment().resetOutput();
|
||||||
|
computer.markChanged();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isOnLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before calling {@link #work()}, setting up any important state.
|
||||||
|
*/
|
||||||
|
void beforeWork()
|
||||||
|
{
|
||||||
|
vRuntimeStart = System.nanoTime();
|
||||||
|
timeout.startTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called after executing {@link #work()}.
|
||||||
|
*
|
||||||
|
* @return If we have more work to do.
|
||||||
|
*/
|
||||||
|
boolean afterWork()
|
||||||
|
{
|
||||||
|
if( interruptedEvent )
|
||||||
|
{
|
||||||
|
timeout.pauseTimer();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
timeout.stopTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
Tracking.addTaskTiming( getComputer(), timeout.nanoCurrent() );
|
||||||
|
|
||||||
|
if( interruptedEvent ) return true;
|
||||||
|
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
if( eventQueue.isEmpty() && command == null ) return onComputerQueue = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main worker function, called by {@link ComputerThread}.
|
||||||
|
*
|
||||||
|
* This either executes a {@link StateCommand} or attempts to run an event
|
||||||
|
*
|
||||||
|
* @throws InterruptedException If various locks could not be acquired.
|
||||||
|
* @see #command
|
||||||
|
* @see #eventQueue
|
||||||
|
*/
|
||||||
|
void work() throws InterruptedException
|
||||||
|
{
|
||||||
|
if( interruptedEvent )
|
||||||
|
{
|
||||||
|
interruptedEvent = false;
|
||||||
|
if( machine != null )
|
||||||
|
{
|
||||||
|
resumeMachine( null, null );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StateCommand command;
|
||||||
|
Event event = null;
|
||||||
|
synchronized( queueLock )
|
||||||
|
{
|
||||||
|
command = this.command;
|
||||||
|
this.command = null;
|
||||||
|
|
||||||
|
// If we've no command, pull something from the event queue instead.
|
||||||
|
if( command == null )
|
||||||
|
{
|
||||||
|
if( !isOn )
|
||||||
|
{
|
||||||
|
// We're not on and had no command, but we had work queued. This should never happen, so clear
|
||||||
|
// the event queue just in case.
|
||||||
|
eventQueue.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event = eventQueue.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( command != null )
|
||||||
|
{
|
||||||
|
switch( command )
|
||||||
|
{
|
||||||
|
case TURN_ON:
|
||||||
|
if( isOn ) return;
|
||||||
|
turnOn();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SHUTDOWN:
|
||||||
|
|
||||||
|
if( !isOn ) return;
|
||||||
|
computer.getTerminal().reset();
|
||||||
|
shutdown();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REBOOT:
|
||||||
|
if( !isOn ) return;
|
||||||
|
computer.getTerminal().reset();
|
||||||
|
shutdown();
|
||||||
|
|
||||||
|
computer.turnOn();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ABORT:
|
||||||
|
if( !isOn ) return;
|
||||||
|
displayFailure( "Error running computer", TimeoutState.ABORT_MESSAGE );
|
||||||
|
shutdown();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if( event != null )
|
||||||
|
{
|
||||||
|
resumeMachine( event.name, event.args );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void displayFailure( String message, String extra )
|
||||||
|
{
|
||||||
|
Terminal terminal = computer.getTerminal();
|
||||||
|
boolean colour = computer.getComputerEnvironment().isColour();
|
||||||
|
terminal.reset();
|
||||||
|
|
||||||
|
// Display our primary error message
|
||||||
|
if( colour ) terminal.setTextColour( 15 - Colour.Red.ordinal() );
|
||||||
|
terminal.write( message );
|
||||||
|
|
||||||
|
if( extra != null )
|
||||||
|
{
|
||||||
|
// Display any additional information. This generally comes from the Lua Machine, such as compilation or
|
||||||
|
// runtime errors.
|
||||||
|
terminal.setCursorPos( 0, terminal.getCursorY() + 1 );
|
||||||
|
terminal.write( extra );
|
||||||
|
}
|
||||||
|
|
||||||
|
// And display our generic "CC may be installed incorrectly" message.
|
||||||
|
terminal.setCursorPos( 0, terminal.getCursorY() + 1 );
|
||||||
|
if( colour ) terminal.setTextColour( 15 - Colour.White.ordinal() );
|
||||||
|
terminal.write( "ComputerCraft may be installed incorrectly" );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resumeMachine( String event, Object[] args ) throws InterruptedException
|
||||||
|
{
|
||||||
|
MachineResult result = machine.handleEvent( event, args );
|
||||||
|
interruptedEvent = result.isPause();
|
||||||
|
if( !result.isError() ) return;
|
||||||
|
|
||||||
|
displayFailure( "Error running computer", result.getMessage() );
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum StateCommand
|
||||||
|
{
|
||||||
|
TURN_ON,
|
||||||
|
SHUTDOWN,
|
||||||
|
REBOOT,
|
||||||
|
ABORT,
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class Event
|
||||||
|
{
|
||||||
|
final String name;
|
||||||
|
final Object[] args;
|
||||||
|
|
||||||
|
private Event( String name, Object[] args )
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.args = args;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.core.computer;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A side on a computer. Unlike {@link net.minecraft.util.EnumFacing}, this is relative to the direction the computer is
|
||||||
|
* facing..
|
||||||
|
*/
|
||||||
|
public enum ComputerSide
|
||||||
|
{
|
||||||
|
BOTTOM( "bottom" ),
|
||||||
|
TOP( "top" ),
|
||||||
|
BACK( "back" ),
|
||||||
|
FRONT( "front" ),
|
||||||
|
RIGHT( "right" ),
|
||||||
|
LEFT( "left" );
|
||||||
|
|
||||||
|
public static final String[] NAMES = new String[] { "bottom", "top", "back", "front", "right", "left" };
|
||||||
|
|
||||||
|
public static final int COUNT = 6;
|
||||||
|
|
||||||
|
private static final ComputerSide[] VALUES = values();
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
ComputerSide( String name ) {this.name = name;}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static ComputerSide valueOf( int side )
|
||||||
|
{
|
||||||
|
return VALUES[side];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static ComputerSide valueOfInsensitive( @Nonnull String name )
|
||||||
|
{
|
||||||
|
for( ComputerSide side : VALUES )
|
||||||
|
{
|
||||||
|
if( side.name.equalsIgnoreCase( name ) ) return side;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@ import javax.annotation.Nonnull;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of {@link IComputerAccess}/{@link IComputerSystem} for external APIs.
|
* Implementation of {@link IComputerAccess}/{@link IComputerSystem} for usage by externally registered APIs.
|
||||||
*
|
*
|
||||||
* @see dan200.computercraft.api.ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
|
* @see dan200.computercraft.api.ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
|
||||||
* @see ILuaAPIFactory
|
* @see ILuaAPIFactory
|
||||||
@@ -26,12 +26,12 @@ import javax.annotation.Nullable;
|
|||||||
*/
|
*/
|
||||||
public class ComputerSystem extends ComputerAccess implements IComputerSystem
|
public class ComputerSystem extends ComputerAccess implements IComputerSystem
|
||||||
{
|
{
|
||||||
private final IAPIEnvironment m_environment;
|
private final IAPIEnvironment environment;
|
||||||
|
|
||||||
ComputerSystem( IAPIEnvironment m_environment )
|
ComputerSystem( IAPIEnvironment environment )
|
||||||
{
|
{
|
||||||
super( m_environment );
|
super( environment );
|
||||||
this.m_environment = m_environment;
|
this.environment = environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -45,7 +45,7 @@ public class ComputerSystem extends ComputerAccess implements IComputerSystem
|
|||||||
@Override
|
@Override
|
||||||
public IFileSystem getFileSystem()
|
public IFileSystem getFileSystem()
|
||||||
{
|
{
|
||||||
FileSystem fs = m_environment.getFileSystem();
|
FileSystem fs = environment.getFileSystem();
|
||||||
return fs == null ? null : fs.getMountWrapper();
|
return fs == null ? null : fs.getMountWrapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +53,6 @@ public class ComputerSystem extends ComputerAccess implements IComputerSystem
|
|||||||
@Override
|
@Override
|
||||||
public String getLabel()
|
public String getLabel()
|
||||||
{
|
{
|
||||||
return m_environment.getLabel();
|
return environment.getLabel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,153 +7,351 @@
|
|||||||
package dan200.computercraft.core.computer;
|
package dan200.computercraft.core.computer;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.core.tracking.Tracking;
|
|
||||||
import dan200.computercraft.shared.util.ThreadUtils;
|
import dan200.computercraft.shared.util.ThreadUtils;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Set;
|
import javax.annotation.Nullable;
|
||||||
import java.util.WeakHashMap;
|
import java.util.TreeSet;
|
||||||
import java.util.concurrent.BlockingQueue;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
import java.util.concurrent.locks.LockSupport;
|
import java.util.concurrent.locks.LockSupport;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class ComputerThread
|
import static dan200.computercraft.core.computer.TimeoutState.ABORT_TIMEOUT;
|
||||||
|
import static dan200.computercraft.core.computer.TimeoutState.TIMEOUT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responsible for running all tasks from a {@link Computer}.
|
||||||
|
*
|
||||||
|
* This is split into two components: the {@link TaskRunner}s, which pull an executor from the queue and execute it, and
|
||||||
|
* a single {@link Monitor} which observes all runners and kills them if they have not been terminated by
|
||||||
|
* {@link TimeoutState#isSoftAborted()}.
|
||||||
|
*
|
||||||
|
* Computers are executed using a priority system, with those who have spent less time executing having a higher
|
||||||
|
* priority than those hogging the thread. This, combined with {@link TimeoutState#isPaused()} means we can reduce the
|
||||||
|
* risk of badly behaved computers stalling execution for everyone else.
|
||||||
|
*
|
||||||
|
* This is done using an implementation of Linux's Completely Fair Scheduler. When a computer executes, we compute what
|
||||||
|
* share of execution time it has used (time executed/number of tasks). We then pick the computer who has the least
|
||||||
|
* "virtual execution time" (aka {@link ComputerExecutor#virtualRuntime}).
|
||||||
|
*
|
||||||
|
* When adding a computer to the queue, we make sure its "virtual runtime" is at least as big as the smallest runtime.
|
||||||
|
* This means that adding computers which have slept a lot do not then have massive priority over everyone else. See
|
||||||
|
* {@link #queue(ComputerExecutor)} for how this is implemented.
|
||||||
|
*
|
||||||
|
* In reality, it's unlikely that more than a few computers are waiting to execute at once, so this will not have much
|
||||||
|
* effect unless you have a computer hogging execution time. However, it is pretty effective in those situations.
|
||||||
|
*
|
||||||
|
* @see TimeoutState For how hard timeouts are handled.
|
||||||
|
* @see ComputerExecutor For how computers actually do execution.
|
||||||
|
*/
|
||||||
|
public final class ComputerThread
|
||||||
{
|
{
|
||||||
private static final int QUEUE_LIMIT = 256;
|
/**
|
||||||
|
* How often the computer thread monitor should run, in milliseconds
|
||||||
|
*
|
||||||
|
* @see Monitor
|
||||||
|
*/
|
||||||
|
private static final int MONITOR_WAKEUP = 100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lock used for modifications to the object
|
* The target latency between executing two tasks on a single machine.
|
||||||
|
*
|
||||||
|
* An average tick takes 50ms, and so we ideally need to have handled a couple of events within that window in order
|
||||||
|
* to have a perceived low latency.
|
||||||
*/
|
*/
|
||||||
private static final Object s_stateLock = new Object();
|
private static final long DEFAULT_LATENCY = TimeUnit.MILLISECONDS.toNanos( 50 );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lock for various task operations
|
* The minimum value that {@link #DEFAULT_LATENCY} can have when scaled.
|
||||||
|
*
|
||||||
|
* From statistics gathered on SwitchCraft, almost all machines will execute under 15ms, 75% under 1.5ms, with the
|
||||||
|
* mean being about 3ms. Most computers shouldn't be too impacted with having such a short period to execute in.
|
||||||
*/
|
*/
|
||||||
private static final Object s_taskLock = new Object();
|
private static final long DEFAULT_MIN_PERIOD = TimeUnit.MILLISECONDS.toNanos( 5 );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map of objects to task list
|
* The maximum number of tasks before we have to start scaling latency linearly.
|
||||||
*/
|
*/
|
||||||
private static final WeakHashMap<Object, BlockingQueue<ITask>> s_computerTaskQueues = new WeakHashMap<>();
|
private static final long LATENCY_MAX_TASKS = DEFAULT_LATENCY / DEFAULT_MIN_PERIOD;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock used for modifications to the array of current threads.
|
||||||
|
*/
|
||||||
|
private static final Object threadLock = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the computer thread system is currently running
|
||||||
|
*/
|
||||||
|
private static volatile boolean running = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current task manager.
|
||||||
|
*/
|
||||||
|
private static Thread monitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The array of current runners, and their owning threads.
|
||||||
|
*/
|
||||||
|
private static TaskRunner[] runners;
|
||||||
|
|
||||||
|
private static long latency;
|
||||||
|
private static long minPeriod;
|
||||||
|
|
||||||
|
private static final ReentrantLock computerLock = new ReentrantLock();
|
||||||
|
|
||||||
|
private static final Condition hasWork = computerLock.newCondition();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Active queues to execute
|
* Active queues to execute
|
||||||
*/
|
*/
|
||||||
private static final BlockingQueue<BlockingQueue<ITask>> s_computerTasksActive = new LinkedBlockingQueue<>();
|
private static final TreeSet<ComputerExecutor> computerQueue = new TreeSet<>( ( a, b ) -> {
|
||||||
private static final Set<BlockingQueue<ITask>> s_computerTasksActiveSet = new HashSet<>();
|
if( a == b ) return 0; // Should never happen, but let's be consistent here
|
||||||
|
|
||||||
|
long at = a.virtualRuntime, bt = b.virtualRuntime;
|
||||||
|
if( at == bt ) return Integer.compare( a.hashCode(), b.hashCode() );
|
||||||
|
return at < bt ? -1 : 1;
|
||||||
|
} );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default object for items which don't have an owner
|
* The minimum {@link ComputerExecutor#virtualRuntime} time on the tree.
|
||||||
*/
|
*/
|
||||||
private static final Object s_defaultOwner = new Object();
|
private static long minimumVirtualRuntime = 0;
|
||||||
|
|
||||||
/**
|
private static final ThreadFactory monitorFactory = ThreadUtils.factory( "Computer-Monitor" );
|
||||||
* Whether the thread is stopped or should be stopped
|
private static final ThreadFactory runnerFactory = ThreadUtils.factory( "Computer-Runner" );
|
||||||
*/
|
|
||||||
private static boolean s_stopped = false;
|
|
||||||
|
|
||||||
/**
|
private ComputerThread() {}
|
||||||
* The thread tasks execute on
|
|
||||||
*/
|
|
||||||
private static Thread[] s_threads = null;
|
|
||||||
|
|
||||||
private static final ThreadFactory s_ManagerFactory = ThreadUtils.factory( "Computer-Manager" );
|
|
||||||
private static final ThreadFactory s_RunnerFactory = ThreadUtils.factory( "Computer-Runner" );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the computer thread
|
* Start the computer thread
|
||||||
*/
|
*/
|
||||||
public static void start()
|
static void start()
|
||||||
{
|
{
|
||||||
synchronized( s_stateLock )
|
synchronized( threadLock )
|
||||||
{
|
{
|
||||||
s_stopped = false;
|
running = true;
|
||||||
if( s_threads == null || s_threads.length != ComputerCraft.computer_threads )
|
if( monitor == null || !monitor.isAlive() ) (monitor = monitorFactory.newThread( new Monitor() )).start();
|
||||||
|
|
||||||
|
if( runners == null )
|
||||||
{
|
{
|
||||||
s_threads = new Thread[ComputerCraft.computer_threads];
|
// TODO: Change the runners length on config reloads
|
||||||
|
runners = new TaskRunner[ComputerCraft.computer_threads];
|
||||||
|
|
||||||
|
// latency and minPeriod are scaled by 1 + floor(log2(threads)). We can afford to execute tasks for
|
||||||
|
// longer when executing on more than one thread.
|
||||||
|
long factor = 64 - Long.numberOfLeadingZeros( runners.length );
|
||||||
|
latency = DEFAULT_LATENCY * factor;
|
||||||
|
minPeriod = DEFAULT_MIN_PERIOD * factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( int i = 0; i < s_threads.length; i++ )
|
for( int i = 0; i < runners.length; i++ )
|
||||||
{
|
{
|
||||||
Thread thread = s_threads[i];
|
TaskRunner runner = runners[i];
|
||||||
if( thread == null || !thread.isAlive() )
|
if( runner == null || runner.owner == null || !runner.owner.isAlive() )
|
||||||
{
|
{
|
||||||
(s_threads[i] = s_ManagerFactory.newThread( new TaskExecutor() )).start();
|
// Mark the old runner as dead, just in case.
|
||||||
|
if( runner != null ) runner.running = false;
|
||||||
|
// And start a new runner
|
||||||
|
runnerFactory.newThread( runners[i] = new TaskRunner() ).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to stop the computer thread
|
* Attempt to stop the computer thread. This interrupts each runner, and clears the task queue.
|
||||||
*/
|
*/
|
||||||
public static void stop()
|
public static void stop()
|
||||||
{
|
{
|
||||||
synchronized( s_stateLock )
|
synchronized( threadLock )
|
||||||
{
|
{
|
||||||
if( s_threads != null )
|
running = false;
|
||||||
|
if( runners != null )
|
||||||
{
|
{
|
||||||
s_stopped = true;
|
for( TaskRunner runner : runners )
|
||||||
for( Thread thread : s_threads )
|
|
||||||
{
|
{
|
||||||
if( thread != null && thread.isAlive() )
|
if( runner == null ) continue;
|
||||||
{
|
|
||||||
thread.interrupt();
|
runner.running = false;
|
||||||
}
|
if( runner.owner != null ) runner.owner.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized( s_taskLock )
|
computerLock.lock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
s_computerTaskQueues.clear();
|
computerQueue.clear();
|
||||||
s_computerTasksActive.clear();
|
}
|
||||||
s_computerTasksActiveSet.clear();
|
finally
|
||||||
|
{
|
||||||
|
computerLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue a task to execute on the thread
|
* Mark a computer as having work, enqueuing it on the thread
|
||||||
*
|
*
|
||||||
* @param task The task to execute
|
* You must be holding {@link ComputerExecutor}'s {@code queueLock} when calling this method - it should only
|
||||||
* @param computer The computer to execute it on, use {@code null} to execute on the default object.
|
* be called from {@code enqueue}.
|
||||||
|
*
|
||||||
|
* @param executor The computer to execute work on.
|
||||||
*/
|
*/
|
||||||
public static void queueTask( ITask task, Computer computer )
|
static void queue( @Nonnull ComputerExecutor executor )
|
||||||
{
|
{
|
||||||
Object queueObject = computer == null ? s_defaultOwner : computer;
|
computerLock.lock();
|
||||||
|
try
|
||||||
BlockingQueue<ITask> queue;
|
|
||||||
synchronized( s_computerTaskQueues )
|
|
||||||
{
|
{
|
||||||
queue = s_computerTaskQueues.get( queueObject );
|
if( executor.onComputerQueue ) throw new IllegalStateException( "Cannot queue already queued executor" );
|
||||||
if( queue == null )
|
executor.onComputerQueue = true;
|
||||||
|
|
||||||
|
updateRuntimes( null );
|
||||||
|
|
||||||
|
// We're not currently on the queue, so update its current execution time to
|
||||||
|
// ensure its at least as high as the minimum.
|
||||||
|
long newRuntime = minimumVirtualRuntime;
|
||||||
|
|
||||||
|
if( executor.virtualRuntime == 0 )
|
||||||
{
|
{
|
||||||
s_computerTaskQueues.put( queueObject, queue = new LinkedBlockingQueue<>( QUEUE_LIMIT ) );
|
// Slow down new computers a little bit.
|
||||||
|
newRuntime += scaledPeriod();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Give a small boost to computers which have slept a little.
|
||||||
|
newRuntime -= latency / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.virtualRuntime = Math.max( newRuntime, executor.virtualRuntime );
|
||||||
|
|
||||||
|
// Add to the queue, and signal the workers.
|
||||||
|
computerQueue.add( executor );
|
||||||
|
hasWork.signal();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
computerLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the {@link ComputerExecutor#virtualRuntime}s of all running tasks, and then update the
|
||||||
|
* {@link #minimumVirtualRuntime} based on the current tasks.
|
||||||
|
*
|
||||||
|
* This is called before queueing tasks, to ensure that {@link #minimumVirtualRuntime} is up-to-date.
|
||||||
|
*/
|
||||||
|
private static void updateRuntimes( @Nullable ComputerExecutor current )
|
||||||
|
{
|
||||||
|
long minRuntime = Long.MAX_VALUE;
|
||||||
|
|
||||||
|
// If we've a task on the queue, use that as our base time.
|
||||||
|
if( !computerQueue.isEmpty() ) minRuntime = computerQueue.first().virtualRuntime;
|
||||||
|
|
||||||
|
// Update all the currently executing tasks
|
||||||
|
long now = System.nanoTime();
|
||||||
|
int tasks = 1 + computerQueue.size();
|
||||||
|
TaskRunner[] currentRunners = runners;
|
||||||
|
if( currentRunners != null )
|
||||||
|
{
|
||||||
|
for( TaskRunner runner : currentRunners )
|
||||||
|
{
|
||||||
|
if( runner == null ) continue;
|
||||||
|
ComputerExecutor executor = runner.currentExecutor.get();
|
||||||
|
if( executor == null ) continue;
|
||||||
|
|
||||||
|
// We do two things here: first we update the task's virtual runtime based on when we
|
||||||
|
// last checked, and then we check the minimum.
|
||||||
|
minRuntime = Math.min( minRuntime, executor.virtualRuntime += (now - executor.vRuntimeStart) / tasks );
|
||||||
|
executor.vRuntimeStart = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized( s_taskLock )
|
// And update the most recently executed one (if set).
|
||||||
|
if( current != null )
|
||||||
{
|
{
|
||||||
if( queue.offer( task ) && !s_computerTasksActiveSet.contains( queue ) )
|
minRuntime = Math.min( minRuntime, current.virtualRuntime += (now - current.vRuntimeStart) / tasks );
|
||||||
{
|
}
|
||||||
s_computerTasksActive.add( queue );
|
|
||||||
s_computerTasksActiveSet.add( queue );
|
if( minRuntime > minimumVirtualRuntime && minRuntime < Long.MAX_VALUE )
|
||||||
}
|
{
|
||||||
|
minimumVirtualRuntime = minRuntime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responsible for pulling and managing computer tasks. This pulls a task from {@link #s_computerTasksActive},
|
* Ensure the "currently working" state of the executor is reset, the timings are updated, and then requeue the
|
||||||
* creates a new thread using {@link TaskRunner} or reuses a previous one and uses that to execute the task.
|
* executor if needed.
|
||||||
*
|
*
|
||||||
* If the task times out, then it will attempt to interrupt the {@link TaskRunner} instance.
|
* @param runner The runner this task was on.
|
||||||
|
* @param executor The executor to requeue
|
||||||
*/
|
*/
|
||||||
private static final class TaskExecutor implements Runnable
|
private static void afterWork( TaskRunner runner, ComputerExecutor executor )
|
||||||
{
|
{
|
||||||
private TaskRunner runner;
|
// Clear the executor's thread.
|
||||||
private Thread thread;
|
Thread currentThread = executor.executingThread.getAndSet( null );
|
||||||
|
if( currentThread != runner.owner )
|
||||||
|
{
|
||||||
|
ComputerCraft.log.error(
|
||||||
|
"Expected computer #{} to be running on {}, but already running on {}. This is a SERIOUS bug, please report with your debug.log.",
|
||||||
|
executor.getComputer().getID(), runner.owner.getName(), currentThread == null ? "nothing" : currentThread.getName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
computerLock.lock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
updateRuntimes( executor );
|
||||||
|
|
||||||
|
// If we've no more tasks, just return.
|
||||||
|
if( !executor.afterWork() ) return;
|
||||||
|
|
||||||
|
// Otherwise, add to the queue, and signal any waiting workers.
|
||||||
|
computerQueue.add( executor );
|
||||||
|
hasWork.signal();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
computerLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scaled period for a single task
|
||||||
|
*
|
||||||
|
* @return The scaled period for the task
|
||||||
|
* @see #DEFAULT_LATENCY
|
||||||
|
* @see #DEFAULT_MIN_PERIOD
|
||||||
|
* @see #LATENCY_MAX_TASKS
|
||||||
|
*/
|
||||||
|
static long scaledPeriod()
|
||||||
|
{
|
||||||
|
// +1 to include the current task
|
||||||
|
int count = 1 + computerQueue.size();
|
||||||
|
return count < LATENCY_MAX_TASKS ? latency / count : minPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the thread has computers queued up
|
||||||
|
*
|
||||||
|
* @return If we have work queued up.
|
||||||
|
*/
|
||||||
|
static boolean hasPendingWork()
|
||||||
|
{
|
||||||
|
return !computerQueue.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observes all currently active {@link TaskRunner}s and terminates their tasks once they have exceeded the hard
|
||||||
|
* abort limit.
|
||||||
|
*
|
||||||
|
* @see TimeoutState
|
||||||
|
*/
|
||||||
|
private static final class Monitor implements Runnable
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
@@ -161,201 +359,171 @@ public class ComputerThread
|
|||||||
{
|
{
|
||||||
while( true )
|
while( true )
|
||||||
{
|
{
|
||||||
// Wait for an active queue to execute
|
Thread.sleep( MONITOR_WAKEUP );
|
||||||
BlockingQueue<ITask> queue = s_computerTasksActive.take();
|
|
||||||
|
|
||||||
// If threads should be stopped then return
|
TaskRunner[] currentRunners = ComputerThread.runners;
|
||||||
synchronized( s_stateLock )
|
if( currentRunners != null )
|
||||||
{
|
{
|
||||||
if( s_stopped ) return;
|
for( int i = 0; i < currentRunners.length; i++ )
|
||||||
}
|
{
|
||||||
|
TaskRunner runner = currentRunners[i];
|
||||||
|
// If we've no runner, skip.
|
||||||
|
if( runner == null ) continue;
|
||||||
|
|
||||||
execute( queue );
|
// If the runner has no work, skip
|
||||||
|
ComputerExecutor executor = runner.currentExecutor.get();
|
||||||
|
if( executor == null ) continue;
|
||||||
|
|
||||||
|
// If we're still within normal execution times (TIMEOUT) or soft abort (ABORT_TIMEOUT),
|
||||||
|
// then we can let the Lua machine do its work.
|
||||||
|
long afterStart = executor.timeout.nanoCumulative();
|
||||||
|
long afterHardAbort = afterStart - TIMEOUT - ABORT_TIMEOUT;
|
||||||
|
if( afterHardAbort < 0 ) continue;
|
||||||
|
|
||||||
|
// Set the hard abort flag.
|
||||||
|
executor.timeout.hardAbort();
|
||||||
|
executor.abort();
|
||||||
|
|
||||||
|
if( afterHardAbort >= ABORT_TIMEOUT )
|
||||||
|
{
|
||||||
|
// If we've hard aborted but we're still not dead, dump the stack trace and interrupt
|
||||||
|
// the task.
|
||||||
|
timeoutTask( executor, runner.owner, afterStart );
|
||||||
|
runner.owner.interrupt();
|
||||||
|
}
|
||||||
|
else if( afterHardAbort >= ABORT_TIMEOUT * 2 )
|
||||||
|
{
|
||||||
|
// If we've hard aborted and interrupted, and we're still not dead, then mark the runner
|
||||||
|
// as dead, finish off the task, and spawn a new runner.
|
||||||
|
timeoutTask( executor, runner.owner, afterStart );
|
||||||
|
runner.running = false;
|
||||||
|
runner.owner.interrupt();
|
||||||
|
|
||||||
|
ComputerExecutor thisExecutor = runner.currentExecutor.getAndSet( null );
|
||||||
|
if( thisExecutor != null ) afterWork( runner, executor );
|
||||||
|
|
||||||
|
synchronized( threadLock )
|
||||||
|
{
|
||||||
|
if( running && runners.length > i && runners[i] == runner )
|
||||||
|
{
|
||||||
|
runnerFactory.newThread( currentRunners[i] = new TaskRunner() ).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( InterruptedException ignored )
|
catch( InterruptedException ignored )
|
||||||
{
|
{
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void execute( BlockingQueue<ITask> queue ) throws InterruptedException
|
|
||||||
{
|
|
||||||
ITask task = queue.remove();
|
|
||||||
|
|
||||||
if( thread == null || !thread.isAlive() )
|
|
||||||
{
|
|
||||||
runner = new TaskRunner();
|
|
||||||
(thread = s_RunnerFactory.newThread( runner )).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
long start = System.nanoTime();
|
|
||||||
|
|
||||||
// Execute the task
|
|
||||||
runner.submit( task );
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// If we timed out rather than exiting:
|
|
||||||
boolean done = runner.await( 7000 );
|
|
||||||
if( !done )
|
|
||||||
{
|
|
||||||
// Attempt to soft then hard abort
|
|
||||||
Computer computer = task.getOwner();
|
|
||||||
if( computer != null )
|
|
||||||
{
|
|
||||||
computer.abort( false );
|
|
||||||
|
|
||||||
done = runner.await( 1500 );
|
|
||||||
if( !done )
|
|
||||||
{
|
|
||||||
computer.abort( true );
|
|
||||||
done = runner.await( 1500 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interrupt the thread
|
|
||||||
if( !done )
|
|
||||||
{
|
|
||||||
if( ComputerCraft.logPeripheralErrors )
|
|
||||||
{
|
|
||||||
long time = System.nanoTime() - start;
|
|
||||||
StringBuilder builder = new StringBuilder( "Terminating " );
|
|
||||||
if( computer != null )
|
|
||||||
{
|
|
||||||
builder.append( "computer #" ).append( computer.getID() );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
builder.append( "unknown computer" );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
builder.append( " due to timeout (running for " )
|
|
||||||
.append( time / 1e9 )
|
|
||||||
.append( " seconds). This is NOT a bug, but may mean a computer is misbehaving. " )
|
|
||||||
.append( thread.getName() )
|
|
||||||
.append( " is currently " )
|
|
||||||
.append( thread.getState() );
|
|
||||||
Object blocking = LockSupport.getBlocker( thread );
|
|
||||||
if( blocking != null ) builder.append( "\n on " ).append( blocking );
|
|
||||||
|
|
||||||
for( StackTraceElement element : thread.getStackTrace() )
|
|
||||||
{
|
|
||||||
builder.append( "\n at " ).append( element );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ComputerCraft.log.warn( builder.toString() );
|
|
||||||
}
|
|
||||||
|
|
||||||
thread.interrupt();
|
|
||||||
thread = null;
|
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
long stop = System.nanoTime();
|
|
||||||
Computer computer = task.getOwner();
|
|
||||||
if( computer != null ) Tracking.addTaskTiming( computer, stop - start );
|
|
||||||
|
|
||||||
// Re-add it back onto the queue or remove it
|
|
||||||
synchronized( s_taskLock )
|
|
||||||
{
|
|
||||||
if( queue.isEmpty() )
|
|
||||||
{
|
|
||||||
s_computerTasksActiveSet.remove( queue );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
s_computerTasksActive.add( queue );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responsible for the actual running of tasks. It waitin for the {@link TaskRunner#input} semaphore to be
|
* Pulls tasks from the {@link #computerQueue} queue and runs them.
|
||||||
* triggered, consumes a task and then triggers {@link TaskRunner#finished}.
|
*
|
||||||
|
* This is responsible for running the {@link ComputerExecutor#work()}, {@link ComputerExecutor#beforeWork()} and
|
||||||
|
* {@link ComputerExecutor#afterWork()} functions. Everything else is either handled by the executor, timeout
|
||||||
|
* state or monitor.
|
||||||
*/
|
*/
|
||||||
private static final class TaskRunner implements Runnable
|
private static final class TaskRunner implements Runnable
|
||||||
{
|
{
|
||||||
private final Semaphore input = new Semaphore();
|
Thread owner;
|
||||||
private final Semaphore finished = new Semaphore();
|
volatile boolean running = true;
|
||||||
private ITask task;
|
|
||||||
|
final AtomicReference<ComputerExecutor> currentExecutor = new AtomicReference<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
try
|
owner = Thread.currentThread();
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
while( running && ComputerThread.running )
|
||||||
{
|
{
|
||||||
while( true )
|
// Wait for an active queue to execute
|
||||||
|
ComputerExecutor executor;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
input.await();
|
computerLock.lockInterruptibly();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
task.execute();
|
while( computerQueue.isEmpty() ) hasWork.await();
|
||||||
|
executor = computerQueue.pollFirst();
|
||||||
|
assert executor != null : "hasWork should ensure we never receive null work";
|
||||||
}
|
}
|
||||||
catch( RuntimeException e )
|
finally
|
||||||
{
|
{
|
||||||
ComputerCraft.log.error( "Error running task.", e );
|
computerLock.unlock();
|
||||||
}
|
}
|
||||||
task = null;
|
}
|
||||||
finished.signal();
|
catch( InterruptedException ignored )
|
||||||
|
{
|
||||||
|
// If we've been interrupted, our running flag has probably been reset, so we'll
|
||||||
|
// just jump into the next iteration.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're trying to executing some task on this computer while someone else is doing work, something
|
||||||
|
// is seriously wrong.
|
||||||
|
while( !executor.executingThread.compareAndSet( null, owner ) )
|
||||||
|
{
|
||||||
|
Thread existing = executor.executingThread.get();
|
||||||
|
if( existing != null )
|
||||||
|
{
|
||||||
|
ComputerCraft.log.error(
|
||||||
|
"Trying to run computer #{} on thread {}, but already running on {}. This is a SERIOUS bug, please report with your debug.log.",
|
||||||
|
executor.getComputer().getID(), owner.getName(), existing.getName()
|
||||||
|
);
|
||||||
|
continue tasks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the timers
|
||||||
|
executor.beforeWork();
|
||||||
|
|
||||||
|
// And then set the current executor. It's important to do it afterwards, as otherwise we introduce
|
||||||
|
// race conditions with the monitor.
|
||||||
|
currentExecutor.set( executor );
|
||||||
|
|
||||||
|
// Execute the task
|
||||||
|
try
|
||||||
|
{
|
||||||
|
executor.work();
|
||||||
|
}
|
||||||
|
catch( Exception e )
|
||||||
|
{
|
||||||
|
ComputerCraft.log.error( "Error running task on computer #" + executor.getComputer().getID(), e );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ComputerExecutor thisExecutor = currentExecutor.getAndSet( null );
|
||||||
|
if( thisExecutor != null ) afterWork( this, executor );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( InterruptedException e )
|
|
||||||
{
|
|
||||||
ComputerCraft.log.error( "Error running task.", e );
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void submit( ITask task )
|
|
||||||
{
|
|
||||||
this.task = task;
|
|
||||||
input.signal();
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean await( long timeout ) throws InterruptedException
|
|
||||||
{
|
|
||||||
return finished.await( timeout );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static void timeoutTask( ComputerExecutor executor, Thread thread, long time )
|
||||||
* A simple method to allow awaiting/providing a signal.
|
|
||||||
*
|
|
||||||
* Java does provide similar classes, but I only needed something simple.
|
|
||||||
*/
|
|
||||||
private static final class Semaphore
|
|
||||||
{
|
{
|
||||||
private volatile boolean state = false;
|
if( !ComputerCraft.logPeripheralErrors ) return;
|
||||||
|
|
||||||
synchronized void signal()
|
StringBuilder builder = new StringBuilder()
|
||||||
|
.append( "Terminating computer #" ).append( executor.getComputer().getID() )
|
||||||
|
.append( " due to timeout (running for " ).append( time * 1e-9 )
|
||||||
|
.append( " seconds). This is NOT a bug, but may mean a computer is misbehaving. " )
|
||||||
|
.append( thread.getName() )
|
||||||
|
.append( " is currently " )
|
||||||
|
.append( thread.getState() );
|
||||||
|
Object blocking = LockSupport.getBlocker( thread );
|
||||||
|
if( blocking != null ) builder.append( "\n on " ).append( blocking );
|
||||||
|
|
||||||
|
for( StackTraceElement element : thread.getStackTrace() )
|
||||||
{
|
{
|
||||||
state = true;
|
builder.append( "\n at " ).append( element );
|
||||||
notify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void await() throws InterruptedException
|
ComputerCraft.log.warn( builder.toString() );
|
||||||
{
|
|
||||||
while( !state ) wait();
|
|
||||||
state = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized boolean await( long timeout ) throws InterruptedException
|
|
||||||
{
|
|
||||||
if( !state )
|
|
||||||
{
|
|
||||||
wait( timeout );
|
|
||||||
if( !state ) return false;
|
|
||||||
}
|
|
||||||
state = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
package dan200.computercraft.core.computer;
|
package dan200.computercraft.core.computer;
|
||||||
|
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
|
import dan200.computercraft.api.peripheral.IWorkMonitor;
|
||||||
import dan200.computercraft.core.apis.IAPIEnvironment;
|
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||||
import dan200.computercraft.core.filesystem.FileSystem;
|
import dan200.computercraft.core.filesystem.FileSystem;
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
@@ -21,7 +22,7 @@ import java.util.Arrays;
|
|||||||
*
|
*
|
||||||
* This handles storing and updating of peripherals and redstone.
|
* This handles storing and updating of peripherals and redstone.
|
||||||
*
|
*
|
||||||
* <h2>Redstone</h2>
|
* <h1>Redstone</h1>
|
||||||
* We holds three kinds of arrays for redstone, in normal and bundled versions:
|
* We holds three kinds of arrays for redstone, in normal and bundled versions:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link #internalOutput} is the redstone output which the computer has currently set. This is read on both
|
* <li>{@link #internalOutput} is the redstone output which the computer has currently set. This is read on both
|
||||||
@@ -32,7 +33,7 @@ import java.util.Arrays;
|
|||||||
* thread.</li>
|
* thread.</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <h2>Peripheral</h2>
|
* <h1>Peripheral</h1>
|
||||||
* We also keep track of peripherals. These are read on both threads, and only written on the main thread.
|
* We also keep track of peripherals. These are read on both threads, and only written on the main thread.
|
||||||
*/
|
*/
|
||||||
public final class Environment implements IAPIEnvironment
|
public final class Environment implements IAPIEnvironment
|
||||||
@@ -40,17 +41,17 @@ public final class Environment implements IAPIEnvironment
|
|||||||
private final Computer computer;
|
private final Computer computer;
|
||||||
|
|
||||||
private boolean internalOutputChanged = false;
|
private boolean internalOutputChanged = false;
|
||||||
private final int[] internalOutput = new int[SIDE_COUNT];
|
private final int[] internalOutput = new int[ComputerSide.COUNT];
|
||||||
private final int[] internalBundledOutput = new int[SIDE_COUNT];
|
private final int[] internalBundledOutput = new int[ComputerSide.COUNT];
|
||||||
|
|
||||||
private final int[] externalOutput = new int[SIDE_COUNT];
|
private final int[] externalOutput = new int[ComputerSide.COUNT];
|
||||||
private final int[] externalBundledOutput = new int[SIDE_COUNT];
|
private final int[] externalBundledOutput = new int[ComputerSide.COUNT];
|
||||||
|
|
||||||
private boolean inputChanged = false;
|
private boolean inputChanged = false;
|
||||||
private final int[] input = new int[SIDE_COUNT];
|
private final int[] input = new int[ComputerSide.COUNT];
|
||||||
private final int[] bundledInput = new int[SIDE_COUNT];
|
private final int[] bundledInput = new int[ComputerSide.COUNT];
|
||||||
|
|
||||||
private final IPeripheral[] peripherals = new IPeripheral[SIDE_COUNT];
|
private final IPeripheral[] peripherals = new IPeripheral[ComputerSide.COUNT];
|
||||||
private IPeripheralChangeListener peripheralListener = null;
|
private IPeripheralChangeListener peripheralListener = null;
|
||||||
|
|
||||||
Environment( Computer computer )
|
Environment( Computer computer )
|
||||||
@@ -58,13 +59,6 @@ public final class Environment implements IAPIEnvironment
|
|||||||
this.computer = computer;
|
this.computer = computer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public Computer getComputer()
|
|
||||||
{
|
|
||||||
return computer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getComputerID()
|
public int getComputerID()
|
||||||
{
|
{
|
||||||
@@ -78,6 +72,13 @@ public final class Environment implements IAPIEnvironment
|
|||||||
return computer.getComputerEnvironment();
|
return computer.getComputerEnvironment();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IWorkMonitor getMainThreadMonitor()
|
||||||
|
{
|
||||||
|
return computer.getMainThreadMonitor();
|
||||||
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public Terminal getTerminal()
|
public Terminal getTerminal()
|
||||||
@@ -110,85 +111,89 @@ public final class Environment implements IAPIEnvironment
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getInput( int side )
|
public int getInput( ComputerSide side )
|
||||||
{
|
{
|
||||||
return input[side];
|
return input[side.ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getBundledInput( int side )
|
public int getBundledInput( ComputerSide side )
|
||||||
{
|
{
|
||||||
return bundledInput[side];
|
return bundledInput[side.ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOutput( int side, int output )
|
public void setOutput( ComputerSide side, int output )
|
||||||
{
|
{
|
||||||
|
int index = side.ordinal();
|
||||||
synchronized( internalOutput )
|
synchronized( internalOutput )
|
||||||
{
|
{
|
||||||
if( internalOutput[side] != output )
|
if( internalOutput[index] != output )
|
||||||
{
|
{
|
||||||
internalOutput[side] = output;
|
internalOutput[index] = output;
|
||||||
internalOutputChanged = true;
|
internalOutputChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getOutput( int side )
|
public int getOutput( ComputerSide side )
|
||||||
{
|
{
|
||||||
synchronized( internalOutput )
|
synchronized( internalOutput )
|
||||||
{
|
{
|
||||||
return computer.isOn() ? internalOutput[side] : 0;
|
return computer.isOn() ? internalOutput[side.ordinal()] : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBundledOutput( int side, int output )
|
public void setBundledOutput( ComputerSide side, int output )
|
||||||
{
|
{
|
||||||
|
int index = side.ordinal();
|
||||||
synchronized( internalOutput )
|
synchronized( internalOutput )
|
||||||
{
|
{
|
||||||
if( internalBundledOutput[side] != output )
|
if( internalBundledOutput[index] != output )
|
||||||
{
|
{
|
||||||
internalBundledOutput[side] = output;
|
internalBundledOutput[index] = output;
|
||||||
internalOutputChanged = true;
|
internalOutputChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getBundledOutput( int side )
|
public int getBundledOutput( ComputerSide side )
|
||||||
{
|
{
|
||||||
synchronized( internalOutput )
|
synchronized( internalOutput )
|
||||||
{
|
{
|
||||||
return computer.isOn() ? internalBundledOutput[side] : 0;
|
return computer.isOn() ? internalBundledOutput[side.ordinal()] : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getExternalRedstoneOutput( int side )
|
public int getExternalRedstoneOutput( ComputerSide side )
|
||||||
{
|
{
|
||||||
return computer.isOn() ? externalOutput[side] : 0;
|
return computer.isOn() ? externalOutput[side.ordinal()] : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getExternalBundledRedstoneOutput( int side )
|
public int getExternalBundledRedstoneOutput( ComputerSide side )
|
||||||
{
|
{
|
||||||
return computer.isOn() ? externalBundledOutput[side] : 0;
|
return computer.isOn() ? externalBundledOutput[side.ordinal()] : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRedstoneInput( int side, int level )
|
public void setRedstoneInput( ComputerSide side, int level )
|
||||||
{
|
{
|
||||||
if( input[side] != level )
|
int index = side.ordinal();
|
||||||
|
if( input[index] != level )
|
||||||
{
|
{
|
||||||
input[side] = level;
|
input[index] = level;
|
||||||
inputChanged = true;
|
inputChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBundledRedstoneInput( int side, int combination )
|
public void setBundledRedstoneInput( ComputerSide side, int combination )
|
||||||
{
|
{
|
||||||
if( bundledInput[side] != combination )
|
int index = side.ordinal();
|
||||||
|
if( bundledInput[index] != combination )
|
||||||
{
|
{
|
||||||
bundledInput[side] = combination;
|
bundledInput[index] = combination;
|
||||||
inputChanged = true;
|
inputChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -214,14 +219,14 @@ public final class Environment implements IAPIEnvironment
|
|||||||
*/
|
*/
|
||||||
boolean updateOutput()
|
boolean updateOutput()
|
||||||
{
|
{
|
||||||
// Set outputchanged if the internal redstone has changed
|
// Mark output as changed if the internal redstone has changed
|
||||||
synchronized( internalOutput )
|
synchronized( internalOutput )
|
||||||
{
|
{
|
||||||
if( !internalOutputChanged ) return false;
|
if( !internalOutputChanged ) return false;
|
||||||
|
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
|
|
||||||
for( int i = 0; i < SIDE_COUNT; i++ )
|
for( int i = 0; i < ComputerSide.COUNT; i++ )
|
||||||
{
|
{
|
||||||
if( externalOutput[i] != internalOutput[i] )
|
if( externalOutput[i] != internalOutput[i] )
|
||||||
{
|
{
|
||||||
@@ -254,24 +259,25 @@ public final class Environment implements IAPIEnvironment
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IPeripheral getPeripheral( int side )
|
public IPeripheral getPeripheral( ComputerSide side )
|
||||||
{
|
{
|
||||||
synchronized( peripherals )
|
synchronized( peripherals )
|
||||||
{
|
{
|
||||||
return peripherals[side];
|
return peripherals[side.ordinal()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPeripheral( int side, IPeripheral peripheral )
|
public void setPeripheral( ComputerSide side, IPeripheral peripheral )
|
||||||
{
|
{
|
||||||
synchronized( peripherals )
|
synchronized( peripherals )
|
||||||
{
|
{
|
||||||
IPeripheral existing = peripherals[side];
|
int index = side.ordinal();
|
||||||
|
IPeripheral existing = peripherals[index];
|
||||||
if( (existing == null && peripheral != null) ||
|
if( (existing == null && peripheral != null) ||
|
||||||
(existing != null && peripheral == null) ||
|
(existing != null && peripheral == null) ||
|
||||||
(existing != null && !existing.equals( peripheral )) )
|
(existing != null && !existing.equals( peripheral )) )
|
||||||
{
|
{
|
||||||
peripherals[side] = peripheral;
|
peripherals[index] = peripheral;
|
||||||
if( peripheralListener != null ) peripheralListener.onPeripheralChanged( side, peripheral );
|
if( peripheralListener != null ) peripheralListener.onPeripheralChanged( side, peripheral );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user